devel_switch_user_list

Versions
mediamosa-21
devel_switch_user_list()

Code

sites/all/modules/devel/devel.module, line 612

<?php
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);
    }
    foreach ($users as $user) {
      $links[$user->uid] = array(
        'title' => theme('placeholder', $user->name),
        'href' => 'devel/switch/'. $user->name,
        'query' => $dest,
        'attributes' => array('title' => t('This user can switch back.')),
        'html' => 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);
      foreach ($users as $user) {
        if (count($links) >= $list_size) {
          break;
        }
        $links[$user->uid] = array(
          'title' => $user->name ? $user->name : 'anon',
          'href' => 'devel/switch/'. $user->name,
          'query' => $dest,
          'attributes' => array('title' => t('Caution: this user will be unable switch back.')),
        );
      }
    }
  }
  return $links;
}
?>