CakePHP 3.0 - 在更改密码时将数据库密码与旧密码进行比较 [英] CakePHP 3.0 - Compare Database Password with the old password while changing password

查看:203
本文介绍了CakePHP 3.0 - 在更改密码时将数据库密码与旧密码进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CakePHP 3.0,我想将数据库密码与在更改密码功能中输入的旧密码进行比较。但我无法这样做,因为密码被散列,每次即使我们使用相同的密码,哈希密码也不同。



请帮助我。



在此先感谢您,
Melee

解决方案

I将在您的UsersTable的验证器中执行此操作。


$ b $ s

  $ validator 
- > notEmpty('current_password')
- > add('current_password','custom',[
'规则'=>

函数($ value,$ context){
$ query = $ this-> find()
- > where([
'id'=> $ context ['data'] ['id']
])
- > first();

$ data = $ query- >指定者() ;

return(new DefaultPasswordHasher) - > check($ value,$ data ['password']);
},
'message'=> '当前密码不正确!'
]);

自定义验证规则


I am working on CakePHP 3.0 and I would like to compare the database password with the old password entered in the form in "Change Password" functionality. But I am not able to do so since the password is hashed and the hashed password is different every time even if we use the same password.

Please help me in this.

Thanks in advance, Melee

解决方案

I would do this in the validator of your UsersTable.

src/Model/Table/UsersTable.php

$validator
            ->notEmpty('current_password')
                ->add('current_password', 'custom', [
                    'rule' => 

                    function($value, $context) {
                        $query = $this->find()
                                ->where([
                                    'id' => $context['data']['id']
                                ])
                                ->first();

                        $data = $query->toArray();

                        return (new DefaultPasswordHasher)->check($value, $data['password']);
                    },
                    'message' => 'Current password is incorrect!'
                ]);

Custom validation rules

这篇关于CakePHP 3.0 - 在更改密码时将数据库密码与旧密码进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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