gpt4 book ai didi

Cakephp 3 : Current password match in controller

转载 作者:行者123 更新时间:2023-12-02 18:10:01 25 4
gpt4 key购买 nike

我尝试在更改之前匹配当前密码。为此,我获取了身份验证密码,然后将其与当前密码进行匹配。但它总是返回错误。

这是我尝试过的 Controller 代码

 if ($this->request->is(['patch', 'post', 'put'])) {
$obj = new DefaultPasswordHasher;
$postpassword = $obj->hash($this->request->data['current_password']);
if($this->Auth->user('password') == $postpassword)
{
// code to save change password.
}
else
$this->Flash->error(__('The password you have entered does not match !'));

}

这里 $postpassword 哈希工作正常,但是 $this->Auth->user('password') 返回值 1. 如何获取身份验证密码并与 $postpassword 匹配?

编辑

我已经获得了一些知识,然后我就这样解决了这个问题

$password     = '$2y$10$pHbHu6xhNAw/v5HuQ1DSjOm5MPkqZukD1.532ACu7YLgD1ef9K7i2';
if ($this->request->is(['patch', 'post', 'put'])) {
$obj = new DefaultPasswordHasher;

$postpassword = $obj->check($this->request->data['current_password'], $password);
if($postpassword==1)
$this -> set('password',"hello");
}

现在我只需要 Controller 中的 $this->Auth->user('password'); 。cakephp auth 组件中可以吗?

最佳答案

这样很简单:

将其插入您的(用户)表:

use Cake\Auth\DefaultPasswordHasher;
use Cake\Validation\Validator;

扩展您的函数validationDefault(Validator $validator),如下所示:

public function validationDefault(Validator $validator ) {

$validator
->add('current_password','custom',[
'rule'=> function($value, $context){
$user = $this->get($context['data']['id']);
if ($user) {
if ((new DefaultPasswordHasher)->check($value, $user->password)) {
return true;
}
}
return false;
},
'message'=>'The old password does not match the current password!',
])
->notEmpty('current_password');

return $validator;
}

就是这样! :)

关于Cakephp 3 : Current password match in controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32263383/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com