2013-04-25 26 views
1

我已經登錄表單的輸入文本字段:多重身份屬性與原則2

  • 集團名稱
  • 用戶名
  • 用戶密碼

我有兩個表

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

我有它的映射實體和關聯。

但是用戶名稱在表users內不唯一,因爲不同的組可以包括具有相同名稱的用戶。因此我需要:

  1. 查找組由name表由namegroups
  2. 發現用戶在表users與條件where group_id=<group_id>

如何使用Doctrine 2這樣做是正確的Zend框架2? 所有官方文檔和示例描述了身份屬性爲單列的情況(example)。

抱歉,我的語言能力差。謝謝。

+0

你有沒有得到旅遊答案?我有同樣的問題 – 2013-12-06 06:54:17

+0

不,我決定寫我自己的認證服務的實現。我將在稍後完成此任務後發佈。 – Microbe 2013-12-10 12:47:53

回答

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(),以確保用戶輸入正確的驗證數據。

0

我有同樣的問題。

我必須在同一個應用程序中做兩個身份驗證,因爲我的老闆不想要兩個數據庫。所以,我必須製作兩個用戶表和兩個登錄頁面。

一條路徑管理 - > /管理/登入 而前端爲其他用戶 - > /登錄

我試圖把更多的教義認證陣列進行驗證,但是它沒有工作。

我想我會在doctrine github頁面上打開一個問題。