[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

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

   1  <?php
   2  // $Id: compare_users.inc,v 1.3.2.2 2009/11/13 00:44:16 merlinofchaos Exp $
   3  
   4  /**
   5   * @file
   6   * Ctools access plugin to provide access/visiblity if two user contexts are equal.
   7   */
   8  
   9  /**
  10   * Plugins are described by creating a $plugin array which will be used
  11   * by the system that includes this file.
  12   */
  13  $plugin = array(
  14    'title' => t("User: compare"),
  15    'description' => t('Compare two users (logged-in user and user being viewed, for example)'),
  16    'callback' => 'ctools_compare_users_access_check',
  17    'default' => array(
  18      'equality' => 1,
  19    ),
  20    'settings form' => 'ctools_compare_users_settings',
  21    'summary' => 'ctools_compare_users_ctools_access_summary',
  22    'required context' => array(
  23      new ctools_context_required(t('First User'), 'user'),
  24      new ctools_context_required(t("Second User"), 'user')
  25    ),
  26  );
  27  
  28  /**
  29   * Settings form for the 'by perm' access plugin
  30   */
  31  function ctools_compare_users_settings(&$form, &$form_state, $conf) {
  32  
  33    $form['settings']['helptext'] = array(
  34      '#type' => 'markup',
  35      '#value' => '<div>'. t('Grant access based on comparison of the two user contexts. For example, to grant access to a user to view their own profile, choose "logged in user" and "user being viewed" and say "grant access if equal". When they\'re the same, access will be granted.') . '</div>',
  36    );
  37  
  38    $form['settings']['equality'] = array(
  39      '#type' => 'radios',
  40      '#title' => t('Grant access if user contexts are'),
  41      '#options' => array(1 => t('Equal'), 0 => t('Not equal')),
  42      '#default_value' => $conf['equality'],
  43    );
  44  }
  45  
  46  /**
  47   * Check for access.
  48   */
  49  function ctools_compare_users_access_check($conf, $context) {
  50  
  51    if (empty($context) || count($context) != 2 || empty($context[0]->data) || empty($context[1]->data)) {
  52      return FALSE;
  53    }
  54    $account1 = $context[0]->data;
  55    $account2 = $context[1]->data;
  56  
  57    // xor returns false if the two bools are the same, and true if they are not.
  58    // i.e, if we asked for equality and they are equal, return true.
  59    // If we asked for inequality and they are equal, return false.
  60    return ($account1->uid == $account2->uid) xor empty($conf['equality']);
  61  }
  62  
  63  /**
  64   * Describe an instance of this plugin.
  65   */
  66  function ctools_compare_users_ctools_access_summary($conf, $context) {
  67    $comparison = !empty($conf['equality']) ? "is" : 'is not';
  68  
  69    return t('@id1 @comp @id2', array('@comp' => $comparison, '@id1' => $context[0]->identifier, '@id2' => $context[1]->identifier));
  70  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7