class AutologoutSubscriber in Automated Logout 8
Defines autologout Subscriber.
Hierarchy
- class \Drupal\autologout\EventSubscriber\AutologoutSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of AutologoutSubscriber
1 string reference to 'AutologoutSubscriber'
1 service uses AutologoutSubscriber
File
- src/
EventSubscriber/ AutologoutSubscriber.php, line 18
Namespace
Drupal\autologout\EventSubscriberView source
class AutologoutSubscriber implements EventSubscriberInterface {
/**
* The autologout manager service.
*
* @var \Drupal\autologout\AutologoutManagerInterface
*/
protected $autoLogoutManager;
/**
* The user account service.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* The Config service.
*
* @var \Drupal\Core\Config\ConfigFactory
*/
protected $config;
/**
* The theme manager service.
*
* @var \Drupal\Core\Theme\ThemeManager
*/
protected $theme;
/**
* The Time Service.
*
* @var \Drupal\Component\Datetime\TimeInterface
*/
protected $time;
/**
* The request stacks service.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;
/**
* Constructs an AutologoutSubscriber object.
*
* @param \Drupal\autologout\AutologoutManagerInterface $autologout
* The autologout manager service.
* @param \Drupal\Core\Session\AccountInterface $account
* The user account service.
* @param \Drupal\Core\Config\ConfigFactory $config
* The Config service.
* @param \Drupal\Core\Theme\ThemeManager $theme
* The theme manager service.
* @param \Drupal\Component\Datetime\TimeInterface $time
* The time service.
* @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
* The request stack service.
*/
public function __construct(AutologoutManagerInterface $autologout, AccountInterface $account, ConfigFactory $config, ThemeManager $theme, TimeInterface $time, RequestStack $requestStack) {
$this->autoLogoutManager = $autologout;
$this->currentUser = $account;
$this->config = $config;
$this->theme = $theme;
$this->time = $time;
$this->requestStack = $requestStack;
}
/**
* Check for autologout JS.
*
* @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
* The request event.
*/
public function onRequest(GetResponseEvent $event) {
$autologout_manager = $this->autoLogoutManager;
$uid = $this->currentUser
->id();
if ($uid == 0) {
$autologout_timeout = $this->requestStack
->getCurrentRequest()->query
->get('autologout_timeout');
$post = $this->requestStack
->getCurrentRequest()->request
->all();
if (!empty($autologout_timeout) && $autologout_timeout == 1 && empty($post)) {
$autologout_manager
->inactivityMessage();
}
return;
}
if ($this->autoLogoutManager
->preventJs()) {
return;
}
$now = $this->time
->getRequestTime();
// Check if anything wants to be refresh only. This URL would include the
// javascript but will keep the login alive whilst that page is opened.
$refresh_only = $autologout_manager
->refreshOnly();
$timeout = $autologout_manager
->getUserTimeout();
$timeout_padding = $this->config
->get('autologout.settings')
->get('padding');
$is_altlogout = strpos($event
->getRequest()
->getRequestUri(), '/autologout_alt_logout') !== FALSE;
// We need a backup plan if JS is disabled.
if (!$is_altlogout && !$refresh_only && isset($_SESSION['autologout_last'])) {
// If time since last access is > timeout + padding, log them out.
$diff = $now - $_SESSION['autologout_last'];
if ($diff >= $timeout + (int) $timeout_padding) {
$autologout_manager
->logout();
// User has changed so force Drupal to remake decisions based on user.
global $theme, $theme_key;
drupal_static_reset();
$theme = NULL;
$theme_key = NULL;
$this->theme
->getActiveTheme();
$autologout_manager
->inactivityMessage();
}
else {
$_SESSION['autologout_last'] = $now;
}
}
else {
$_SESSION['autologout_last'] = $now;
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[KernelEvents::REQUEST][] = [
'onRequest',
100,
];
return $events;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AutologoutSubscriber:: |
protected | property | The autologout manager service. | |
AutologoutSubscriber:: |
protected | property | The Config service. | |
AutologoutSubscriber:: |
protected | property | The user account service. | |
AutologoutSubscriber:: |
protected | property | The request stacks service. | |
AutologoutSubscriber:: |
protected | property | The theme manager service. | |
AutologoutSubscriber:: |
protected | property | The Time Service. | |
AutologoutSubscriber:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
AutologoutSubscriber:: |
public | function | Check for autologout JS. | |
AutologoutSubscriber:: |
public | function | Constructs an AutologoutSubscriber object. |