You are here

class AuthDecorator in Mail Login 8.2

Validates user authentication credentials.

Hierarchy

Expanded class hierarchy of AuthDecorator

1 string reference to 'AuthDecorator'
mail_login.services.yml in ./mail_login.services.yml
mail_login.services.yml
1 service uses AuthDecorator
mail_login.auth in ./mail_login.services.yml
Drupal\mail_login\AuthDecorator

File

src/AuthDecorator.php, line 12

Namespace

Drupal\mail_login
View source
class AuthDecorator implements UserAuthInterface {
  use DependencySerializationTrait;

  /**
   * The original user authentication service.
   *
   * @var \Drupal\user\UserAuthInterface
   */
  protected $userAuth;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs a UserAuth object.
   *
   * @param \Drupal\user\UserAuthInterface $user_auth
   *   The original user authentication service.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_managerk
   *   The entity type manager.
   */
  public function __construct(UserAuthInterface $user_auth, EntityTypeManagerInterface $entity_type_manager) {
    $this->userAuth = $user_auth;
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function authenticate($username, $password) {
    $config_factory = \Drupal::configFactory();
    $config = $config_factory
      ->get('mail_login.settings');

    // If we have an email lookup the username by email.
    if ($config
      ->get('mail_login_enabled') && !empty($username)) {
      if (filter_var($username, FILTER_VALIDATE_EMAIL)) {
        $account_search = $this->entityTypeManager
          ->getStorage('user')
          ->loadByProperties([
          'mail' => $username,
        ]);
        if ($account = reset($account_search)) {
          $username = $account
            ->getAccountName();
        }
      }
      else {
        if ($config
          ->get('mail_login_email_only')) {

          // Display a custom login error message.
          \Drupal::messenger()
            ->addError(t('Login by username has been disabled, please use email address instead.'));
          return NULL;
        }
      }
    }
    return $this->userAuth
      ->authenticate($username, $password);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AuthDecorator::$entityTypeManager protected property The entity type manager.
AuthDecorator::$userAuth protected property The original user authentication service.
AuthDecorator::authenticate public function Validates user authentication credentials. Overrides UserAuthInterface::authenticate
AuthDecorator::__construct public function Constructs a UserAuth object.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2