summaryrefslogtreecommitdiff
path: root/sites/all/modules/ctools/plugins/access/compare_users.inc
blob: c271ff4e3cdafcd528455e623e75c820deab43d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php

/**
 * @file
 * Ctools access plugin to provide access/visiblity if two user contexts are equal.
 */

/**
 * Plugins are described by creating a $plugin array which will be used
 * by the system that includes this file.
 */
$plugin = array(
  'title' => t("User: compare"),
  'description' => t('Compare two users (logged-in user and user being viewed, for example)'),
  'callback' => 'ctools_compare_users_access_check',
  'default' => array(
    'equality' => 1,
  ),
  'settings form' => 'ctools_compare_users_settings',
  'summary' => 'ctools_compare_users_ctools_access_summary',
  'required context' => array(
    new ctools_context_required(t('First User'), 'user'),
    new ctools_context_required(t("Second User"), 'user')
  ),
);

/**
 * Settings form for the 'by perm' access plugin
 */
function ctools_compare_users_settings($form, &$form_state, $conf) {

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

  $form['settings']['equality'] = array(
    '#type' => 'radios',
    '#title' => t('Grant access if user contexts are'),
    '#options' => array(1 => t('Equal'), 0 => t('Not equal')),
    '#default_value' => $conf['equality'],
  );
  return $form;
}

/**
 * Check for access.
 */
function ctools_compare_users_access_check($conf, $context) {

  if (empty($context) || count($context) != 2 || empty($context[0]->data) || empty($context[1]->data)) {
    return FALSE;
  }
  $account1 = $context[0]->data;
  $account2 = $context[1]->data;

  // xor returns false if the two bools are the same, and true if they are not.
  // i.e, if we asked for equality and they are equal, return true.
  // If we asked for inequality and they are equal, return false.
  return ($account1->uid == $account2->uid) xor empty($conf['equality']);
}

/**
 * Describe an instance of this plugin.
 */
function ctools_compare_users_ctools_access_summary($conf, $context) {
  $comparison = !empty($conf['equality']) ? "is" : 'is not';

  return t('@id1 @comp @id2', array('@comp' => $comparison, '@id1' => $context[0]->identifier, '@id2' => $context[1]->identifier));
}