Authentication Zend2 + Doctrine2

I would like to have authentication using Doctrine 2 and ZF2. To get some help, I used the Zend 2 + doctrine 2 Auth Adapter , but every time I call $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. It shows the following:

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

But if I did var_dump in authService, all sets are null. In my service, where I want to login, I call

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

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

Other things in $ authAdabter:

 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 getAuth attribute is as follows:

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

So can someone tell me how to set the parameters correctly?

+6
source share
2 answers

It looks like you are using the wrong configuration values. If you look at the DoctrineORM documentation, they use the following to configure the authentication adapter configuration:

 '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 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 the following:

 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 .

+10
source

If you use "Zend \ Authentication \ AuthenticationService" in Module.php as suggested by Hochner, this will not work with the BjyAuthorize Module and ACL roles. BjyAuthorize will use its own default configuration for the authentication service, which uses "ZfcUser \ Authentication \ Storage \ Db" by default. To force BjyAuthorize to use the Doctrine identifier, replace it (or add it) with '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' ); 
0
source

All Articles