You are here

function masquerade_user_view in Masquerade 7

Same name and namespace in other branches
  1. 8.2 masquerade.module \masquerade_user_view()

Implements hook_user_view().

File

./masquerade.module, line 407
The masquerade module allows administrators to masquerade as other user.

Code

function masquerade_user_view($account, $view_mode, $langcode) {

  // check if user qualifies as admin
  $roles = array_keys(array_filter(variable_get('masquerade_admin_roles', array())));
  $perm = $account->uid == 1 || array_intersect(array_keys((array) $account->roles), $roles) ? 'masquerade as admin' : 'masquerade as user';
  global $user;

  // Query allowed uids so the "masquerade as <user>" link can be shown or
  // hidden.
  $allowed_uids = db_query("SELECT uid_to FROM {masquerade_users} WHERE uid_from = :uid_from", array(
    ':uid_from' => $user->uid,
  ))
    ->fetchCol();
  $can_masquerade_as_user = in_array($account->uid, $allowed_uids) || user_access('masquerade as any user');
  if (user_access($perm) && empty($account->masquerading) && $user->uid != $account->uid && $can_masquerade_as_user) {
    $account->content['masquerade'] = array(
      '#markup' => l(t('Masquerade as !user', array(
        '!user' => $account->name,
      )), 'masquerade/switch/' . $account->uid, array(
        'query' => array(
          'token' => drupal_get_token('masquerade/switch/' . $account->uid),
        ),
        'destination' => $_GET['q'],
        'attributes' => array(
          'class' => array(
            'masquerade-switch',
          ),
        ),
      )),
      '#weight' => 10,
    );
  }
}