You are here

function devel_switch_user_list in Devel 5

Same name and namespace in other branches
  1. 6 devel.module \devel_switch_user_list()
  2. 7 devel.module \devel_switch_user_list()
1 call to devel_switch_user_list()
devel_block in ./devel.module
Implementation of hook_block().

File

./devel.module, line 410

Code

function devel_switch_user_list() {
  $links = array();
  if (user_access('switch users')) {
    $list_size = variable_get('devel_switch_user_list_size', 10);
    $dest = drupal_get_destination();

    // Try to find at least $list_size users that can switch.
    $roles = user_roles(1, 'switch users');
    if (isset($roles[2])) {

      // If authenticated users have this permission, just grab
      // the last $list_size users, since there won't be records in
      // {user_roles} and every user on the system can switch.
      $users = db_query_range("SELECT DISTINCT u.uid, u.name, u.access FROM {users} u WHERE u.uid > 0 ORDER BY u.access DESC", 0, $list_size);
    }
    else {
      $where = array(
        'u.uid = 1',
      );
      if (count($roles)) {
        $where[] = 'r.rid IN (' . implode(',', array_keys($roles)) . ')';
      }
      $where_sql = implode(' OR ', $where);
      $users = db_query_range("SELECT DISTINCT u.uid, u.name, u.access FROM {users} u LEFT JOIN {users_roles} r ON u.uid = r.uid WHERE {$where_sql} ORDER BY u.access DESC", 0, $list_size);
    }
    while ($user = db_fetch_object($users)) {
      $links[$user->uid] = l(theme('placeholder', $user->name), 'devel/switch/' . $user->name, array(
        'title' => t('This user can switch back.'),
      ), $dest, NULL, FALSE, TRUE);
    }
    $num_links = count($links);
    if ($num_links < $list_size) {

      // If we don't have enough, add distinct uids until we hit $list_size.
      $users = db_query_range('SELECT uid, name, access FROM {users} WHERE uid > 0 AND uid NOT IN (' . implode(',', array_keys($links)) . ') ORDER BY access DESC', 0, $list_size - $num_links);
      while (($user = db_fetch_object($users)) && count($links) < $list_size) {
        $links[$user->uid] = l($user->name, 'devel/switch/' . $user->name, array(
          'title' => t('Caution: this user will be unable to switch back.'),
        ), $dest);
      }
    }
  }
  return $links;
}