Implement user password checking

This commit is contained in:
Tom Wiesing 2022-11-25 15:06:01 +01:00
parent 8e2d2cce3e
commit 996ecb9f80
No known key found for this signature in database
25 changed files with 10762 additions and 224 deletions

View file

@ -0,0 +1,56 @@
<?php
use Drupal\Core\Url;
use Drupal\user\Entity\User;
/** lists all the users */
function list_users(): mixed {
$users = [];
foreach (User::loadMultiple(NULL) as $user) {
$fields = array_map(function ($field) {
return $field->getString();
}, $user->getFields());
if (empty($fields['name'])) continue;
$users[] = $fields;
}
return $users;
}
function set_user_password($name, $password): bool {
$user = user_load_by_name($name);
if (!$user) return false;
$user->setPassword($password);
$user->save();
return true;
}
function get_password_hash($name): string {
$user = user_load_by_name($name);
if (!$user) return "";
return $user->get('pass')->getString();
}
function check_password_hash($password, $hash): bool {
return \Drupal::service('password')->check($password, $hash);
}
function get_login_link($name): string {
$account = user_load_by_name($name);
if (!$account) return "";
$timestamp = \Drupal::time()->getRequestTime();
return Url::fromRoute(
'user.reset.login',
[
'uid' => $account->id(),
'timestamp' => $timestamp,
'hash' => user_pass_rehash($account, $timestamp),
],
[
'absolute' => false,
'query' => ['destination' => '/'],
'language' => \Drupal::languageManager()->getLanguage($account->getPreferredLangcode()),
]
)->toString();
}