AutologoutManager.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace Drupal\autologout;
  3. use Drupal\Core\Config\ConfigFactoryInterface;
  4. use Drupal\Core\Extension\ModuleHandlerInterface;
  5. use Drupal\user\Entity\User;
  6. use Drupal\Core\Session\AnonymousUserSession;
  7. /**
  8. * Defines an AutologoutManager service.
  9. */
  10. class AutologoutManager implements AutologoutManagerInterface {
  11. /**
  12. * The module manager service.
  13. *
  14. * @var \Drupal\Core\Extension\ModuleHandlerInterface
  15. */
  16. protected $moduleHandler;
  17. /**
  18. * The config object for 'autologout.settings'.
  19. *
  20. * @var \Drupal\Core\Config\Config
  21. */
  22. protected $autoLogoutSettings;
  23. /**
  24. * The config factory service.
  25. *
  26. * @var \Drupal\Core\Config\ConfigFactoryInterface
  27. */
  28. protected $configFactory;
  29. /**
  30. * Constructs an AutologoutManager object.
  31. *
  32. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
  33. * The module handler.
  34. */
  35. public function __construct(ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config_factory) {
  36. $this->moduleHandler = $module_handler;
  37. $this->autoLogoutSettings = $config_factory->get('autologout.settings');
  38. $this->configFactory = $config_factory;
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function preventJs() {
  44. foreach ($this->moduleHandler->invokeAll('autologout_prevent') as $prevent) {
  45. if (!empty($prevent)) {
  46. return TRUE;
  47. }
  48. }
  49. return FALSE;
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function refreshOnly() {
  55. foreach ($this->moduleHandler->invokeAll('autologout_refresh_only') as $module_refresh_only) {
  56. if (!empty($module_refresh_only)) {
  57. return TRUE;
  58. }
  59. }
  60. return FALSE;
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function inactivityMessage() {
  66. $message = $this->autoLogoutSettings->get('inactivity_message');
  67. if (!empty($message)) {
  68. drupal_set_message($message);
  69. }
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function logout() {
  75. $user = \Drupal::currentUser();
  76. if ($this->autoLogoutSettings->get('use_watchdog')) {
  77. \Drupal::logger('user')->info('Session automatically closed for %name by autologout.', ['%name' => $user->getAccountName()]);
  78. }
  79. // Destroy the current session.
  80. $this->moduleHandler->invokeAll('user_logout', [$user]);
  81. \Drupal::service('session_manager')->destroy();
  82. $user->setAccount(new AnonymousUserSession());
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function getRoleTimeout() {
  88. $roles = user_roles(TRUE);
  89. $role_timeout = [];
  90. // Go through roles, get timeouts for each and return as array.
  91. foreach ($roles as $name => $role) {
  92. $role_settings = $this->configFactory->get('autologout.role.' . $name);
  93. if ($role_settings->get('enabled')) {
  94. $timeout_role = $role_settings->get('timeout');
  95. $role_timeout[$name] = $timeout_role;
  96. }
  97. }
  98. return $role_timeout;
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function getRemainingTime() {
  104. $timeout = $this->getUserTimeout();
  105. $time_passed = isset($_SESSION['autologout_last']) ? REQUEST_TIME - $_SESSION['autologout_last'] : 0;
  106. return $timeout - $time_passed;
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function createTimer() {
  112. return $this->getRemainingTime();
  113. }
  114. /**
  115. * {@inheritdoc}
  116. */
  117. public function getUserTimeout($uid = NULL) {
  118. if (is_null($uid)) {
  119. // If $uid is not provided, use the logged in user.
  120. $user = \Drupal::currentUser();
  121. }
  122. else {
  123. $user = User::load($uid);
  124. }
  125. if ($user->id() == 0) {
  126. // Anonymous doesn't get logged out.
  127. return 0;
  128. }
  129. $user_timeout = \Drupal::service('user.data')->get('autologout', $user->id(), 'timeout');
  130. if (is_numeric($user_timeout)) {
  131. // User timeout takes precedence.
  132. return $user_timeout;
  133. }
  134. // Get role timeouts for user.
  135. if ($this->autoLogoutSettings->get('role_logout')) {
  136. $user_roles = $user->getRoles();
  137. $output = [];
  138. $timeouts = $this->getRoleTimeout();
  139. foreach ($user_roles as $rid => $role) {
  140. if (isset($timeouts[$role])) {
  141. $output[$rid] = $timeouts[$role];
  142. }
  143. }
  144. // Assign the lowest timeout value to be session timeout value.
  145. if (!empty($output)) {
  146. // If one of the user's roles has a unique timeout, use this.
  147. return min($output);
  148. }
  149. }
  150. // If no user or role override exists, return the default timeout.
  151. return $this->autoLogoutSettings->get('timeout');
  152. }
  153. /**
  154. * {@inheritdoc}
  155. */
  156. public function logoutRole($user) {
  157. if ($this->autoLogoutSettings->get('role_logout')) {
  158. foreach ($user->roles as $name => $role) {
  159. if ($this->configFactory->get('autologout.role.' . $name . '.enabled')) {
  160. return TRUE;
  161. }
  162. }
  163. }
  164. return FALSE;
  165. }
  166. }