session_limit.tokens.inc 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * @file
  4. *
  5. * Provide tokens for session limit.
  6. */
  7. /**
  8. * Implements hook_token_info().
  9. *
  10. * Create tokens for the current user.
  11. */
  12. function session_limit_token_info() {
  13. $type = array(
  14. 'name' => t('Session limit'),
  15. 'description' => t('Tokens related to session limit module.'),
  16. 'needs-data' => 'user',
  17. );
  18. $session_limit['default'] = array(
  19. 'name' => t('Default sessions'),
  20. 'description' => t('Maximum number of active sessions configured specific to the user.'),
  21. );
  22. $session_limit['max'] = array(
  23. 'name' => t('Maximum sessions'),
  24. 'description' => t('Maximum number of active sessions allowed, accounting for all configuration possibilities.'),
  25. );
  26. $session_limit['role'] = array(
  27. 'name' => t('Maximum sessions by role'),
  28. 'description' => t('Maximum number of active sessions allowed by role.'),
  29. );
  30. $session_limit['user'] = array(
  31. 'name' => t('Maximum sessions for user'),
  32. 'description' => t('Maximum number of active sessions configured specific to the user.'),
  33. );
  34. return array(
  35. 'types' => array('session_limit' => $type),
  36. 'tokens' => array('session_limit' => $session_limit),
  37. );
  38. }
  39. /**
  40. * Implements hook_tokens().
  41. */
  42. function session_limit_tokens($type, $tokens, array $data = array(), array $options = array()) {
  43. $replacements = array();
  44. // The session limit variable does not need the user context.
  45. if ($type == 'session_limit') {
  46. foreach ($tokens as $name => $original) {
  47. if ($name == 'default') {
  48. $replacements[$original] = variable_get('session_limit_max', 1);
  49. }
  50. }
  51. }
  52. if ($type == 'session_limit' && !empty($data['user'])) {
  53. $account = user_load($data['user']->uid);
  54. foreach ($tokens as $name => $original) {
  55. switch ($name) {
  56. case 'max':
  57. $replacements[$original] = session_limit_user_max_sessions($account);
  58. break;
  59. case 'role':
  60. $replacements[$original] = session_limit_user_max_sessions_byrole($account);
  61. break;
  62. case 'user':
  63. $replacements[$original] = empty($account->session_limit) ? 0 : check_plain($account->session_limit);
  64. break;
  65. }
  66. }
  67. }
  68. return $replacements;
  69. }