You are here

function autologout_autologout_prevent in Automated Logout 8

Same name and namespace in other branches
  1. 6.4 autologout.module \autologout_autologout_prevent()
  2. 7.4 autologout.module \autologout_autologout_prevent()

Implements hook_autologout_prevent().

File

./autologout.module, line 107
Automated Logout - Module.

Code

function autologout_autologout_prevent() {
  $user = \Drupal::currentUser();
  $user_ip = \Drupal::request()
    ->getClientIp();

  // Don't include autologout JS checks on ajax callbacks.
  $paths = [
    'system',
    'autologout_ajax_get_time_left',
    'autologout_ajax_logout',
    'autologout_ajax_set_last',
  ];

  // getPath is used because Url::fromRoute('<current>')->toString() doesn't
  // give correct path for XHR request.
  $url = \Drupal::service('path.current')
    ->getPath();
  $path_args = explode('/', $url);

  // Check if user IP address is in the whitelist.
  $ip_address_whitelist = array_map('trim', explode("\n", trim(\Drupal::config('autologout.settings')
    ->get('whitelisted_ip_addresses'))));
  if (in_array($path_args[1], $paths)) {
    return TRUE;
  }

  // If user is anonymous or has no timeout set.
  if ($user
    ->id() == 0 || !\Drupal::service('autologout.manager')
    ->getUserTimeout()) {
    return TRUE;
  }

  // If the user has checked remember_me via the remember_me module.
  $remember_me = \Drupal::service('user.data')
    ->get('remember_me', $user
    ->id(), 'remember_me');
  if (!empty($remember_me)) {
    return TRUE;
  }

  // If the user has checked Remember me on the login page via
  // the persistent_login module.
  if (\Drupal::hasService('persistent_login.token_manager')) {
    $request = \Drupal::request();

    // Get cookie from request.
    $cookie = \Drupal::service('persistent_login.cookie_helper')
      ->getCookieValue($request);
    if (isset($cookie)) {

      // Get all user's tokens.
      $rememberPersistentTokens = \Drupal::service('persistent_login.token_manager')
        ->getTokensForUser(User::load($user
        ->id()));
      $count = count($rememberPersistentTokens);
      if ($count > 0) {

        // Get current token.
        $currentToken = \Drupal::service('persistent_login.token_handler')
          ->getTokenFromCookie($request);
        foreach ($rememberPersistentTokens as $value) {
          if (1 == $value
            ->getStatus() && $value
            ->getSeries() == $currentToken
            ->getSeries()) {
            return TRUE;
          }
        }
      }
    }
  }
  if (!empty($ip_address_whitelist) && in_array($user_ip, $ip_address_whitelist)) {
    return TRUE;
  }
}