2
// $Id: views_plugin_argument_validate_user.inc,v 1.2 2009/02/17 23:32:33 merlinofchaos Exp $
5
* Validate whether an argument is a valid user.
7
* This supports either numeric arguments (UID) or strings (username) and
8
* converts either one into the user's UID. This validator also sets the
9
* argument's title to the username.
11
class views_plugin_argument_validate_user extends views_plugin_argument_validate {
12
function validate_form(&$form, &$form_state) {
13
// We are unable to rely on options having already been set, so let's make
14
// sure defaults are here:
15
if (!isset($this->argument->options['validate_user_argument_type'])) {
16
$this->argument->options['validate_user_argument_type'] = 'uid';
17
$this->argument->options['validate_user_roles'] = array();
20
$form['validate_user_argument_type'] = array(
22
'#title' => t('Type of user argument to allow'),
24
'uid' => t('Only allow numeric UIDs'),
25
'name' => t('Only allow string usernames'),
26
'either' => t('Allow both numeric UIDs and string usernames'),
28
'#default_value' => $this->argument->options['validate_user_argument_type'],
29
'#process' => array('expand_radios', 'views_process_dependency'),
30
'#dependency' => array('edit-options-validate-type' => array($this->id)),
31
'#prefix' => '<div id="edit-options-validate-user-argument-type-wrapper">',
32
'#suffix' => '</div>',
35
$form['validate_user_restrict_roles'] = array(
36
'#type' => 'checkbox',
37
'#title' => t('Restrict user based on role'),
38
'#default_value' => !empty($this->argument->options['validate_user_restrict_roles']),
39
'#process' => array('views_process_dependency'),
40
'#dependency' => array('edit-options-validate-type' => array($this->id)),
43
$form['validate_user_roles'] = array(
44
'#type' => 'checkboxes',
45
'#prefix' => '<div id="edit-options-validate-user-roles-wrapper">',
46
'#suffix' => '</div>',
47
'#title' => t('Restrict to the selected roles'),
48
'#options' => user_roles(TRUE),
49
'#default_value' => $this->argument->options['validate_user_roles'],
50
'#description' => t('If no roles are selected, users from any role will be allowed.'),
51
'#process' => array('expand_checkboxes', 'views_process_dependency'),
52
'#dependency' => array(
53
'edit-options-validate-type' => array($this->id),
54
'edit-options-validate-user-restrict-roles' => array(1),
56
'#dependency_count' => 2,
60
function validate_argument($argument) {
61
$type = $this->argument->options['validate_user_argument_type'];
62
// is_numeric() can return false positives, so we ensure it's an integer.
63
// However, is_integer() will always fail, since $argument is a string.
64
if (is_numeric($argument) && $argument == (int)$argument) {
65
if ($type == 'uid' || $type == 'either') {
70
if ($type == 'name' || $type == 'either') {
71
$where = "name = '%s'";
75
// If we don't have a WHERE clause, the argument is invalid.
80
$query = "SELECT uid, name FROM {users} WHERE $where";
81
$account = db_fetch_object(db_query($query, $argument));
82
if (empty($account)) {
87
// See if we're filtering users based on roles.
88
if (!empty($this->argument->options['validate_user_restrict_roles']) && !empty($this->argument->options['validate_user_roles'])) {
89
$roles = $this->argument->options['validate_user_roles'];
90
$acccont->roles = array();
91
$account->roles[] = $account->uid ? DRUPAL_AUTHENTICATED_RID : DRUPAL_ANONYMOUS_RID;
92
$result = db_query('SELECT rid FROM {users_roles} WHERE uid = %d', $account->uid);
93
while ($role = db_fetch_object($result)) {
94
$account->roles[] = $role->rid;
96
if (!(bool)array_intersect($account->roles, $roles)) {
101
$this->argument->argument = $account->uid;
102
$this->argument->validated_title = check_plain($account->name);