【问题标题】:Only update password if field is filled in仅在填写字段时更新密码
【发布时间】:2011-11-28 11:41:24
【问题描述】:

我的看法:

  <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['User']['password'] 总是一个哈希值,永远不会为空!

【问题讨论】:

  • 散列出现在哪里?是蛋糕做的吗?

标签: php cakephp cakephp-1.3


【解决方案1】:

CakePHP 1.3 自动散列password 字段。 CakePHP 2.0 没有。

你有几个选择(在我看来,从最坏到最好):

  1. 重命名字段并在保存前交换它

    if ($data['User']['new_password'] != '') {
        $data['User']['password'] = $this->Auth->password($data['User']['new_password']);
    }
    
  2. 在相等性检查中散列字符串

    if ($data['User']['password'] == $this->Auth->password('')) {
        unset($data['User']['password']);
    }
    
  3. 将散列函数更改为不对空密码进行散列的散列函数[配置见book]:

    function hashPasswords($data) {
        if (!empty($this->data['User']['password'])) {
            $this->data['User']['password'] = $this->Auth->password($this->data['User']['password']);
        }
        return $data;
    }
    

【讨论】:

  • +1,非常有帮助。虽然我确实想在数据库中散列我的密码。我的问题是当我使用空白密码字段保存用户配置文件时(因为我不想更改密码),它会将数据库中的密码更改为散列空值的密码。除非我在表单字段中键入某些内容,否则我只想保留密码。谢谢。
  • 编辑,结合您的答案,我设法让它完美地工作,谢谢!
  • 更新的答案:所有三种方法现在都在密码进入数据库之前对其进行哈希处理(我对第一种方法进行了疏忽并简化了第三种方法)。很高兴你成功了!
【解决方案2】:

如果密码字段为空,请取消设置。

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'));
       }
}

【讨论】:

  • 但是$this-&gt;data['User']['password']是一个空值的哈希,所以它永远不会为空?
【解决方案3】:

你可以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'));
        }
    }
}

【讨论】:

  • 但是$this-&gt;data['User']['password'] 是空白值的哈希,所以它永远不等于''?
【解决方案4】:

您可以尝试根据 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
       }

【讨论】:

    【解决方案5】:

    您可以使用为您处理它的行为: http://www.dereuromark.de/2011/08/25/working-with-passwords-in-cakephp/

    然后您将使用字段“pwd”并将确认设置为 false(如果您不想要 pwd_confirm 字段)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-19
      • 1970-01-01
      • 2018-01-05
      • 1970-01-01
      • 2017-12-28
      相关资源
      最近更新 更多