Zend 2 + 学说 2 认证适配器 [英] Zend 2 + doctrine 2 Auth Adapter

查看:23
本文介绍了Zend 2 + 学说 2 认证适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找有关使用 Zend 2 和 Doctrine 2 进行身份验证的教程.特别是控制器和适配器的创建.

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 global not help me enough.

谢谢

我使用Doctrine Entity"(命名空间 UserEntity;)

i use "Doctrine Entity" (namespace UserEntity;)

实体在module.config.php文件中注册:

The Entity is register in module.config.php file :

'doctrine' => array(
    'driver' => array(
        __NAMESPACE__ . '_driver' => array(
            'class' => 'DoctrineORMMappingDriverAnnotationDriver',
            'cache' => 'array',
            'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity')
        ),
        'orm_default' => array(
            'drivers' => array(
                __NAMESPACE__ . 'Entity' => __NAMESPACE__ . '_driver'
            )
        )          
    ),
)

但是现在,我如何将我的 identityClass 键指向我的适配器?控制器:

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

use ZendMvcControllerAbstractActionController,
    ZendViewModelViewModel,
    ZendAuthenticationAuthenticationService,
    DoctrineORMEntityManager,
    DoctrineModuleAuthenticationAdapterObjectRepository as DoctrineAdapter,        
    UserEntityUser,  
    UserFormUserForm;
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('DoctrineORMEntityManager');
    return $this->em;
} 

public function getRepository()
{
    if (null === $this->em) 
        $this->em = $this->getEntityManager()->getRepository('UserEntityUser');
    return $this->em;
} 

public function loginAction()
{
    ....
    ????????????
    $adapter = new DoctrineAdapter();
    $adapter->setIdentityValue($username);
    $adapter->setCredentialValue($password);
    $auth = new AuthenticationService();    
    $result=$auth->authenticate($adapter);
    ????????????

}

}

我遇到了这个错误:在第 132 行的 ...doctrinedoctrine-modulesrcDoctrineModuleOptionsAuthenticationAdapter.php 中的非对象上调用成员函数 getRepository()第 123 行:返回 $this->objectManager->getRepository($this->identityClass);

I've got this error : Call to a member function getRepository() on a non-object in ...doctrinedoctrine-modulesrcDoctrineModuleOptionsAuthenticationAdapter.php on line 132 line 123 : return $this->objectManager->getRepository($this->identityClass);

推荐答案

有很多方法可以做到,但是 DoctrineModule for zf2 附带了一个基于 DoctrineModuleAuthenticationAdapterObjectRepository 的认证适配器(DoctrineModuleAuthenticationAdapterObjectRepository).还有一个工厂来创建适配器 (DoctrineModuleServiceAuthenticationAdapterFactory).DoctrineMongoODMModule 设置了 module.config.php 来使用这些服务.(请注意,工厂和适配器将与 ORM 一起使用,但我不确定是否已将配置键添加到 DoctrineORMModule 中 - 也许阅读此内容的人会为此创建 PR?)这些是相关的配置键:

There are lots of ways to do it, but DoctrineModule for zf2 ships with a doctrine based authentication adapter (DoctrineModuleAuthenticationAdapterObjectRepository). There is also a factory to create the adapter (DoctrineModuleServiceAuthenticationAdapterFactory). 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' => 'ApplicationModelUser',
            'identityProperty' => 'username',
            'credentialProperty' => 'password',
            'credentialCallable' => 'ApplicationModelUser::hashPassword'
        ),
    ),

identityClass 是代表经过身份验证的用户的准则文档.identityProperty 通常是用户名.getUsername 将被适配器调用以访问它.credentialProperty 通常是密码.getPassword 将被适配器调用以访问它.credentialCallable 是可选的.它应该是一个可调用的(方法、静态方法、闭包),它将散列 credentialProperty - 您不需要这样做,但这通常是一个好主意.适配器期望可调用对象具有以下形式:function hashPassword($identity, $plaintext).

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 ZendAuthenticationAuthenticationService
$result = $authService->authenticate($adapter);

这会将经过身份验证的用户的整个学说文档存储在会话对象中.如果您只想在会话对象中存储文档 ID,并在每个请求中从数据库中检索经过身份验证的用户文档的其余部分,请查看 DoctrineModuleAuthenticationStorageObjectRepository.这为 ZendAuthenticationAuthenticationService 提供了一个新的 StorageInterface.

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 DoctrineModuleAuthenticationStorageObjectRepository. This provides a new StorageInterface for the ZendAuthenticationAuthenticationService.

这篇关于Zend 2 + 学说 2 认证适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆