2014-01-21 105 views
0

我使用Employees作爲模型來處理用戶名和密碼。它重定向到正確的控制器和操作,但$ this-> Auth-> login()似乎沒有正常工作。有沒有人看到問題?在自定義模型上使用CakePHP身份驗證

EmployeesController

public function index() { 
     //Set layout to login 
     $this->layout = 'login'; 

     //Set salt 
     //Convert user password to md5 
     $this->request->data['password'] = md5($this->request->data['password'].$salt); 

     //if already logged-in, redirect 
     if($this->Session->check('Auth.User')){ 
      $this->redirect(array('action' => 'index'));  
     } 
     if ($this->request->is('post')) { 
       echo "here"; 
      if ($this->Auth->login()) { 
       echo $this->Auth->user('username'); 
       $this->redirect($this->Auth->redirectUrl()); 
      } else { 
       echo "Invlaid Username"; 
      } 
     } 

    } 

AppController的

public $components = array(
     'Session', 
     'Auth' => array(
      'loginAction' => array(
       'controller' => 'Employees', 
       'action' => 'index', 
      ), 
      'authenticate' => array(
       'all' => array('userModel' => 'Employee'), 
       'Form' => array(
        'userModel' => 'Employee', 
        'fields' => array(
         'username' => 'employee_id', 
         'password' => 'password', 
        ) 
       ) 
      ) 
     ), 
    ); 

的$這 - 的print_r>請求 - >數據

陣列([僱員] =>數組([用戶名] => tmoorlag [password] => a8c407a6218250985ccd24ff9ec6))

回答

1

它應該是

$this->request->data['Employee']['password'] = md5($this->request->data['Employee']['password'].$salt); 
+0

仍然沒有變化。我如何告訴Auth使用除默認用戶模型之外的模型? – Subie

+0

try:'authenticate'=> array('all'=> array('userModel'=>'Emplyee'),'Form'=> array(...)。你是否收到「invalid username」錯誤?在嘗試登錄之前調試$ this-> request-> data,看看數據是否正確? – cornelb

+0

進行了更改。它表示無效的用戶名,我將print_r的結果放在最上面 – Subie