Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Deprecating DoctrineEntity logic, moving to DoctrineObject

  • Loading branch information...
commit 22eed840d2187d5578d6e54d46eaff54dce611c6 1 parent a5a3ac8
@Ocramius Ocramius authored
View
286 src/DoctrineModule/Authentication/Adapter/DoctrineEntity.php
@@ -29,112 +29,27 @@
/**
* Authentication adapter that uses a Doctrine Entity for verification.
- *
+ *
* @deprecated please use DoctrineModule\Authentication\Adapter\DoctrineObject
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org
- * @since 1.0
+ * @since 0.1.0
* @version $Revision$
* @author Kyle Spraggs <theman@spiffyjr.me>
*/
-class DoctrineEntity implements AdapterInterface
+class DoctrineEntity extends DoctrineObject
{
/**
- * Doctrine EntityManager instance
- *
- * @var \Doctrine\ORM\EntityManager
- */
- protected $em;
-
- /**
- * Entity class to use.
- *
- * @var string
- */
- protected $entity;
-
- /**
- * Identity column to check credential against.
- *
- * @var string
- */
- protected $identityColumn;
-
- /**
- * Credential column to check credential against.
- *
- * @var string
- */
- protected $credentialColumn;
-
- /**
- * Use supplied identity.
- *
- * @var string
- */
- protected $identity;
-
- /**
- * User supplied credential.
- *
- * @var string
+ * {@inheritDoc}
*/
- protected $credential;
-
- /**
- * User supplied credential.
- *
- * @var mixed
- */
- protected $credentialCallable;
-
- /**
- * Contains the authentication results.
- *
- * @var array
- */
- protected $authenticateResultInfo = array();
-
- /**
- * __construct() - Sets configuration options
- *
- * @param \Doctrine\ORM\EntityManager $em
- * @param string $tableName
- * @param string $identityColumn
- * @param string $credentialColumn
- * @param null|array|Closure $credentialCallable
- * @return void
- */
- public function __construct(EntityManager $em, $entity, $identityColumn = 'username',
- $credentialColumn = 'password', $credentialCallable = null)
- {
- $this->setEntityManager($em);
- $this->setEntity($entity);
- $this->setIdentityColumn($identityColumn);
- $this->setCredentialColumn($credentialColumn);
- $this->setCredentialCallable($credentialCallable);
- }
-
- /**
- * Defined by Zend_Auth_Adapter_Interface. This method is called to
- * attempt an authentication. Previous to this call, this adapter would have already
- * been configured with all necessary information to successfully connect to a database
- * table and attempt to find a record matching the provided identity.
- *
- * @throws \Zend\Authentication\Adapter\Exception if answering the authentication query is impossible
- * @return \Zend\Authentication\Result
- */
- public function authenticate()
- {
- $this->authenticateSetup();
- $query = $this->authenticateCreateQuery();
-
- if (!($identity = $this->authenticateValidateQuery($query))) {
- return $this->authenticateCreateAuthResult();
- }
-
- $authResult = $this->authenticateValidateIdentity($identity);
- return $authResult;
+ public function __construct(
+ EntityManager $em,
+ $entity,
+ $identityColumn = 'username',
+ $credentialColumn = 'password',
+ $credentialCallable = null
+ ) {
+ parent::__construct($em, $entity, $identityColumn, $credentialColumn, $credentialCallable);
}
@@ -143,11 +58,11 @@ public function authenticate()
*
* @param \Doctrine\ORM\EntityManager $em
* @return \DoctrineModule\Authentication\Adapater\DoctrineEntity
+ * @deprecated please use DoctrineModule\Authentication\Adapter\DoctrineObject
*/
public function setEntityManager(EntityManager $em)
{
- $this->em = $em;
- return $this;
+ $this->setObjectManager($em);
}
/**
@@ -155,10 +70,11 @@ public function setEntityManager(EntityManager $em)
*
* @param string $entity
* @return \DoctrineModule\Authentication\Adapater\DoctrineEntity
+ * @deprecated please use DoctrineModule\Authentication\Adapter\DoctrineObject
*/
public function setEntity($entity)
{
- $this->entity = $entity;
+ $this->setIdentityClassName($entity);
return $this;
}
@@ -167,10 +83,11 @@ public function setEntity($entity)
*
* @param string $value
* @return \DoctrineModule\Authentication\Adapater\DoctrineEntity
+ * @deprecated please use DoctrineModule\Authentication\Adapter\DoctrineObject
*/
public function setIdentity($value)
{
- $this->identity = $value;
+ $this->setIdentityValue($value);
return $this;
}
@@ -179,23 +96,11 @@ public function setIdentity($value)
*
* @param string $credential
* @return \DoctrineModule\Authentication\Adapater\DoctrineEntity
+ * @deprecated please use DoctrineModule\Authentication\Adapter\DoctrineObject
*/
public function setCredential($credential)
{
- $this->credential = $credential;
- return $this;
- }
-
- /**
- * Set the credential callable to be used to transform the password
- * before checking.
- *
- * @param string $callable
- * @return \DoctrineModule\Authentication\Adapater\DoctrineEntity
- */
- public function setCredentialCallable($callable)
- {
- $this->credentialCallable = $callable;
+ $this->setCredentialValue($credential);
return $this;
}
@@ -204,10 +109,11 @@ public function setCredentialCallable($callable)
*
* @param string $identityColumn
* @return \DoctrineModule\Authentication\Adapater\DoctrineEntity
+ * @deprecated please use DoctrineModule\Authentication\Adapter\DoctrineObject
*/
public function setIdentityColumn($identityColumn)
{
- $this->identityColumn = $identityColumn;
+ $this->setIdentityProperty($identityColumn);
return $this;
}
@@ -216,155 +122,11 @@ public function setIdentityColumn($identityColumn)
*
* @param string $credentialColumn
* @return \Zend\Authentication\Adapter\DbTable Provides a fluent interface
+ * @deprecated please use DoctrineModule\Authentication\Adapter\DoctrineObject
*/
public function setCredentialColumn($credentialColumn)
{
- $this->credentialColumn = $credentialColumn;
+ $this->setCredentialProperty($credentialColumn);
return $this;
}
-
- /**
- * Prepares the query by building it from QueryBuilder based on the
- * entity, credentialColumn and identityColumn.
- *
- * @return \Doctrine\ORM\Query
- */
- protected function authenticateCreateQuery()
- {
- $mdata = $this->em->getClassMetadata($this->entity);
- $qb = $this->em->createQueryBuilder();
-
- $qb->select('q')
- ->from($this->entity, 'q')
- ->where($qb->expr()->eq(
- 'q.' . $this->identityColumn,
- $qb->expr()->literal($this->identity)
- ));
-
- return $qb->getQuery();
- }
-
- /**
- * This method attempts to validate that the record in the resultset is indeed a
- * record that matched the identity provided to this adapter.
- *
- * @param object $identity
- * @return \Zend\Authentication\Result
- */
- protected function authenticateValidateIdentity($identity)
- {
- $getter = 'get' . ucfirst($this->credentialColumn);
- $vars = get_object_vars($identity);
- $entityCredential = null;
-
- if (method_exists($identity, $getter)) {
- $entityCredential = $identity->$getter();
- } else if (isset($identity->{$this->credentialColumn}) || isset($vars[$this->credentialColumn])) {
- $entityCredential = $identity->{$this->credentialColumn};
- } else {
- throw new \BadMethodCallException(sprintf(
- 'Property (%s) in (%s) is not accessible. You should implement %s::%s()',
- $this->credentialColumn,
- get_class($identity),
- get_class($identity),
- $getter
- ));
- }
-
- $credential = $this->credential;
- $callable = $this->credentialCallable;
- if ($callable) {
- if (!is_callable($callable)) {
- throw new RuntimeException(sprintf(
- 'failed to call algorithm function %s::%s(), does it exist?',
- $algorithm[0],
- $algorithm[1]
- ));
- }
- $credential = call_user_func($callable, $identity, $credential);
- }
-
- if ($credential != $entityCredential) {
- $this->authenticateResultInfo['code'] = AuthenticationResult::FAILURE_CREDENTIAL_INVALID;
- $this->authenticateResultInfo['messages'][] = 'Supplied credential is invalid.';
- return $this->authenticateCreateAuthResult();
- }
-
- $this->authenticateResultInfo['code'] = AuthenticationResult::SUCCESS;
- $this->authenticateResultInfo['identity'] = $identity;
- $this->authenticateResultInfo['messages'][] = 'Authentication successful.';
- return $this->authenticateCreateAuthResult();
- }
-
- /**
- * Validates the query. Catches exceptions from Doctrine and populates authenticate results
- * appropriately.
- *
- * @return bool|object
- */
- protected function authenticateValidateQuery(Query $query)
- {
- try {
- return $query->getSingleResult();
- } catch (NoResultException $e) {
- $this->authenticateResultInfo['code'] = AuthenticationResult::FAILURE_IDENTITY_NOT_FOUND;
- $this->authenticateResultInfo['messages'][] = 'A record with the supplied identity could not be found.';
- } catch (NonUniqueResultException $e) {
- $this->authenticateResultInfo['code'] = AuthenticationResult::FAILURE_IDENTITY_AMBIGUOUS;
- $this->authenticateResultInfo['messages'][] = 'More than one record matches the supplied identity.';
- }
-
- return false;
- }
-
- /**
- * This method abstracts the steps involved with making sure that this adapter was
- * indeed setup properly with all required pieces of information.
- *
- * @throws \Zend\Authentication\Adapter\Exception - in the event that setup was not done properly
- * @return bool
- */
- protected function authenticateSetup()
- {
- $exception = null;
-
- if ($this->entity == '') {
- $exception = 'An entity must be supplied for the DoctrineEntity authentication adapter.';
- } elseif ($this->identityColumn == '') {
- $exception = 'An identity column must be supplied for the DoctrineEntity authentication adapter.';
- } elseif ($this->credentialColumn == '') {
- $exception = 'A credential column must be supplied for the DoctrineEntity authentication adapter.';
- } elseif ($this->identity == '') {
- $exception = 'A value for the identity was not provided prior to authentication with DoctrineEntity.';
- } elseif ($this->credential === null) {
- $exception = 'A credential value was not provided prior to authentication with DoctrineEntity.';
- }
-
- if (null !== $exception) {
- throw new Exception\RuntimeException($exception);
- }
-
- $this->authenticateResultInfo = array(
- 'code' => AuthenticationResult::FAILURE,
- 'identity' => $this->identity,
- 'messages' => array()
- );
-
- return true;
- }
-
- /**
- * Creates a Zend_Auth_Result object from the information that has been collected
- * during the authenticate() attempt.
- *
- * @return \Zend\Authentication\Result
- */
- protected function authenticateCreateAuthResult()
- {
- return new AuthenticationResult(
- $this->authenticateResultInfo['code'],
- $this->authenticateResultInfo['identity'],
- $this->authenticateResultInfo['messages']
- );
- }
}
View
205 src/DoctrineModule/Authentication/Adapter/DoctrineObject.php
@@ -20,77 +20,76 @@
namespace DoctrineModule\Authentication\Adapter;
use Doctrine\Common\Persistence\ObjectManager,
- Zend\Authentication\Adapter\AdapterInterface,
- Zend\Authentication\Adapter\Exception,
- Zend\Authentication\Result as AuthenticationResult;
+Zend\Authentication\Adapter\AdapterInterface,
+Zend\Authentication\Adapter\Exception,
+Zend\Authentication\Result as AuthenticationResult;
/**
* Abstract authentication adapter that uses a Doctrine object for verification.
*
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org
- * @since 1.0
- * @version $Revision$
+ * @since 0.2.0
* @author Tim Roediger <superdweebie@gmail.com>
*/
class DoctrineObject implements AdapterInterface
{
/**
* Doctrine ObjectManager instance
- *
+ *
* @var \Doctrine\Common\Persistence\ObjectManager
*/
protected $om;
-
+
/**
* Doctrine object class that holds the identity.
- *
+ *
* @var string
*/
protected $identityClassName;
-
+
/**
* Identity property to check credential against.
- *
+ *
* @var string
*/
protected $identityProperty;
-
+
/**
* Credential property to check credential against.
- *
+ *
* @var string
*/
protected $credentialProperty;
-
+
/**
* User supplied identity.
- *
+ *
* @var string
*/
protected $identityValue;
-
+
/**
* User supplied credential.
- *
+ *
* @var string
*/
protected $credentialValue;
-
+
/**
* User supplied credential.
- *
+ *
* @var mixed
*/
protected $credentialCallable;
-
+
/**
* Contains the authentication results.
- *
+ *
* @var array
*/
protected $authenticationResultInfo = null;
-
+
/**
* __construct() - Sets configuration options
*
@@ -98,74 +97,76 @@ class DoctrineObject implements AdapterInterface
* @param string $identityClassName
* @param string $identityProperty
* @param string $credentialProperty
- * @param null|array|Closure $credentialCallable
+ * @param null|array|Closure $credentialCallable
* @return void
*/
public function __construct(
- ObjectManager $objectManager,
- $identityClassName,
+ ObjectManager $objectManager,
+ $identityClassName,
$identityProperty = 'username',
- $credentialProperty = 'password',
+ $credentialProperty = 'password',
$credentialCallable = null
) {
$this->setObjectManager($objectManager);
$this->setIdentityClassName($identityClassName);
$this->setIdentityProperty($identityProperty);
- $this->setCredentialProperty($credentialProperty);
+ $this->setCredentialProperty($credentialProperty);
+
if (null !== $credentialCallable) {
$this->setCredentialCallable($credentialCallable);
- }
+ }
}
-
+
/**
* {@inheritDoc}
- *
+ *
* @return \Zend\Authentication\Result
*/
public function authenticate()
- {
- $this->authenticateSetup();
- $repository = $this->om->getRepository($this->identityClassName);
+ {
+ $this->authenticateSetup();
+ $repository = $this->om->getRepository($this->identityClassName);
$identity = $repository->findOneBy(array($this->identityProperty => $this->identityValue));
- if(!$identity)
- {
- $this->authenticateResultInfo['code'] = AuthenticationResult::FAILURE_IDENTITY_NOT_FOUND;
- $this->authenticateResultInfo['messages'][] = 'A record with the supplied identity could not be found.';
- return $this->authenticateCreateAuthResult();
- }
+
+ if (!$identity) {
+ $this->authenticationResultInfo['code'] = AuthenticationResult::FAILURE_IDENTITY_NOT_FOUND;
+ $this->authenticationResultInfo['messages'][] = 'A record with the supplied identity could not be found.';
+ return $this->authenticateCreateAuthResult();
+ }
+
$authResult = $this->authenticateValidateIdentity($identity);
return $authResult;
}
-
+
/**
* Sets the object manager to use.
- *
+ *
* @param \Doctrine\Common\Persistence\ObjectManager $om
- * @return \DoctrineModule\Authentication\Adapater\DoctrineObject
+ * @return self
*/
public function setObjectManager(ObjectManager $om)
{
$this->om = $om;
return $this;
}
-
+
/**
* Sets the identity class to use for authentication.
- *
+ *
* @param string $identityClassName
- * @return \DoctrineModule\Authentication\Adapater\DoctrineObject
+ * @return self
*/
public function setIdentityClassName($identityClassName)
{
$this->identityClassName = (string) $identityClassName;
return $this;
}
-
+
/**
* Set the value to be used as the identity
*
* @param mixed $value
- * @return \DoctrineModule\Authentication\Adapater\DoctrineObject
+ * @return self
*/
public function setIdentityValue($identityValue)
{
@@ -177,40 +178,40 @@ public function setIdentityValue($identityValue)
* Set the credential value to be used.
*
* @param mixed $credentialValue
- * @return \DoctrineModule\Authentication\Adapater\DoctrineObject
+ * @return self
*/
public function setCredentialValue($credentialValue)
{
$this->credentialValue = $credentialValue;
return $this;
}
-
+
/**
* Set the credential callable to be used to transform the password
* before checking.
*
* @param string $callable
- * @throws \InvalidArgumentException if argument is not a callable function
- * @return \DoctrineModule\Authentication\Adapater\DoctrineObject
+ * @throws \InvalidArgumentException if argument is not a callable function
+ * @return self
*/
public function setCredentialCallable($callable)
{
- if(is_callable($callable)){
- $this->credentialCallable = $callable;
- } else {
+ if (!is_callable($callable)) {
throw new \InvalidArgumentException(sprintf(
- '%s is not a callable fuction',
- $callable
+ '"%s" is not a callable fuction',
+ is_string($callable) ? $callable : gettype($callable)
));
}
+
+ $this->credentialCallable = $callable;
return $this;
}
-
+
/**
* Set the property name to be used as the identity property
*
* @param string $identityProperty
- * @return \DoctrineModule\Authentication\Adapater\DoctrineObject
+ * @return self
*/
public function setIdentityProperty($identityProperty)
{
@@ -222,37 +223,37 @@ public function setIdentityProperty($identityProperty)
* Set the property name to be used as the credential property
*
* @param string $credentialField
- * @return \DoctrineModule\Authentication\Adapater\DoctrineObject
+ * @return self
*/
public function setCredentialProperty($credentialProperty)
{
$this->credentialProperty = (string) $credentialProperty;
return $this;
}
-
+
/**
- * This method attempts to validate that the record in the resultset is indeed a
+ * This method attempts to validate that the record in the resultset is indeed a
* record that matched the identity provided to this adapter.
*
* @param object $identity
* @throws \UnexpectedValueException - if the identity is not the class expected
- * @throws \BadMethodCallException - if the credentialProperty cannot be accessed on identity
+ * @throws \BadMethodCallException - if the credentialProperty cannot be accessed on identity
* @return \Zend\Authentication\Result
*/
protected function authenticateValidateIdentity($identity)
{
- if (!$identity instanceof $this->identityClassName){
+ if (!$identity instanceof $this->identityClassName) {
throw new \UnexpectedValueException(sprintf(
'Identity class type expected was %s, but got %s',
$this->identityClassName,
get_class($identity)
));
}
-
+
$getter = 'get' . ucfirst($this->credentialProperty);
$vars = get_object_vars($identity);
$documentCredential = null;
-
+
if (method_exists($identity, $getter)) {
$documentCredential = $identity->$getter();
} else if (isset($identity->{$this->credentialProperty}) || isset($vars[$this->credentialProperty])) {
@@ -266,27 +267,28 @@ protected function authenticateValidateIdentity($identity)
$getter
));
}
-
+
$credentialValue = $this->credentialValue;
- $callable = $this->credentialCallable;
+ $callable = $this->credentialCallable;
+
if ($callable) {
$credentialValue = call_user_func($callable, $identity, $credentialValue);
}
-
+
if ($credentialValue != $documentCredential) {
- $this->authenticateResultInfo['code'] = AuthenticationResult::FAILURE_CREDENTIAL_INVALID;
- $this->authenticateResultInfo['messages'][] = 'Supplied credential is invalid.';
+ $this->authenticationResultInfo['code'] = AuthenticationResult::FAILURE_CREDENTIAL_INVALID;
+ $this->authenticationResultInfo['messages'][] = 'Supplied credential is invalid.';
return $this->authenticateCreateAuthResult();
}
- $this->authenticateResultInfo['code'] = AuthenticationResult::SUCCESS;
- $this->authenticateResultInfo['identity'] = $identity;
- $this->authenticateResultInfo['messages'][] = 'Authentication successful.';
+ $this->authenticationResultInfo['code'] = AuthenticationResult::SUCCESS;
+ $this->authenticationResultInfo['identity'] = $identity;
+ $this->authenticationResultInfo['messages'][] = 'Authentication successful.';
return $this->authenticateCreateAuthResult();
}
-
+
/**
- * This method abstracts the steps involved with making sure that this adapter was
+ * This method abstracts the steps involved with making sure that this adapter was
* indeed setup properly with all required pieces of information.
*
* @throws \Zend\Authentication\Adapter\Exception\RuntimeException - in the event that setup was not done properly
@@ -294,28 +296,39 @@ protected function authenticateValidateIdentity($identity)
*/
protected function authenticateSetup()
{
- $exception = null;
-
- if ($this->identityClassName == '') {
- $exception = 'An identityClassName must be supplied for the DoctrineObject authentication adapter.';
- } elseif ($this->identityProperty == '') {
- $exception = 'An identity property must be supplied for the DoctrineObject authentication adapter.';
- } elseif ($this->credentialProperty == '') {
- $exception = 'A credential property must be supplied for the DoctrineObject authentication adapter.';
- } elseif ($this->identityValue == '') {
- $exception = 'A value for the identity was not provided prior to authentication with '
- .'DoctrineObject authentication adapter.';
- } elseif ($this->credentialValue === null) {
- $exception = 'A credential value was not provided prior to authentication with '
- .'DoctrineObject authentication adapter.';
+ if (!$this->identityClassName) {
+ throw new Exception\RuntimeException(
+ 'An identityClassName must be supplied for the DoctrineObject authentication adapter'
+ );
+ }
+
+ if (!$this->identityProperty) {
+ throw new Exception\RuntimeException(
+ 'An identity property must be supplied for the DoctrineObject authentication adapter'
+ );
+ }
+
+ if (!$this->credentialProperty) {
+ throw new Exception\RuntimeException(
+ 'A credential property must be supplied for the DoctrineObject authentication adapter'
+ );
+ }
+
+ if (null === $this->identityValue) {
+ throw new Exception\RuntimeException(
+ 'A value for the identity was not provided prior to authentication with DoctrineObject authentication '
+ . 'adapter'
+ );
}
- if (null !== $exception) {
- throw new Exception\RuntimeException($exception);
+ if (null === $this->credentialValue) {
+ throw new Exception\RuntimeException(
+ 'A credential value was not provided prior to authentication with DoctrineObject authentication adapter'
+ );
}
- $this->authenticateResultInfo = array(
- 'code' => AuthenticationResult::FAILURE,
+ $this->authenticationResultInfo = array(
+ 'code' => AuthenticationResult::FAILURE,
'identity' => $this->identityValue,
'messages' => array()
);
@@ -324,7 +337,7 @@ protected function authenticateSetup()
}
/**
- * Creates a Zend_Auth_Result object from the information that has been collected
+ * Creates a Zend_Auth_Result object from the information that has been collected
* during the authenticate() attempt.
*
* @return \Zend\Authentication\Result
@@ -332,9 +345,9 @@ protected function authenticateSetup()
protected function authenticateCreateAuthResult()
{
return new AuthenticationResult(
- $this->authenticateResultInfo['code'],
- $this->authenticateResultInfo['identity'],
- $this->authenticateResultInfo['messages']
+ $this->authenticationResultInfo['code'],
+ $this->authenticationResultInfo['identity'],
+ $this->authenticationResultInfo['messages']
);
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.