[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/ctools/ctools_plugin_example/plugins/access/ -> example_role.inc (source)

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


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7