0

我有一个用户配置文件更新页面,它使用从 $this->request->data 收集的用户信息预先填充文本字段。一切正常,除了密码字段预先填充了似乎是来自数据库的加密密码,所以保存时,由于密码超过允许的字符数,验证失败,即使它存储,它也会然后用加密的相同密码替换用户的实际密码,如果这有意义的话。

最好的解决方法是什么?

控制器:

 public function profile() {
    if($this->request->is('post') || $this->request->is('put')) {
        if($this->Auth->user("id") == $this->request->data['User']['id']) {
            $this->request->data['User']['user_status_id'] = $this->Auth->user("user_status_id");
            $this->request->data['User']['user_type_id'] = $this->Auth->user("user_type_id");
            if($this->User->save($this->request->data)) {
                $this->Session->setFlash('Your profile has been updated','default',array('class'=>'success'));
            } else {
                $this->Session->setFlash("An error has occured updating your profile.");
            }
        } else {
            $this->Session->setFlash("Unable to save this result as there is an ID mismatch.");
        }
    } else {
        $this->request->data = $this->User->read(null,$this->Auth->user("id"));
    }
}

看法:

  <h1>Profile</h1>
 <h3>Update your profile</h3>
 <div class="left">
<div class="formContainer">
<?php 
    echo $this->Form->create('User');
        echo $this->Form->input('username',array("id" => "profileUsername"));
        echo $this->Form->input('password',array("id" => "profilePassword"));
        echo $this->Form->input('company');
        echo $this->Form->input('first_name');
        echo $this->Form->input('last_name');
        echo $this->Form->input('telephone');
        echo $this->Form->input('fax');
        echo $this->Form->input('id',array('type' => 'hidden'));
    echo $this->Form->end(__('Update'));
    ?>
 </div>
 </div>
4

4 回答 4

2

您遇到的问题是,如果您按照应有的方式存储了密码(使用单向哈希),则无法将其反转以显示纯文本版本,因此您可以简单地将密码数据设置为 null(保留密码字段空的):

$this->request->data['User']['password'] = NULL;

然后你有两个选择:

  1. 要求用户密码更新他们的数据(一个很好的小安全措施)

  2. 仅在用户输入新密码时更新用户密码

如果您已将密码验证规则设置为 ,则第二个选项可能会破坏您的验证allowEmpty => false,在这种情况下,您可以使用多个验证集,您可以通过遵循其中任何一个(所有在 cakephp 2.x 中工作)多验证行为多个验证集来完成

于 2012-07-22T08:23:15.200 回答
0

阅读并使用此行为: http: //www.dereuromark.de/2011/08/25/working-with-passwords-in-cakephp

由于哈希的技术细节,密码是一张单程票。

基本上,您永远不会将密码哈希传递回视图。始终显示空白字段(就像您应该在 www 中的几乎所有其他网站上看到的那样)。

然后您忽略并发布为空的字段。只有在新设置密码字段时,您才会真正触发验证。

于 2012-07-22T10:53:34.497 回答
0
<?php echo $this->Form->input('password', array('label' => false,'type'=>'password','required'=>'false')); ?>

输出

 <input name="data[User][password]" type="password" id="UserPassword">

当心!仅在编辑部分使用它

于 2013-12-22T07:06:39.237 回答
-1
$this->Form->input('password',array('value'=>null));
于 2013-09-27T07:53:45.653 回答