2012-07-05 1 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 : AdministratorController에서

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']를 디버깅했으며 데이터베이스에서와 같은 해시를 가졌습니다.

나는 항상 사용자 이름/암호가 잘못되었다고 말하는 이유를 모르겠습니다.

+0

저장하기 전에 두 번 해시하지 않았습니까? 그리고 그것은 올바른 비교를 위해 로그인 필드를 해싱합니까? – tyjkenn

+0

체크 됨. :) 그게 아니고. 고맙습니다. –

답변

1

AuthComponent::password이 아니라 AuthController::password입니다.

+0

변경됨. 아직도 그렇지 않습니다. :) 고맙습니다. –

4

다음 코드를 AppController에 포함하면됩니다. 당신의 관리자에

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

모델 :의 AppController으로, beforeFilter (에서

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

) 방법은 다음과 같은 쓰기

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

$ this-> request-> data [ 'Administrator'] [ 'username']처럼 USER_LOGINNAME에 뭔가를 써야합니까? –

+1

관리자 테이블에서도 기본 입력란으로 사용자 이름과 비밀번호를 포함하지 않으면 USER_LOGINNAME 및 USER_PASSWORD가 맞춤 입력란입니다. –

+0

오 ... 관리자 테이블에있는 사용자 이름과 비밀번호입니다. 이해 했어. 고맙습니다. 나는 그것을 지금 시도 할 것이다. –

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