Zend 2 + doctrine 2 Auth Adapter

I am looking for an authentication tutorial with Zend 2 and Doctrine 2. In particular, creating a controller and adapter.

The official documentation is too global to be enough for me.

Thank you

EDIT:

I use "Entity Doctrine" (User \ Entity namespace;)

The object is registered in the module.config.php file:

'doctrine' => array( 'driver' => array( __NAMESPACE__ . '_driver' => array( 'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver', 'cache' => 'array', 'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity') ), 'orm_default' => array( 'drivers' => array( __NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver' ) ) ), ) 

But now, how can I point my identityClass key to my adapter? Controller:

 use Zend\Mvc\Controller\AbstractActionController, Zend\View\Model\ViewModel, Zend\Authentication\AuthenticationService, Doctrine\ORM\EntityManager, DoctrineModule\Authentication\Adapter\ObjectRepository as DoctrineAdapter, User\Entity\User, User\Form\UserForm; class UserController extends AbstractActionController { protected $em; public function setEntityManager(EntityManager $em) { $this->em = $em; } public function getEntityManager() { if (null === $this->em) $this->em = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager'); return $this->em; } public function getRepository() { if (null === $this->em) $this->em = $this->getEntityManager()->getRepository('User\Entity\User'); return $this->em; } public function loginAction() { .... ???????????? $adapter = new DoctrineAdapter(); $adapter->setIdentityValue($username); $adapter->setCredentialValue($password); $auth = new AuthenticationService(); $result=$auth->authenticate($adapter); ???????????? } } 

I have this error: call the getRepository () member function for the non-object in ... doctrine \ doctrine-module \ src \ DoctrineModule \ Options \ AuthenticationAdapter.php on line 132 line 123: return $ this-> objectManager-> getRepository ($ this-> identityClass);

+6
source share
1 answer

There are many ways to do this, but the DoctrineModule for zf2 comes with a doctrine-based authentication adapter ( DoctrineModule\Authentication\Adapter\ObjectRepository ). There is a factory ( DoctrineModule\Service\AuthenticationAdapterFactory ) to create an adapter. DoctrineMongoODMModule has a module.config.php module for using these services. (Note that the factory and adapter will work with ORM, but I'm not sure if the configuration keys are already added to the DoctrineORMModule. Maybe someone who reads this would like to create a PR for this?) This is the appropriate key configuration:

  'authenticationadapter' => array( 'odm_default' => array( 'objectManager' => 'doctrine.documentmanager.odm_default', 'identityClass' => 'Application\Model\User', 'identityProperty' => 'username', 'credentialProperty' => 'password', 'credentialCallable' => 'Application\Model\User::hashPassword' ), ), 

identityClass is a doctrine document that your authenticated user represents. identityProperty usually the username. getUsername will be called by the adapter to access this. credentialProperty usually a password. getPassword will be called by the adapter to access this. credentialCallable is optional. It should be a callable (method, static method, closure) that will hash credentialProperty - you don't need to do this, but it's usually a good idea. The adapter expects the caller to have the following form: function hashPassword($identity, $plaintext) .

To use the authentication adapter:

 $serviceLocator->get('doctrine.authenticationadapter.odm_default'); 

Please note that all this gives you only the authentication adapter, it does not actually perform authentication. Authentication is performed like this:

 $adapter = $serviceLocator->get('doctrine.authenticationadapter.odm_default'); $adapter->setIdentityValue($username); $adapter->setCredentialValue($password); $authService = new Zend\Authentication\AuthenticationService $result = $authService->authenticate($adapter); 

This will save the entire authenticated user doctrine document in the session object. If you want to store only the document identifier in the session object and retrieve the rest of the authenticated user document from the database of each request, look at DoctrineModule\Authentication\Storage\ObjectRepository . This gives a new StorageInterface for Zend\Authentication\AuthenticationService .

+15
source

Source: https://habr.com/ru/post/923556/


All Articles