You are here

function masquerade_switch_user in Masquerade 7

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

Allows a user with the right permissions to become the selected user.

Parameters

$uid: The user ID to switch to.

Return value

TRUE if the user was sucessfully switched, or FALSE if there was an error.

3 calls to masquerade_switch_user()
masquerade_block_1_submit in ./masquerade.module
Masquerade block form submission.
masquerade_switch_user_page in ./masquerade.module
Page callback to switch users.
masquerade_user_operations_masquerade in ./masquerade.module
Callback for user operation.

File

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

Code

function masquerade_switch_user($uid) {
  global $user;
  if (!is_numeric($uid)) {
    drupal_set_message(t('A user id was not correctly passed to the switching function.'));
    watchdog('masquerade', 'The user id provided to switch users was not numeric.', NULL, WATCHDOG_ERROR);
    return drupal_goto($_SERVER['HTTP_REFERER']);
  }
  $new_user = user_load($uid);
  $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.
  $results = db_query_range('SELECT 1 FROM {masquerade_users} WHERE uid_from = :uid_from AND uid_to = :uid_to', 0, 1, array(
    ':uid_from' => $user->uid,
    ':uid_to' => $new_user->uid,
  ));
  if (!user_access($perm) && !isset($_SESSION['masquerading']) && !$results
    ->fetchField()) {
    watchdog('masquerade', 'This user requires administrative permissions to switch to the user %user.', array(
      '%user' => $new_user->name,
    ), WATCHDOG_ERROR);
    return FALSE;
  }
  if ($user->uid == $uid || isset($user->masquerading)) {
    watchdog('masquerade', 'This user is already %user.', array(
      '%user' => $new_user->name,
    ), WATCHDOG_ERROR);
    return FALSE;
  }
  if (variable_get('maintenance_mode', 0) && !user_access('access site in maintenance mode', $new_user)) {
    drupal_set_message(t('It is not possible to masquerade in off-line mode as %user does not have the %config-perm permission. Please <a href="@site-maintenance">set the site status</a> to "online" to masquerade as %user.', array(
      '%user' => $new_user->name,
      '%config-perm' => 'use the site in maintenance mode',
      '@site-maintenance' => url('admin/settings/site-maintenance'),
    )));
    return FALSE;
  }

  // Call logout hooks when switching from original user.
  masquerade_user_logout($user);
  drupal_session_regenerate();
  $query = db_insert('masquerade');
  $query
    ->fields(array(
    'uid_from' => $user->uid,
    'uid_as' => $new_user->uid,
    'sid' => session_id(),
  ));
  $query
    ->execute();

  // switch user
  watchdog('masquerade', 'User %user now masquerading as %masq_as.', array(
    '%user' => $user->name,
    '%masq_as' => $new_user->name ? $new_user->name : variable_get('anonymous', t('Anonymous')),
  ), WATCHDOG_INFO);
  drupal_set_message(t('You are now masquerading as !masq_as.', array(
    '!masq_as' => theme('username', array(
      'account' => $new_user,
    )),
  )));
  $user->masquerading = $new_user->uid;
  $user = $new_user;

  // Call all login hooks when switching to masquerading user.
  $edit = array();

  // Passed by reference.
  user_module_invoke('login', $edit, $user);
  return TRUE;
}