6

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?

Community
  • 1
  • 1

2 Answers2

10

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.

hohner
  • 11,498
  • 8
  • 49
  • 84
  • Okay I change it but now it tells me `Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for doctrine.authentication.orm_default`. I change the getter to `public function getAuthAdapter() { if (null === $this->authAdapter) { $this->authAdapter = $this->getServiceManager() ->get('doctrine.authentication.orm_default'); } return $this->authAdapter;` – Matus von Matushausen Mar 02 '13 at 15:28
  • Thanks for your help. Now is only one problem left. After `$authResult = $authService->authenticate();` I get a empty page. No error, nothing. – Matus von Matushausen Mar 02 '13 at 15:56
  • In your Entity, are you implementing any interfaces or extending any base classes? – hohner Mar 02 '13 at 15:59
  • No only the entity generated with `vendor/bin/doctrine-module orm:convert-mapping --from-database annotation module/Profile/src/ --namespace="Profile\\Entity\\" ` and the setter and getter. – Matus von Matushausen Mar 02 '13 at 16:05
  • Okay I don't know what the error was but now it works. Thanks for your great help and please excuse me for my english it's not my native language. – Matus von Matushausen Mar 02 '13 at 16:30
  • @MatusvonMatushausen Feel free to mark this question as resolved if you have your answer :) – hohner Mar 02 '13 at 17:01
  • When I find the link to do that :) Or only edit the title with 'resolved:' ? – Matus von Matushausen Apr 03 '13 at 09:08
0

If you are using 'Zend\Authentication\AuthenticationService' in Module.php as suggested by Hohner, this will not work with the BjyAuthorize Module roles and ACL. BjyAuthorize will default to its own default configuration of the Authentication service which uses 'ZfcUser\Authentication\Storage\Db'. To get BjyAuthorize to use the Doctrine identity replace it with (or add ) 'zfcuser_auth_service' as follows:

'zfcuser_auth_service' => function ($serviceManager) {
    return  $authenticationService = $serviceManager->get('doctrine.authenticationservice.orm_default');
},

Then you also use it in the controller in a similar way:

$authService = $this->getServiceLocator()->get( 'zfcuser_auth_service' );
Onshop
  • 2,915
  • 1
  • 27
  • 17