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