【发布时间】:2014-07-22 18:17:41
【问题描述】:
我的 Cake2.4 应用程序有一个密码重置表单,这给我带来了一些验证问题。
当我尝试对用户进行简单保存时,我遇到了一个验证错误,即电子邮件地址不是唯一的——即使我只是在更新用户的密码。
当我在我的保存中将$validate 设置为 false 时,表单可以正常工作。但是,这会跳过所有验证规则,包括我的密码验证规则。
如何进行此保存,以便它不会抱怨电子邮件地址不唯一,并且仍能正确验证密码?
电子邮件地址是唯一的,据我所知,我的控制器操作并未尝试更改它。在我的模型中规定“创建时”将无济于事 - 否则用户可以将他们的电子邮件地址设置为其他人的。
public function reset_password($token = null) {
if ($this->Session->read('Auth.User')) {
$this->Session->setFlash(__('You\'re already logged in. Update your password here.'), 'flash/success');
$this->redirect(array('action' => 'profile'));
}
if (!empty($token)){
if ($user = $this->User->findBytoken($token)){
$id = $user['User']['id'];
$this->User->id = $id;
}
elseif (count($this->User->findBytoken($token) == 0)){
$this->Session->setFlash(__('Your password reset link was invalid. Please request a new one'), 'flash/error');
$this->redirect(array('action' => 'forgot_password'));
}
if (strtotime($user['User']['token_expires']) < strtotime('now')){
$this->Session->setFlash(__('For your safety, your reset link has expired after 24 hours. Please request a new one'), 'flash/error');
$this->redirect(array('action' => 'forgot_password'));
}
} else {
$this->Session->setFlash(__('You need a reset link to do that. Request one with your email address here.'), 'flash/success');
$this->redirect(array('action' => 'forgot_password'));
}
if (!$this->User->exists($this->User->id)) {
throw new NotFoundException(__('Invalid user'));
}
if ($this->request->is('post') || $this->request->is('put')) {
$this->request->data['User']['token'] = null;
$this->request->data['User']['token_expires'] = null;
$this->request->data['User']['modified'] = false;
unset($this->request->data['User']['image']);
if ($this->User->save($this->request->data, $validate = false)) {
$this->Session->setFlash(__('Your password has been updated. You can log in with your new password now'), 'flash/success');
$this->redirect(array('action' => 'login'));
} else {
$options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
$this->set('user', $this->User->find('first', $options));
$this->Session->setFlash(__('Your password could not be updated. Please, try again.'), 'flash/error');
}
} else {
$options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
$this->set('user', $this->User->find('first', $options));
$this->request->data = $this->User->find('first', $options);
}
}
【问题讨论】:
标签: php cakephp cakephp-2.0 cakephp-2.4