You are here

function devel_switch_user_list in Devel 7

Same name and namespace in other branches
  1. 5 devel.module \devel_switch_user_list()
  2. 6 devel.module \devel_switch_user_list()

Provides the Switch user list.

Return value

array An array of links, each of which can be used to switch to a user.

1 call to devel_switch_user_list()
devel_block_switch_user in ./devel.module
Provides the Switch user block.

File

./devel.module, line 903
This module holds functions useful for Drupal development.

Code

function devel_switch_user_list() {
  global $user;
  $links = array();
  if (user_access('switch users')) {
    $list_size = variable_get('devel_switch_user_list_size', 10);
    if ($include_anon = variable_get('devel_switch_user_include_anon', FALSE)) {
      --$list_size;
    }
    $dest = drupal_get_destination();

    // Try to find at least $list_size users that can switch.
    // Inactive users are omitted from all of the following db selects.
    $roles = user_roles(TRUE, 'switch users');
    $query = db_select('users', 'u');
    $query
      ->addField('u', 'uid');
    $query
      ->addField('u', 'access');
    $query
      ->distinct();
    $query
      ->condition('u.uid', 0, '>');
    $query
      ->condition('u.status', 0, '>');
    $query
      ->orderBy('u.access', 'DESC');
    $query
      ->range(0, $list_size);
    if (!isset($roles[DRUPAL_AUTHENTICATED_RID])) {
      $query
        ->leftJoin('users_roles', 'r', 'u.uid = r.uid');
      $or_condition = db_or();
      $or_condition
        ->condition('u.uid', 1);
      if (!empty($roles)) {
        $or_condition
          ->condition('r.rid', array_keys($roles), 'IN');
      }
      $query
        ->condition($or_condition);
    }
    $uids = $query
      ->execute()
      ->fetchCol();
    $accounts = user_load_multiple($uids);
    foreach ($accounts as $account) {
      $path = 'devel/switch/' . $account->name;
      $links[$account->uid] = array(
        'title' => drupal_placeholder(format_username($account)),
        'href' => $path,
        'query' => $dest + array(
          'token' => drupal_get_token($path),
        ),
        'attributes' => array(
          'title' => t('This user can switch back.'),
        ),
        'html' => TRUE,
        'last_access' => $account->access,
      );
    }
    $num_links = count($links);
    if ($num_links < $list_size) {

      // If we don't have enough, add distinct uids until we hit $list_size.
      $uids = db_query_range('SELECT uid FROM {users} WHERE uid > 0 AND uid NOT IN (:uids) AND status > 0 ORDER BY access DESC', 0, $list_size - $num_links, array(
        ':uids' => array_keys($links),
      ))
        ->fetchCol();
      $accounts = user_load_multiple($uids);
      foreach ($accounts as $account) {
        $path = 'devel/switch/' . $account->name;
        $links[$account->uid] = array(
          'title' => format_username($account),
          'href' => $path,
          'query' => array(
            'token' => drupal_get_token($path),
          ),
          'attributes' => array(
            'title' => t('Caution: this user will be unable to switch back.'),
          ),
          'last_access' => $account->access,
        );
      }
      uasort($links, '_devel_switch_user_list_cmp');
    }
    if ($include_anon) {
      $path = 'devel/switch';
      $link = array(
        'title' => format_username(drupal_anonymous_user()),
        'href' => $path,
        'query' => $dest + array(
          'token' => drupal_get_token($path),
        ),
        'attributes' => array(
          'title' => t('Caution: the anonymous user will be unable to switch back.'),
        ),
      );
      if (user_access('switch users', drupal_anonymous_user())) {
        $link['title'] = drupal_placeholder($link['title']);
        $link['attributes'] = array(
          'title' => t('This user can switch back.'),
        );
        $link['html'] = TRUE;
      }
      $links[] = $link;
    }
  }
  if (array_key_exists($user->uid, $links)) {
    $links[$user->uid]['title'] = '<strong>' . $links[$user->uid]['title'] . '</strong>';
  }
  return $links;
}