class views_plugin_argument_validate_user

  1. 3.x modules/user/views_plugin_argument_validate_user.inc views_plugin_argument_validate_user
  2. 2.x modules/user/views_plugin_argument_validate_user.inc views_plugin_argument_validate_user

Validate whether an argument is a valid user.

This supports either numeric arguments (UID) or strings (username) and converts either one into the user's UID. This validator also sets the argument's title to the username.

Hierarchy

Expanded class hierarchy of views_plugin_argument_validate_user

1 string reference to 'views_plugin_argument_validate_user'

File

modules/user/views_plugin_argument_validate_user.inc, line 15
Definition of views_plugin_argument_validate_user.

View source
class views_plugin_argument_validate_user extends views_plugin_argument_validate {
  function option_definition() {
    $options = parent::option_definition();
    $options['type'] = array('default' => 'uid');
    $options['restrict_roles'] = array('default' => FALSE, 'bool' => TRUE);
    $options['roles'] = array('default' => array());

    return $options;
  }

  function options_form(&$form, &$form_state) {
    $form['type'] = array(
      '#type' => 'radios',
      '#title' => t('Type of user filter value to allow'),
      '#options' => array(
        'uid' => t('Only allow numeric UIDs'),
        'name' => t('Only allow string usernames'),
        'either' => t('Allow both numeric UIDs and string usernames'),
      ),
      '#default_value' => $this->options['type'],
    );

    $form['restrict_roles'] = array(
      '#type' => 'checkbox',
      '#title' => t('Restrict user based on role'),
      '#default_value' => $this->options['restrict_roles'],
    );

    $form['roles'] = array(
      '#type' => 'checkboxes',
      '#prefix' => '<div id="edit-options-validate-options-user-roles-wrapper">',
      '#suffix' => '</div>',
      '#title' => t('Restrict to the selected roles'),
      '#options' => array_map('check_plain', user_roles(TRUE)),
      '#default_value' => $this->options['roles'],
      '#description' => t('If no roles are selected, users from any role will be allowed.'),
      '#dependency' => array(
        'edit-options-validate-options-user-restrict-roles' => array(1),
      ),
    );
  }

  function options_submit(&$form, &$form_state, &$options = array()) {
    // filter trash out of the options so we don't store giant unnecessary arrays
    $options['roles'] = array_filter($options['roles']);
  }

  function convert_options(&$options) {
    if (!isset($options['type']) && isset($this->argument->options['validate_user_argument_type'])) {
      $options['type'] = $this->argument->options['validate_user_argument_type'];
      $options['restrict_roles'] = $this->argument->options['validate_user_restrict_roles'];
      $options['roles'] = $this->argument->options['validate_user_roles'];
    }
  }

  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 : variable_get('anonymous', t('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 ? DRUPAL_AUTHENTICATED_RID : DRUPAL_ANONYMOUS_RID;
      $result = db_query('SELECT rid FROM {users_roles} WHERE uid = :uid', array(':uid' => $account->uid));
      foreach ($result as $role) {
        $account->roles[] = $role->rid;
      }
      if (!(bool) array_intersect($account->roles, $roles)) {
        return FALSE;
      }
    }

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

  function process_summary_arguments(&$args) {
    // If the validation says the input is an username, we should reverse the
    // argument so it works for example for generation summary urls.
    $uids_arg_keys = array_flip($args);
    if ($this->options['type'] == 'name') {
      $users = user_load_multiple($args);
      foreach ($users as $uid => $account) {
        $args[$uids_arg_keys[$uid]] = $account->name;
      }
    }
  }
}

Members

Contains filters are case sensitive
Namesort descending Modifiers Type Description
views_object::construct function Views handlers use a special construct function so that we can more easily construct them with variable arguments.
views_object::destroy function
views_object::export_option function
views_object::export_options function
views_object::export_option_always function Always exports the option, regardless of the default value.
views_object::options Deprecated function Set default options on this object. Called by the constructor in a complex chain to deal with backward compatibility.
views_object::set_default_options function Set default options. For backward compatibility, it sends the options array; this is a feature that will likely disappear at some point.
views_object::set_definition function Let the handler know what its full definition is.
views_object::unpack_options function Unpack options over our existing defaults, drilling down into arrays so that defaults don't get totally blown away.
views_object::unpack_translatable function Unpack a single option definition.
views_object::unpack_translatables function Unpacks each handler to store translatable texts.
views_object::_set_option_defaults function
views_plugin::additional_theme_functions function Provide a list of additional theme functions for the theme information page
views_plugin::plugin_title function Return the human readable name of the display.
views_plugin::query function Add anything to the query that we might need to.
views_plugin::summary_title function Returns the summary of the settings in the display.
views_plugin::theme_functions function Provide a full list of possible theme templates used by this style.
views_plugin::validate function Validate that the plugin is correct and can be saved.
views_plugin_argument_validate::access function Determine if the administrator has the privileges to use this plugin
views_plugin_argument_validate::check_access function If we don't have access to the form but are showing it anyway, ensure that the form is safe and cannot be changed from user input.
views_plugin_argument_validate::init function Initialize this plugin with the view and the argument it is linked to.
views_plugin_argument_validate::options_validate function Provide the default form form for validating options Overrides views_plugin::options_validate
views_plugin_argument_validate_user::convert_options function Convert options from the older style. Overrides views_plugin_argument_validate::convert_options
views_plugin_argument_validate_user::options_form function Provide the default form for setting options. Overrides views_plugin_argument_validate::options_form
views_plugin_argument_validate_user::options_submit function Provide the default form form for submitting options Overrides views_plugin_argument_validate::options_submit
views_plugin_argument_validate_user::option_definition function Retrieve the options when this is a new access control plugin Overrides views_plugin_argument_validate::option_definition
views_plugin_argument_validate_user::process_summary_arguments function Process the summary arguments for displaying. Overrides views_plugin_argument_validate::process_summary_arguments
views_plugin_argument_validate_user::validate_argument function Overrides views_plugin_argument_validate::validate_argument