You are here

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'
autologout.services.yml in ./autologout.services.yml
autologout.services.yml
1 service uses AutologoutSubscriber
autologout_event_subscriber in ./autologout.services.yml
Drupal\autologout\EventSubscriber\AutologoutSubscriber

File

src/EventSubscriber/AutologoutSubscriber.php, line 18

Namespace

Drupal\autologout\EventSubscriber
View 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

Namesort descending Modifiers Type Description Overrides
AutologoutSubscriber::$autoLogoutManager protected property The autologout manager service.
AutologoutSubscriber::$config protected property The Config service.
AutologoutSubscriber::$currentUser protected property The user account service.
AutologoutSubscriber::$requestStack protected property The request stacks service.
AutologoutSubscriber::$theme protected property The theme manager service.
AutologoutSubscriber::$time protected property The Time Service.
AutologoutSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
AutologoutSubscriber::onRequest public function Check for autologout JS.
AutologoutSubscriber::__construct public function Constructs an AutologoutSubscriber object.