You are here

function autologout_user_login in Automated Logout 8

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

Implements hook_user_login().

Delete stale sessions for the user on login. This stops session_limit module thinking the user has reached their session limit.

File

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

Code

function autologout_user_login($account) {

  // Cleanup old sessions.
  $timeout = \Drupal::service('autologout.manager')
    ->getUserTimeout($account
    ->id());
  if (empty($timeout)) {

    // Users that don't get logged have their sessions left.
    return;
  }
  $timeout_padding = \Drupal::config('autologout.settings')
    ->get('padding');
  $timestamp = \Drupal::time()
    ->getRequestTime() - ($timeout + $timeout_padding);

  // Find all stale sessions.
  $database = \Drupal::database();
  $sids = $database
    ->select('sessions', 's')
    ->fields('s', [
    'sid',
  ])
    ->condition('uid', $account
    ->id())
    ->condition('timestamp', $timestamp, '<')
    ->orderBy('timestamp', 'DESC')
    ->execute()
    ->fetchCol();
  if (!empty($sids)) {

    // Delete stale sessions at login.
    $database
      ->delete('sessions')
      ->condition('sid', $sids, 'IN')
      ->execute();
  }

  // Add login time cookie.
  user_cookie_save([
    'autologout_login' => \Drupal::time()
      ->getCurrentTime(),
  ]);
}