2012-07-05 34 views
1

我在使用AuthController的用戶登錄不同模型時遇到問題。CakePHP 2.2.0 - AuthComponent - 與用戶不同的模型 - 密碼始終不正確

這是管理員,而不是用戶表。

在我的管理模式,我有:

function beforeSave(){ 
     if(isset($this->data['Administrator']['password'])) 
      $this->data['Administrator']['password'] = AuthController::password($this->data['Administrator']['password']); 
      return true; 
    } 

的AppController:

public $components = array(
     'Session', 
     'Auth' => array(
      'loginAction' => array('controller' => 'administrators', 'action' => 'login'), 
      'loginRedirect' => array('controller' => 'Home', 'action' => 'index'), 
      'logoutRedirect' => array('controller' => 'Home', 'action' => 'index'), 
      'authError' => "You can't access that page.", 
      'authorize' => array('Controller')  
     ) 
    ); 


    public function beforeFilter(){ 
     $this->Auth->userModel = 'Administrator'; 
    } 

在AdministratorController:

public function login(){ 
      if($this->request->is('post')){ 
       if($this->Auth->login()){ 
        $this->redirect($this->Auth->redirect()); 
       } 
       else{ 
        $this->Session->setFlash('Your username/password combination was incorrect.'); 
       } 
      } 
     } 

public function logout(){ 
     $this->redirect($this->Auth->logout()); 
} 

public function beforeFilter(){ 
     parent::beforeFilter(); 
} 

,並查看:

<?php 
echo $this->Form->create('Administrator', array('controller' => 'administrators', 'action' => 'login')); 
echo $this->Form->input('username'); 
echo $this->Form->input('password'); 
echo $this->Form->end('Login'); 
?> 

我調試$ this-> request-> data ['Administrator'] ['password'],並且我得到了與數據庫中相同的散列。

我不知道爲什麼它總是說用戶名/密碼不正確。

+0

你確定它在保存之前不會散列兩次嗎?它是哈希登錄領域的正確比較? – tyjkenn 2012-07-05 22:57:08

+0

已檢查。 :) 這並不是說。謝謝。 – 2012-07-06 08:57:47

回答

1

這是AuthComponent::password而不是AuthController::password

+0

已更改。還是沒有。 :) 謝謝。 – 2012-07-06 08:58:08

4

您可以通過在您的AppController中包含以下代碼來完成此操作。

public $components = array(
'Auth' => array(
     'authenticate' => array(
      'Form' => array(
       'userModel' => 'Administrator', 
       'fields' => array(
        'username' => 'USER_LOGINNAME', 
        'password' => 'USER_PASSWORD' 
       ) 
      ) 
     ) 
    ) 
); 

在AppController的beforeFilter()方法來寫:

public function beforeFilter() 
{   
    if($this->Auth->user('USER_ID')) 
    { 
     $this->set('logged_in', true);      
    } 
    else 
    { 
     $this->set('logged_in', false); 
    } 

    //Configure AuthComponent 
    $this->Auth->userScope = array('Administrator.USER_STATUS' => '1'); 
    $this->Auth->loginAction = array('controller' => 'administrators', 'action' => 'login'); 
    $this->Auth->logoutRedirect = array('controller' => 'administrators', 'action' => 'login'); 
    $this->Auth->loginRedirect = array('controller' => 'administrators', 'action' => 'dashboard');   
}  

在您的管理員型號:

public function beforeSave() 
{ 
    if(!empty($this->data['Administrator']['USER_PASSWORD'])) 
    { 
     $this->data['Administrator']['USER_PASSWORD'] = AuthComponent::password($this->data['Administrator']['USER_PASSWORD']);  
    }   
    return true; 
} 

而在你的管理員控制器:

public function beforeFilter() 
{ 
    parent::beforeFilter(); 
    $this->Auth->allow('login'); 
} 
+0

我必須爲USER_LOGINNAME添加一些內容,例如$ this-> request-> data ['Administrator'] ['username']? – 2012-07-06 08:57:31

+1

即使您的管理員表不包含用戶名和密碼作爲默認字段,USER_LOGINNAME和USER_PASSWORD也是自定義字段。 – 2012-07-06 09:05:03

+0

噢...它是管理員表中的用戶名和密碼。我明白。謝謝。我現在就試一試。 – 2012-07-06 09:17:43

0
public function login(){ 
      if($this->request->is('post')){ 
       if($this->Auth->login()){ 

// dont use this line ///////////////////////////////////////////////////// 
        $this->redirect($this->Auth->redirect()); 
//////////////////////////////////////////////////////////////////////////// 
use this 
'loginAction' => array('controller' => 'administrators', 'action' => 'login'), 
//////////////////////////////////////////////////////////////////////////     
} 
       else{ 
        $this->Session->setFlash('Your username/password combination was incorrect.'); 
       } 
      } 
     } 

public function logout(){ 
     $this->redirect($this->Auth->logout()); 
} 

public function beforeFilter(){ 
     parent::beforeFilter(); 
} 
+0

你應該更詳細地解釋你發佈的解決方案。 – Meryovi 2013-08-15 22:51:32

相關問題