1

AuthController を使用したユーザーとは異なるモデルでのログインに問題があります。

ユーザーテーブルではなく、管理者です。

私の管理者モデルには次のものがあります。

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'] をデバッグしたところ、データベースと同じハッシュが得られました。

ユーザー名/パスワードが間違っていると常に言われる理由がわかりません。

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