class RegistrationController in User registration password 8
User registration password controller class.
Hierarchy
- class \Drupal\Core\Controller\ControllerBase implements ContainerInjectionInterface uses LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\user_registrationpassword\Controller\RegistrationController
Expanded class hierarchy of RegistrationController
File
- src/
Controller/ RegistrationController.php, line 17
Namespace
Drupal\user_registrationpassword\ControllerView source
class RegistrationController extends ControllerBase {
/**
* The date formatter service.
*
* @var \Drupal\Core\Datetime\DateFormatterInterface
*/
protected $dateFormatter;
/**
* The user storage.
*
* @var \Drupal\user\UserStorageInterface
*/
protected $userStorage;
/**
* The Messenger service.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;
/**
* The time service.
*
* @var \Drupal\Component\Datetime\TimeInterface
*/
protected $time;
/**
* Constructs a UserController object.
*
* @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
* The date formatter service.
* @param \Drupal\user\UserStorageInterface $user_storage
* The user storage.
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The status message.
* @param \Drupal\Component\Datetime\TimeInterface $time
* The time service.
*/
public function __construct(DateFormatterInterface $date_formatter, UserStorageInterface $user_storage, MessengerInterface $messenger, TimeInterface $time) {
$this->dateFormatter = $date_formatter;
$this->userStorage = $user_storage;
$this->messenger = $messenger;
$this->time = $time;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('date.formatter'), $container
->get('entity_type.manager')
->getStorage('user'), $container
->get('messenger'), $container
->get('datetime.time'));
}
/**
* Confirms a user account.
*
* @param int $uid
* UID of user requesting confirmation.
* @param int $timestamp
* The current timestamp.
* @param string $hash
* Login link hash.
*
* @return array|\Symfony\Component\HttpFoundation\RedirectResponse
* The form structure or a redirect response.
*
* @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
* If the login link is for a blocked user or invalid user ID.
*/
public function confirmAccount($uid, $timestamp, $hash) {
$route_name = '<front>';
$route_options = [];
$current_user = $this
->currentUser();
// Verify that the user exists.
if ($current_user === NULL) {
throw new AccessDeniedHttpException();
}
// When processing the one-time login link, we have to make sure that a user
// isn't already logged in.
if ($current_user
->isAuthenticated()) {
// The existing user is already logged in.
if ($current_user
->id() == $uid) {
$this->messenger
->addMessage($this
->t('You are currently authenticated as user %user.', [
'%user' => $current_user
->getAccountName(),
]));
// Redirect to user page.
$route_name = 'user.page';
$route_options = [
'user' => $current_user
->id(),
];
}
else {
$reset_link_account = $this->userStorage
->load($uid);
if (!empty($reset_link_account)) {
$this->messenger
->addMessage($this
->t('Another user (%other_user) is already logged into the site on this computer, but you tried to use a one-time link for user %resetting_user. Please <a href=":logout">log out</a> and try using the link again.', [
'%other_user' => $current_user
->getDisplayName(),
'%resetting_user' => $reset_link_account
->getDisplayName(),
':logout' => Url::fromRoute('user.logout')
->toString(),
]), 'warning');
}
else {
// Invalid one-time link specifies an unknown user.
$this->messenger
->addMessage($this
->t('You have tried to use a one-time login link that has either been used or is no longer valid. Please request a new one using the form below.'));
$route_name = 'user.pass';
}
}
}
else {
// Time out, in seconds, until login URL expires. 24 hours = 86400
// seconds.
$timeout = $this
->config('user_registrationpassword.settings')
->get('registration_ftll_timeout');
$current = $this->time
->getRequestTime();
$timestamp_created = $timestamp - $timeout;
// Some redundant checks for extra security ?
$users = $this->userStorage
->getQuery()
->condition('uid', $uid)
->condition('status', 0)
->condition('access', 0)
->execute();
// Timestamp can not be larger then current.
/** @var \Drupal\user\UserInterface $account */
if ($timestamp_created <= $current && !empty($users) && ($account = $this->userStorage
->load(reset($users)))) {
// Check if we have to enforce expiration for activation links.
if ($this
->config('user_registrationpassword.settings')
->get('registration_ftll_expire') && !$account
->getLastLoginTime() && $current - $timestamp > $timeout) {
$this->messenger
->addMessage($this
->t('You have tried to use a one-time login link that has either been used or is no longer valid. Please request a new one using the form below.'));
$route_name = 'user.pass';
}
elseif ($account
->id() && $timestamp >= $account
->getCreatedTime() && !$account
->getLastLoginTime() && $hash == user_pass_rehash($account, $timestamp)) {
// Format the date, so the logs are a bit more readable.
$date = $this->dateFormatter
->format($timestamp);
$this
->getLogger('user')
->notice('User %name used one-time login link at time %timestamp.', [
'%name' => $account
->getAccountName(),
'%timestamp' => $date,
]);
// Activate the user and update the access and login time to $current.
$account
->activate()
->setLastAccessTime($current)
->setLastLoginTime($current)
->save();
// user_login_finalize() also updates the login timestamp of the
// user, which invalidates further use of the one-time login link.
user_login_finalize($account);
// Display default welcome message.
$this->messenger
->addMessage($this
->t('You have just used your one-time login link. Your account is now active and you are authenticated.'));
// Redirect to user.
$route_name = 'user.page';
$route_options = [
'user' => $account
->id(),
];
}
else {
$this->messenger
->addMessage($this
->t('You have tried to use a one-time login link that has either been used or is no longer valid. Please request a new one using the form below.'));
$route_name = 'user.pass';
}
}
else {
// Deny access, no more clues.
// Everything will be in the watchdog's
// URL for the administrator to check.
$this->messenger
->addMessage($this
->t('You have tried to use a one-time login link that has either been used or is no longer valid. Please request a new one using the form below.'));
$route_name = 'user.pass';
}
}
return $this
->redirect($route_name, $route_options);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ControllerBase:: |
protected | property | The configuration factory. | |
ControllerBase:: |
protected | property | The current user service. | 1 |
ControllerBase:: |
protected | property | The entity form builder. | |
ControllerBase:: |
protected | property | The entity manager. | |
ControllerBase:: |
protected | property | The entity type manager. | |
ControllerBase:: |
protected | property | The form builder. | 2 |
ControllerBase:: |
protected | property | The key-value storage. | 1 |
ControllerBase:: |
protected | property | The language manager. | 1 |
ControllerBase:: |
protected | property | The module handler. | 2 |
ControllerBase:: |
protected | property | The state service. | |
ControllerBase:: |
protected | function | Returns the requested cache bin. | |
ControllerBase:: |
protected | function | Retrieves a configuration object. | |
ControllerBase:: |
private | function | Returns the service container. | |
ControllerBase:: |
protected | function | Returns the current user. | 1 |
ControllerBase:: |
protected | function | Retrieves the entity form builder. | |
ControllerBase:: |
protected | function | Retrieves the entity manager service. | |
ControllerBase:: |
protected | function | Retrieves the entity type manager. | |
ControllerBase:: |
protected | function | Returns the form builder service. | 2 |
ControllerBase:: |
protected | function | Returns a key/value storage collection. | 1 |
ControllerBase:: |
protected | function | Returns the language manager service. | 1 |
ControllerBase:: |
protected | function | Returns the module handler. | 2 |
ControllerBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
ControllerBase:: |
protected | function | Returns the state storage service. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
RegistrationController:: |
protected | property | The date formatter service. | |
RegistrationController:: |
protected | property |
The Messenger service. Overrides MessengerTrait:: |
|
RegistrationController:: |
protected | property | The time service. | |
RegistrationController:: |
protected | property | The user storage. | |
RegistrationController:: |
public | function | Confirms a user account. | |
RegistrationController:: |
public static | function |
Instantiates a new instance of this class. Overrides ControllerBase:: |
|
RegistrationController:: |
public | function | Constructs a UserController object. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |