1

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

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

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

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

应用控制器:

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';
    }

在管理员控制器中:

    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'] 并且我得到了与数据库中相同的哈希值。

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

4

3 回答 3

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');
}   
于 2012-07-06T07:19:43.410 回答
1

AuthComponent::password,不是AuthController::password

于 2012-07-05T23:33:48.727 回答
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();
}
于 2013-08-15T22:27:49.540 回答