You are here

public function AutologoutManager::getUserTimeout in Automated Logout 8

Get a user's timeout in seconds.

Parameters

int $uid: (Optional) Provide a user's uid to get the timeout for. Default is the logged in user.

Return value

int The number of seconds the user can be idle for before being logged out. A value of 0 means no timeout.

Overrides AutologoutManagerInterface::getUserTimeout

1 call to AutologoutManager::getUserTimeout()
AutologoutManager::getRemainingTime in src/AutologoutManager.php
Get the time remaining before logout.

File

src/AutologoutManager.php, line 270

Class

AutologoutManager
Defines an AutologoutManager service.

Namespace

Drupal\autologout

Code

public function getUserTimeout($uid = NULL) {
  if (is_null($uid)) {

    // If $uid is not provided, use the logged in user.
    $user = $this->currentUser;
  }
  else {
    $user = $this->entityTypeManager
      ->getStorage('user')
      ->load($uid);
  }
  if ($user
    ->id() == 0) {

    // Anonymous doesn't get logged out.
    return 0;
  }
  $user_timeout = $this->userData
    ->get('autologout', $user
    ->id(), 'timeout');
  if (is_numeric($user_timeout)) {

    // User timeout takes precedence.
    return $user_timeout;
  }

  // Get role timeouts for user.
  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];
      }
    }

    // Assign the lowest/highest timeout value to be session timeout value.
    if (!empty($output)) {

      // If one of the user's roles has a unique timeout, use this.
      if ($this->autoLogoutSettings
        ->get('role_logout_max')) {
        return max($output);
      }
      else {
        return min($output);
      }
    }
  }

  // If no user or role override exists, return the default timeout.
  return $this->autoLogoutSettings
    ->get('timeout');
}