views_plugin_access_role.inc

  1. 3.x plugins/views_plugin_access_role.inc
  2. 2.x plugins/views_plugin_access_role.inc

Definition of views_plugin_access_role.

File

plugins/views_plugin_access_role.inc
View source
  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. function access($account) {
  13. return views_check_roles(array_filter($this->options['role']), $account);
  14. }
  15. function get_access_callback() {
  16. return array('views_check_roles', array(array_filter($this->options['role'])));
  17. }
  18. function summary_title() {
  19. $count = count($this->options['role']);
  20. if ($count < 1) {
  21. return t('No role(s) selected');
  22. }
  23. elseif ($count > 1) {
  24. return t('Multiple roles');
  25. }
  26. else {
  27. $rids = views_ui_get_roles();
  28. $rid = reset($this->options['role']);
  29. return check_plain($rids[$rid]);
  30. }
  31. }
  32. function option_definition() {
  33. $options = parent::option_definition();
  34. $options['role'] = array('default' => array());
  35. return $options;
  36. }
  37. function options_form(&$form, &$form_state) {
  38. parent::options_form($form, $form_state);
  39. $form['role'] = array(
  40. '#type' => 'checkboxes',
  41. '#title' => t('Role'),
  42. '#default_value' => $this->options['role'],
  43. '#options' => array_map('check_plain', views_ui_get_roles()),
  44. '#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.'),
  45. );
  46. }
  47. function options_validate(&$form, &$form_state) {
  48. if (!array_filter($form_state['values']['access_options']['role'])) {
  49. form_error($form['role'], t('You must select at least one role if type is "by role"'));
  50. }
  51. }
  52. function options_submit(&$form, &$form_state) {
  53. // I hate checkboxes.
  54. $form_state['values']['access_options']['role'] = array_filter($form_state['values']['access_options']['role']);
  55. }
  56. }