2013-04-10 60 views
0

存儲之前散列這是AppController代碼:密碼沒有在數據庫

public $components = array(
    'Session', 
    'Auth' => array(
     'loginRedirect' => array('controller' => 'dashboard', 'action' => 'index'), 
     'logoutRedirect' => array('controller' => 'admins', 'action' => 'index'), 
     'authorize' => array('Controller') 
    ) 
); 

public function beforeFilter() 
{ 
    $this->Auth->loginAction = array('controller' => 'admins', 'action' => 'login'); 
} 

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

這是AdminsController代碼:

public $name = 'Admins'; 

public function beforeFilter() 
{ 
    parent::beforeFilter(); 

    $this->Auth->fields = array('username' => 'email', 'password' => 'password'); 
    $this->Auth->allow('signup'); 
} 

public function index() 
{ 
    $this->Admin->recursive = 0; 
    $this->set('admins', $this->Admin->find('all')); 
} 

public function login() 
{ 
    $this->set('title_for_layout', 'Polkadot - Admin Login'); 

    if($this->request->is('post')) 
    { 
     if($this->Auth->login()) 
     { 
      $this->Session->setFlash('Login successfull!'); 
      $this->redirect($this->Auth->redirect()); 
     } 
     else 
     { 
      $this->Session->setFlash('Login failed!'); 
     } 
    } 
} 

public function signup() 
{ 
    $this->set('title_for_layout', 'Polkadot - Admin Sign Up'); 

    if($this->request->is('post')) 
    { 
     if($this->Admin->save($this->request->data)) 
     { 
      $this->Session->setFlash('Account created successfully!'); 
      $this->redirect(array('action' => 'index')); 
     } 
     else 
     { 
      $this->Session->setFlash('Account could not be created, please try again!'); 
     } 
    } 
} 

我可以看到帳戶創建,但在密碼數據庫是純文本。當我嘗試登錄時,它失敗。我做錯了什麼?

我想在管理員登錄時打開Dashboard控制器的index操作。帳戶註冊使用四個輸入字段:名稱(文本),電子郵件(文本),密碼(密碼)和密碼確認(密碼)。因此,登錄表單使用兩個字段:電子郵件(文本)和密碼(密碼)。

使用的CakePHP版本是2.3.1

所有幫助表示感謝!

[編輯]:以下是錯誤日誌

2013-04-10 19:31:13 Error: [MissingControllerException] Controller class CssController could not be found. 
Exception Attributes: array (
    'class' => 'CssController', 
    'plugin' => NULL, 
) 
Request URL: /polkapanel/css/cake.generic.css 
Stack Trace: 
#0 C:\wamp\www\polkapanel\app\webroot\index.php(109): Dispatcher->dispatch(Object(CakeRequest), Object(CakeResponse)) 
#1 {main} 
2013-04-10 19:31:13 Error: [MissingControllerException] Controller class ImgController could not be found. 
Exception Attributes: array (
    'class' => 'ImgController', 
    'plugin' => NULL, 
) 
Request URL: /polkapanel/img/cake.power.gif 
Stack Trace: 
#0 C:\wamp\www\polkapanel\app\webroot\index.php(109): Dispatcher->dispatch(Object(CakeRequest), Object(CakeResponse)) 
#1 {main} 
2013-04-10 19:31:25 Error: [MissingControllerException] Controller class CssController could not be found. 
Exception Attributes: array (
    'class' => 'CssController', 
    'plugin' => NULL, 
) 
Request URL: /polkapanel/css/cake.generic.css 
Stack Trace: 
#0 C:\wamp\www\polkapanel\app\webroot\index.php(109): Dispatcher->dispatch(Object(CakeRequest), Object(CakeResponse)) 
#1 {main} 
2013-04-10 19:31:25 Error: [MissingControllerException] Controller class ImgController could not be found. 
Exception Attributes: array (
    'class' => 'ImgController', 
    'plugin' => NULL, 
) 
Request URL: /polkapanel/img/cake.power.gif 
Stack Trace: 
#0 C:\wamp\www\polkapanel\app\webroot\index.php(109): Dispatcher->dispatch(Object(CakeRequest), Object(CakeResponse)) 
#1 {main} 
+0

控制器中沒有beforeSave()函數,它應該放在模型上(AppModel或User.ctp,取決於你想要的位置管理密碼) – Nunser 2013-04-10 18:49:32

回答

2

你必須把beforeSave功能在您的Admin型號:

admin.php的(或者無論你怎麼稱呼它):

public function beforeSave() { 
    if(isset($this->data['Admin']['password'])) { 
     $this->data['Admin']['password'] = AuthComponent::password($this->data['Admin']['password']); 
    } 
    return true; 
} 
+0

啊,我犯了一個愚蠢的錯誤。所以,現在密碼在被存儲在數據庫中之前被散列。但仍然登錄失敗。在login()函數中,在使用存儲的密碼進行驗證之前,是否必須再次散列管理員輸入的密碼?如果是的話,我該怎麼做? – 2013-04-10 19:18:09

+0

你可以把錯誤日誌?您不需要在'login'方法中驗證密碼,CakePHP會自動執行它 – 2013-04-10 19:22:09

+0

如何查看錯誤日誌?抱歉,我是CakePHP的新手。 – 2013-04-10 19:24:50

相關問題