compare_users.inc 2.3 KB

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