You are here

function content_lock_timeout_user_logout in Content locking (anti-concurrent editing) 8

Same name and namespace in other branches
  1. 8.2 modules/content_lock_timeout/content_lock_timeout.module \content_lock_timeout_user_logout()

Implements hook_user_logout().

File

modules/content_lock_timeout/content_lock_timeout.module, line 106
Allowed time-based automatic unlocking of nodes.

Code

function content_lock_timeout_user_logout($account) {

  /** @var \Drupal\Core\Session\AccountInterface $account */

  // Only remove locks if there is a timeout given.
  $config = \Drupal::config('content_lock_timeout.settings');
  $timeout_minutes = $config
    ->get('content_lock_timeout_minutes');
  if ($timeout_minutes == 0) {
    return;
  }

  // Only do the database check if the original drupal session manager is used.
  // Otherwise its not sure if sessions table has correct data. As it would be
  // possible to extend the Class, instanceof is not used here!
  if (get_class(Drupal::service('session_manager')) == SessionManager::class) {
    $query = \Drupal::database()
      ->select('sessions');
    $query
      ->condition('uid', $account
      ->id());
    $query = $query
      ->countQuery();
    $session_count = (int) $query
      ->execute()
      ->fetchField();
  }
  else {
    $session_count = FALSE;
  }

  // Only remove all locks of user if its the last session of the user.
  if ($session_count === 1) {

    /** @var \Drupal\content_lock\ContentLock\ContentLock $lock_service */
    $lock_service = \Drupal::service('content_lock');
    $lock_service
      ->releaseAllUserLocks($account
      ->id());
  }
}