我一直在使用這個tutorial來重寫我的登錄/註銷功能來處理模型中的驗證和匹配密碼。Cakephp驗證hashPasswords重寫問題
它現在完美地添加了用戶,但登錄後用戶名/密碼字段被拒絕。似乎登錄函數正在尋找一個不同於我的新hashPasswords()函數存儲的哈希密碼。關於這個問題可能會有什麼建議?
這是一個相當標準的設置。謝謝你的幫助!
的代碼相關章節:
用戶模型:
var $validate = array(
'password' => array(
'The Password must be filled' =>array(
'rule' => 'notEmpty',
'message' => 'Please supply a valid password.'
),
'The Password must be between 5 and 15 characters' => array(
'rule' => array('between', 5, 15),
'message' => 'The password must be between 5 and 15 characters.'
),
'The passwords do not match' => array(
'rule' => 'matchPasswords',
'message' => 'The passwords do not match.'
)
),
function hashPasswords($data) {
if (isset($this->data['User']['password'])) {
$this->data['User']['password'] = Security::hash($this->data['User']['password'], NULL, TRUE);
return $data;
}
return $data;
}
function beforeSave() {
$this->hashPasswords(NULL, TRUE);
return TRUE;
}
用戶控制器:
function beforeFilter() {
parent::beforeFilter();
if ($this->action == 'add' || $this->action == 'edit') {
$this->Auth->authenticate = $this->User;
}
}
function add() {
if (!empty($this->data)) {
if ($this->User->save($this->data)) {
$this->Session->setFlash('Your Account Has Been Created.');
$this->redirect(array('action' => 'homepage'));
}
}
}
function login() {
}
AuthComponent正在爲您處理密碼散列。你爲什麼手動哈希?瀏覽完該視頻後:也許你還需要觀看系列中的下一個視頻,但這本身似乎有點毫無意義...: -/ – deceze
它來自整個視頻。我相信密碼哈希是分開完成的,因爲它們將它延遲並保存在beforeSave中。目的是讓我們可以匹配模型中的密碼,並避免在控制器中進行驗證。 – tnichols