Zend 2 + doctrine 2验证适配器 [英] Zend 2 + doctrine 2 Auth Adapter

查看:177
本文介绍了Zend 2 + doctrine 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(命名空间User\Entity;)

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

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

The Entity is register in 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'
            )
        )          
    ),
)

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

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);
    ????????????

}

}

得到这个错误:调用一个非对象上的成员函数getRepository()... doctrine\doctrine-module\src\DoctrineModule\Options\AuthenticationAdapter.php第132行
第123行:return $ this-> objectManager-> getRepository($ this-> identityClass);

I've got this error : Call to a member function getRepository() on a non-object in ...doctrine\doctrine-module\src\DoctrineModule\Options\AuthenticationAdapter.php on line 132 line 123 : return $this->objectManager->getRepository($this->identityClass);

推荐答案

有很多方法可以做到,但是ZF2的DoctrineModule附带了基于教义的认证适配器( DoctrineModule\Authentication\Adapter\ObjectRepository )。还有一个工厂来创建适配器( DoctrineModule\Service\AuthenticationAdapterFactory )。 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 (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'
        ),
    ),

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).

要使用认证适配器:

$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);

这将将已验证用户的整个原则文档存储在会话对象中。如果要仅将文档ID存储在会话对象中,并从每个请求的DB中检索其他自动使用的用户文档,那么请查看 DoctrineModule\Authentication\Storage\ObjectRepository 。这为 Zend\Authentication\AuthenticationService 提供了一个新的 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 DoctrineModule\Authentication\Storage\ObjectRepository. This provides a new StorageInterface for the Zend\Authentication\AuthenticationService.

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

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