【发布时间】: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 行 .