使用Doctrine 2进行Zend 2身份验证检查是否已登录

时间:2013-04-15 19:15:42

标签: php authentication login doctrine-orm zend-framework2

我安装了Zend 2,将Doctrine 2作为我的ORM。

目前我尝试使用doctrine2实现身份验证。

这很有效。

但是现在我想在用户登录时检查layout.phtml。

我不知道如何从layout.phtml访问authentifcation服务 在这个页面上(https://github.com/doctrine/DoctrineModule/blob/master/docs/authentication.md),他们说     $这 - >标识()

但是这没用。它每次都返回NULL。

module.config.php

'authentication' => array(
            'orm_default' => array(
                'object_manager' => 'Doctrine\ORM\EntityManager',
                'identity_class' => 'Application\Model\User',
                'identity_property' => 'id',
                'credential_property' => 'password'
            ),
        ),

Module.php

public function getServiceConfig()
{
    return array(
        'factories'=>array(
            'Application\Model\AuthStorage' => function($sm){
                return new \Application\Model\AuthStorage('site');
            },

            'Zend\Authentication\AuthenticationService' => function($serviceManager) {
                return $serviceManager->get('doctrine.authenticationservice.orm_default');
            },
        ),
    );
}

1 个答案:

答案 0 :(得分:0)

如果您使用md5加密密码,请尝试将orm_default更改为:

'orm_default' => array(
    'object_manager' => 'Doctrine\ORM\EntityManager',
    'identity_class' => 'Application\Entity\User',
    'identity_property' => 'username',
    'credential_property' => 'password',
    'credentialCallable' => function ($userObj, $password) {
        return ($userObj->getPassword() === md5($password));
     }
),

如果您没有使用md5进行加密,则可以根据您的使用情况进行调整。