You are here

protected function SwitchUserBlock::getUsers in Devel 8.3

Same name and namespace in other branches
  1. 8 src/Plugin/Block/SwitchUserBlock.php \Drupal\devel\Plugin\Block\SwitchUserBlock::getUsers()
  2. 8.2 src/Plugin/Block/SwitchUserBlock.php \Drupal\devel\Plugin\Block\SwitchUserBlock::getUsers()
  3. 4.x src/Plugin/Block/SwitchUserBlock.php \Drupal\devel\Plugin\Block\SwitchUserBlock::getUsers()

Provides the list of accounts that can be used for the user switch.

Inactive users are omitted from all of the following db selects. Users with 'switch users' permission and anonymous user if include_anon property is set to TRUE, are prioritized.

Return value

\Drupal\Core\Session\AccountInterface[] List of accounts to be used for the switch.

1 call to SwitchUserBlock::getUsers()
SwitchUserBlock::build in src/Plugin/Block/SwitchUserBlock.php
Builds and returns the renderable array for this block plugin.

File

src/Plugin/Block/SwitchUserBlock.php, line 176

Class

SwitchUserBlock
Provides a block for switching users.

Namespace

Drupal\devel\Plugin\Block

Code

protected function getUsers() {
  $list_size = $this->configuration['list_size'];
  $include_anonymous = $this->configuration['include_anon'];
  $list_size = $include_anonymous ? $list_size - 1 : $list_size;

  // Users with 'switch users' permission are prioritized so
  // we try to load first users with this permission.
  $query = $this->userStorage
    ->getQuery()
    ->condition('uid', 0, '>')
    ->condition('status', 0, '>')
    ->sort('access', 'DESC')
    ->range(0, $list_size);
  $roles = user_roles(TRUE, 'switch users');
  if (!empty($roles) && !isset($roles[Role::AUTHENTICATED_ID])) {
    $query
      ->condition('roles', array_keys($roles), 'IN');
  }
  $user_ids = $query
    ->execute();

  // If we don't have enough users with 'switch users' permission, add
  // uids until we hit $list_size.
  if (count($user_ids) < $list_size) {
    $query = $this->userStorage
      ->getQuery()
      ->condition('uid', 0, '>')
      ->condition('status', 0, '>')
      ->sort('access', 'DESC')
      ->range(0, $list_size);

    // Excludes the prioritized user ids only if the previous query return
    // some records.
    if (!empty($user_ids)) {
      $query
        ->condition('uid', array_keys($user_ids), 'NOT IN');
      $query
        ->range(0, $list_size - count($user_ids));
    }
    $user_ids += $query
      ->execute();
  }

  /** @var \Drupal\Core\Session\AccountInterface[] $accounts */
  $accounts = $this->userStorage
    ->loadMultiple($user_ids);
  if ($include_anonymous) {
    $anonymous = new AnonymousUserSession();
    $accounts[$anonymous
      ->id()] = $anonymous;
  }
  uasort($accounts, 'static::sortUserList');
  return $accounts;
}