~spreadubuntu/spreadubuntu/devel-drupal6

« back to all changes in this revision

Viewing changes to sites/all/modules/views/modules/user/views_plugin_argument_validate_user.inc

  • Committer: ruben
  • Date: 2009-06-08 09:38:49 UTC
  • Revision ID: ruben@captive-20090608093849-s1qtsyctv2vwp1x1
SpreadUbuntu moving to Drupal6. Based on ubuntu-drupal theme and adding our modules

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
// $Id: views_plugin_argument_validate_user.inc,v 1.2 2009/02/17 23:32:33 merlinofchaos Exp $
 
3
 
 
4
/**
 
5
 * Validate whether an argument is a valid user.
 
6
 *
 
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.
 
10
 */
 
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();
 
18
    }
 
19
 
 
20
    $form['validate_user_argument_type'] = array(
 
21
      '#type' => 'radios',
 
22
      '#title' => t('Type of user argument to allow'),
 
23
      '#options' => array(
 
24
        'uid' => t('Only allow numeric UIDs'),
 
25
        'name' => t('Only allow string usernames'),
 
26
        'either' => t('Allow both numeric UIDs and string usernames'),
 
27
      ),
 
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>',
 
33
    );
 
34
 
 
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)),
 
41
    );
 
42
 
 
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),
 
55
      ),
 
56
      '#dependency_count' => 2,
 
57
    );
 
58
  }
 
59
 
 
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') {
 
66
        $where = 'uid = %d';
 
67
      }
 
68
    }
 
69
    else {
 
70
      if ($type == 'name' || $type == 'either') {
 
71
        $where = "name = '%s'";
 
72
      }
 
73
    }
 
74
 
 
75
    // If we don't have a WHERE clause, the argument is invalid.
 
76
    if (empty($where)) {
 
77
      return FALSE;
 
78
    }
 
79
 
 
80
    $query = "SELECT uid, name FROM {users} WHERE $where";
 
81
    $account = db_fetch_object(db_query($query, $argument));
 
82
    if (empty($account)) {
 
83
      // User not found.
 
84
      return FALSE;
 
85
    }
 
86
 
 
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;
 
95
      }
 
96
      if (!(bool)array_intersect($account->roles, $roles)) {
 
97
        return FALSE;
 
98
      }
 
99
    }
 
100
 
 
101
    $this->argument->argument = $account->uid;
 
102
    $this->argument->validated_title = check_plain($account->name);
 
103
    return TRUE;
 
104
  }
 
105
}
 
106