1

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

  • 团队名字
  • 用户名
  • 用户密码

我有两张桌子

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

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

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

  1. name在表格中查找分组依据groups
  2. 在有条件name的表中查找用户userswhere group_id=<group_id>

如何在 Zend Framework 2 中使用 Doctrine 2 正确地做到这一点?所有官方文档和示例都描述了身份属性为单列(示例)的情况。

对不起我的语言不好。谢谢。

4

2 回答 2

1

我没有自己实现 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;
    }
}

$form->isValid()在控制器内部,调用以确保用户输入了正确的身份验证数据就足够了。

于 2013-12-14T20:38:55.023 回答
0

我也有同样的问题。

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

一条通往管理员的路线 -> /admin/login 和其他用户的前端 -> /login

我试图在学说身份验证数组中进行更多身份验证,但没有奏效。

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

于 2013-04-28T04:57:36.017 回答