1

几天以来,差不多一周,我有一个问题,要比较 cakephp 中的密码。

我正在准备编辑用户视图,并且在用户能够更改他的当前密码之前,他需要输入他的旧密码。(我正在从 cakebook 扩展授权教程。)

当用户创建他的密码时,他的密码在 User.php(模型)中进行散列

public function beforeSave($options = array()) {
    {
        if(isset($this->data[$this->alias]['password']))
        {
                $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
        }
        return true;
}

我尝试将 AuthComponent 之后的字段 old_password 与(接收用户通行证的任何方式)进行比较like $this->Session->read('Auth.User.password'),但当然它失败了,我尝试发送 old_password 并将其散列在模型 User.php 中,我也在这个模型中创建

App::uses('AuthComponent', 'Controller/Component');
class User extends AppModel
{
public $validate = array(
    'username'=>array(
        'required'=>array(
            'rule'=>array('notEmpty'),
            'message'=>'Write correct Login'
        )
    ),
    'password'=>array(
        'required'=>array(
            'rule'=>array('notEmpty'),
            'message'=>'Please re-enter your password twice so that the values match'
        )
    ),
    'old_password'=>array(
        'required'=>array(
            'rule'=>array('equalTo'=>'password'),
            'message'=>'Wrong'
        )
    )
);

使用不同的方法,'equalTo',password或者'equalTo','password' 我也尝试将 old_password 输入与 edit.ctp 中的数据库之一进行比较,但我所有的工作都失败了。

请给我一些提示。


编辑(因为我的声誉很低,我不能在询问后 8 小时前回答我自己的帖子,所以我将这部分编辑为)


Anil Kumar 你给了我很好的建议。我按照你的方式休闲,但An Internal Error Has Occurred. Error: An Internal Error Has Occurred. 每次我都会在途中更改这部分代码,作为休闲方式,它完美地工作,当然感谢你 Anil Kumar。

public function password_verifies()
{
    //$this->User->id = $this->data[$this->alias]['id'];
    //return AuthComponent::password($this->data[$this->alias]['password']) == $this->User->field('password');
    $od = AuthComponent::password($this->data['User']['old_password']);
    if($od == $this->field('password'))
    {
        return true;
    }
    return false;
}
4

2 回答 2

0

为旧密码定义规则

'old_password' => array(
    'rule' => 'password_verifies',
    'message' => 'wrong'
)

验证密码匹配的功能

public function password_verifies() {
    // getting password via field method by assuming you're setting $this->User->id from your controller
    return AuthComponent::password($this->data[$this->alias]['password']) == $this->User->field('password'); 
}

从控制器验证

在您的控制器中,您必须在验证之前设置 id。

$this->User->id = $this->Auth->user('id');
$this->User->set($this->request->data);
if ($this->User->validates()) {
    // do your stuff..
}
于 2013-11-07T10:52:54.650 回答
0

以下是我的网站比较密码的工作示例,

/**
     * Validation rules
     * @var array
     */
    var $validate = array(
        'changepass' => array(
            'notEmpty' => array(
                'rule' => 'notEmpty',
                'message' => 'Please insert Password',
                'last' => true
            ),
            'minLength' => array(
                'rule' => array('minLength', 8),
                'message' => 'Your password must be at least 8 characters long',
                'last' => true
            )
        ),
        'checkpassword' => array(
            'notEmpty' => array(
                'rule' => 'notEmpty',
                'message' => 'Please insert Confirm Password',
                'last' => true
            ),
            'checkValue' => array(
                'rule' => array('comparePassword', 'changepass'),
                'message' => 'Please enter the same password as above',
                'last' => true
            )
        )
    );

// Validating the values of two fields to not to be identical to each other
function comparePassword($field = array(), $compareField = null) {
    foreach ($field as $key => $value) {
        $v1 = $value;
        $v2 = $this->data[$this->name][$compareField];

        if ($v1 != $v2)
            return false;
        else
            continue;
    }
    return true;
}
于 2013-11-08T07:36:35.950 回答