【问题标题】:CakePHP: Edit Users without changing passwordCakePHP:编辑用户而不更改密码
【发布时间】:2014-01-18 22:01:01
【问题描述】:

如何在我的 CakePHP 应用程序中保存用户而不要求他们每次都更改密码?

我有代码可以检查两个密码字段并应用一些验证规则,这对于注册和在“编辑”视图中更改密码非常有用。但是,如果编辑视图中的密码字段为空,我如何跳过验证规则并保存密码?显然,我不想跳过这个注册要求。

register.ctp 和 edit.ctp:

echo $form->create('User');
echo $form->input('username');
echo $form->input('pwd');
echo $form->input('pwd_repeat');
echo $form->end('Submit');

User.ctp 验证规则:

'pwd' => array(
        'length' => array(
            'rule'      => array('between', 8, 40),
            'message'   => 'Your password must be between 8 and 40 characters.',
        ),
    ),
    'pwd_repeat' => array(
        'length' => array(
            'rule'      => array('between', 8, 40),
            'message'   => 'Your password must be between 8 and 40 characters.',
        ),
        'compare'    => array(
            'rule'      => array('validate_passwords'),
            'message' => 'The passwords you entered do not match.',
        ),
    ),

以及保存前的User.ctp逻辑:

public function validate_passwords() { //password match check
return $this->data[$this->alias]['pwd'] === $this->data[$this->alias]['pwd_repeat'];
}

public function beforeSave($options = array()) { //set alias to real thing and hash password

    $this->data['User']['password'] = $this->data[$this->alias]['pwd'];
    $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
    return true;
}

【问题讨论】:

  • 最好为表单中的密码字段使用不同的名称 - 请参阅 working-with-passwords-in-cakephp
  • 不是吗?我以为我正在使用 pwd 和 pwd_repeat ......虽然回想起来我浪费时间将 pwd 转换为密码然后对其进行哈希处理,而我可以将 pwd 哈希为密码。
  • 对。只是为了让它工作而错过了!empty() 部分。您应该始终提及您正在使用的确切 cakephp 版本。从代码来看,它看起来像 1.2。从 1.3 开始,它是 $this->Form.

标签: php cakephp passwords


【解决方案1】:
var $validate = array(
    'pwd' => array(
        'length' => array(
            'rule'      => array('between', 8, 40),
            'message'   => 'Your password must be between 8 and 40 characters.',
            'on'        => 'create',  // we only need this validation on create
        ),
    ),

    // if we have a password entered, we need it to match pwd_repeat (both create and update)
    // we no longer need the length validation
    'pwd_repeat' => array(
        'compare' => array(
            'rule'    => array('validate_passwords'),
            'message' => 'Please confirm the password',
        ),
    ),
);


public function validate_passwords() { //password match check
    return $this->data[$this->alias]['pwd'] === $this->data[$this->alias]['pwd_repeat'];
}

public function beforeSave($options = Array()) {
    // if we have a password, we hash it before saving
    if (isset($this->data[$this->alias]['pwd'])) {
        $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['pwd_repeat']);
    }
    return true;
}

【讨论】:

  • 嗯,它仍然会弹出一条消息,要求我填写该字段。
  • 编辑:echo $this->Form->input('pwd', array('required' => false)); echo $this->Form->input('pwd_repeat', array('required' => false));
  • 可行,但如果该字段为空,它不会跳过保存密码,因此我将 beforeSave 包装器更改为:if (!empty($this->data[$this->alias]['pwd']) and !empty($this->data[$this->alias]['pwd_repeat'])) { 这是最好的方法吗?
【解决方案2】:

如果您使用的是 CakePHP 2.2:

http://book.cakephp.org/2.0/en/models/data-validation.html#removing-rules-from-the-set

同样在 beforeSave 函数中,如果两个密码字段都不为空,则将前两行包装在条件 for 中。

【讨论】:

    【解决方案3】:

    对于那些想要一些东西而不改变模型(如果发送新密码并保持规则更新)的人:Updating user with or without password - CakePHP

    TL;DR:

    // add in your view `app/View/Users/edit.ctp`
    // a 'fake' field you'll only use on the controller
    echo $this->Form->input('new_password');
    

    // add in your controller `app/Model/User.php`
    // if we have a new password, create key `password` in data
    if(!empty($new_password = $this->request->data['User']['new_password']))
      $this->request->data['User']['password'] = $new_password;
    else // else, we remove the rules on password
      $this->User->validator()->remove('password');
    

    【讨论】:

      【解决方案4】:

      只需从 edit.ctp 中删除该字段

      echo $form->create('User');
      echo $form->input('username');
      //echo $form->input('pwd');
      //echo $form->input('pwd_repeat');
      echo $form->end('Submit');
      

      因为 this->request->data 在密码字段中填充散列密码。当您再次保存用户密码哈希并变得与原始密码不同时

      【讨论】:

        猜你喜欢
        • 2013-05-12
        • 2017-09-14
        • 2012-11-17
        • 2015-08-06
        • 1970-01-01
        • 2015-07-02
        • 1970-01-01
        • 2014-10-23
        • 2018-12-16
        相关资源
        最近更新 更多