You are here

function _autologout_invalidate_other_sessions in Automated Logout 7.2

Same name and namespace in other branches
  1. 6.2 autologout.module \_autologout_invalidate_other_sessions()
1 call to _autologout_invalidate_other_sessions()
autologout_user_login in ./autologout.module
Implements hook_user_login().

File

./autologout.module, line 545
Used to automagically log out a user after a preset time, AjK May 2006

Code

function _autologout_invalidate_other_sessions($account) {

  // Do nothing if anonymous.
  if ($account->uid == 0) {
    return;
  }

  // check to see if the user is already logged in somewhere else
  // if so deactivate that login and let the user know that the
  // other session has been deactivated
  $result = db_select('sessions', 'ss')
    ->fields('ss')
    ->condition('ss.uid', $account->uid)
    ->condition('ss.sid', session_id(), '<>')
    ->countQuery()
    ->execute()
    ->fetchField();
  if ($result != 0) {

    // Send the message to the current session
    drupal_set_message(t('You are only allowed 1 open session at a time. Your other session has been terminated.'), 'error');

    // Logout OTHER sessions, and send them their message
    $other_session_msg = t('You have been automatically logged out.  You are only allowed 1 open session at a time, and another open session was detected.');
    $other_session_msg = 'messages|' . serialize(array(
      'error' => array(
        $other_session_msg,
      ),
    ));
    db_update('sessions')
      ->fields(array(
      'uid' => '0',
      'session' => $other_session_msg,
    ))
      ->condition('uid', $account->uid, '=')
      ->condition('sid', session_id(), '<>')
      ->execute();

    // Write a watchdog message for the site admin.
    watchdog('Automated Logout', 'One Session automatically logged out user.', array(), WATCHDOG_WARNING);
  }
}