Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend2 + Doctrine2 Authentication

I would like to have authentication using Doctrine 2 and ZF2. To get some help I used Zend 2 + doctrine 2 Auth Adapter, but every time I call the $authService->authenticate($adapter); I get an error that the class '' does not exist.

It seems that the config from my module.config.php won#t be used. It shows like this:

'authenticationadapter' => array(
        'orm_default' => array(
            'objectManager' => 'doctrine.entitymanager.orm_default',
            'identityClass' => 'Profile\Entity\User',
            'identityProperty' => 'username',
            'credentialProperty' => 'password',
        ),
    ),

But if i made a var_dump on the authService all sets are null. In my service where I want to do the login I call

$authAdapter = $this->getAuthAdapter();
$authAdapter->setIdentityValue($username);
$authAdapter->setCredentialValue($password);

A dump from the $authAdapter tells me that the IdentityValue and the CredentialValue are set correctly.

The other things in the $authAdabter are:

protected 'options' => 
    object(DoctrineModule\Options\Authentication)[281]
      protected 'objectManager' => 
        object(Doctrine\ORM\EntityManager)[248]
          private 'config' => 
            object(Doctrine\ORM\Configuration)[250]
              ...
          private 'conn' => 
            object(Doctrine\DBAL\Connection)[252]
              ...
          private 'metadataFactory' => 
            object(Doctrine\ORM\Mapping\ClassMetadataFactory)[266]
              ...
          private 'repositories' => 
            array (size=0)
              ...
          private 'unitOfWork' => 
            object(Doctrine\ORM\UnitOfWork)[267]
              ...
          private 'eventManager' => 
            object(Doctrine\Common\EventManager)[242]
              ...
          private 'hydrators' => 
            array (size=0)
              ...
          private 'proxyFactory' => 
            object(Doctrine\ORM\Proxy\ProxyFactory)[268]
              ...
          private 'expressionBuilder' => null
          private 'closed' => boolean false
          private 'filterCollection' => null
      protected 'objectRepository' => null
      protected 'identityClass' => null
      protected 'identityProperty' => null
      protected 'credentialProperty' => null
      protected 'credentialCallable' => null
      protected 'classMetadata' => null
      protected 'storage' => null
      protected '__strictMode__' => boolean true
  protected 'authenticationResultInfo' => null

The getAuthAdapter shows like this:

public function getAuthAdapter()
{
    if (null === $this->authAdapter) {
        $this->authAdapter = $this->getServiceManager()
            ->get('doctrine.authenticationadapter.ormdefault');
    }
    return $this->authAdapter;
}

So can some one tell me how to set the options correctly?

like image 713
Matus von Matushausen Avatar asked Mar 02 '13 14:03

Matus von Matushausen


1 Answers

It looks like you're using the wrong configuration values. If you look at the DoctrineORM documentation, they use the following to set the configuration for the authentication adapter:

'doctrine' => array(
    'authentication' => array(
        'orm_default' => array(
            'object_manager' => 'Doctrine\ORM\EntityManager',
            'identity_class' => 'Application\Entity\User',
            'identity_property' => 'email',
            'credential_property' => 'password',
        ),
    ),
)

So, instead of using authenticationadapter use authentication in your module.config.php; instead of using objectManager use object_manager, etc.


In your Module.php, you will need to add this:

use Zend\Authentication\AuthenticationService;

...

public function getServiceConfig()
{
    return array(
        'factories' => array(
            'Zend\Authentication\AuthenticationService' => function($serviceManager) {
                return $serviceManager->get('doctrine.authenticationservice.orm_default');

            }
        )
    );
}

And in your controller, you can access the Adapter using:

$authService = $this->getServiceLocator()->get('Zend\Authentication\AuthenticationService');

$adapter = $authService->getAdapter();
$adapter->setIdentityValue($data['login']);
$adapter->setCredentialValue($data['password']);

Please follow the documentation.

like image 184
hohner Avatar answered Sep 19 '22 22:09

hohner