3
0
Fork 0
mirror of https://github.com/farmOS/farmOS.git synced 2024-02-23 11:37:38 +01:00

Authenticate users by username and email.

This commit is contained in:
paul121 2021-02-03 09:13:55 -08:00 committed by Michael Stenta
parent 2f896780e5
commit 36761b6c00
2 changed files with 44 additions and 0 deletions

View file

@ -0,0 +1,5 @@
services:
farm_login.user.auth:
class: Drupal\farm_login\UserAuth
decorates: user.auth
arguments: ['@entity_type.manager', '@password']

View file

@ -0,0 +1,39 @@
<?php
namespace Drupal\farm_login;
use Drupal\user\UserAuth as CoreUserAuth;
/**
* Extends the core user.auth service to load users by their email.
*/
class UserAuth extends CoreUserAuth {
/**
* {@inheritdoc}
*/
public function authenticate($username, $password) {
$uid = parent::authenticate($username, $password);
// If the parent failed to authenticate, try loading the user by email.
if (empty($uid) && !empty($username) && strlen($password) > 0) {
$account_search = $this->entityTypeManager->getStorage('user')->loadByProperties(['mail' => $username]);
if ($account = reset($account_search)) {
if ($this->passwordChecker->check($password, $account->getPassword())) {
// Successful authentication.
$uid = $account->id();
// Update user to new password scheme if needed.
if ($this->passwordChecker->needsRehash($account->getPassword())) {
$account->setPassword($password);
$account->save();
}
}
}
}
return $uid;
}
}