public function PasswordPolicyEventSubscriber::checkForUserPasswordExpiration in Password Policy 8.3
Event callback to look for users expired password.
File
- src/
EventSubscriber/ PasswordPolicyEventSubscriber.php, line 77
Class
- PasswordPolicyEventSubscriber
- Enforces password reset functionality.
Namespace
Drupal\password_policy\EventSubscriberCode
public function checkForUserPasswordExpiration(GetResponseEvent $event) {
$route_name = $this->request->attributes
->get(RouteObjectInterface::ROUTE_NAME);
$ignore_route = in_array($route_name, [
'entity.user.edit_form',
'system.ajax',
'user.logout',
'admin_toolbar_tools.flush',
]);
// Ignore route for jsonapi calls.
if (strpos($route_name, 'jsonapi') !== FALSE) {
return;
}
// There needs to be an explicit check for non-anonymous or else
// this will be tripped and a forced redirect will occur.
if ($this->currentUser
->isAuthenticated()) {
/* @var $user \Drupal\user\UserInterface */
$user = $this->userStorage
->load($this->currentUser
->id());
$is_ajax = $this->request->headers
->get('X_REQUESTED_WITH') === 'XMLHttpRequest';
$user_expired = FALSE;
if ($user && $user
->hasField('field_password_expiration') && $user
->get('field_password_expiration')
->get(0)) {
$user_expired = $user
->get('field_password_expiration')
->get(0)
->getValue();
$user_expired = $user_expired['value'];
}
// TODO - Consider excluding admins here.
if ($user_expired && !$ignore_route && !$is_ajax) {
$url = new Url('entity.user.edit_form', [
'user' => $user
->id(),
]);
$url = $url
->setAbsolute()
->toString();
$event
->setResponse(new RedirectResponse($url));
$this->messenger
->addError($this
->t('Your password has expired, please update it'));
}
}
}