You are here

function masquerade_ctools_user_from_masquerade_context in Masquerade Extras 7.2

Same name and namespace in other branches
  1. 6.2 masquerade_ctools/plugins/relationships/user_from_masquerade.inc \masquerade_ctools_user_from_masquerade_context()

Retrieves the user info from the context.

@link https://drupal.org/node/2178335

@returns Returns a context object containing the user we want. @retval ctools_context

Parameters

bool $context: The current contexts.

array $conf: The plugin configuration.

bool $unknown: ??? There is a mystery argument which appears to be a boolean.

1 string reference to 'masquerade_ctools_user_from_masquerade_context'
user_from_masquerade.inc in masquerade_ctools/plugins/relationships/user_from_masquerade.inc
Allows the user to access related user info during a masquerade.

File

masquerade_ctools/plugins/relationships/user_from_masquerade.inc, line 52
Allows the user to access related user info during a masquerade.

Code

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->uid;

  // NOTE: If we want to know who the masquerader is,
  // we need to check if the current user is BEING masqueraded.
  if ('masquerader' == $conf['mode'] && masquerade_ctools_is_being_masqueraded($argument)) {
    $query = db_select('masquerade', 'm')
      ->fields('m', array(
      'uid_from',
    ))
      ->condition('uid_as', $argument, '=')
      ->range(0, 1)
      ->execute();
    $result = $query
      ->fetchCol();
  }

  // NOTE: If we want to know who the masqueradee is,
  // we need to check if the current user IS masquerading.
  if ('masqueradee' == $conf['mode'] && masquerade_ctools_is_masquerading($argument)) {
    $query = db_select('masquerade', 'm')
      ->fields('m', array(
      'uid_as',
    ))
      ->condition('uid_from', $argument, '=')
      ->range(0, 1)
      ->execute();
    $result = $query
      ->fetchCol();
  }

  // If we didnt get a result, return an empty user context.
  if (empty($result)) {
    return ctools_context_create_empty('user', NULL);
  }
  $account = user_load($result[0]);
  return ctools_context_create('user', $account);
}