Project

General

Profile

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

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

1
<?php
2

    
3
/**
4
 * @file
5
 * CTools access plugin to provide access/visiblity if two user contexts are equal.
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: compare"),
14
  'description' => t('Compare two users (logged-in user and user being viewed, for example)'),
15
  'callback' => 'ctools_compare_users_access_check',
16
  'default' => array(
17
    'equality' => 1,
18
  ),
19
  'settings form' => 'ctools_compare_users_settings',
20
  'summary' => 'ctools_compare_users_ctools_access_summary',
21
  'required context' => array(
22
    new ctools_context_required(t('First User'), 'user'),
23
    new ctools_context_required(t("Second User"), 'user'),
24
  ),
25
);
26

    
27
/**
28
 * Settings form for the 'by perm' access plugin.
29
 */
30
function ctools_compare_users_settings($form, &$form_state, $conf) {
31

    
32
  $form['settings']['helptext'] = array(
33
    '#type' => 'markup',
34
    '#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>',
35
  );
36

    
37
  $form['settings']['equality'] = array(
38
    '#type' => 'radios',
39
    '#title' => t('Grant access if user contexts are'),
40
    '#options' => array(1 => t('Equal'), 0 => t('Not equal')),
41
    '#default_value' => $conf['equality'],
42
  );
43
  return $form;
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
}