CakePHP:编辑用户而不更改密码

时间:2014-01-18 22:01:01

标签: php cakephp passwords

如何在我的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;
}

4 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:1)

如果您使用的是CakePHP 2.2:

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

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

答案 2 :(得分:0)

只需从edit.ctp

中删除该字段即可
echo $form->create('User');
echo $form->input('username');
//echo $form->input('pwd');
//echo $form->input('pwd_repeat');
echo $form->end('Submit');

因为此> request->数据在密码字段中填充哈希密码。当您保存用户密码时再次进行哈希处理并变为原始密码

答案 3 :(得分:0)

对于那些在不更改模型的情况下需要某些内容的人(如果发送新密码,则保持规则onUpdate):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');