如何在drupal8中解密和加密用户密码

时间:2018-07-28 11:50:31

标签: php

从移动应用程序中,如果值将与数据库匹配,则我将传递用户名和密码,它将显示用户的所有详细信息,否则用户名和密码无效。为此,我正在生成api。如何在drupal8中加密或解密密码?

class Userverifier extends ResourceBase
{
    public function get($username = 'NULL',$password = 'NULL') {

        $result = \Drupal::database()->select('users_field_data`', 'n')
            ->fields('n', array('uid', 'name', 'pass', 'mail'))
            ->condition('n.name', $username, '=')
            //->condition('n.pass', $decrypted_pass, '=')
            ->execute()->fetchAllAssoc('uid');

        $rows = array();
        foreach ($result as $row => $content) {
            $rows[] = array('data' => array($content->uid, $content->name, $content->pass, $content->mail));
        }
        return new ResourceResponse($rows);
    }
}

3 个答案:

答案 0 :(得分:0)

Drupal中有一个密码界面:

https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Password%21PasswordInterface.php/interface/PasswordInterface/8.2.x

Drupal StackExchange上有一个类似的问题,提供了有关如何使用该接口的示例-并非完美满足您的要求,但会有所帮助。

https://drupal.stackexchange.com/questions/195669/how-do-i-check-if-the-current-password-that-the-user-gives-in-the-password-reset

这实际上使用了用户类,而不是尝试自己驱动数据库-我建议这样做,因为它更“面向未来”。

答案 1 :(得分:0)

不可能在Drupal 8数据库中解密密码!

使用如下代码来验证凭据:

$account = User::load($theUsersId);
$saltedPwd = $account->getPassword();
$match = $this->passwordService->check($givenPwd, $saltedPwd);

您应该注入passwordService,否则请使用Drupal::service('password')

答案 2 :(得分:0)

这段代码对我有很大帮助。

$password_hasher = \Drupal::service('password');
$match = $password_hasher->check($currpwd, $user->getPassword());

$match将为bool(对或错)。 希望这对您也有帮助。