session_limit.module 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. <?php
  2. /**
  3. * @file
  4. * Limits multiple sessions per user.
  5. */
  6. /**
  7. * Do nothing if the session limit is exceeded.
  8. */
  9. define('SESSION_LIMIT_DO_NOTHING', 0);
  10. /**
  11. * Automatically drop sessions that would exceed the limit.
  12. */
  13. define('SESSION_LIMIT_DROP', 1);
  14. /**
  15. * Disallow sessions that would exceed the limit.
  16. */
  17. define('SESSION_LIMIT_DISALLOW_NEW', 2);
  18. /**
  19. * Default drupal_set_message type for logout message.
  20. */
  21. define('SESSION_LIMIT_LOGGED_OUT_MESSAGE_SEVERITY', 'error');
  22. /**
  23. * Default message displayed when a user is logged out.
  24. */
  25. define('SESSION_LIMIT_LOGGED_OUT_MESSAGE', 'You have been automatically logged out. Someone else has logged in with your username and password and the maximum number of @number simultaneous sessions was exceeded. This may indicate that your account has been compromised or that account sharing is not allowed on this site. Please contact the site administrator if you suspect your account has been compromised.');
  26. /**
  27. * Implements hook_help().
  28. */
  29. function session_limit_help($path, $args) {
  30. switch ($path) {
  31. case 'admin/help#session_limit':
  32. $output = '<p>' . t("The two major notice messages to users are passed through Drupal's t() function. This maintains locale support, allowing you to override strings in any language, but such text is also available to be changed through other modules like !stringoverridesurl.", array('!stringoverridesurl' => l('String Overrides', 'http://drupal.org/project/stringoverrides'))) . '</p>';
  33. $output .= '<p>' . t("The two major strings are as follows:") . '</p>';
  34. $output .= '<p><blockquote>';
  35. $output .= 'The maximum number of simultaneous sessions (@number) for your account has been reached. You did not log off from a previous session or someone else is logged on to your account. This may indicate that your account has been compromised or that account sharing is limited on this site. Please contact the site administrator if you suspect your account has been compromised.';
  36. $output .= '</blockquote></p><p><blockquote>';
  37. $output .= 'You have been automatically logged out. Someone else has logged in with your username and password and the maximum number of @number simultaneous sessions was exceeded. This may indicate that your account has been compromised or that account sharing is not allowed on this site. Please contact the site administrator if you suspect your account has been compromised.';
  38. $output .= '</blockquote></p>';
  39. return $output;
  40. case 'session/limit':
  41. return t('The maximum number of simultaneous sessions (@number) for your account has been reached. You did not log off from a previous session or someone else is logged on to your account. This may indicate that your account has been compromised or that account sharing is limited on this site. Please contact the site administrator if you suspect your account has been compromised.', array('@number' => session_limit_user_max_sessions()));
  42. }
  43. }
  44. /**
  45. * Implements hook_permission().
  46. */
  47. function session_limit_permission() {
  48. return array(
  49. 'administer session limits by role' => array(
  50. 'title' => t('Administer session limits by role'),
  51. 'description' => t(''),
  52. ),
  53. 'administer session limits per user' => array(
  54. 'title' => t('Administer session limits by user'),
  55. 'description' => t(''),
  56. ),
  57. );
  58. }
  59. /**
  60. * Implements hook_menu().
  61. */
  62. function session_limit_menu() {
  63. $items['session/limit'] = array(
  64. 'title' => 'Session limit exceeded',
  65. 'page callback' => 'drupal_get_form',
  66. 'page arguments' => array('session_limit_page'),
  67. 'access callback' => 'user_is_logged_in',
  68. 'type' => MENU_CALLBACK,
  69. );
  70. $items['mysessions'] = array(
  71. 'title' => 'My sessions',
  72. 'page callback' => 'drupal_get_form',
  73. 'page arguments' => array('session_limit_page'),
  74. 'access callback' => 'user_is_logged_in',
  75. 'type' => MENU_SUGGESTED_ITEM,
  76. );
  77. $items['admin/config/people/session-limit'] = array(
  78. 'title' => 'Session limit',
  79. 'description' => 'Configure session limits.',
  80. 'page callback' => 'drupal_get_form',
  81. 'page arguments' => array('session_limit_settings'),
  82. 'access arguments' => array('administer site configuration'),
  83. 'type' => MENU_NORMAL_ITEM,
  84. );
  85. $items['admin/config/people/session_limit/defaults'] = array(
  86. 'title' => 'Defaults',
  87. 'page callback' => 'drupal_get_form',
  88. 'page arguments' => array('session_limit_settings'),
  89. 'access arguments' => array('administer site configuration'),
  90. 'type' => MENU_DEFAULT_LOCAL_TASK,
  91. );
  92. $items['admin/config/people/session-limit/roles'] = array(
  93. 'title' => 'Role limits',
  94. 'description' => 'Configure session limits by role.',
  95. 'page callback' => 'drupal_get_form',
  96. 'page arguments' => array('session_limit_settings_byrole'),
  97. 'access arguments' => array('administer session limits by role'),
  98. 'type' => MENU_LOCAL_TASK,
  99. );
  100. $items['user/%user/session-limit'] = array(
  101. 'title' => 'Session limit',
  102. 'description' => 'Configure session limit for one user.',
  103. 'page callback' => 'drupal_get_form',
  104. 'page arguments' => array('session_limit_user_settings', 1),
  105. 'access arguments' => array('administer session limits per user'),
  106. 'type' => MENU_LOCAL_TASK,
  107. );
  108. return $items;
  109. }
  110. function session_limit_settings() {
  111. $form['session_limit_max'] = array(
  112. '#type' => 'textfield',
  113. '#title' => t('Default maximum number of active sessions'),
  114. '#default_value' => variable_get('session_limit_max', 1),
  115. '#size' => 2,
  116. '#maxlength' => 3,
  117. '#description' => t('The maximum number of active sessions a user can have. 0 implies unlimited sessions.'),
  118. );
  119. $limit_behaviours = array(
  120. SESSION_LIMIT_DO_NOTHING => t('Do nothing.'),
  121. SESSION_LIMIT_DROP => t('Automatically drop the oldest sessions without prompting.'),
  122. SESSION_LIMIT_DISALLOW_NEW => t('Prevent new session.'),
  123. );
  124. $form['session_limit_behaviour'] = array(
  125. '#type' => 'radios',
  126. '#title' => t('When the session limit is exceeded'),
  127. '#default_value' => variable_get('session_limit_behaviour', SESSION_LIMIT_DO_NOTHING),
  128. '#options' => $limit_behaviours,
  129. );
  130. if (module_exists('masquerade')) {
  131. $form['session_limit_masquerade_ignore'] = array(
  132. '#type' => 'checkbox',
  133. '#title' => t('Ignore masqueraded sessions.'),
  134. '#description' => t("When a user administrator uses the masquerade module to impersonate a different user, it won't count against the session limit counter"),
  135. '#default_value' => variable_get('session_limit_masquerade_ignore', FALSE),
  136. );
  137. }
  138. $form['session_limit_logged_out_message_severity'] = array(
  139. '#type' => 'select',
  140. '#title' => t('Logged out message severity'),
  141. '#default_value' => variable_get('session_limit_logged_out_message_severity', SESSION_LIMIT_LOGGED_OUT_MESSAGE_SEVERITY),
  142. '#options' => array(
  143. 'error' => t('Error'),
  144. 'warning' => t('Warning'),
  145. 'status' => t('Status'),
  146. '_none' => t('No Message'),
  147. ),
  148. '#description' => t('The Drupal message type. Defaults to Error.'),
  149. );
  150. $form['session_limit_logged_out_message'] = array(
  151. '#type' => 'textarea',
  152. '#title' => t('Logged out message'),
  153. '#default_value' => variable_get('session_limit_logged_out_message', SESSION_LIMIT_LOGGED_OUT_MESSAGE),
  154. '#description' => t('The message that is displayed to a user if the workstation has been logged out.<br />
  155. @number is replaced with the maximum number of simultaneous sessions.'),
  156. );
  157. return system_settings_form($form);
  158. }
  159. /**
  160. * Settings validation form.
  161. */
  162. function session_limit_settings_validate($form, &$form_state) {
  163. $maxsessions = $form_state['values']['session_limit_max'];
  164. if (!is_numeric($maxsessions)) {
  165. form_set_error('session_limit_max', t('You must enter a number for the maximum number of active sessions'));
  166. }
  167. elseif ($maxsessions < 0) {
  168. form_set_error('session_limit_max', t('Maximum number of active sessions must be positive'));
  169. }
  170. }
  171. /**
  172. * Settings by role form.
  173. */
  174. function session_limit_settings_byrole() {
  175. $result = db_select('variable', 'v')
  176. ->fields('v', array('name', 'value'))
  177. ->condition('name', 'session_limit_rid_%', 'LIKE')
  178. ->orderBy('name')
  179. ->execute();
  180. foreach ($result as $setting) {
  181. $role_limits[$setting->name] = unserialize($setting->value);
  182. }
  183. $roles = user_roles(TRUE);
  184. foreach ($roles as $rid => $role) {
  185. $form["session_limit_rid_$rid"] = array(
  186. '#type' => 'select',
  187. '#options' => _session_limit_user_options(),
  188. '#title' => check_plain($role),
  189. '#default_value' => empty($role_limits["session_limit_rid_$rid"]) ? 0 : $role_limits["session_limit_rid_$rid"],
  190. );
  191. }
  192. $form['submit'] = array(
  193. '#type' => 'submit',
  194. '#value' => t('Save permissions'),
  195. );
  196. return $form;
  197. }
  198. /**
  199. * Set session limits by role form submission.
  200. */
  201. function session_limit_settings_byrole_submit($form, &$form_state) {
  202. db_delete('variable')
  203. ->condition('name', 'session_limit_rid_%', 'LIKE')
  204. ->execute();
  205. foreach ($form_state['values'] as $setting_name => $setting_limit) {
  206. variable_set($setting_name, $setting_limit);
  207. }
  208. drupal_set_message(t('Role settings updated.'), 'status');
  209. watchdog('session_limit', 'Role limits updated.', array(), WATCHDOG_INFO);
  210. }
  211. /**
  212. * Implements hook_init().
  213. *
  214. * Determine whether session has been verified. Redirect user if over session
  215. * limit. Established Sessions do NOT need to verify every page load. The new
  216. * session must deal w/ determining which connection is cut.
  217. *
  218. * This intentionally doesn't use hook_user()'s login feature because that's
  219. * only really useful if the login event always boots off at least one other
  220. * active session. Doing it this way makes sure that the newest session can't
  221. * browse to a different page after their login has validated.
  222. */
  223. function session_limit_init() {
  224. global $user;
  225. if ($user->uid > 1 && !isset($_SESSION['session_limit'])) {
  226. if (_session_limit_bypass()) {
  227. // Bypass the session limitation on this page callback.
  228. return;
  229. }
  230. $query = db_select('sessions', 's')
  231. // Use distict so that HTTP and HTTPS sessions
  232. // are considered a single session.
  233. ->distinct()
  234. ->fields('s', array('sid'))
  235. ->condition('s.uid', $user->uid);
  236. if (module_exists('masquerade') && variable_get('session_limit_masquerade_ignore', FALSE)) {
  237. $query->leftJoin('masquerade', 'm', 's.uid = m.uid_as AND s.sid = m.sid');
  238. $query->isNull('m.sid');
  239. }
  240. $active_sessions = $query->countQuery()->execute()->fetchField();
  241. $max_sessions = session_limit_user_max_sessions();
  242. if (!empty($max_sessions) && $active_sessions > $max_sessions) {
  243. session_limit_invoke_session_limit(session_id(), 'collision');
  244. }
  245. else {
  246. // force checking this twice as there's a race condition around session creation.
  247. // see issue #1176412
  248. if (!isset($_SESSION['session_limit_checkonce'])) {
  249. $_SESSION['session_limit_checkonce'] = TRUE;
  250. }
  251. else {
  252. // mark session as verified to bypass this in future.
  253. $_SESSION['session_limit'] = TRUE;
  254. }
  255. }
  256. }
  257. }
  258. /**
  259. * Implements hook_action_info_alter().
  260. */
  261. function session_limit_action_info_alter(&$info) {
  262. if (module_exists('token_actions')) {
  263. foreach ($info as $type => $data) {
  264. if (stripos($type, "token_actions_") === 0 || stripos($type, "system_") === 0) {
  265. if (isset($info[$type]['hooks']['session_limit'])) {
  266. array_merge($info[$type]['hooks']['session_limit'], array('collision', 'disconnect'));
  267. }
  268. else {
  269. $info[$type]['hooks']['session_limit'] = array('collision', 'disconnect');
  270. }
  271. }
  272. }
  273. }
  274. }
  275. /**
  276. * Implements hook_field_extra_fields().
  277. */
  278. function session_limit_field_extra_fields() {
  279. $extra['user']['user']['display'] = array(
  280. 'session_limit' => array(
  281. 'label' => t('Session limit'),
  282. 'description' => t('Session limit.'),
  283. 'weight' => 10,
  284. ),
  285. );
  286. return $extra;
  287. }
  288. /**
  289. * Implements hook_user_view().
  290. */
  291. function session_limit_user_view($account, $view_mode) {
  292. if (user_access('administer session limits per user')) {
  293. $account->content['session_limit'] = array(
  294. '#title' => t('Session limit'),
  295. '#type' => 'user_profile_category',
  296. 'session_limit' => array(
  297. '#markup' => empty($account->data['session_limit']) ? t('Default') : $account->data['session_limit'],
  298. )
  299. );
  300. }
  301. }
  302. /**
  303. * Session limit user settings form.
  304. */
  305. function session_limit_user_settings($form, $form_state, $account) {
  306. $form['account'] = array(
  307. '#type' => 'value',
  308. '#value' => $account,
  309. );
  310. $form['session_limit'] = array(
  311. '#type' => 'select',
  312. '#title' => t('Maximum sessions'),
  313. '#description' => t('Total number simultaneous active sessions this user may have at one time. The default defers to the limits that apply to each of the user\'s roles.'),
  314. '#required' => FALSE,
  315. '#default_value' => empty($account->data['session_limit']) ? 0 : $account->data['session_limit'],
  316. '#options' => _session_limit_user_options(),
  317. );
  318. $form['submit'] = array(
  319. '#type' => 'submit',
  320. '#value' => t('Save configuration'),
  321. );
  322. return $form;
  323. }
  324. /**
  325. * Session limit user settings form validation.
  326. */
  327. function session_limit_user_settings_validate($form, &$form_state) {
  328. if (!is_numeric($form_state['values']['session_limit'])) {
  329. form_set_error('session_limit', t('Only numeric submissions are valid.'));
  330. watchdog('session_limit', 'Invalid session limit submission for @user.', array('@user' => $form_state['values']['account']->name), WATCHDOG_DEBUG);
  331. }
  332. }
  333. /**
  334. * Session limit user settings form submission.
  335. */
  336. function session_limit_user_settings_submit($form, &$form_state) {
  337. watchdog('session_limit', 'Maximum sessions for @user updated to @count.', array('@user' => $form_state['values']['account']->name, '@count' => $form_state['values']['session_limit']), WATCHDOG_INFO, l(t('view'), "user/{$form_state['values']['account']->uid}"));
  338. if (empty($form_state['values']['session_limit'])) {
  339. $form_state['values']['session_limit'] = NULL;
  340. }
  341. user_save($form_state['values']['account'], array('data' => array('session_limit' => $form_state['values']['session_limit'])));
  342. drupal_set_message(t('Session limit updated for %user.', array('%user' => $form_state['values']['account']->name)), 'status', TRUE);
  343. }
  344. /**
  345. * Display or delete sessions form.
  346. */
  347. function session_limit_page() {
  348. global $user;
  349. if (variable_get('session_limit_behaviour', SESSION_LIMIT_DO_NOTHING) == SESSION_LIMIT_DISALLOW_NEW) {
  350. session_destroy();
  351. $user = drupal_anonymous_user();
  352. return;
  353. }
  354. $result = db_query('SELECT * FROM {sessions} WHERE uid = :uid', array(':uid' => $user->uid));
  355. foreach ($result as $obj) {
  356. $message = $user->sid == $obj->sid ? t('Your current session.') : '';
  357. $sids[$obj->sid] = t('<strong>Host:</strong> %host (idle: %time) <b>@message</b>',
  358. array(
  359. '%host' => $obj->hostname,
  360. '@message' => $message,
  361. '%time' => format_interval(time() - $obj->timestamp))
  362. );
  363. }
  364. $form['sid'] = array(
  365. '#type' => 'radios',
  366. '#title' => t('Select a session to disconnect.'),
  367. '#options' => $sids,
  368. );
  369. $form['submit'] = array(
  370. '#type' => 'submit',
  371. '#value' => t('Disconnect session'),
  372. );
  373. return $form;
  374. }
  375. /**
  376. * Handler for submissions from session_limit_page().
  377. */
  378. function session_limit_page_submit($form, &$form_state) {
  379. if ($GLOBALS['user']->sid == $form_state['values']['sid']) {
  380. drupal_goto('user/logout');
  381. }
  382. else {
  383. session_limit_invoke_session_limit($form_state['values']['sid'], 'disconnect');
  384. drupal_goto();
  385. }
  386. }
  387. /**
  388. * Helper function for populating the values of the settings form select.
  389. */
  390. function _session_limit_user_options() {
  391. $options = drupal_map_assoc(range(0, 250));
  392. $options[0] = t('Default');
  393. $options['999999'] = t('Disabled');
  394. return $options;
  395. }
  396. /**
  397. * Get the maximum number of sessions for a user.
  398. *
  399. * @param user $account
  400. * (optional) The user account to check. If not
  401. * supplied the active user account is used.
  402. */
  403. function session_limit_user_max_sessions($account = NULL) {
  404. $limits = &drupal_static(__FUNCTION__, array());
  405. if (empty($account)) {
  406. $account = $GLOBALS['user'];
  407. }
  408. if (!isset($limits[$account->uid])) {
  409. $limits[$account->uid] = (int) variable_get('session_limit_max', 1);
  410. $limit_account = session_limit_user_max_sessions_byuser($account);
  411. $limit_role = session_limit_user_max_sessions_byrole($account);
  412. if ($limit_account > 0) {
  413. $limits[$account->uid] = $limit_account;
  414. }
  415. elseif ($limit_role > 0) {
  416. $limits[$account->uid] = $limit_role;
  417. }
  418. $limits[$account->uid] = (int) $limits[$account->uid];
  419. }
  420. return $limits[$account->uid];
  421. }
  422. /**
  423. * Get user specified session limit.
  424. *
  425. * @param user $account
  426. * The user account to get the session limit for
  427. *
  428. * @return int
  429. * Maximum number of sessions.
  430. * A value of 0 means that no user limit is set for the current user
  431. * and so the role limit should be used (or default if no role limit either).
  432. */
  433. function session_limit_user_max_sessions_byuser($account) {
  434. return (int) empty($account->data['session_limit']) ? 0 : $account->data['session_limit'];
  435. }
  436. /**
  437. * Get the maximum number of sessions allowed by the roles of an account.
  438. *
  439. * @param user $account
  440. * The account to check the roles of.
  441. *
  442. * @return int
  443. * The maximum number of sessions the user is allowed by their roles.
  444. * A value of 0 means that no role limit exists for this user and so
  445. * the default should be used.
  446. */
  447. function session_limit_user_max_sessions_byrole($account) {
  448. $limits = array();
  449. foreach ($account->roles as $rid => $name) {
  450. $role_limit = variable_get("session_limit_rid_$rid", FALSE);
  451. if (!empty($role_limit)) {
  452. $limits[] = (int) $role_limit;
  453. }
  454. }
  455. return empty($limits) ? 0 : max($limits);
  456. }
  457. /**
  458. * Implements hook_trigger_info().
  459. */
  460. function session_limit_trigger_info() {
  461. return array(
  462. 'session_limit' => array(
  463. 'session_limit_collision' => array(
  464. 'label' => t('User logs in and has too many active sessions'),
  465. ),
  466. 'session_limit_disconnect' => array(
  467. 'label' => t('When an active user is logged out by a newer session'),
  468. ),
  469. ),
  470. );
  471. }
  472. /**
  473. * Limit a users access to the sites based on the current session.
  474. *
  475. * @param string $session
  476. * The session id string which identifies the current session.
  477. * @param string $op
  478. * The action which caused the session limitation event. This is
  479. * either 'collision' or 'disconnect'.
  480. *
  481. * @return array
  482. * The results of all hook_session_limit functions.
  483. * Note that in a collision event, a Drupal goto is executed so
  484. * this function does not return.
  485. */
  486. function session_limit_invoke_session_limit($session, $op) {
  487. $return = array();
  488. // Execute the hook_session_limit().
  489. foreach (module_implements('session_limit') as $name) {
  490. $function = $name . '_session_limit';
  491. $result = $function($session, $op);
  492. if (isset($result) && is_array($result)) {
  493. $return = array_merge($return, $result);
  494. }
  495. elseif (isset($result)) {
  496. $return[] = $result;
  497. }
  498. }
  499. // In the event of a collision, redirect to session handler.
  500. if ($op == 'collision') {
  501. if (variable_get('session_limit_behaviour', SESSION_LIMIT_DO_NOTHING) == SESSION_LIMIT_DROP) {
  502. global $user;
  503. // Get the number of sessions that should be removed.
  504. $limit = db_query("SELECT COUNT(DISTINCT(sid)) - :max_sessions FROM {sessions} WHERE uid = :uid", array(
  505. ':max_sessions' => session_limit_user_max_sessions($user),
  506. ':uid' => $user->uid,
  507. ))->fetchField();
  508. if ($limit > 0) {
  509. // Secure session ids are seperate rows in the database, but we don't want to kick
  510. // the user off there http session and not there https session or vice versa. This
  511. // is why this query is DISTINCT.
  512. $result = db_select('sessions', 's')
  513. ->distinct()
  514. ->fields('s', array('sid'))
  515. ->condition('s.uid', $user->uid)
  516. ->orderBy('timestamp', 'ASC')
  517. ->range(0, $limit)
  518. ->execute();
  519. foreach ($result as $session) {
  520. session_limit_invoke_session_limit($session->sid, 'disconnect');
  521. }
  522. }
  523. }
  524. else {
  525. // Otherwise re-direct to the session handler page so the user can
  526. // choose which action they would like to take.
  527. drupal_goto('session/limit');
  528. }
  529. }
  530. return $return;
  531. }
  532. /**
  533. * Implements hook_session_limit().
  534. */
  535. function session_limit_session_limit($sid, $op) {
  536. switch ($op) {
  537. case 'collision':
  538. watchdog('session_limit', 'Exceeded maximum allowed active sessions.', array(), WATCHDOG_INFO);
  539. break;
  540. case 'disconnect':
  541. $message = variable_get('session_limit_logged_out_message', SESSION_LIMIT_LOGGED_OUT_MESSAGE);
  542. $message_severity = variable_get('session_limit_logged_out_message_severity', SESSION_LIMIT_LOGGED_OUT_MESSAGE_SEVERITY);
  543. $fields['session'] = '';
  544. if ($message_severity != '_none' && !empty($message)) {
  545. $logout_message = t($message, array('@number' => session_limit_user_max_sessions()));
  546. $logout_message = 'messages|' . serialize(array($message_severity => array($logout_message)));
  547. $fields['session'] = $logout_message;
  548. }
  549. $fields['uid'] = 0;
  550. db_update('sessions')
  551. ->fields($fields)
  552. ->condition('sid', $sid)
  553. ->execute();
  554. watchdog('session_limit', 'Disconnected for excess active sessions.', array(), WATCHDOG_NOTICE);
  555. break;
  556. }
  557. }
  558. /**
  559. * Implements hook_session_limit().
  560. *
  561. * This implements the hook on behalf of the trigger module.
  562. */
  563. function trigger_session_limit($sid, $op) {
  564. // Find all the actions against this $op.
  565. // Note: this notation requires the $op to match the bit in the trigger info keys after session_limit!
  566. $aids = trigger_get_assigned_actions('session_limit_' . $op);
  567. $context = array(
  568. 'hook' => 'session_limit',
  569. 'op' => $op,
  570. 'sid' => $sid,
  571. );
  572. actions_do(array_keys($aids), $GLOBALS['user'], $context);
  573. }
  574. /**
  575. * Implements hook_session_limit().
  576. *
  577. * This implements the hook on behalf of the rules module.
  578. */
  579. function rules_session_limit($sid, $op) {
  580. global $user;
  581. rules_invoke_event('session_limit_' . $op, $user, $sid);
  582. }
  583. /**
  584. * Implements hook_session_limit_bypass().
  585. *
  586. * @return bool
  587. * TRUE if the page request should bypass session limitation restrictions.
  588. */
  589. function session_limit_session_limit_bypass() {
  590. if ((arg(0) == 'session' && arg(1) == 'limit') || (arg(0) == 'user' && arg(1) == 'logout')) {
  591. return TRUE;
  592. }
  593. }
  594. /**
  595. * Execute the session limit bypass hook.
  596. *
  597. * Allow other modules to prevent session limits in their own requirements.
  598. *
  599. * @return bool
  600. * TRUE if session limitation should be bypassed.
  601. */
  602. function _session_limit_bypass() {
  603. foreach (module_invoke_all('session_limit_bypass') as $bypass) {
  604. if (!empty($bypass)) {
  605. return TRUE;
  606. }
  607. }
  608. return FALSE;
  609. }
  610. /**
  611. * Implements hook_variable_group_info().
  612. */
  613. function session_limit_variable_group_info() {
  614. $groups['session_limit'] = array(
  615. 'title' => t('Session limit'),
  616. 'access' => 'administer site configuration',
  617. 'path' => array('admin/config/people/session_limit/defaults'),
  618. );
  619. return $groups;
  620. }
  621. /**
  622. * Implements hook_variable_info().
  623. */
  624. function session_limit_variable_info($options) {
  625. $variables['session_limit_logged_out_message'] = array(
  626. 'title' => t('Logged out message', array(), $options),
  627. 'description' => t('The message that is displayed to a user if the workstation has been logged out.<br />
  628. @number is replaced with the maximum number of simultaneous sessions.', array(), $options),
  629. 'type' => 'string',
  630. 'localize' => TRUE,
  631. 'group' => 'session_limit',
  632. );
  633. return $variables;
  634. }