example_role.inc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * @file
  4. * Plugin to provide access control based upon role membership.
  5. * This is directly from the ctools module, but serves as a good
  6. * example of an access plugin.
  7. */
  8. /**
  9. * Plugins are described by creating a $plugin array which will be used
  10. * by the system that includes this file.
  11. */
  12. $plugin = array(
  13. 'title' => t("CTools example: role"),
  14. 'description' => t('Control access by role.'),
  15. 'callback' => 'ctools_plugin_example_example_role_ctools_access_check',
  16. 'default' => array('rids' => array()),
  17. 'settings form' => 'ctools_plugin_example_example_role_ctools_access_settings',
  18. 'summary' => 'ctools_plugin_example_example_role_ctools_access_summary',
  19. 'required context' => new ctools_context_required(t('User'), 'user'),
  20. );
  21. /**
  22. * Settings form for the 'by role' access plugin.
  23. */
  24. function ctools_plugin_example_example_role_ctools_access_settings(&$form, &$form_state, $conf) {
  25. $form['settings']['rids'] = array(
  26. '#type' => 'checkboxes',
  27. '#title' => t('Role'),
  28. '#default_value' => $conf['rids'],
  29. '#options' => ctools_get_roles(),
  30. '#description' => t('Only the checked roles will be granted access.'),
  31. );
  32. }
  33. /**
  34. * Compress the roles allowed to the minimum.
  35. */
  36. function ctools_plugin_example_example_role_ctools_access_settings_submit(&$form, &$form_state) {
  37. $form_state['values']['settings']['rids'] = array_keys(array_filter($form_state['values']['settings']['rids']));
  38. }
  39. /**
  40. * Check for access.
  41. */
  42. function ctools_plugin_example_example_role_ctools_access_check($conf, $context) {
  43. // As far as I know there should always be a context at this point, but this
  44. // is safe.
  45. if (empty($context) || empty($context->data) || !isset($context->data->roles)) {
  46. return FALSE;
  47. }
  48. $roles = array_keys($context->data->roles);
  49. $roles[] = $context->data->uid ? DRUPAL_AUTHENTICATED_RID : DRUPAL_ANONYMOUS_RID;
  50. return (bool) array_intersect($conf['rids'], $roles);
  51. }
  52. /**
  53. * Provide a summary description based upon the checked roles.
  54. */
  55. function ctools_plugin_example_example_role_ctools_access_summary($conf, $context) {
  56. if (!isset($conf['rids'])) {
  57. $conf['rids'] = array();
  58. }
  59. $roles = ctools_get_roles();
  60. $names = array();
  61. foreach (array_filter($conf['rids']) as $rid) {
  62. $names[] = check_plain($roles[$rid]);
  63. }
  64. if (empty($names)) {
  65. return t('@identifier can have any role', array('@identifier' => $context->identifier));
  66. }
  67. return format_plural(count($names), '@identifier must have role "@roles"', '@identifier can be one of "@roles"', array('@roles' => implode(', ', $names), '@identifier' => $context->identifier));
  68. }