summaryrefslogtreecommitdiff
path: root/sites/all/modules/ctools/ctools_access_ruleset/plugins/access/ruleset.inc
blob: f8abea6df74b42a97ff069748b446ab6e72d2b87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php

/**
 * @file
 * Plugin to provide access control based on user rulesetission strings.
 */

/**
 * Plugins are described by creating a $plugin array which will be used
 * by the system that includes this file.
 */
$plugin = array(
  'title' => '',
  'description' => '',
  'callback' => 'ctools_ruleset_ctools_access_check',
  'settings form' => 'ctools_ruleset_ctools_access_settings',
  'summary' => 'ctools_ruleset_ctools_access_summary',

  // This access plugin actually just contains child plugins that are
  // exportable, UI configured rulesets.
  'get child' => 'ctools_ruleset_ctools_access_get_child',
  'get children' => 'ctools_ruleset_ctools_access_get_children',
);

/**
 * Merge the main access plugin with a loaded ruleset to form a child plugin.
 */
function ctools_ruleset_ctools_access_merge_plugin($plugin, $parent, $item) {
  $plugin['name'] = $parent . ':' . $item->name;
  $plugin['title'] = check_plain($item->admin_title);
  $plugin['description'] = check_plain($item->admin_description);

  // TODO: Generalize this in CTools.
  if (!empty($item->requiredcontexts)) {
    $plugin['required context'] = array();
    foreach ($item->requiredcontexts as $context) {
      $info = ctools_get_context($context['name']);
      // TODO: allow an optional setting
      $plugin['required context'][] = new ctools_context_required($context['identifier'], $info['context name']);
    }
  }

  // Store the loaded ruleset in the plugin.
  $plugin['ruleset'] = $item;
  return $plugin;
}

/**
 * Get a single child access plugin.
 */
function ctools_ruleset_ctools_access_get_child($plugin, $parent, $child) {
  ctools_include('export');
  $item = ctools_export_crud_load('ctools_access_ruleset', $child);
  if ($item) {
    return ctools_ruleset_ctools_access_merge_plugin($plugin, $parent, $item);
  }
}

/**
 * Get all child access plugins.
 */
function ctools_ruleset_ctools_access_get_children($plugin, $parent) {
  $plugins = array();
  ctools_include('export');
  $items = ctools_export_crud_load_all('ctools_access_ruleset');
  foreach ($items as $name => $item) {
    $child = ctools_ruleset_ctools_access_merge_plugin($plugin, $parent, $item);
    $plugins[$child['name']] = $child;
  }

  return $plugins;
}

/**
 * Settings form for the 'by ruleset' access plugin
 */
function ctools_ruleset_ctools_access_settings(&$form, &$form_state, $conf) {
  if (!empty($form_state['plugin']['ruleset']->admin_description)) {
    $form['markup'] = array(
      '#markup' => '<div class="description">' . check_plain($form_state['plugin']['ruleset']->admin_description) . '</div>',
    );
  }

  return $form;
}

/**
 * Check for access.
 */
function ctools_ruleset_ctools_access_check($conf, $context, $plugin) {
  // Load up any contexts we might be using.
  $contexts = ctools_context_match_required_contexts($plugin['ruleset']->requiredcontexts, $context);
  $contexts = ctools_context_load_contexts($plugin['ruleset'], FALSE, $contexts);

  return ctools_access($plugin['ruleset']->access, $contexts);
}

/**
 * Provide a summary description based upon the checked roles.
 */
function ctools_ruleset_ctools_access_summary($conf, $context, $plugin) {
  if (!empty($plugin['ruleset']->admin_description)) {
    return check_plain($plugin['ruleset']->admin_description);
  }
  else {
    return check_plain($plugin['ruleset']->admin_title);
  }
}