You are here

function views_plugin_argument_validate_user::validate_argument in Views (for Drupal 7) 6.2

Same name and namespace in other branches
  1. 6.3 modules/user/views_plugin_argument_validate_user.inc \views_plugin_argument_validate_user::validate_argument()
  2. 7.3 modules/user/views_plugin_argument_validate_user.inc \views_plugin_argument_validate_user::validate_argument()

Overrides views_plugin_argument_validate::validate_argument

File

modules/user/views_plugin_argument_validate_user.inc, line 59

Class

views_plugin_argument_validate_user
Validate whether an argument is a valid user.

Code

function validate_argument($argument) {
  $type = $this->argument->options['validate_user_argument_type'];

  // is_numeric() can return false positives, so we ensure it's an integer.
  // However, is_integer() will always fail, since $argument is a string.
  if (is_numeric($argument) && $argument == (int) $argument) {
    if ($type == 'uid' || $type == 'either') {
      if ($argument == $GLOBALS['user']->uid) {

        // If you assign an object to a variable in PHP, the variable
        // automatically acts as a reference, not a copy, so we use
        // drupal_clone() to ensure that we don't actually mess with the
        // real global $user object.
        $account = drupal_clone($GLOBALS['user']);
      }
      $where = 'uid = %d';
    }
  }
  else {
    if ($type == 'name' || $type == 'either') {
      if ($argument == $GLOBALS['user']->name) {
        $account = drupal_clone($GLOBALS['user']);
      }
      $where = "name = '%s'";
    }
  }

  // If we don't have a WHERE clause, the argument is invalid.
  if (empty($where)) {
    return FALSE;
  }
  if (!isset($account)) {
    $query = "SELECT uid, name FROM {users} WHERE {$where}";
    $account = db_fetch_object(db_query($query, $argument));
  }
  if (empty($account)) {

    // User not found.
    return FALSE;
  }

  // See if we're filtering users based on roles.
  if (!empty($this->argument->options['validate_user_restrict_roles']) && !empty($this->argument->options['validate_user_roles'])) {
    $roles = $this->argument->options['validate_user_roles'];
    $account->roles = array();
    $account->roles[] = $account->uid ? DRUPAL_AUTHENTICATED_RID : DRUPAL_ANONYMOUS_RID;
    $result = db_query('SELECT rid FROM {users_roles} WHERE uid = %d', $account->uid);
    while ($role = db_fetch_object($result)) {
      $account->roles[] = $role->rid;
    }
    if (!(bool) array_intersect($account->roles, $roles)) {
      return FALSE;
    }
  }
  $this->argument->argument = $account->uid;
  $this->argument->validated_title = check_plain($account->name);
  return TRUE;
}