使用Doctrine 2在ZF2中进行身份验证的多个标识属性

时间:2013-04-25 15:11:26

标签: authentication doctrine-orm zend-framework2

我有输入文本字段的登录表单:

  • 群组名称
  • 用户名
  • 用户密码

我有两张桌子

  • groups
    • id
    • name
  • users
    • id
    • name
    • group_id

我有它的映射实体和关联。

但是用户名在表users中并不唯一,因为不同的组可以包含名称相同的用户。因此我需要:

  1. 在表name
  2. 中按groups查找分组
  3. 在条件name
  4. 的表users中按where group_id=<group_id>查找用户

    如何使用Doctrine 2在Zend Framework 2中正确执行此操作? 所有官方文档和示例都描述了情境,其中identity属性是单列(example)。

    抱歉我的语言不好。感谢。

2 个答案:

答案 0 :(得分:1)

我决定通过我的身份验证表单的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;
    }
}

在内部控制器中,只需调用$form->isValid()即可确保用户输入正确的身份验证数据。

答案 1 :(得分:0)

我有同样的问题。

我必须在同一个应用程序中进行两次身份验证,因为我的老板并不想要两个数据库。所以,我必须制作两个用户表和两个登录页面。

管理员的一条路线 - &gt; /管理/登入 而其他用户的前端 - &gt; /登录

我试图在doctrine身份验证数组中进行更多身份验证,但它没有用。

我想我会在doctrine github页面上打开一个问题。