views_plugin_access_role.inc 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_plugin_access_role.
  5. */
  6. /**
  7. * Access plugin that provides role-based access control.
  8. *
  9. * @ingroup views_access_plugins
  10. */
  11. class views_plugin_access_role extends views_plugin_access {
  12. /**
  13. * {@inheritdoc}
  14. */
  15. public function access($account) {
  16. return views_check_roles(array_filter($this->options['role']), $account);
  17. }
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public function get_access_callback() {
  22. return array('views_check_roles', array(array_filter($this->options['role'])));
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function summary_title() {
  28. $count = count($this->options['role']);
  29. if ($count < 1) {
  30. return t('No role(s) selected');
  31. }
  32. elseif ($count > 1) {
  33. return t('Multiple roles');
  34. }
  35. else {
  36. $rids = views_ui_get_roles();
  37. $rid = reset($this->options['role']);
  38. return check_plain($rids[$rid]);
  39. }
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function option_definition() {
  45. $options = parent::option_definition();
  46. $options['role'] = array('default' => array());
  47. return $options;
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function options_form(&$form, &$form_state) {
  53. parent::options_form($form, $form_state);
  54. $form['role'] = array(
  55. '#type' => 'checkboxes',
  56. '#title' => t('Role'),
  57. '#default_value' => $this->options['role'],
  58. '#options' => array_map('check_plain', views_ui_get_roles()),
  59. '#description' => t('Only the checked roles will be able to access this display. Note that users with "access all views" can see any view, regardless of role.'),
  60. );
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function options_validate(&$form, &$form_state) {
  66. if (!array_filter($form_state['values']['access_options']['role'])) {
  67. form_error($form['role'], t('You must select at least one role if type is "by role"'));
  68. }
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function options_submit(&$form, &$form_state) {
  74. // I hate checkboxes.
  75. $form_state['values']['access_options']['role'] = array_filter($form_state['values']['access_options']['role']);
  76. }
  77. }