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

File

modules/user/views_plugin_argument_validate_user.inc
View source
  1. <?php
  2. /**
  3. * Validate whether an argument is a valid user.
  4. *
  5. * This supports either numeric arguments (UID) or strings (username) and
  6. * converts either one into the user's UID. This validator also sets the
  7. * argument's title to the username.
  8. */
  9. class views_plugin_argument_validate_user extends views_plugin_argument_validate {
  10. function validate_form(&$form, &$form_state) {
  11. // We are unable to rely on options having already been set, so let's make
  12. // sure defaults are here:
  13. if (!isset($this->argument->options['validate_user_argument_type'])) {
  14. $this->argument->options['validate_user_argument_type'] = 'uid';
  15. $this->argument->options['validate_user_roles'] = array();
  16. }
  17. $form['validate_user_argument_type'] = array(
  18. '#type' => 'radios',
  19. '#title' => t('Type of user argument to allow'),
  20. '#options' => array(
  21. 'uid' => t('Only allow numeric UIDs'),
  22. 'name' => t('Only allow string usernames'),
  23. 'either' => t('Allow both numeric UIDs and string usernames'),
  24. ),
  25. '#default_value' => $this->argument->options['validate_user_argument_type'],
  26. '#process' => array('expand_radios', 'views_process_dependency'),
  27. '#dependency' => array('edit-options-validate-type' => array($this->id)),
  28. '#prefix' => '<div id="edit-options-validate-user-argument-type-wrapper">',
  29. '#suffix' => '</div>',
  30. );
  31. $form['validate_user_restrict_roles'] = array(
  32. '#type' => 'checkbox',
  33. '#title' => t('Restrict user based on role'),
  34. '#default_value' => !empty($this->argument->options['validate_user_restrict_roles']),
  35. '#process' => array('views_process_dependency'),
  36. '#dependency' => array('edit-options-validate-type' => array($this->id)),
  37. );
  38. $form['validate_user_roles'] = array(
  39. '#type' => 'checkboxes',
  40. '#prefix' => '<div id="edit-options-validate-user-roles-wrapper">',
  41. '#suffix' => '</div>',
  42. '#title' => t('Restrict to the selected roles'),
  43. '#options' => user_roles(TRUE),
  44. '#default_value' => $this->argument->options['validate_user_roles'],
  45. '#description' => t('If no roles are selected, users from any role will be allowed.'),
  46. '#process' => array('expand_checkboxes', 'views_process_dependency'),
  47. '#dependency' => array(
  48. 'edit-options-validate-type' => array($this->id),
  49. 'edit-options-validate-user-restrict-roles' => array(1),
  50. ),
  51. '#dependency_count' => 2,
  52. );
  53. }
  54. function validate_argument($argument) {
  55. $type = $this->argument->options['validate_user_argument_type'];
  56. // is_numeric() can return false positives, so we ensure it's an integer.
  57. // However, is_integer() will always fail, since $argument is a string.
  58. if (is_numeric($argument) && $argument == (int)$argument) {
  59. if ($type == 'uid' || $type == 'either') {
  60. if ($argument == $GLOBALS['user']->uid) {
  61. // If you assign an object to a variable in PHP, the variable
  62. // automatically acts as a reference, not a copy, so we use
  63. // drupal_clone() to ensure that we don't actually mess with the
  64. // real global $user object.
  65. $account = drupal_clone($GLOBALS['user']);
  66. }
  67. $where = 'uid = %d';
  68. }
  69. }
  70. else {
  71. if ($type == 'name' || $type == 'either') {
  72. if ($argument == $GLOBALS['user']->name) {
  73. $account = drupal_clone($GLOBALS['user']);
  74. }
  75. $where = "name = '%s'";
  76. }
  77. }
  78. // If we don't have a WHERE clause, the argument is invalid.
  79. if (empty($where)) {
  80. return FALSE;
  81. }
  82. if (!isset($account)) {
  83. $query = "SELECT uid, name FROM {users} WHERE $where";
  84. $account = db_fetch_object(db_query($query, $argument));
  85. }
  86. if (empty($account)) {
  87. // User not found.
  88. return FALSE;
  89. }
  90. // See if we're filtering users based on roles.
  91. if (!empty($this->argument->options['validate_user_restrict_roles']) && !empty($this->argument->options['validate_user_roles'])) {
  92. $roles = $this->argument->options['validate_user_roles'];
  93. $account->roles = array();
  94. $account->roles[] = $account->uid ? DRUPAL_AUTHENTICATED_RID : DRUPAL_ANONYMOUS_RID;
  95. $result = db_query('SELECT rid FROM {users_roles} WHERE uid = %d', $account->uid);
  96. while ($role = db_fetch_object($result)) {
  97. $account->roles[] = $role->rid;
  98. }
  99. if (!(bool)array_intersect($account->roles, $roles)) {
  100. return FALSE;
  101. }
  102. }
  103. $this->argument->argument = $account->uid;
  104. $this->argument->validated_title = check_plain($account->name);
  105. return TRUE;
  106. }
  107. }