cakePHP预先填充密码字段并在更新时中断

时间:2012-07-22 07:15:53

标签: cakephp

我有一个用户个人资料更新页面,该页面使用从$ this-> request->数据收集的用户信息预先填充文本字段。一切正常,除了密码字段预先填充了来自数据库的加密密码,因此在保存时,验证失败,因为密码超过了允许的字符数,即使它存储了,它也会然后用加密的相同密码替换用户的实际密码,如果这是有道理的。

最好的解决办法是什么?

控制器:

 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 个答案:

答案 0 :(得分:2)

您遇到的问题是,如果您已经存储了密码(使用单向哈希),则无法将其反转以显示纯文本版本,因此您只需将密码数据设置为null即可(将密码字段留空):

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

您有两个选择:

  1. 要求用户密码更新其数据(一个不错的安全措施)

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

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

答案 1 :(得分:0)

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

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

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

然后你忽略并发布空的字段。只有在新设置密码字段时才会实际触发验证。

答案 2 :(得分:0)

<?php echo $this->Form->input('password', array('label' => false,'type'=>'password','required'=>'false')); ?>

输出

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

小心!仅在编辑部分使用此功能

答案 3 :(得分:-1)

$this->Form->input('password',array('value'=>null));
相关问题