Zend 2 + doctrine 2 Auth Adapter

前端 未结 1 1424
醉话见心
醉话见心 2020-12-16 05:37

I\'m looking for a tutorial on authentication with Zend 2 and Doctrine 2. In particular the creation of the controller and adapter.

The official documentation is too

相关标签:
1条回答
  • 2020-12-16 06:24

    There are lots of ways to do it, but DoctrineModule for zf2 ships with a doctrine based authentication adapter (DoctrineModule\Authentication\Adapter\ObjectRepository). There is also a factory to create the adapter (DoctrineModule\Service\AuthenticationAdapterFactory). DoctrineMongoODMModule has it's module.config.php set up to use these services. (Note that the factory and adapter will work with ORM, but I'm not sure if the config keys have been added to DoctrineORMModule yet - perhaps someone who reads this would like create a PR for that?) These are the relevant config keys:

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

    The identityClass is the doctrine document that represents your authenticated user. The identityProperty is the normally the username. getUsername will be called by the adapter to access this. credentialProperty is normally the 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 the credentialProperty - you don't need to do this, but it's normally a good idea. The adapter will expect the callable to have the following form: function hashPassword($identity, $plaintext).

    To get the authentication adapter use:

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

    Note that all this only gives you an authetication adapter, it doesn't actually do the authentication. Authentication is done something 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 store the whole doctrine document of the authenticated user in the session object. If you want to store only the document ID in the session object, and retrieve the rest of the authetnicated user document form the DB each request, then take a look at DoctrineModule\Authentication\Storage\ObjectRepository. This provides a new StorageInterface for the Zend\Authentication\AuthenticationService.

    0 讨论(0)
提交回复
热议问题