| Commit | Line | Data |
|---|---|---|
| fe148241 | 1 | <?php |
| fe148241 EM |
2 | |
| 3 | /** | |
| 4 | * @file | |
| efd617ba | 5 | * Plugin to provide access control based on user permission strings. |
| fe148241 EM |
6 | */ |
| 7 | ||
| 8 | /** | |
| 891e0b3b EM |
9 | * Plugins are described by creating a $plugin array which will be used |
| 10 | * by the system that includes this file. | |
| fe148241 | 11 | */ |
| 891e0b3b EM |
12 | $plugin = array( |
| 13 | 'title' => t("User: permission"), | |
| 14 | 'description' => t('Control access by permission string.'), | |
| 15 | 'callback' => 'ctools_perm_ctools_access_check', | |
| 16 | 'default' => array('perm' => 'access content'), | |
| 17 | 'settings form' => 'ctools_perm_ctools_access_settings', | |
| aeed3fef | 18 | 'summary' => 'ctools_perm_ctools_access_summary', |
| 891e0b3b EM |
19 | 'required context' => new ctools_context_required(t('User'), 'user'), |
| 20 | ); | |
| fe148241 EM |
21 | |
| 22 | /** | |
| 23 | * Settings form for the 'by perm' access plugin | |
| 24 | */ | |
| a40482ac | 25 | function ctools_perm_ctools_access_settings($form, &$form_state, $conf) { |
| fe148241 EM |
26 | $perms = array(); |
| 27 | // Get list of permissions | |
| 28 | foreach (module_list(FALSE, FALSE, TRUE) as $module) { | |
| 29 | // By keeping them keyed by module we can use optgroups with the | |
| 30 | // 'select' type. | |
| a40482ac SB |
31 | if ($permissions = module_invoke($module, 'permission')) { |
| 32 | foreach ($permissions as $id => $permission) { | |
| 33 | $perms[$module][$id] = $permission['title']; | |
| 34 | } | |
| fe148241 EM |
35 | } |
| 36 | } | |
| 37 | ||
| 38 | $form['settings']['perm'] = array( | |
| 39 | '#type' => 'select', | |
| 40 | '#options' => $perms, | |
| 41 | '#title' => t('Permission'), | |
| 42 | '#default_value' => $conf['perm'], | |
| 43 | '#description' => t('Only users with the selected permission flag will be able to access this.'), | |
| 44 | ); | |
| a40482ac SB |
45 | |
| 46 | return $form; | |
| fe148241 EM |
47 | } |
| 48 | ||
| 49 | /** | |
| 50 | * Check for access. | |
| 51 | */ | |
| efd617ba EM |
52 | function ctools_perm_ctools_access_check($conf, $context) { |
| 53 | // As far as I know there should always be a context at this point, but this | |
| 54 | // is safe. | |
| 55 | if (empty($context) || empty($context->data)) { | |
| 56 | return FALSE; | |
| 57 | } | |
| 58 | ||
| 59 | return user_access($conf['perm'], $context->data); | |
| 60 | } | |
| 61 | ||
| 62 | /** | |
| 63 | * Provide a summary description based upon the checked roles. | |
| 64 | */ | |
| aeed3fef | 65 | function ctools_perm_ctools_access_summary($conf, $context) { |
| efd617ba EM |
66 | if (!isset($conf['perm'])) { |
| 67 | return t('Error, unset permission'); | |
| 68 | } | |
| 69 | ||
| a40482ac SB |
70 | $permissions = module_invoke_all('permission'); |
| 71 | return t('@identifier has "@perm"', array('@identifier' => $context->identifier, '@perm' => $permissions[$conf['perm']]['title'])); | |
| efd617ba | 72 | } |
| 5b52254b | 73 |