AutologoutManager.php in Automated Logout 8
File
src/AutologoutManager.php
View source
<?php
namespace Drupal\autologout;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\SessionManager;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Session\AnonymousUserSession;
use Drupal\user\UserData;
use Drupal\Component\Utility\Xss;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\user\UserInterface;
class AutologoutManager implements AutologoutManagerInterface {
use StringTranslationTrait;
protected $moduleHandler;
protected $autoLogoutSettings;
protected $configFactory;
protected $messenger;
protected $currentUser;
protected $logger;
protected $session;
protected $userData;
protected $time;
protected $entityTypeManager;
protected $stringTranslation;
public function __construct(ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config_factory, MessengerInterface $messenger, AccountInterface $current_user, LoggerChannelFactoryInterface $logger, SessionManager $sessionManager, UserData $userData, TimeInterface $time, EntityTypeManagerInterface $entityTypeManager, TranslationInterface $stringTranslation) {
$this->moduleHandler = $module_handler;
$this->autoLogoutSettings = $config_factory
->get('autologout.settings');
$this->configFactory = $config_factory;
$this->messenger = $messenger;
$this->currentUser = $current_user;
$this->logger = $logger
->get('autologout');
$this->session = $sessionManager;
$this->userData = $userData;
$this->time = $time;
$this->entityTypeManager = $entityTypeManager;
$this->stringTranslation = $stringTranslation;
}
public function preventJs() {
foreach ($this->moduleHandler
->invokeAll('autologout_prevent') as $prevent) {
if (!empty($prevent)) {
return TRUE;
}
}
return FALSE;
}
public function refreshOnly() {
foreach ($this->moduleHandler
->invokeAll('autologout_refresh_only') as $module_refresh_only) {
if (!empty($module_refresh_only)) {
return TRUE;
}
}
return FALSE;
}
public function inactivityMessage() {
$message = Xss::filter($this->autoLogoutSettings
->get('inactivity_message'));
$type = $this->autoLogoutSettings
->get('inactivity_message_type');
if (!empty($message)) {
$this->messenger
->addMessage($this
->t('@message', [
'@message' => $message,
]), $type);
}
}
public function logout() {
$user = $this->currentUser;
if ($this->autoLogoutSettings
->get('use_watchdog')) {
$this->logger
->info('Session automatically closed for %name by autologout.', [
'%name' => $user
->getAccountName(),
]);
}
$this->moduleHandler
->invokeAll('user_logout', [
$user,
]);
$this->session
->destroy();
$user
->setAccount(new AnonymousUserSession());
}
public function getRoleTimeout() {
$roles = user_roles(TRUE);
$role_timeout = [];
foreach ($roles as $name => $role) {
$role_settings = $this->configFactory
->get('autologout.role.' . $name);
if ($role_settings
->get('enabled')) {
$timeout_role = $role_settings
->get('timeout');
$role_timeout[$name] = $timeout_role;
}
}
return $role_timeout;
}
public function getRoleUrl() {
$roles = user_roles(TRUE);
$role_url = [];
foreach ($roles as $name => $role) {
$role_settings = $this->configFactory
->get('autologout.role.' . $name);
if ($role_settings
->get('enabled')) {
$url_role = $role_settings
->get('url');
$role_url[$name] = $url_role;
}
}
return $role_url;
}
public function getRemainingTime() {
if ($this->configFactory
->get('logout_regardless_of_activity')) {
$time_passed = $this->time
->getRequestTime() - $_COOKIE['Drupal_visitor_autologout_login'];
}
else {
$time_passed = isset($_SESSION['autologout_last']) ? $this->time
->getRequestTime() - $_SESSION['autologout_last'] : 0;
}
$timeout = $this
->getUserTimeout();
return $timeout - $time_passed;
}
public function createTimer() {
return $this
->getRemainingTime();
}
public function getUserTimeout($uid = NULL) {
if (is_null($uid)) {
$user = $this->currentUser;
}
else {
$user = $this->entityTypeManager
->getStorage('user')
->load($uid);
}
if ($user
->id() == 0) {
return 0;
}
$user_timeout = $this->userData
->get('autologout', $user
->id(), 'timeout');
if (is_numeric($user_timeout)) {
return $user_timeout;
}
if ($this->autoLogoutSettings
->get('role_logout')) {
$user_roles = $user
->getRoles();
$output = [];
$timeouts = $this
->getRoleTimeout();
foreach ($user_roles as $rid => $role) {
if (isset($timeouts[$role])) {
$output[$rid] = $timeouts[$role];
}
}
if (!empty($output)) {
if ($this->autoLogoutSettings
->get('role_logout_max')) {
return max($output);
}
else {
return min($output);
}
}
}
return $this->autoLogoutSettings
->get('timeout');
}
public function getUserRedirectUrl($uid = NULL) {
if (is_null($uid)) {
$user = $this->entityTypeManager
->getStorage('user')
->load($this->currentUser
->id());
}
else {
$user = $this->entityTypeManager
->getStorage('user')
->load($uid);
}
if ($user
->id() == 0) {
return;
}
if ($this->autoLogoutSettings
->get('role_logout')) {
$user_roles = $user
->getRoles();
$output = [];
$urls = $this
->getRoleUrl();
foreach ($user_roles as $rid => $role) {
if (isset($urls[$role])) {
$output[$rid] = $urls[$role];
}
}
if (!empty($output) && !empty(reset($output))) {
return reset($output);
}
}
return $this->autoLogoutSettings
->get('redirect_url');
}
public function logoutRole(UserInterface $user) {
if ($this->autoLogoutSettings
->get('role_logout')) {
foreach ($user->roles as $name => $role) {
if ($this->configFactory
->get('autologout.role.' . $name . '.enabled')) {
return TRUE;
}
}
}
return FALSE;
}
}