You are here

function masquerade_switch_user in Masquerade 5

Same name and namespace in other branches
  1. 6 masquerade.module \masquerade_switch_user()
  2. 7 masquerade.module \masquerade_switch_user()

Page callback that allows a user with the right permissions to become the selected user.

1 call to masquerade_switch_user()
masquerade_block_1_submit in ./masquerade.module
Masquerade block form submission. Implementation of hook_submit().
1 string reference to 'masquerade_switch_user'
masquerade_menu in ./masquerade.module
Implementation of hook_menu().

File

./masquerade.module, line 326
masquerade.module

Code

function masquerade_switch_user($uid) {
  $new_user = user_load(array(
    'uid' => $uid,
  ));
  if (!$new_user->uid) {
    return drupal_not_found();
  }
  $roles = array_keys(array_filter(variable_get('masquerade_admin_roles', array())));
  $perm = $uid == 1 || array_intersect(array_keys($new_user->roles), $roles) ? 'masquerade as admin' : 'masquerade as user';

  // check to see if we need admin permission
  if (!user_access($perm) && !$GLOBALS['masquerading']) {
    return drupal_access_denied();
  }
  global $user;
  if ($user->uid == $uid || $user->masquerading) {
    return drupal_access_denied();
  }
  if (variable_get('site_offline', 0) && !user_access('administer site configuration', $new_user)) {
    drupal_set_message(t('The user is not allowed to access site in off-line mode!'), 'error');
    return drupal_access_denied();
  }
  db_query("INSERT INTO {masquerade} (uid_from, uid_as, sid) VALUES (%d, %d, '%s')", $user->uid, $new_user->uid, session_id());

  // switch user
  watchdog('masquerade', t('User %user now masquerading as %masq_as.', array(
    '%user' => $user->name,
    '%masq_as' => $new_user->name,
  )));
  drupal_set_message(t('Now masquerading as %masq_as.', array(
    '%masq_as' => $new_user->name,
  )));
  $user->masquerading = $new_user->uid;
  $user = $new_user;
  drupal_goto(referer_uri());
}