我的观点:
<tr>
<td>Username</td>
<td><?php echo $this->Form->input('User.username', array('label' => '')); ?></td>
</tr>
<tr>
<td>Password</td>
<td><?php echo $this->Form->password('User.password', array('label' => '', 'value'=>'')); ?></td>
</tr>
我的控制器:
function edit($id = null) {
$this->User->id = $id;
$data = $this->data;
# print_r($data);
if (empty($data)) {
$this->data = $this->User->read();
} else {
if ($this->User->save($data)) {
$this->Session->setFlash('The user details have been updated.');
$this->redirect(array('action' => 'index'));
}
}
}
当我使用空白密码提交表单时,数据库中的哈希值仍会发生变化。如果填写了新密码,我怎么才能更新密码哈希。
感谢。
编辑:$ data ['用户'] ['密码']始终是哈希值,永远不会为空!
答案 0 :(得分:3)
如果密码字段为空,请取消设置。
else {
if(empty($data['User']['password']))
{
unset($data['User']['password']);
}
if ($this->User->save($data)) {
$this->Session->setFlash('The user details have been updated.');
$this->redirect(array('action' => 'index'));
}
}
答案 1 :(得分:3)
CakePHP 1.3自动散列password
字段。 CakePHP 2.0没有。
你有几个选择(在我看来,从最差到最好):
重命名您的字段并在保存之前进行交换
if ($data['User']['new_password'] != '') {
$data['User']['password'] = $this->Auth->password($data['User']['new_password']);
}
在等式检查中隐藏字符串
if ($data['User']['password'] == $this->Auth->password('')) {
unset($data['User']['password']);
}
将散列函数更改为不散列空密码的函数[请参阅book了解配置]:
function hashPasswords($data) {
if (!empty($this->data['User']['password'])) {
$this->data['User']['password'] = $this->Auth->password($this->data['User']['password']);
}
return $data;
}
答案 2 :(得分:1)
您可以unset
$data
function edit($id = null) {
$this->User->id = $id;
$data = $this->data;
if (empty($data)) {
$this->data = $this->User->read();
} else {
if($data['User']['password'] == ''){
unset($data['User']['password']);
}
if ($this->User->save($data)) {
$this->Session->setFlash('The user details have been updated.');
$this->redirect(array('action' => 'index'));
}
}
}
{{1}}
答案 3 :(得分:0)
您可以尝试仅根据IF情况更新特定字段:
if(empty($data['User']['password']))
{
$this->User->id = $user_i; // specify which User to update
//update only username
$this->User->save(array('User'=>array('User.username'=>$data['User']['username'])));
}
else{
//save as before
}
答案 4 :(得分:0)
您可以使用为您处理它的行为: http://www.dereuromark.de/2011/08/25/working-with-passwords-in-cakephp/
然后你会使用字段“pwd”并将confirm设置为false(如果你不想要pwd_confirm字段)