Project

General

Profile

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

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

1
<?php
2

    
3
/**
4
 * @file
5
 * Plugin to provide access control based upon role membership.
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: role"),
14
  'description' => t('Control access by role.'),
15
  'callback' => 'ctools_role_ctools_access_check',
16
  'default' => array('rids' => array()),
17
  'settings form' => 'ctools_role_ctools_access_settings',
18
  'settings form submit' => 'ctools_role_ctools_access_settings_submit',
19
  'summary' => 'ctools_role_ctools_access_summary',
20
  'required context' => new ctools_context_required(t('User'), 'user'),
21
);
22

    
23
/**
24
 * Settings form for the 'by role' access plugin.
25
 */
26
function ctools_role_ctools_access_settings($form, &$form_state, $conf) {
27
  $form['settings']['rids'] = array(
28
    '#type' => 'checkboxes',
29
    '#title' => t('Role'),
30
    '#default_value' => $conf['rids'],
31
    '#options' => ctools_get_roles(),
32
    '#description' => t('Only the checked roles will be granted access.'),
33
  );
34
  return $form;
35
}
36

    
37
/**
38
 * Compress the roles allowed to the minimum.
39
 */
40
function ctools_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_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_role_ctools_access_summary($conf, $context) {
63
  if (!isset($conf['rids'])) {
64
    $conf['rids'] = array();
65
  }
66
  $roles = ctools_get_roles();
67

    
68
  $names = array();
69
  foreach (array_filter($conf['rids']) as $rid) {
70
    $names[] = check_plain($roles[$rid]);
71
  }
72

    
73
  if (empty($names)) {
74
    return t('@identifier can have any role', array('@identifier' => $context->identifier));
75
  }
76

    
77
  return format_plural(count($names), '@identifier has role "@roles"', '@identifier has one of "@roles"', array('@roles' => implode(', ', $names), '@identifier' => $context->identifier));
78
}