【问题标题】:Cakephp 3 : Current password match in controllerCakephp 3:控制器中的当前密码匹配
【发布时间】:2015-08-28 04:32:29
【问题描述】:

如果更改之前,我正在尝试匹配当前密码。为此,我使用了 Auth 密码,然后将其与当前密码匹配。但它总是返回错误的。

这是我尝试过的控制器代码

 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");
       }

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

【问题讨论】:

  • 我建议不要在控制器层中放置太多逻辑;)查看this 以及模型顶部的简单行为如何使其成为简单的单线。干燥和清洁。您现在只需调用 save() 即可完成其余的工作。

标签: cakephp cakephp-3.0


【解决方案1】:

这样很简单:

将此插入您的(Users)Table

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

扩展您的function 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;
}

就是这样! :)

【讨论】:

    【解决方案2】:

    身份验证适配器在返回已识别用户的数据之前删除密码,但是您应该收到null,而不是1

    无论如何,如果您需要该信息,则必须手动阅读,例如

    $Table->get($this->Auth->user('id'))->password
    

    但是,正如 mentioned in the comments 一样,最好使用验证或应用程序规则来完成这项工作。

    参见例如 DefaultPasswordHasher generating different hash for the same value

    【讨论】:

      【解决方案3】:

      无需哈希密码,Auth将执行所有。

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

      【讨论】:

      • $this->Auth->user('password') is return 1. 我在 make hash 之前尝试过这段代码它也不起作用。
      • 在查询中使用该方法例如
      • $this->User->find('first', array('conditions' => array( 'User.password' => $this->Auth->password($this->数据['用户']['密码'])));
      • 实际上不是,您可以编辑您的答案并提供详细信息吗?
      • 触发查找查询条件比较您的密码字段与 $this->Auth->password($this->data['User']['password'])
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-12
      • 1970-01-01
      • 2011-09-06
      • 2022-01-03
      • 1970-01-01
      相关资源
      最近更新 更多