【问题标题】:cakePHP pre-populates password field and breaks on updatecakePHP 预填充密码字段并在更新时中断
【发布时间】:2013-12-22 15:06:39
【问题描述】:

我有一个用户配置文件更新页面,它使用从 $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>

【问题讨论】:

    标签: cakephp


    【解决方案1】:

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

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

    然后你有两个选择:

    1. 需要用户密码才能更新他们的数据(一个不错的小安全措施)

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

    如果您将密码验证规则设置为allowEmpty =&gt; false,则第二个选项可能会破坏您的验证,在这种情况下,您可以使用多个验证集,您可以按照其中任何一个来执行(所有在 cakephp 2.x 中都有效)@ 987654321@,multiple validation sets

    【讨论】:

      【解决方案2】:

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

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

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

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

      【讨论】:

        【解决方案3】:
        <?php echo $this->Form->input('password', array('label' => false,'type'=>'password','required'=>'false')); ?>
        

        输出

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

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

        【讨论】:

          【解决方案4】:
          $this->Form->input('password',array('value'=>null));
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-03-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-12-06
            • 1970-01-01
            • 2022-06-29
            相关资源
            最近更新 更多