2014-12-19 55 views
0

我有一個表名admin_users,我想在這裏使用auth組件。所以,我已經寫在AppController的婁代碼CakePHP:2.4身份驗證組件不工作

public $components = array('Session','RequestHandler','Paginator'=>array('limit'=>4),'Auth'=>array(
     'loginAction'=>array(
     'controller'=>'adminusers', 
     'action'=>'login' 
     ), 
     'loginRedirect' => array('controller' => 'adminusers','action' => 'index'), 
     'logoutRedirect' => array('controller' => 'adminusers','action' => 'index'), 
     'authError'=>'You can not access this page!!', 
     )); 

對於組用戶自己編寫

public function beforeFilter() { 
    //$this->Auth->allow(); 
    $this->set('logged_in', $this->Auth->loggedIn()); 
    $this->set('current_user',$this->Auth->user()); 
    parent::beforeFilter(); 
     $this->Paginator->settings = array(
      'limit'=>4 
    ); 


} 

我在adminusers控制器

public function login() { 
    $this->layout = 'login'; 
    if ($this->request->is('post')) { 
     if ($this->Auth->login()) { 
      return $this->redirect($this->Auth->redirect()); 
     } 
     $this->Session->setFlash(__('Invalid username or password, try again')); 
    } 
} 

上進行的方法調用的login()哈希密碼我已經寫在AdminUser模型

public function beforeSave($options = array()) { 
    if (isset($this->data[$this->alias]['password'])) { 
     $passwordHasher = new SimplePasswordHasher(); 
     $this->data[$this->alias]['password'] = $passwordHasher->hash(
      $this->data[$this->alias]['password'] 
     ); 
    } 
    return true; 
} 

這是

   <?php echo $this->Form->create('AdminUser'); ?> 
       <?php 
        echo $this->Form->input('username',array(
         'label' => false, 
         'placeholder'=>'UserName' 
         )); 
        echo $this->Form->input('password',array(
         'label' => false, 
         'placeholder'=>'Password' 
        )); 
       ?> 

使用允許()馬託後,我創建了一個用戶,在這裏哈希密碼fine.But問題工作login.ctp是,當我試圖登錄,這是給我。「無效的用戶名或密碼,請再試一次「。

+0

您是否檢查查詢生成?並檢查散列密碼 – Dreamer 2014-12-19 12:14:13

+0

您的控制器名稱似乎並不常規:adminusers也應該是admin_users。 – mark 2014-12-19 12:28:04

回答

1

在您的login.ctp中創建一個AdminUser。 Auth默認使用模型'User'。 您需要在Auth-Component中定義自己的模型。

public $components = array('Session','RequestHandler','Paginator'=>array('limit'=>4),'Auth'=>array(
    'loginAction'=>array(
    'controller'=>'adminusers', 
    'action'=>'login' 
    ), 
    'loginRedirect' => array('controller' => 'adminusers','action' => 'index'), 
    'logoutRedirect' => array('controller' => 'adminusers','action' => 'index'), 
    'authError'=>'You can not access this page!!', 
    'authenticate' => array(
     'Form' => array(
      'userModel' => 'AdminUser', 
      'passwordHasher' => 'Blowfish' 
     ) 
    ) 
));