danielebarchiesi@0
|
1 <?php
|
danielebarchiesi@0
|
2
|
danielebarchiesi@0
|
3 /**
|
danielebarchiesi@0
|
4 * @file
|
danielebarchiesi@0
|
5 * Contains administrative screens for the access control plugins.
|
danielebarchiesi@0
|
6 *
|
danielebarchiesi@0
|
7 * Access control can be implemented by creating a list of 0 or more access
|
danielebarchiesi@0
|
8 * plugins, each with settings. This list can be ANDed together or ORed
|
danielebarchiesi@0
|
9 * together. When testing access, each plugin is tested until success
|
danielebarchiesi@0
|
10 * or failure can be determined. We use short circuiting techniques to
|
danielebarchiesi@0
|
11 * ensure we are as efficient as possible.
|
danielebarchiesi@0
|
12 *
|
danielebarchiesi@0
|
13 * Access plugins are part of the context system, and as such can require
|
danielebarchiesi@0
|
14 * contexts to work. That allows the use of access based upon visibility
|
danielebarchiesi@0
|
15 * of an object, or even more esoteric things such as node type, node language
|
danielebarchiesi@0
|
16 * etc. Since a lot of access depends on the logged in user, the logged in
|
danielebarchiesi@0
|
17 * user should always be provided as a context.
|
danielebarchiesi@0
|
18 *
|
danielebarchiesi@0
|
19 * In the UI, the user is presented with a table and a 'add access method' select.
|
danielebarchiesi@0
|
20 * When added, the user will be presented with the config wizard and, when
|
danielebarchiesi@0
|
21 * confirmed, table will be refreshed via AJAX to show the new access method.
|
danielebarchiesi@0
|
22 * Each item in the table will have controls to change the settings or remove
|
danielebarchiesi@0
|
23 * the item. Changing the settings will invoke the modal for update.
|
danielebarchiesi@0
|
24 *
|
danielebarchiesi@0
|
25 * Currently the modal is not degradable, but it could be with only a small
|
danielebarchiesi@0
|
26 * amount of work.
|
danielebarchiesi@0
|
27 *
|
danielebarchiesi@0
|
28 * A simple radio
|
danielebarchiesi@0
|
29 * control is used to let the user pick the and/or logic.
|
danielebarchiesi@0
|
30 *
|
danielebarchiesi@0
|
31 * Access control is stored in an array:
|
danielebarchiesi@0
|
32 * @code
|
danielebarchiesi@0
|
33 * array(
|
danielebarchiesi@0
|
34 * 'plugins' => array(
|
danielebarchiesi@0
|
35 * 0 => array(
|
danielebarchiesi@0
|
36 * 'name' => 'name of access plugin',
|
danielebarchiesi@0
|
37 * 'settings' => array(), // These will be set by the form
|
danielebarchiesi@0
|
38 * ),
|
danielebarchiesi@0
|
39 * // ... as many as needed
|
danielebarchiesi@0
|
40 * ),
|
danielebarchiesi@0
|
41 * 'logic' => 'AND', // or 'OR',
|
danielebarchiesi@0
|
42 * ),
|
danielebarchiesi@0
|
43 * @endcode
|
danielebarchiesi@0
|
44 *
|
danielebarchiesi@0
|
45 * To add this widget to your UI, you need to do a little bit of setup.
|
danielebarchiesi@0
|
46 *
|
danielebarchiesi@0
|
47 * The form will utilize two callbacks, one to get the cached version
|
danielebarchiesi@0
|
48 * of the access settings, and one to store the cached version of the
|
danielebarchiesi@0
|
49 * access settings. These will be used from AJAX forms, so they will
|
danielebarchiesi@0
|
50 * be completely out of the context of this page load and will not have
|
danielebarchiesi@0
|
51 * knowledge of anything sent to this form (the 'module' and 'argument'
|
danielebarchiesi@0
|
52 * will be preserved through the URL only).
|
danielebarchiesi@0
|
53 *
|
danielebarchiesi@0
|
54 * The 'module' is used to determine the location of the callback. It
|
danielebarchiesi@0
|
55 * does not strictly need to be a module, so that if your module defines
|
danielebarchiesi@0
|
56 * multiple systems that use this callback, it can use anything within the
|
danielebarchiesi@0
|
57 * module's namespace it likes.
|
danielebarchiesi@0
|
58 *
|
danielebarchiesi@0
|
59 * When retrieving the cache, the cache may not have already been set up;
|
danielebarchiesi@0
|
60 * In order to efficiently use cache space, we want to cache the stored
|
danielebarchiesi@0
|
61 * settings *only* when they have changed. Therefore, the get access cache
|
danielebarchiesi@0
|
62 * callback should first look for cache, and if it finds nothing, return
|
danielebarchiesi@0
|
63 * the original settings.
|
danielebarchiesi@0
|
64 *
|
danielebarchiesi@0
|
65 * The callbacks:
|
danielebarchiesi@0
|
66 * - $module . _ctools_access_get($argument) -- get the 'access' settings
|
danielebarchiesi@0
|
67 * from cache. Must return array($access, $contexts); This callback can
|
danielebarchiesi@0
|
68 * perform access checking to make sure this URL is not being gamed.
|
danielebarchiesi@0
|
69 * - $module . _ctools_access_set($argument, $access) -- set the 'access'
|
danielebarchiesi@0
|
70 * settings in cache.
|
danielebarchiesi@0
|
71 * - $module . _ctools_access_clear($argument) -- clear the cache.
|
danielebarchiesi@0
|
72 *
|
danielebarchiesi@0
|
73 * The ctools_object_cache is recommended for this purpose, but you can use
|
danielebarchiesi@0
|
74 * any caching mechanism you like. An example:
|
danielebarchiesi@0
|
75 *
|
danielebarchiesi@0
|
76 * @code{
|
danielebarchiesi@0
|
77 * ctools_include('object-cache');
|
danielebarchiesi@0
|
78 * ctools_object_cache_set("$module:argument", $access);
|
danielebarchiesi@0
|
79 * }
|
danielebarchiesi@0
|
80 *
|
danielebarchiesi@0
|
81 * To utilize this form:
|
danielebarchiesi@0
|
82 * @code
|
danielebarchiesi@0
|
83 * ctools_include('context-access-admin');
|
danielebarchiesi@0
|
84 * $form_state = array(
|
danielebarchiesi@0
|
85 * 'access' => $access,
|
danielebarchiesi@0
|
86 * 'module' => 'module name',
|
danielebarchiesi@0
|
87 * 'callback argument' => 'some string',
|
danielebarchiesi@0
|
88 * 'contexts' => $contexts, // an array of contexts. Optional if no contexts.
|
danielebarchiesi@0
|
89 * // 'logged-in-user' will be added if not present as the access system
|
danielebarchiesi@0
|
90 * // requires this context.
|
danielebarchiesi@0
|
91 * ),
|
danielebarchiesi@0
|
92 * $output = drupal_build_form('ctools_access_admin_form', $form_state);
|
danielebarchiesi@0
|
93 * if (!empty($form_state['executed'])) {
|
danielebarchiesi@0
|
94 * // save $form_state['access'] however you like.
|
danielebarchiesi@0
|
95 * }
|
danielebarchiesi@0
|
96 * @endcode
|
danielebarchiesi@0
|
97 *
|
danielebarchiesi@0
|
98 * Additionally, you may add 'no buttons' => TRUE if you wish to embed this
|
danielebarchiesi@0
|
99 * form into your own, and instead call
|
danielebarchiesi@0
|
100 *
|
danielebarchiesi@0
|
101 * @code{
|
danielebarchiesi@0
|
102 * $form = ctools_access_admin_form($form, $form_state);
|
danielebarchiesi@0
|
103 * }
|
danielebarchiesi@0
|
104 *
|
danielebarchiesi@0
|
105 * You'll be responsible for adding a submit button.
|
danielebarchiesi@0
|
106 *
|
danielebarchiesi@0
|
107 * You may use ctools_access($access, $contexts) which will return
|
danielebarchiesi@0
|
108 * TRUE if access is passed or FALSE if access is not passed.
|
danielebarchiesi@0
|
109 */
|
danielebarchiesi@0
|
110
|
danielebarchiesi@0
|
111 /**
|
danielebarchiesi@0
|
112 * Administrative form for access control.
|
danielebarchiesi@0
|
113 */
|
danielebarchiesi@0
|
114 function ctools_access_admin_form($form, &$form_state) {
|
danielebarchiesi@0
|
115 ctools_include('context');
|
danielebarchiesi@0
|
116 $argument = isset($form_state['callback argument']) ? $form_state['callback argument'] : '';
|
danielebarchiesi@0
|
117 $fragment = $form_state['module'];
|
danielebarchiesi@0
|
118 if ($argument) {
|
danielebarchiesi@0
|
119 $fragment .= '-' . $argument;
|
danielebarchiesi@0
|
120 }
|
danielebarchiesi@0
|
121
|
danielebarchiesi@0
|
122 $contexts = isset($form_state['contexts']) ? $form_state['contexts'] : array();
|
danielebarchiesi@0
|
123
|
danielebarchiesi@0
|
124 $form['access_table'] = array(
|
danielebarchiesi@0
|
125 '#markup' => ctools_access_admin_render_table($form_state['access'], $fragment, $contexts),
|
danielebarchiesi@0
|
126 );
|
danielebarchiesi@0
|
127
|
danielebarchiesi@0
|
128 $form['add-button'] = array(
|
danielebarchiesi@0
|
129 '#theme' => 'ctools_access_admin_add',
|
danielebarchiesi@0
|
130 );
|
danielebarchiesi@0
|
131 // This sets up the URL for the add access modal.
|
danielebarchiesi@0
|
132 $form['add-button']['add-url'] = array(
|
danielebarchiesi@0
|
133 '#attributes' => array('class' => array("ctools-access-add-url")),
|
danielebarchiesi@0
|
134 '#type' => 'hidden',
|
danielebarchiesi@0
|
135 '#value' => url("ctools/context/ajax/access/add/$fragment", array('absolute' => TRUE)),
|
danielebarchiesi@0
|
136 );
|
danielebarchiesi@0
|
137
|
danielebarchiesi@0
|
138 $plugins = ctools_get_relevant_access_plugins($contexts);
|
danielebarchiesi@0
|
139 $options = array();
|
danielebarchiesi@0
|
140 foreach ($plugins as $id => $plugin) {
|
danielebarchiesi@0
|
141 $options[$id] = $plugin['title'];
|
danielebarchiesi@0
|
142 }
|
danielebarchiesi@0
|
143
|
danielebarchiesi@0
|
144 asort($options);
|
danielebarchiesi@0
|
145
|
danielebarchiesi@0
|
146 $form['add-button']['type'] = array(
|
danielebarchiesi@0
|
147 // This ensures that the form item is added to the URL.
|
danielebarchiesi@0
|
148 '#attributes' => array('class' => array("ctools-access-add-url")),
|
danielebarchiesi@0
|
149 '#type' => 'select',
|
danielebarchiesi@0
|
150 '#options' => $options,
|
danielebarchiesi@0
|
151 '#required' => FALSE,
|
danielebarchiesi@0
|
152 );
|
danielebarchiesi@0
|
153
|
danielebarchiesi@0
|
154 $form['add-button']['add'] = array(
|
danielebarchiesi@0
|
155 '#type' => 'submit',
|
danielebarchiesi@0
|
156 '#attributes' => array('class' => array('ctools-use-modal')),
|
danielebarchiesi@0
|
157 '#id' => "ctools-access-add",
|
danielebarchiesi@0
|
158 '#value' => t('Add'),
|
danielebarchiesi@0
|
159 );
|
danielebarchiesi@0
|
160
|
danielebarchiesi@0
|
161 $form['logic'] = array(
|
danielebarchiesi@0
|
162 '#type' => 'radios',
|
danielebarchiesi@0
|
163 '#options' => array(
|
danielebarchiesi@0
|
164 'and' => t('All criteria must pass.'),
|
danielebarchiesi@0
|
165 'or' => t('Only one criteria must pass.'),
|
danielebarchiesi@0
|
166 ),
|
danielebarchiesi@0
|
167 '#default_value' => isset($form_state['access']['logic']) ? $form_state['access']['logic'] : 'and',
|
danielebarchiesi@0
|
168 );
|
danielebarchiesi@0
|
169
|
danielebarchiesi@0
|
170 if (empty($form_state['no buttons'])) {
|
danielebarchiesi@0
|
171 $form['buttons']['save'] = array(
|
danielebarchiesi@0
|
172 '#type' => 'submit',
|
danielebarchiesi@0
|
173 '#value' => t('Save'),
|
danielebarchiesi@0
|
174 '#submit' => array('ctools_access_admin_form_submit'),
|
danielebarchiesi@0
|
175 );
|
danielebarchiesi@0
|
176 }
|
danielebarchiesi@0
|
177
|
danielebarchiesi@0
|
178 return $form;
|
danielebarchiesi@0
|
179 }
|
danielebarchiesi@0
|
180
|
danielebarchiesi@0
|
181 /**
|
danielebarchiesi@0
|
182 * Render the table. This is used both to render it initially and to rerender
|
danielebarchiesi@0
|
183 * it upon ajax response.
|
danielebarchiesi@0
|
184 */
|
danielebarchiesi@0
|
185 function ctools_access_admin_render_table($access, $fragment, $contexts) {
|
danielebarchiesi@0
|
186 ctools_include('ajax');
|
danielebarchiesi@0
|
187 ctools_include('modal');
|
danielebarchiesi@0
|
188 $rows = array();
|
danielebarchiesi@0
|
189
|
danielebarchiesi@0
|
190 if (empty($access['plugins'])) {
|
danielebarchiesi@0
|
191 $access['plugins'] = array();
|
danielebarchiesi@0
|
192 }
|
danielebarchiesi@0
|
193
|
danielebarchiesi@0
|
194 foreach ($access['plugins'] as $id => $test) {
|
danielebarchiesi@0
|
195 $row = array();
|
danielebarchiesi@0
|
196 $plugin = ctools_get_access_plugin($test['name']);
|
danielebarchiesi@0
|
197 $title = isset($plugin['title']) ? $plugin['title'] : t('Broken/missing access plugin %plugin', array('%plugin' => $test['name']));
|
danielebarchiesi@0
|
198
|
danielebarchiesi@0
|
199 $row[] = array('data' => $title, 'class' => array('ctools-access-title'));
|
danielebarchiesi@0
|
200
|
danielebarchiesi@0
|
201 $description = ctools_access_summary($plugin, $contexts, $test);
|
danielebarchiesi@0
|
202 $row[] = array('data' => $description, 'class' => array('ctools-access-description'));
|
danielebarchiesi@0
|
203
|
danielebarchiesi@0
|
204 $operations = ctools_modal_image_button(ctools_image_path('icon-configure.png'), "ctools/context/ajax/access/configure/$fragment/$id", t('Configure settings for this item.'));
|
danielebarchiesi@0
|
205 $operations .= ctools_ajax_image_button(ctools_image_path('icon-delete.png'), "ctools/context/ajax/access/delete/$fragment/$id", t('Remove this item.'));
|
danielebarchiesi@0
|
206
|
danielebarchiesi@0
|
207 $row[] = array('data' => $operations, 'class' => array('ctools-access-operations'), 'align' => 'right');
|
danielebarchiesi@0
|
208
|
danielebarchiesi@0
|
209 $rows[] = $row;
|
danielebarchiesi@0
|
210 }
|
danielebarchiesi@0
|
211
|
danielebarchiesi@0
|
212 $header = array(
|
danielebarchiesi@0
|
213 array('data' => t('Title'), 'class' => array('ctools-access-title')),
|
danielebarchiesi@0
|
214 array('data' => t('Description'), 'class' => array('ctools-access-description')),
|
danielebarchiesi@0
|
215 array('data' => '', 'class' => array('ctools-access-operations'), 'align' => 'right'),
|
danielebarchiesi@0
|
216 );
|
danielebarchiesi@0
|
217
|
danielebarchiesi@0
|
218 if (empty($rows)) {
|
danielebarchiesi@0
|
219 $rows[] = array(array('data' => t('No criteria selected, this test will pass.'), 'colspan' => count($header)));
|
danielebarchiesi@0
|
220 }
|
danielebarchiesi@0
|
221
|
danielebarchiesi@0
|
222 ctools_modal_add_js();
|
danielebarchiesi@0
|
223 return theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'ctools-access-table')));
|
danielebarchiesi@0
|
224 }
|
danielebarchiesi@0
|
225
|
danielebarchiesi@0
|
226 /**
|
danielebarchiesi@0
|
227 * Theme the 'add' portion of the access form into a table.
|
danielebarchiesi@0
|
228 */
|
danielebarchiesi@0
|
229 function theme_ctools_access_admin_add($vars) {
|
danielebarchiesi@0
|
230 $rows = array(array(drupal_render_children($vars['form'])));
|
danielebarchiesi@0
|
231 $output = '<div class="container-inline">';
|
danielebarchiesi@0
|
232 $output .= theme('table', array('rows' => $rows));
|
danielebarchiesi@0
|
233 $output .= '</div>';
|
danielebarchiesi@0
|
234 return $output;
|
danielebarchiesi@0
|
235 }
|
danielebarchiesi@0
|
236
|
danielebarchiesi@0
|
237 function ctools_access_admin_form_submit($form, &$form_state) {
|
danielebarchiesi@0
|
238 $form_state['access']['logic'] = $form_state['values']['logic'];
|
danielebarchiesi@0
|
239
|
danielebarchiesi@0
|
240 $function = $form_state['module'] . '_ctools_access_clear';
|
danielebarchiesi@0
|
241 if (function_exists($function)) {
|
danielebarchiesi@0
|
242 $function($form_state['callback argument']);
|
danielebarchiesi@0
|
243 }
|
danielebarchiesi@0
|
244 }
|
danielebarchiesi@0
|
245
|
danielebarchiesi@0
|
246 // --------------------------------------------------------------------------
|
danielebarchiesi@0
|
247 // AJAX menu entry points.
|
danielebarchiesi@0
|
248
|
danielebarchiesi@0
|
249 /**
|
danielebarchiesi@0
|
250 * AJAX callback to add a new access test to the list.
|
danielebarchiesi@0
|
251 */
|
danielebarchiesi@0
|
252 function ctools_access_ajax_add($fragment = NULL, $name = NULL) {
|
danielebarchiesi@0
|
253 ctools_include('ajax');
|
danielebarchiesi@0
|
254 ctools_include('modal');
|
danielebarchiesi@0
|
255 ctools_include('context');
|
danielebarchiesi@0
|
256
|
danielebarchiesi@0
|
257 if (empty($fragment) || empty($name)) {
|
danielebarchiesi@0
|
258 ctools_ajax_render_error();
|
danielebarchiesi@0
|
259 }
|
danielebarchiesi@0
|
260
|
danielebarchiesi@0
|
261 $plugin = ctools_get_access_plugin($name);
|
danielebarchiesi@0
|
262 if (empty($plugin)) {
|
danielebarchiesi@0
|
263 ctools_ajax_render_error();
|
danielebarchiesi@0
|
264 }
|
danielebarchiesi@0
|
265
|
danielebarchiesi@0
|
266 // Separate the fragment into 'module' and 'argument'
|
danielebarchiesi@0
|
267 if (strpos($fragment, '-') === FALSE) {
|
danielebarchiesi@0
|
268 $module = $fragment;
|
danielebarchiesi@0
|
269 $argument = NULL;
|
danielebarchiesi@0
|
270 }
|
danielebarchiesi@0
|
271 else {
|
danielebarchiesi@0
|
272 list($module, $argument) = explode('-', $fragment, 2);
|
danielebarchiesi@0
|
273 }
|
danielebarchiesi@0
|
274
|
danielebarchiesi@0
|
275 $function = $module . '_ctools_access_get';
|
danielebarchiesi@0
|
276 if (!function_exists($function)) {
|
danielebarchiesi@0
|
277 ctools_ajax_render_error(t('Missing callback hooks.'));
|
danielebarchiesi@0
|
278 }
|
danielebarchiesi@0
|
279
|
danielebarchiesi@0
|
280 list($access, $contexts) = $function($argument);
|
danielebarchiesi@0
|
281
|
danielebarchiesi@0
|
282 // Make sure we have the logged in user context
|
danielebarchiesi@0
|
283 if (!isset($contexts['logged-in-user'])) {
|
danielebarchiesi@0
|
284 $contexts['logged-in-user'] = ctools_access_get_loggedin_context();
|
danielebarchiesi@0
|
285 }
|
danielebarchiesi@0
|
286
|
danielebarchiesi@0
|
287 if (empty($access['plugins'])) {
|
danielebarchiesi@0
|
288 $access['plugins'] = array();
|
danielebarchiesi@0
|
289 }
|
danielebarchiesi@0
|
290
|
danielebarchiesi@0
|
291 $test = ctools_access_new_test($plugin);
|
danielebarchiesi@0
|
292
|
danielebarchiesi@0
|
293 $id = $access['plugins'] ? max(array_keys($access['plugins'])) + 1 : 0;
|
danielebarchiesi@0
|
294 $access['plugins'][$id] = $test;
|
danielebarchiesi@0
|
295
|
danielebarchiesi@0
|
296 $form_state = array(
|
danielebarchiesi@0
|
297 'plugin' => $plugin,
|
danielebarchiesi@0
|
298 'id' => $id,
|
danielebarchiesi@0
|
299 'test' => &$access['plugins'][$id],
|
danielebarchiesi@0
|
300 'access' => &$access,
|
danielebarchiesi@0
|
301 'contexts' => $contexts,
|
danielebarchiesi@0
|
302 'title' => t('Add criteria'),
|
danielebarchiesi@0
|
303 'ajax' => TRUE,
|
danielebarchiesi@0
|
304 'modal' => TRUE,
|
danielebarchiesi@0
|
305 'modal return' => TRUE,
|
danielebarchiesi@0
|
306 );
|
danielebarchiesi@0
|
307
|
danielebarchiesi@0
|
308 $output = ctools_modal_form_wrapper('ctools_access_ajax_edit_item', $form_state);
|
danielebarchiesi@0
|
309 if (!isset($output[0])) {
|
danielebarchiesi@0
|
310 $function = $module . '_ctools_access_set';
|
danielebarchiesi@0
|
311 if (function_exists($function)) {
|
danielebarchiesi@0
|
312 $function($argument, $access);
|
danielebarchiesi@0
|
313 }
|
danielebarchiesi@0
|
314
|
danielebarchiesi@0
|
315 $table = ctools_access_admin_render_table($access, $fragment, $contexts);
|
danielebarchiesi@0
|
316 $output = array();
|
danielebarchiesi@0
|
317 $output[] = ajax_command_replace('table#ctools-access-table', $table);
|
danielebarchiesi@0
|
318 $output[] = ctools_modal_command_dismiss();
|
danielebarchiesi@0
|
319 }
|
danielebarchiesi@0
|
320
|
danielebarchiesi@0
|
321 print ajax_render($output);
|
danielebarchiesi@0
|
322 }
|
danielebarchiesi@0
|
323
|
danielebarchiesi@0
|
324 /**
|
danielebarchiesi@0
|
325 * AJAX callback to edit an access test in the list.
|
danielebarchiesi@0
|
326 */
|
danielebarchiesi@0
|
327 function ctools_access_ajax_edit($fragment = NULL, $id = NULL) {
|
danielebarchiesi@0
|
328 ctools_include('ajax');
|
danielebarchiesi@0
|
329 ctools_include('modal');
|
danielebarchiesi@0
|
330 ctools_include('context');
|
danielebarchiesi@0
|
331
|
danielebarchiesi@0
|
332 if (empty($fragment) || !isset($id)) {
|
danielebarchiesi@0
|
333 ctools_ajax_render_error();
|
danielebarchiesi@0
|
334 }
|
danielebarchiesi@0
|
335
|
danielebarchiesi@0
|
336 // Separate the fragment into 'module' and 'argument'
|
danielebarchiesi@0
|
337 if (strpos($fragment, '-') === FALSE) {
|
danielebarchiesi@0
|
338 $module = $fragment;
|
danielebarchiesi@0
|
339 $argument = NULL;
|
danielebarchiesi@0
|
340 }
|
danielebarchiesi@0
|
341 else {
|
danielebarchiesi@0
|
342 list($module, $argument) = explode('-', $fragment, 2);
|
danielebarchiesi@0
|
343 }
|
danielebarchiesi@0
|
344
|
danielebarchiesi@0
|
345 $function = $module . '_ctools_access_get';
|
danielebarchiesi@0
|
346 if (!function_exists($function)) {
|
danielebarchiesi@0
|
347 ctools_ajax_render_error(t('Missing callback hooks.'));
|
danielebarchiesi@0
|
348 }
|
danielebarchiesi@0
|
349
|
danielebarchiesi@0
|
350 list($access, $contexts) = $function($argument);
|
danielebarchiesi@0
|
351
|
danielebarchiesi@0
|
352 if (empty($access['plugins'][$id])) {
|
danielebarchiesi@0
|
353 ctools_ajax_render_error();
|
danielebarchiesi@0
|
354 }
|
danielebarchiesi@0
|
355
|
danielebarchiesi@0
|
356 // Make sure we have the logged in user context
|
danielebarchiesi@0
|
357 if (!isset($contexts['logged-in-user'])) {
|
danielebarchiesi@0
|
358 $contexts['logged-in-user'] = ctools_access_get_loggedin_context();
|
danielebarchiesi@0
|
359 }
|
danielebarchiesi@0
|
360
|
danielebarchiesi@0
|
361 $plugin = ctools_get_access_plugin($access['plugins'][$id]['name']);
|
danielebarchiesi@0
|
362 $form_state = array(
|
danielebarchiesi@0
|
363 'plugin' => $plugin,
|
danielebarchiesi@0
|
364 'id' => $id,
|
danielebarchiesi@0
|
365 'test' => &$access['plugins'][$id],
|
danielebarchiesi@0
|
366 'access' => &$access,
|
danielebarchiesi@0
|
367 'contexts' => $contexts,
|
danielebarchiesi@0
|
368 'title' => t('Edit criteria'),
|
danielebarchiesi@0
|
369 'ajax' => TRUE,
|
danielebarchiesi@0
|
370 'ajax' => TRUE,
|
danielebarchiesi@0
|
371 'modal' => TRUE,
|
danielebarchiesi@0
|
372 'modal return' => TRUE,
|
danielebarchiesi@0
|
373 );
|
danielebarchiesi@0
|
374
|
danielebarchiesi@0
|
375 $output = ctools_modal_form_wrapper('ctools_access_ajax_edit_item', $form_state);
|
danielebarchiesi@0
|
376 if (!isset($output[0])) {
|
danielebarchiesi@0
|
377 $function = $module . '_ctools_access_set';
|
danielebarchiesi@0
|
378 if (function_exists($function)) {
|
danielebarchiesi@0
|
379 $function($argument, $access);
|
danielebarchiesi@0
|
380 }
|
danielebarchiesi@0
|
381
|
danielebarchiesi@0
|
382 $table = ctools_access_admin_render_table($access, $fragment, $contexts);
|
danielebarchiesi@0
|
383 $output = array();
|
danielebarchiesi@0
|
384 $output[] = ajax_command_replace('table#ctools-access-table', $table);
|
danielebarchiesi@0
|
385 $output[] = ctools_modal_command_dismiss();
|
danielebarchiesi@0
|
386 }
|
danielebarchiesi@0
|
387
|
danielebarchiesi@0
|
388 print ajax_render($output);
|
danielebarchiesi@0
|
389 }
|
danielebarchiesi@0
|
390
|
danielebarchiesi@0
|
391 /**
|
danielebarchiesi@0
|
392 * Form to edit the settings of an access test.
|
danielebarchiesi@0
|
393 */
|
danielebarchiesi@0
|
394 function ctools_access_ajax_edit_item($form, &$form_state) {
|
danielebarchiesi@0
|
395 $test = &$form_state['test'];
|
danielebarchiesi@0
|
396 $plugin = &$form_state['plugin'];
|
danielebarchiesi@0
|
397 if (isset($plugin['required context'])) {
|
danielebarchiesi@0
|
398 $form['context'] = ctools_context_selector($form_state['contexts'], $plugin['required context'], $test['context']);
|
danielebarchiesi@0
|
399 }
|
danielebarchiesi@0
|
400 $form['settings'] = array('#tree' => TRUE);
|
danielebarchiesi@0
|
401 if ($function = ctools_plugin_get_function($plugin, 'settings form')) {
|
danielebarchiesi@0
|
402 $form = $function($form, $form_state, $test['settings']);
|
danielebarchiesi@0
|
403 }
|
danielebarchiesi@0
|
404
|
danielebarchiesi@0
|
405 $form['not'] = array(
|
danielebarchiesi@0
|
406 '#type' => 'checkbox',
|
danielebarchiesi@0
|
407 '#title' => t('Reverse (NOT)'),
|
danielebarchiesi@0
|
408 '#default_value' => !empty($test['not']),
|
danielebarchiesi@0
|
409 );
|
danielebarchiesi@0
|
410
|
danielebarchiesi@0
|
411 $form['save'] = array(
|
danielebarchiesi@0
|
412 '#type' => 'submit',
|
danielebarchiesi@0
|
413 '#value' => t('Save'),
|
danielebarchiesi@0
|
414 );
|
danielebarchiesi@0
|
415
|
danielebarchiesi@0
|
416 return $form;
|
danielebarchiesi@0
|
417 }
|
danielebarchiesi@0
|
418
|
danielebarchiesi@0
|
419 /**
|
danielebarchiesi@0
|
420 * Validate handler for argument settings.
|
danielebarchiesi@0
|
421 */
|
danielebarchiesi@0
|
422 function ctools_access_ajax_edit_item_validate($form, &$form_state) {
|
danielebarchiesi@0
|
423 if ($function = ctools_plugin_get_function($form_state['plugin'], 'settings form validate')) {
|
danielebarchiesi@0
|
424 $function($form, $form_state);
|
danielebarchiesi@0
|
425 }
|
danielebarchiesi@0
|
426 }
|
danielebarchiesi@0
|
427
|
danielebarchiesi@0
|
428 /**
|
danielebarchiesi@0
|
429 * Submit handler for argument settings.
|
danielebarchiesi@0
|
430 */
|
danielebarchiesi@0
|
431 function ctools_access_ajax_edit_item_submit($form, &$form_state) {
|
danielebarchiesi@0
|
432 if ($function = ctools_plugin_get_function($form_state['plugin'], 'settings form submit')) {
|
danielebarchiesi@0
|
433 $function($form, $form_state);
|
danielebarchiesi@0
|
434 }
|
danielebarchiesi@0
|
435
|
danielebarchiesi@0
|
436 $form_state['test']['settings'] = $form_state['values']['settings'];
|
danielebarchiesi@0
|
437 if (isset($form_state['values']['context'])) {
|
danielebarchiesi@0
|
438 $form_state['test']['context'] = $form_state['values']['context'];
|
danielebarchiesi@0
|
439 }
|
danielebarchiesi@0
|
440 $form_state['test']['not'] = !empty($form_state['values']['not']);
|
danielebarchiesi@0
|
441 }
|
danielebarchiesi@0
|
442
|
danielebarchiesi@0
|
443 /**
|
danielebarchiesi@0
|
444 * AJAX command to remove an access control item.
|
danielebarchiesi@0
|
445 */
|
danielebarchiesi@0
|
446 function ctools_access_ajax_delete($fragment = NULL, $id = NULL) {
|
danielebarchiesi@0
|
447 ctools_include('ajax');
|
danielebarchiesi@0
|
448 ctools_include('modal');
|
danielebarchiesi@0
|
449 ctools_include('context');
|
danielebarchiesi@0
|
450
|
danielebarchiesi@0
|
451 if (empty($fragment) || !isset($id)) {
|
danielebarchiesi@0
|
452 ajax_render_error();
|
danielebarchiesi@0
|
453 }
|
danielebarchiesi@0
|
454
|
danielebarchiesi@0
|
455 // Separate the fragment into 'module' and 'argument'
|
danielebarchiesi@0
|
456 if (strpos($fragment, '-') === FALSE) {
|
danielebarchiesi@0
|
457 $module = $fragment;
|
danielebarchiesi@0
|
458 $argument = NULL;
|
danielebarchiesi@0
|
459 }
|
danielebarchiesi@0
|
460 else {
|
danielebarchiesi@0
|
461 list($module, $argument) = explode('-', $fragment, 2);
|
danielebarchiesi@0
|
462 }
|
danielebarchiesi@0
|
463
|
danielebarchiesi@0
|
464 $function = $module . '_ctools_access_get';
|
danielebarchiesi@0
|
465 if (!function_exists($function)) {
|
danielebarchiesi@0
|
466 ajax_render_error(t('Missing callback hooks.'));
|
danielebarchiesi@0
|
467 }
|
danielebarchiesi@0
|
468
|
danielebarchiesi@0
|
469 list($access, $contexts) = $function($argument);
|
danielebarchiesi@0
|
470
|
danielebarchiesi@0
|
471 if (isset($access['plugins'][$id])) {
|
danielebarchiesi@0
|
472 unset($access['plugins'][$id]);
|
danielebarchiesi@0
|
473 }
|
danielebarchiesi@0
|
474
|
danielebarchiesi@0
|
475 // re-cache
|
danielebarchiesi@0
|
476 $function = $module . '_ctools_access_set';
|
danielebarchiesi@0
|
477 if (function_exists($function)) {
|
danielebarchiesi@0
|
478 $function($argument, $access);
|
danielebarchiesi@0
|
479 }
|
danielebarchiesi@0
|
480
|
danielebarchiesi@0
|
481 $table = ctools_access_admin_render_table($access, $fragment, $contexts);
|
danielebarchiesi@0
|
482 $output = array();
|
danielebarchiesi@0
|
483 $output[] = ajax_command_replace('table#ctools-access-table', $table);
|
danielebarchiesi@0
|
484
|
danielebarchiesi@0
|
485 print ajax_render($output);
|
danielebarchiesi@0
|
486 }
|