You are here

function autologout_user_login in Automated Logout 7.4

Same name and namespace in other branches
  1. 8 autologout.module \autologout_user_login()
  2. 7.2 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 747
Used to automagically log out a user after a preset time.

Code

function autologout_user_login(&$edit, $account) {

  // Cleanup old sessions.
  $timeout = _autologout_get_user_timeout($account->uid);
  if (empty($timeout)) {

    // Users that don't get logged have their sessions left.
    return;
  }
  $timeout_padding = variable_get('autologout_padding', 10);
  $timestamp = time() - ($timeout + $timeout_padding);

  // Find all stale sessions.
  $results = db_select('sessions', 's')
    ->fields('s')
    ->condition('uid', $account->uid)
    ->condition('timestamp', $timestamp, '<')
    ->orderBy('timestamp', 'DESC')
    ->execute();
  $sids = array();
  foreach ($results as $session) {
    $sids[] = $session->sid;
  }
  if (!empty($sids)) {

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