class LoginCommand

Generate a one-time login link.

@internal

Attributes

#[AsCommand(name: 'user:login', description: 'Generate a one-time login link.', aliases: [ 'uli', ])]

Hierarchy

Expanded class hierarchy of LoginCommand

2 files declare their use of LoginCommand
LoginCommandTest.php in core/modules/user/tests/src/Kernel/Command/LoginCommandTest.php
LoginCommandTest.php in core/modules/user/tests/src/Functional/Command/LoginCommandTest.php

File

core/modules/user/src/Command/LoginCommand.php, line 21

Namespace

Drupal\user\Command
View source
class LoginCommand {
  use StringTranslationTrait;
  public function __construct(protected readonly EntityTypeManagerInterface $entityTypeManager, protected readonly OneTimeAuthentication $oneTimeAuthentication) {
  }
  
  /**
   * Generate a one-time login link.
   */
  public function __invoke(OutputInterface $output, #[Argument('Optional path to redirect to after logging in.')] ?string $path, #[Option('A user name to log in as.')] ?string $name = NULL, #[Option('A user ID to log in as.')] ?string $uid = NULL, #[Option('A user email to log in as.')] ?string $mail = NULL) : int {
    $account = NULL;
    $storage = $this->entityTypeManager
      ->getStorage('user');
    $candidates = [
      'name' => $name,
      'uid' => $uid,
      'mail' => $mail,
    ];
    foreach ($candidates as $property => $value) {
      if ($value) {
        if (!$account = $storage->loadByProperties([
          $property => $value,
        ])) {
          throw new \InvalidArgumentException(sprintf('Unable to load user by %s: %s', $property, $value));
        }
        $account = reset($account);
        break;

      }
    }
    if (empty($account)) {
      $account = $storage->load(1);
    }
    if ($account->isBlocked()) {
      throw new \InvalidArgumentException(sprintf('Account %s is blocked and thus cannot login.', $account->getAccountName()));
    }
    $options = [
      'query' => $path ? [
        'destination' => $path,
      ] : [],
    ];
    $url = $this->oneTimeAuthentication
      ->generateOneTimeLoginUrl($account, $options, immediate: TRUE)
      ->mergeOptions($options);
    $output->writeln($url->toString());
    return Command::SUCCESS;
  }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
LoginCommand::__construct public function
LoginCommand::__invoke public function Generate a one-time login link.
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language. 1

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.