【问题标题】:How do I check if the current password that the user gives in the password reset form matches with the hashed password in Drupal 8如何检查用户在密码重置表单中提供的当前密码是否与 Drupal 8 中的散列密码匹配
【发布时间】:2016-03-23 10:34:08
【问题描述】:

我正在尝试在 Drupal 8 中以编程方式重置用户密码,但没有电子邮件链接。为此,第一步是检查用户在密码重置表单中输入的密码(明文)是否与相应用户的散列密码匹配。然后保存新密码。

每次我对密码进行哈希处理时,它都会给出不同的值,这是因为它是加盐的。如何将用户在表单中输入的密码与表中的哈希密码进行比较。

这是我在控制器中用于检查当前密码的代码:

<?php
/**
* @file
* contains \Drupal\customer_profile\Controller\ProfileUpdateController
*/

 namespace Drupal\customer_profile\Controller;

 use Drupal\Core\Controller\ControllerBase;

 use Symfony\Component\HttpFoundation\JsonResponse;
 use Drupal\Core\Password\PhpassHashedPassword;
 use Drupal\Core\Password\PasswordInterface;

 class ProfileUpdateController extends ControllerBase {

   //$user = User::load(1);
   //$user->setPassword('secret');
   //$pass = $user->save();
   $user =\Drupal\user\Entity\User::load(\Drupal::currentUser()->id());
   $ret = \Drupal\Core\Password\PasswordInterface::check('secret', $user);

   $response->password = $ret;
   return new JsonResponse($response);
  }
 }

使用 \Drupal\Core\Password\PasswordInterface::check() 方法后,将纯文本密码与哈希密码进行比较,我得到这个致命错误:

致命错误:无法静态调用非静态方法 Drupal\Core\Password\PasswordInterface::check(),假设 $this 来自 C:\xampp\htdocs\ijarah\modules\custom\ 中的不兼容上下文customer_profile\src\Controller\ProfileUpdateController.php 在第 725 行

在 Drupal 7 中,我们有 user_check_password 来检查密码字符串和 user_hash_password 哈希。 我如何在 Drupal 8 中实现同样的目标。

请帮忙。

【问题讨论】:

  • 我也尝试过使用 \Drupal\Core\Password\PasswordInterface::check(),但没有成功。返回一个致命错误,说 Fatal error: Non-static method Drupal\Core\Password\PasswordInterface::check() 不能被静态调用,假设 $this 来自 C:\xampp\htdocs 中的不兼容上下文\ijarah\modules\custom\customer_profile\src\Controller\ProfileUpdateController.php 在第 725 行 .

标签: drupal drupal-8


【解决方案1】:

好的,解决方案是使用依赖注入,然后使用 check() 方法。这是代码:

<?php    
/**
* @file
* contains \Drupal\customer_profile\Controller\ProfileUpdateController
*/
namespace Drupal\customer_profile\Controller;

use Drupal\Core\Controller\ControllerBase;

use Symfony\Component\HttpFoundation\JsonResponse;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface; 
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Password\PasswordInterface;

 class ProfileUpdateController extends ControllerBase implements ContainerInjectionInterface {

  public function __construct(PasswordInterface $password_hasher, AccountInterface $account) {
   $this->passwordHasher = $password_hasher;
   $this->account = $account;
  }

   public static function create(ContainerInterface $container) {
     return new static(
       $container->get('password'),
       $container->get('current_user')
     );
   }

   public function updatePassword() {
     //global $user;
     $response = new \stdClass();
      //check the plain password with the hashed password from db
     $pass = $this->passwordHasher->check('secret', 'hashed_password_from_db');

     $response->password = $pass;
     // this will return true if the password matches or false vice-versa
     return new JsonResponse($response);
   }
  }
   ?>

检查密码后,我们可以使用保存新密码

  $user = User::load(1);
  $user->setPassword('new_password_secret');
  $user->save();

希望这对其他人有帮助:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-06
    • 2019-04-30
    • 1970-01-01
    • 2014-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-18
    相关资源
    最近更新 更多