- <?php
- * @file
- * Definition of 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.
- */
- 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',
- '#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.'),
- '#states' => array(
- 'visible' => array(
- ':input[name="options[validate][options][user][restrict_roles]"]' => array('checked' => TRUE),
- ),
- ),
- );
- }
-
- function options_submit(&$form, &$form_state, &$options = array()) {
-
- $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'];
-
-
- if (is_numeric($argument) && $argument == (int)$argument) {
- if ($type == 'uid' || $type == 'either') {
- if ($argument == $GLOBALS['user']->uid) {
-
-
-
-
- $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 (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)) {
-
- return FALSE;
- }
-
-
- 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;
- }
-
- function process_summary_arguments(&$args) {
-
-
- $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;
- }
- }
- }
- }