CakePHP的。如何在数据库中比较密码和密码

时间:2013-11-07 10:43:59

标签: php cakephp passwords

几天,差不多一周,我有一个问题,比较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中的数据库1进行比较,但我的所有工作都失败了。

请给我一些提示。


编辑(由于我的声誉很低,我可以在询问之后8小时回答我自己的帖子,所以我编辑这部分)


Anil Kumar你给了我很好的建议。我休息一下,但An Internal Error Has Occurred. Error: An Internal Error Has Occurred.  每一次,我都会在途中改变这部分代码,作为休闲,并且完美地运作,感谢You 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;
}

2 个答案:

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

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