uc_order_plugin_argument_validate_user_perm.inc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * Validate whether an argument is the current user or has a permission.
  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 uc_order_plugin_argument_validate_user_perm extends views_plugin_argument_validate_user {
  10. function option_definition() {
  11. $options = parent::option_definition();
  12. $options['perm'] = array('default' => 'view all orders');
  13. return $options;
  14. }
  15. function options_form(&$form, &$form_state) {
  16. parent::options_form($form, $form_state);
  17. $form['restrict_roles']['#access'] = FALSE;
  18. $form['roles']['#access'] = FALSE;
  19. $perms = array();
  20. $module_info = system_get_info('module');
  21. // Get list of permissions
  22. foreach (module_implements('permission') as $module) {
  23. $permissions = module_invoke($module, 'permission');
  24. foreach ($permissions as $name => $perm) {
  25. $perms[$module_info[$module]['name']][$name] = strip_tags($perm['title']);
  26. }
  27. }
  28. asort($perms);
  29. $form['perm'] = array(
  30. '#type' => 'select',
  31. '#options' => $perms,
  32. '#title' => t('Permission'),
  33. '#default_value' => $this->options['perm'],
  34. '#description' => t('Users with the selected permission flag will be able to bypass validation.'),
  35. );
  36. }
  37. function validate_argument($argument) {
  38. $type = $this->options['type'];
  39. // is_numeric() can return false positives, so we ensure it's an integer.
  40. // However, is_integer() will always fail, since $argument is a string.
  41. if (is_numeric($argument) && $argument == (int) $argument) {
  42. if ($type == 'uid' || $type == 'either') {
  43. if ($argument == $GLOBALS['user']->uid) {
  44. // If you assign an object to a variable in PHP, the variable
  45. // automatically acts as a reference, not a copy, so we use
  46. // clone to ensure that we don't actually mess with the
  47. // real global $user object.
  48. $account = clone $GLOBALS['user'];
  49. }
  50. $where = 'uid = :argument';
  51. }
  52. }
  53. else {
  54. if ($type == 'name' || $type == 'either') {
  55. $name = !empty($GLOBALS['user']->name) ? $GLOBALS['user']->name : variable_get('anonymous', t('Anonymous'));
  56. if ($argument == $name) {
  57. $account = clone $GLOBALS['user'];
  58. }
  59. $where = "name = :argument";
  60. }
  61. }
  62. // If we don't have a WHERE clause, the argument is invalid.
  63. if (empty($where)) {
  64. return FALSE;
  65. }
  66. if (!isset($account)) {
  67. $query = "SELECT uid, name FROM {users} WHERE $where";
  68. $account = db_query($query, array(':argument' => $argument))->fetchObject();
  69. }
  70. if (empty($account)) {
  71. // User not found.
  72. return FALSE;
  73. }
  74. // If the current user is not the account specified by the argument
  75. // and doesn't have the correct permission, validation fails.
  76. if ($GLOBALS['user']->uid != $account->uid && !user_access($this->options['perm'])) {
  77. return FALSE;
  78. }
  79. $this->argument->argument = $account->uid;
  80. $this->argument->validated_title = isset($account->name) ? check_plain($account->name) : check_plain(variable_get('anonymous', t('Anonymous')));
  81. return TRUE;
  82. }
  83. }