CakePHP:在不更改密码的情况下编辑用户 [英] CakePHP: Edit Users without changing password

查看:118
本文介绍了CakePHP:在不更改密码的情况下编辑用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在CakePHP应用程式中储存使用者,而不需要每次都变更密码?

How do I save users in my CakePHP app without requiring them to change their password each time?

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

I have code in place to check the two password fields and apply some verification rules, which works great for registration and for changing passwords in the 'edit' view. However, how do I skip the verification rules and saving the password if the password fields are left empty in the Edit view? Obviously, I don't want to skip this requirement on registration.

register.ctp和edit.ctp:

register.ctp and 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验证规则:

User.ctp validation rules:

'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逻辑:

and the User.ctp logic before saving:

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


推荐答案

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

这篇关于CakePHP:在不更改密码的情况下编辑用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆