views_plugin_argument_validate_user.inc

  1. 3.x modules/user/views_plugin_argument_validate_user.inc
  2. 2.x modules/user/views_plugin_argument_validate_user.inc

Definition of views_plugin_argument_validate_user.

File

modules/user/views_plugin_argument_validate_user.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_plugin_argument_validate_user.
  5. */
  6. /**
  7. * Validate whether an argument is a valid user.
  8. *
  9. * This supports either numeric arguments (UID) or strings (username) and
  10. * converts either one into the user's UID. This validator also sets the
  11. * argument's title to the username.
  12. */
  13. class views_plugin_argument_validate_user extends views_plugin_argument_validate {
  14. function option_definition() {
  15. $options = parent::option_definition();
  16. $options['type'] = array('default' => 'uid');
  17. $options['restrict_roles'] = array('default' => FALSE, 'bool' => TRUE);
  18. $options['roles'] = array('default' => array());
  19. return $options;
  20. }
  21. function options_form(&$form, &$form_state) {
  22. $form['type'] = array(
  23. '#type' => 'radios',
  24. '#title' => t('Type of user filter value to allow'),
  25. '#options' => array(
  26. 'uid' => t('Only allow numeric UIDs'),
  27. 'name' => t('Only allow string usernames'),
  28. 'either' => t('Allow both numeric UIDs and string usernames'),
  29. ),
  30. '#default_value' => $this->options['type'],
  31. );
  32. $form['restrict_roles'] = array(
  33. '#type' => 'checkbox',
  34. '#title' => t('Restrict user based on role'),
  35. '#default_value' => $this->options['restrict_roles'],
  36. );
  37. $form['roles'] = array(
  38. '#type' => 'checkboxes',
  39. '#prefix' => '<div id="edit-options-validate-options-user-roles-wrapper">',
  40. '#suffix' => '</div>',
  41. '#title' => t('Restrict to the selected roles'),
  42. '#options' => array_map('check_plain', user_roles(TRUE)),
  43. '#default_value' => $this->options['roles'],
  44. '#description' => t('If no roles are selected, users from any role will be allowed.'),
  45. '#dependency' => array(
  46. 'edit-options-validate-options-user-restrict-roles' => array(1),
  47. ),
  48. );
  49. }
  50. function options_submit(&$form, &$form_state, &$options = array()) {
  51. // filter trash out of the options so we don't store giant unnecessary arrays
  52. $options['roles'] = array_filter($options['roles']);
  53. }
  54. function convert_options(&$options) {
  55. if (!isset($options['type']) && isset($this->argument->options['validate_user_argument_type'])) {
  56. $options['type'] = $this->argument->options['validate_user_argument_type'];
  57. $options['restrict_roles'] = $this->argument->options['validate_user_restrict_roles'];
  58. $options['roles'] = $this->argument->options['validate_user_roles'];
  59. }
  60. }
  61. function validate_argument($argument) {
  62. $type = $this->options['type'];
  63. // is_numeric() can return false positives, so we ensure it's an integer.
  64. // However, is_integer() will always fail, since $argument is a string.
  65. if (is_numeric($argument) && $argument == (int)$argument) {
  66. if ($type == 'uid' || $type == 'either') {
  67. if ($argument == $GLOBALS['user']->uid) {
  68. // If you assign an object to a variable in PHP, the variable
  69. // automatically acts as a reference, not a copy, so we use
  70. // clone to ensure that we don't actually mess with the
  71. // real global $user object.
  72. $account = clone $GLOBALS['user'];
  73. }
  74. $where = 'uid = :argument';
  75. }
  76. }
  77. else {
  78. if ($type == 'name' || $type == 'either') {
  79. $name = !empty($GLOBALS['user']->name) ? $GLOBALS['user']->name : variable_get('anonymous', t('Anonymous'));
  80. if ($argument == $name) {
  81. $account = clone $GLOBALS['user'];
  82. }
  83. $where = "name = :argument";
  84. }
  85. }
  86. // If we don't have a WHERE clause, the argument is invalid.
  87. if (empty($where)) {
  88. return FALSE;
  89. }
  90. if (!isset($account)) {
  91. $query = "SELECT uid, name FROM {users} WHERE $where";
  92. $account = db_query($query, array(':argument' => $argument))->fetchObject();
  93. }
  94. if (empty($account)) {
  95. // User not found.
  96. return FALSE;
  97. }
  98. // See if we're filtering users based on roles.
  99. if (!empty($this->options['restrict_roles']) && !empty($this->options['roles'])) {
  100. $roles = $this->options['roles'];
  101. $account->roles = array();
  102. $account->roles[] = $account->uid ? DRUPAL_AUTHENTICATED_RID : DRUPAL_ANONYMOUS_RID;
  103. $result = db_query('SELECT rid FROM {users_roles} WHERE uid = :uid', array(':uid' => $account->uid));
  104. foreach ($result as $role) {
  105. $account->roles[] = $role->rid;
  106. }
  107. if (!(bool) array_intersect($account->roles, $roles)) {
  108. return FALSE;
  109. }
  110. }
  111. $this->argument->argument = $account->uid;
  112. $this->argument->validated_title = check_plain(format_username($account));
  113. return TRUE;
  114. }
  115. function process_summary_arguments(&$args) {
  116. // If the validation says the input is an username, we should reverse the
  117. // argument so it works for example for generation summary urls.
  118. $uids_arg_keys = array_flip($args);
  119. if ($this->options['type'] == 'name') {
  120. $users = user_load_multiple($args);
  121. foreach ($users as $uid => $account) {
  122. $args[$uids_arg_keys[$uid]] = $account->name;
  123. }
  124. }
  125. }
  126. }