1

入力テキストフィールドを持つログインフォームがあります:

  • グループ名
  • ユーザー名
  • 利用者パスワード

私は2つのテーブルを持っています

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

マッピング エンティティと関連付けがあります。

usersただし、異なるグループには同じ名前のユーザーが含まれる可能性があるため、ユーザー名は table 内で一意ではありません。したがって、私は必要です:

  1. nameテーブルでグループ化を検索groups
  2. 条件付きnameのテーブルでユーザーを見つけるuserswhere group_id=<group_id>

Doctrine 2を使用してZend Framework 2で正しく行うには? すべての公式ドキュメントと例は、identity プロパティが単一の列 ( example ) である状況を示しています。

私の悪い言語で申し訳ありません。ありがとう。

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 に答える