0

使用 Auth ORM,如何在更改密码之前判断旧密码是否正确。我见过使用 find_salt 方法的旧版本 Kohana 的代码,但这在 3.3 版中不再适用。

有任何想法吗?

4

2 回答 2

2

使用 Validation 类有更好的方法:

if($post = $this->request->post()) {
    $user = Auth::instance()->get_user();

    $validation = Validation::factory($post)
        ->rule('old_password', array(Auth::instance(), 'check_password'));

    // Rules for password (model rules applies after hash)
    $extra_rules = Validation::factory($post)
        ->rule('password', 'not_empty')
        ->rule('password', 'min_length', array(':value', 8))
        ->rule('password', 'matches', array(':validation', 'password', 'password_confirm'));

    try {
        if(!$validation->check()) {
            throw new ORM_Validation_Exception('password', $validation);
        }
        $user->password = $post['password'];
        $user->update($extra_rules);
    }catch(ORM_Validation_Exception $e){
        // Handle errors
    }
}
于 2015-04-07T15:36:57.030 回答
1

在与存储的比较之后,使用该hash()方法对密码字符串进行哈希处理。

$auth = Auth::instance();
$user = ORM::factory('user')
    ->where('username', '=', 'User')
    ->find();

if ($auth->hash($_POST['password']) == $user->password)
{
    // Passwords match, change here.
}
else
{
    // Passwords not match.
}
于 2014-06-19T12:04:28.680 回答