1 views_plugin_argument_validate_user.inc views_plugin_argument_validate_user::validate_argument($argument)

Overrides views_plugin_argument_validate::validate_argument

File

core/modules/user/views/views_plugin_argument_validate_user.inc, line 69
Definition of views_plugin_argument_validate_user.

Class

views_plugin_argument_validate_user
Validate whether an argument is a valid user.

Code

function validate_argument($argument) {
  $type = $this->options['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
        // clone to ensure that we don't actually mess with the
        // real global $user object.
        $account = clone $GLOBALS['user'];
      }
      $where = 'uid = :argument';
    }
  }
  else {
    if ($type == 'name' || $type == 'either') {
      $name = !empty($GLOBALS['user']->name) ? $GLOBALS['user']->name : config_get('system.core', 'anonymous');
      if ($argument == $name) {
        $account = clone $GLOBALS['user'];
      }
      $where = "name = :argument";
    }
  }

  // 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_query($query, array(':argument' => $argument))->fetchObject();
  }
  if (empty($account)) {
    // User not found.
    return FALSE;
  }

  // See if we're filtering users based on roles.
  if (!empty($this->options['restrict_roles']) && !empty($this->options['roles'])) {
    $roles = $this->options['roles'];
    $account->roles = array();
    $account->roles[] = $account->uid ? BACKDROP_AUTHENTICATED_ROLE : BACKDROP_ANONYMOUS_ROLE;
    $result = db_query('SELECT role FROM {users_roles} WHERE uid = :uid', array(':uid' => $account->uid));
    foreach ($result as $role) {
      $account->roles[] = $role->role;
    }
    if (!(bool) array_intersect($account->roles, $roles)) {
      return FALSE;
    }
  }

  $this->argument->argument = $account->uid;
  $this->argument->validated_title = check_plain(user_format_name($account));
  return TRUE;
}