You are here

class views_plugin_argument_validate_user 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
  2. 7.3 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'
user_views_plugins in modules/user.views.inc
Implementation of hook_views_plugins

File

modules/user/views_plugin_argument_validate_user.inc, line 10

View source
class views_plugin_argument_validate_user extends views_plugin_argument_validate {
  function validate_form(&$form, &$form_state) {

    // We are unable to rely on options having already been set, so let's make
    // sure defaults are here:
    if (!isset($this->argument->options['validate_user_argument_type'])) {
      $this->argument->options['validate_user_argument_type'] = 'uid';
      $this->argument->options['validate_user_roles'] = array();
    }
    $form['validate_user_argument_type'] = array(
      '#type' => 'radios',
      '#title' => t('Type of user argument 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->argument->options['validate_user_argument_type'],
      '#process' => array(
        'expand_radios',
        'views_process_dependency',
      ),
      '#dependency' => array(
        'edit-options-validate-type' => array(
          $this->id,
        ),
      ),
      '#prefix' => '<div id="edit-options-validate-user-argument-type-wrapper">',
      '#suffix' => '</div>',
    );
    $form['validate_user_restrict_roles'] = array(
      '#type' => 'checkbox',
      '#title' => t('Restrict user based on role'),
      '#default_value' => !empty($this->argument->options['validate_user_restrict_roles']),
      '#process' => array(
        'views_process_dependency',
      ),
      '#dependency' => array(
        'edit-options-validate-type' => array(
          $this->id,
        ),
      ),
    );
    $form['validate_user_roles'] = array(
      '#type' => 'checkboxes',
      '#prefix' => '<div id="edit-options-validate-user-roles-wrapper">',
      '#suffix' => '</div>',
      '#title' => t('Restrict to the selected roles'),
      '#options' => user_roles(TRUE),
      '#default_value' => $this->argument->options['validate_user_roles'],
      '#description' => t('If no roles are selected, users from any role will be allowed.'),
      '#process' => array(
        'expand_checkboxes',
        'views_process_dependency',
      ),
      '#dependency' => array(
        'edit-options-validate-type' => array(
          $this->id,
        ),
        'edit-options-validate-user-restrict-roles' => array(
          1,
        ),
      ),
      '#dependency_count' => 2,
    );
  }
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
views_object::$options property Except for displays, options for the object will be held here. 1
views_object::construct function Views handlers use a special construct function so that we can more easily construct them with variable arguments. 5
views_object::destroy function 2
views_object::options function Set default options on this object. Called by the constructor in a complex chain to deal with backward compatibility. 1
views_object::option_definition function Information about options for all kinds of purposes will be held here. 9
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::_set_option_defaults function 1
views_plugin::additional_theme_functions function Provide a list of additional theme functions for the theme information page
views_plugin::options_form function Provide a form to edit options for this plugin. 5
views_plugin::options_submit function Handle any special handling on the validate form. 3
views_plugin::options_validate function Validate the options form. 3
views_plugin::query function Add anything to the query that we might need to. 3
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. 2
views_plugin_argument_validate::$option_name property 3
views_plugin_argument_validate::access function Determine if the administrator has the privileges to use this plugin 2
views_plugin_argument_validate::argument_form function
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::get_argument function Return the validate argument.
views_plugin_argument_validate::init function Initialize this plugin with the view and the argument it is linked to. Overrides views_plugin::init
views_plugin_argument_validate_user::validate_argument function Overrides views_plugin_argument_validate::validate_argument
views_plugin_argument_validate_user::validate_form function Overrides views_plugin_argument_validate::validate_form