bew*_*eed 6 zend-auth doctrine-orm zend-framework2
我在寻找身份验证的教程与Zend 2和学说2.特别是创建控制器和适配器.
官方文档过于全球化对我没有帮助.
谢谢
编辑:
我使用"Doctrine Entity"(名称空间User\Entity;)
实体在module.config.php文件中注册:
'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'
)
)
),
)
Run Code Online (Sandbox Code Playgroud)
但是现在,我如何将我的identityClass键指向我的适配器?控制器:
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);
????????????
}
}
Run Code Online (Sandbox Code Playgroud)
我有这个错误:在第132行第123行的... doctrine\doctrine-module\src\DoctrineModule\Options\AuthenticationAdapter.php中的非对象上调用成员函数getRepository():return $ this-> objectManager-> getRepository($这个 - > identityClass);
sup*_*bie 15
有很多方法可以做到这一点,但是ztr2的DoctrineModule附带了一个基于学说的身份验证适配器(DoctrineModule\Authentication\Adapter\ObjectRepository).还有一个工厂来创建适配器(DoctrineModule\Service\AuthenticationAdapterFactory).DoctrineMongoODMModule将它的module.config.php设置为使用这些服务.(请注意,工厂和适配器将与ORM一起使用,但我不确定配置键是否已添加到DoctrineORMModule中 - 也许读取此内容的人想为此创建PR?)这些是相关的配置键:
'authenticationadapter' => array(
'odm_default' => array(
'objectManager' => 'doctrine.documentmanager.odm_default',
'identityClass' => 'Application\Model\User',
'identityProperty' => 'username',
'credentialProperty' => 'password',
'credentialCallable' => 'Application\Model\User::hashPassword'
),
),
Run Code Online (Sandbox Code Playgroud)
这identityClass是代表经过身份验证的用户的学说文档.该identityProperty是正常的用户名.getUsername适配器将调用它来访问它.credentialProperty通常是密码.getPassword适配器将调用它来访问它.credentialCallable是可选的.它应该是一个可调用的(方法,静态方法,闭包),它将对credentialProperty进行散列 - 你不需要这样做,但它通常是一个好主意.适配器将期望callable具有以下形式:function hashPassword($identity, $plaintext).
要使用身份验证适配器:
$serviceLocator->get('doctrine.authenticationadapter.odm_default');
Run Code Online (Sandbox Code Playgroud)
请注意,所有这些只为您提供了一个验证适配器,它实际上并不进行身份验证.身份验证是这样完成的:
$adapter = $serviceLocator->get('doctrine.authenticationadapter.odm_default');
$adapter->setIdentityValue($username);
$adapter->setCredentialValue($password);
$authService = new Zend\Authentication\AuthenticationService
$result = $authService->authenticate($adapter);
Run Code Online (Sandbox Code Playgroud)
这将把经过身份验证的用户的整个学说文档存储在会话对象中.如果您只想在会话对象中存储文档ID,并从每个请求的DB中检索其余的已验证用户文档,那么请查看DoctrineModule\Authentication\Storage\ObjectRepository.这提供了新StorageInterface的Zend\Authentication\AuthenticationService.
| 归档时间: |
|
| 查看次数: |
9727 次 |
| 最近记录: |