You are here

class UserAuth in farmOS 2.x

Extends the core user.auth service to load users by their email.

Hierarchy

Expanded class hierarchy of UserAuth

1 string reference to 'UserAuth'
farm_login.services.yml in modules/core/login/farm_login.services.yml
modules/core/login/farm_login.services.yml
1 service uses UserAuth
farm_login.user.auth in modules/core/login/farm_login.services.yml
Drupal\farm_login\UserAuth

File

modules/core/login/src/UserAuth.php, line 10

Namespace

Drupal\farm_login
View source
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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
UserAuth::$entityTypeManager protected property The entity type manager.
UserAuth::$passwordChecker protected property The password hashing service.
UserAuth::authenticate public function Validates user authentication credentials. Overrides UserAuth::authenticate
UserAuth::__construct public function Constructs a UserAuth object.