function LoginCommand::__invoke
Generate a one-time login link.
File
-
core/
modules/ user/ src/ Command/ LoginCommand.php, line 40
Class
- LoginCommand
- Generate a one-time login link.
Namespace
Drupal\user\CommandCode
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;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.