Project

General

Profile

Paste
Download (2.08 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / ctools / plugins / access / perm.inc @ 6e3ce7c2

1
<?php
2

    
3
/**
4
 * @file
5
 * Plugin to provide access control based on user permission strings.
6
 */
7

    
8
/**
9
 * Plugins are described by creating a $plugin array which will be used
10
 * by the system that includes this file.
11
 */
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',
18
  'summary' => 'ctools_perm_ctools_access_summary',
19
  'required context' => new ctools_context_required(t('User'), 'user'),
20
);
21

    
22
/**
23
 * Settings form for the 'by perm' access plugin.
24
 */
25
function ctools_perm_ctools_access_settings($form, &$form_state, $conf) {
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.
31
    if ($permissions = module_invoke($module, 'permission')) {
32
      foreach ($permissions as $id => $permission) {
33
        $perms[$module][$id] = $permission['title'];
34
      }
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
  );
45

    
46
  return $form;
47
}
48

    
49
/**
50
 * Check for access.
51
 */
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
 */
65
function ctools_perm_ctools_access_summary($conf, $context) {
66
  if (!isset($conf['perm'])) {
67
    return t('Error, unset permission');
68
  }
69

    
70
  $permissions = module_invoke_all('permission');
71
  return t('@identifier has "@perm"', array('@identifier' => $context->identifier, '@perm' => $permissions[$conf['perm']]['title']));
72
}