You are here

user_from_masquerade.inc in Masquerade Extras 6.2

Same filename and directory in other branches
  1. 7.2 masquerade_ctools/plugins/relationships/user_from_masquerade.inc

Allows the user to access related user info during a masquerade.

To some, this plugin may be confusing. The idea behind it is that we can provide access to the user on the opposite side of a masquerade based on an available user. (Most often, the "current" user).

For instance:

  • Joe Smith is masquerading as "John Jacobson".
  • We want "John Jacobson" to know if, when, and who is using his account as he browses certain pages.
  • So, this plugin would allow you to place information about "Joe Smith" into the page while "John Jacobson" is browsing.

File

masquerade_ctools/plugins/relationships/user_from_masquerade.inc
View source
<?php

/**
 * @file
 *  Allows the user to access related user info during a masquerade.
 *
 *  To some, this plugin may be confusing. The idea behind it is that we can
 *  provide access to the user on the opposite side of a masquerade based
 *  on an available user. (Most often, the "current" user).
 *
 *  For instance:
 *  - Joe Smith is masquerading as "John Jacobson".
 *  - We want "John Jacobson" to know if, when, and who is using his
 *    account as he browses certain pages.
 *  - So, this plugin would allow you to place information about
 *    "Joe Smith" into the page while "John Jacobson" is browsing.
 */

/**
 * Plugin definition.
 * @todo: I had trouble finding solid documentation around this,
 *        so, we're basically building-by-example.
 */
$plugin = array(
  'title' => t('User from Masquerade'),
  'keyword' => 'masquerade',
  'description' => t('Creates user contexts from a masquerade.'),
  'context' => 'masquerade_ctools_user_from_masquerade_context',
  'settings form' => 'masquerade_ctools_user_from_masquerade_settings_form',
  'defaults' => array(
    'mode' => 'masqueradee',
  ),
  'required context' => array(
    new ctools_context_required(t('User'), 'user'),
  ),
);

/**
 * Retrieves the user info from the context.
 *
 * @param bool $context
 *  The current contexts.
 * @param array $conf
 *  The plugin configuration.
 * @param bool $unknown
 *  ??? There is a mystery argument which appears to be a boolean.
 * @returns
 *  Returns a context object containing the user we want.
 * @retval ctools_context
 */
function masquerade_ctools_user_from_masquerade_context($context, $conf) {
  global $user;

  // @todo: This function is database intensive, we either need:
  //  a way to cache results, or a way to use fewer queries.
  if (empty($context[0]->data) || !isset($context[0]->data->uid) || empty($conf)) {
    return ctools_context_create_empty('user', NULL);
  }

  // We evaluate using isset() to ensure that the "anonymous"
  // user is not passed over.
  $argument = $context[0]->data;
  $query = FALSE;

  // NOTE: If we want to know who the masquerader is,
  // we need to check if the current user is BEING masqueraded.
  if ('masquerader' == $conf['settings']['mode'] && masquerade_ctools_is_being_masqueraded($argument)) {
    $query = db_query("SELECT `uid_from`\n         FROM {masquerade}\n        WHERE `uid_as` = %d\n        LIMIT 1", $argument->uid);
  }

  // NOTE: If we want to know who the masqueradee is,
  // we need to check if the current user IS masquerading.
  if ('masqueradee' == $conf['settings']['mode'] && masquerade_ctools_is_masquerading($argument)) {
    $query = db_query("SELECT `uid_as`\n         FROM {masquerade}\n        WHERE `uid_from` = %d\n        LIMIT 1", $argument->uid);
  }

  // Load the user account and return.
  if (FALSE !== ($result = db_result($query))) {
    $account = user_load($result);
    return ctools_context_create('user', $account);
  }

  // Return an empty user context if neither case is matched.
  return ctools_context_create_empty('user', NULL);
}

/**
 * Allow the user to configure the context in the UI.
 * @param array $conf
 *  Configuration defaults/settings passed to the form.
 * @returns
 *  A form the user will see when configuring this plugin.
 * @retval array
 */
function masquerade_ctools_user_from_masquerade_settings_form($conf) {
  $form = array();
  $form['settings']['mode'] = array(
    '#weight' => 0,
    '#title' => t('Target user'),
    '#type' => 'radios',
    '#options' => array(
      'masquerader' => t('The person masquerading'),
      'masqueradee' => t('The person being masqueraded'),
    ),
    '#description' => t('Which person in the masquerade do you need access to?') . t('You should only need this relationship if you need the REVERSE of the "current user".'),
    '#default_value' => $conf['settings']['mode'],
  );
  return $form;
}

Functions

Namesort descending Description
masquerade_ctools_user_from_masquerade_context Retrieves the user info from the context.
masquerade_ctools_user_from_masquerade_settings_form Allow the user to configure the context in the UI.