Authentication ZF2中认证的多个身份属性与原则2
我有带有输入文本字段的登录表单:Authentication ZF2中认证的多个身份属性与原则2,authentication,doctrine-orm,zend-framework2,Authentication,Doctrine Orm,Zend Framework2,我有带有输入文本字段的登录表单: 组名 用户名 用户密码 我有两张桌子 组 id name 用户 id name group\u id 我有它的映射实体和关联 但用户名在表users中不唯一,因为不同的组可以包含具有相同名称的用户。因此,我需要: 在表groups 在表users中按name查找用户,条件为where group\u id= 如何使用条令2在Zend Framework 2中正确执行? 所有官方文档和示例都描述了身份属性为单列()的情况 对不起,我的语言不好。谢谢
- 组名
- 用户名
- 用户密码
组
id
name
用户
id
name
group\u id
users
中不唯一,因为不同的组可以包含具有相同名称的用户。因此,我需要:
groups
users
中按name
查找用户,条件为where group\u id=
对不起,我的语言不好。谢谢。我也有同样的问题 我必须在同一个应用程序中进行两次身份验证,因为我的老板不想要两个数据库。因此,我必须创建两个用户表和两个登录页面 一个到admin->/admin/login的路由 以及其他用户的前端->/login 我曾尝试在doctrine authentication array中添加更多身份验证,但没有成功
我想我会在github页面上打开一个问题。我也有同样的问题 我必须在同一个应用程序中进行两次身份验证,因为我的老板不想要两个数据库。因此,我必须创建两个用户表和两个登录页面 一个到admin->/admin/login的路由 以及其他用户的前端->/login 我曾尝试在doctrine authentication array中添加更多身份验证,但没有成功
我想我会在doctrine github页面上打开一个问题。我没有自己实现doctrine的身份验证服务,而是决定通过身份验证表单的isValid()方法中的表单验证来实现它 例如:
<?php
namespace My\Form\Namespace;
use Zend\Form\Form;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\InputFilter\InputFilterProviderInterface;
class Auth extends Form implement InputFilterProviderInterface
{
protected $_em;
public function __construct(ServiceLocatorInterface $sm)
{
parent::__construct('auth');
// inject Doctrine's Entity Manager
$this->_em = $sm->get('Doctrine\ORM\EntityManager');
// login field
$this->add(...);
// password field
$this->add(...);
// group_name field
$this->add(...);
}
public function getInputFilterSpecification()
{
//Input filter specification here
...
}
public function isValid()
{
/*
* input filter validations
*/
if (!parent::isValid())
return false;
/*
* group exists validation
*/
$group = $this->_em
->getRepository('<Group\Entity\Namespace>')
->findOneBy(array(
'name' => $this->get('group_name')->getValue(),
));
if (!$group){
$this->get('group_name')
->setMessages(array(
'Group not found',
));
return false;
}
/*
* user exists validation
*/
$user = $this->_em
->getRepository('<User\Entity\Namespace>')
->findOneBy(array(
'group_id' => $group->getId(),
'name' => $this->get('login')->getValue(),
));
if (!$user){
/*
* It's not good idea to tell that user not found,
* so let it be password error
*/
$this->get('password')
->setMessages(array(
'Login or password wrong',
));
return false;
}
/*
* password validation
*/
$password = $this->get('password')->getValue();
// assume that password hash just md5 of password string
if (md5($password) !== $user->getPassword()){
$this->get('password')
->setMessages(array(
'Login or password wrong',
));
return false;
}
return true;
}
}
我决定通过身份验证表单的isValid()方法中的表单验证来实现Doctrine的身份验证服务,而不是自己实现
例如:
<?php
namespace My\Form\Namespace;
use Zend\Form\Form;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\InputFilter\InputFilterProviderInterface;
class Auth extends Form implement InputFilterProviderInterface
{
protected $_em;
public function __construct(ServiceLocatorInterface $sm)
{
parent::__construct('auth');
// inject Doctrine's Entity Manager
$this->_em = $sm->get('Doctrine\ORM\EntityManager');
// login field
$this->add(...);
// password field
$this->add(...);
// group_name field
$this->add(...);
}
public function getInputFilterSpecification()
{
//Input filter specification here
...
}
public function isValid()
{
/*
* input filter validations
*/
if (!parent::isValid())
return false;
/*
* group exists validation
*/
$group = $this->_em
->getRepository('<Group\Entity\Namespace>')
->findOneBy(array(
'name' => $this->get('group_name')->getValue(),
));
if (!$group){
$this->get('group_name')
->setMessages(array(
'Group not found',
));
return false;
}
/*
* user exists validation
*/
$user = $this->_em
->getRepository('<User\Entity\Namespace>')
->findOneBy(array(
'group_id' => $group->getId(),
'name' => $this->get('login')->getValue(),
));
if (!$user){
/*
* It's not good idea to tell that user not found,
* so let it be password error
*/
$this->get('password')
->setMessages(array(
'Login or password wrong',
));
return false;
}
/*
* password validation
*/
$password = $this->get('password')->getValue();
// assume that password hash just md5 of password string
if (md5($password) !== $user->getPassword()){
$this->get('password')
->setMessages(array(
'Login or password wrong',
));
return false;
}
return true;
}
}
你得到答案了吗?我也有同样的问题。我决定编写自己的身份验证服务实现。当我完成这个任务后,我会发布它。你得到答案了吗?我也有同样的问题。我决定编写自己的身份验证服务实现。我将在以后完成此任务时发布它。