ruleset.inc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * @file
  4. * Plugin to provide access control based on user rulesetission strings.
  5. */
  6. /**
  7. * Plugins are described by creating a $plugin array which will be used
  8. * by the system that includes this file.
  9. */
  10. $plugin = array(
  11. 'title' => '',
  12. 'description' => '',
  13. 'callback' => 'ctools_ruleset_ctools_access_check',
  14. 'settings form' => 'ctools_ruleset_ctools_access_settings',
  15. 'summary' => 'ctools_ruleset_ctools_access_summary',
  16. // This access plugin actually just contains child plugins that are
  17. // exportable, UI configured rulesets.
  18. 'get child' => 'ctools_ruleset_ctools_access_get_child',
  19. 'get children' => 'ctools_ruleset_ctools_access_get_children',
  20. );
  21. /**
  22. * Merge the main access plugin with a loaded ruleset to form a child plugin.
  23. */
  24. function ctools_ruleset_ctools_access_merge_plugin($plugin, $parent, $item) {
  25. $plugin['name'] = $parent . ':' . $item->name;
  26. $plugin['title'] = check_plain($item->admin_title);
  27. $plugin['description'] = check_plain($item->admin_description);
  28. // TODO: Generalize this in CTools.
  29. if (!empty($item->requiredcontexts)) {
  30. $plugin['required context'] = array();
  31. foreach ($item->requiredcontexts as $context) {
  32. $info = ctools_get_context($context['name']);
  33. // TODO: allow an optional setting.
  34. $plugin['required context'][] = new ctools_context_required($context['identifier'], $info['context name']);
  35. }
  36. }
  37. // Store the loaded ruleset in the plugin.
  38. $plugin['ruleset'] = $item;
  39. return $plugin;
  40. }
  41. /**
  42. * Get a single child access plugin.
  43. */
  44. function ctools_ruleset_ctools_access_get_child($plugin, $parent, $child) {
  45. ctools_include('export');
  46. $item = ctools_export_crud_load('ctools_access_ruleset', $child);
  47. if ($item) {
  48. return ctools_ruleset_ctools_access_merge_plugin($plugin, $parent, $item);
  49. }
  50. }
  51. /**
  52. * Get all child access plugins.
  53. */
  54. function ctools_ruleset_ctools_access_get_children($plugin, $parent) {
  55. $plugins = array();
  56. ctools_include('export');
  57. $items = ctools_export_crud_load_all('ctools_access_ruleset');
  58. foreach ($items as $name => $item) {
  59. $child = ctools_ruleset_ctools_access_merge_plugin($plugin, $parent, $item);
  60. $plugins[$child['name']] = $child;
  61. }
  62. return $plugins;
  63. }
  64. /**
  65. * Settings form for the 'by ruleset' access plugin.
  66. */
  67. function ctools_ruleset_ctools_access_settings(&$form, &$form_state, $conf) {
  68. if (!empty($form_state['plugin']['ruleset']->admin_description)) {
  69. $form['markup'] = array(
  70. '#markup' => '<div class="description">' . check_plain($form_state['plugin']['ruleset']->admin_description) . '</div>',
  71. );
  72. }
  73. return $form;
  74. }
  75. /**
  76. * Check for access.
  77. */
  78. function ctools_ruleset_ctools_access_check($conf, $context, $plugin) {
  79. // Load up any contexts we might be using.
  80. $contexts = ctools_context_match_required_contexts($plugin['ruleset']->requiredcontexts, $context);
  81. $contexts = ctools_context_load_contexts($plugin['ruleset'], FALSE, $contexts);
  82. return ctools_access($plugin['ruleset']->access, $contexts);
  83. }
  84. /**
  85. * Provide a summary description based upon the checked roles.
  86. */
  87. function ctools_ruleset_ctools_access_summary($conf, $context, $plugin) {
  88. if (!empty($plugin['ruleset']->admin_description)) {
  89. return check_plain($plugin['ruleset']->admin_description);
  90. }
  91. else {
  92. return check_plain($plugin['ruleset']->admin_title);
  93. }
  94. }