diff --git a/modules/user/views_plugin_argument_validate_user.inc b/modules/user/views_plugin_argument_validate_user.inc index b727094..c08213c 100644 --- a/modules/user/views_plugin_argument_validate_user.inc +++ b/modules/user/views_plugin_argument_validate_user.inc @@ -28,13 +28,14 @@ class views_plugin_argument_validate_user extends views_plugin_argument_validate '#title' => t('Type of user filter value to allow'), '#options' => array( 'uid' => t('Only allow numeric UIDs'), + 'uids' => t('Only allow numeric UIDs, separated by , or +'), 'name' => t('Only allow string usernames'), 'either' => t('Allow both numeric UIDs and string usernames'), ), '#default_value' => $this->options['type'], ); - - $form['restrict_roles'] = array( + + $form['restrict_roles'] = array( '#type' => 'checkbox', '#title' => t('Restrict user based on role'), '#default_value' => $this->options['restrict_roles'], @@ -68,7 +69,117 @@ class views_plugin_argument_validate_user extends views_plugin_argument_validate } function validate_argument($argument) { - $type = $this->options['type']; + $type = $this->options['type']; + + if($type != 'uids'){ + // 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; + } + //Multiple UIDs + if ($type == 'uids') { + $uids = new stdClass(); + $uids->value = array($argument); + $uids = views_break_phrase($argument, $uids); + if ($uids->value == array(-1)) { + return FALSE; + } + + $test = drupal_map_assoc($uids->value); + + $results = db_query("SELECT uid, name FROM {users} WHERE uid IN (:uids)", array(':uids' => $uids->value)); + + if (empty($results)) { + // Users not found. + return FALSE; + } + + foreach($results as $account){ + + // 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; + } + } + } + } + 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; + } + } + } + + function _validate_uid_username($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) { @@ -124,17 +235,5 @@ class views_plugin_argument_validate_user extends views_plugin_argument_validate $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; - } - } - } + } }