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:
parent
2f896780e5
commit
36761b6c00
2 changed files with 44 additions and 0 deletions
5
modules/core/login/farm_login.services.yml
Normal file
5
modules/core/login/farm_login.services.yml
Normal file
|
@ -0,0 +1,5 @@
|
|||
services:
|
||||
farm_login.user.auth:
|
||||
class: Drupal\farm_login\UserAuth
|
||||
decorates: user.auth
|
||||
arguments: ['@entity_type.manager', '@password']
|
39
modules/core/login/src/UserAuth.php
Normal file
39
modules/core/login/src/UserAuth.php
Normal 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;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue