CakePHP 2.2.0 - AuthComponent - 与用户不同的模型 - 密码始终不正确

时间:2012-07-05 22:13:38

标签: cakephp cakephp-2.0

使用AuthController登录与User不同的模型时遇到问题。

这是管理员,而不是用户表。

在我的管理员模型中,我有:

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-&gt; request-&gt; data ['Administrator'] ['password'],我和数据库中的哈希一样。

我不知道为什么总是说用户名/密码不正确。

3 个答案:

答案 0 :(得分: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');
}   

答案 1 :(得分:1)

它是AuthComponent::password,而不是AuthController::password

答案 2 :(得分: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();
}