views_plugin_access_role.inc

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

File

plugins/views_plugin_access_role.inc
View source
  1. <?php
  2. /**
  3. * Access plugin that provides role-based access control.
  4. */
  5. class views_plugin_access_role extends views_plugin_access {
  6. function access($account) {
  7. return views_check_roles(array_filter($this->options['role']), $account);
  8. }
  9. function get_access_callback() {
  10. return array('views_check_roles', array(array_filter($this->options['role'])));
  11. }
  12. function summary_title() {
  13. $count = count($this->options['role']);
  14. if ($count < 1) {
  15. return t('No role(s) selected');
  16. }
  17. else if ($count > 1) {
  18. return t('Multiple roles');
  19. }
  20. else {
  21. $rids = views_ui_get_roles();
  22. $rid = reset($this->options['role']);
  23. return $rids[$rid];
  24. }
  25. }
  26. function option_defaults(&$options) {
  27. $options['role'] = array();
  28. }
  29. function options_form(&$form, &$form_state) {
  30. $form['role'] = array(
  31. '#type' => 'checkboxes',
  32. '#title' => t('Role'),
  33. '#default_value' => $this->options['role'],
  34. '#options' => views_ui_get_roles(),
  35. '#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.'),
  36. );
  37. }
  38. function options_validate(&$form, &$form_state) {
  39. if (!array_filter($form_state['values']['access_options']['role'])) {
  40. form_error($form['role'], t('You must select at least one role if type is "by role"'));
  41. }
  42. }
  43. function options_submit(&$form, &$form_state) {
  44. // I hate checkboxes.
  45. $form_state['values']['access_options']['role'] = array_filter($form_state['values']['access_options']['role']);
  46. }
  47. }