flag_bookmark_plugin_validate_user.inc 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * @file
  4. * Contains the Flag Bookmark view argument validator.
  5. */
  6. /**
  7. * Validates whether an argument is a valid UID.
  8. *
  9. * @ingroup views
  10. */
  11. class flag_bookmark_plugin_validate_user extends views_plugin_argument_validate_user {
  12. /**
  13. * Define the options for the plugin, including the default permission.
  14. * @return multitype:string
  15. */
  16. function option_definition() {
  17. // Initialize the base class.
  18. $options = parent::option_definition();
  19. // Set the default permission.
  20. $options['bypass_perm'] = array('default' => 'administer users');
  21. return $options;
  22. }
  23. /**
  24. * Returns a option form for the plugin.
  25. */
  26. function options_form(&$form, &$form_state) {
  27. // Get the options form from the base class.
  28. parent::options_form($form, $form_state);
  29. $perms = array();
  30. $module_info = system_get_info('module');
  31. $perms[] = t(' - None - ');
  32. // Produce an array of permissions keyed by module name.
  33. foreach (module_implements('permission') as $module) {
  34. $permissions = module_invoke($module, 'permission');
  35. foreach ($permissions as $name => $perm) {
  36. $perms[$module_info[$module]['name']][$name] = strip_tags($perm['title']);
  37. }
  38. }
  39. asort($perms);
  40. // Create the form field for the validator. Returned by reference.
  41. $form['bypass_perm'] = array(
  42. '#type' => 'select',
  43. '#options' => $perms,
  44. '#title' => t('Override permission'),
  45. '#default_value' => $this->options['bypass_perm'],
  46. '#description' => t('Users with this permission bypass the argument check and are granted access.'),
  47. );
  48. }
  49. /**
  50. * Validates the argument to be a proper UID.
  51. * @param mixed $argument
  52. * @return boolean
  53. */
  54. function validate_argument($argument) {
  55. // The parent class takes care of all its options, returning TRUE if the
  56. // argument value validates to a user account, and an account that has the
  57. // required role.
  58. $argument_validates = parent::validate_argument($argument);
  59. // If the parent didn't validate the argument, then we certainly can't
  60. // either.
  61. if ($argument_validates == FALSE) {
  62. return $argument_validates;
  63. }
  64. // If the current user has the bypass permission, then we're done: return
  65. // the validation status we got from the parent.
  66. if (!empty($this->options['bypass_perm']) && user_access($this->options['bypass_perm'])) {
  67. return $argument_validates;
  68. }
  69. // Otherwise, perform our additional check to enforce that the argument
  70. // user ID is the current user.
  71. // The parent method has stored the uid from the argument.
  72. return ($this->argument->argument == $GLOBALS['user']->uid);
  73. }
  74. }