function masquerade_switch_user in Masquerade 6
Same name and namespace in other branches
- 5 masquerade.module \masquerade_switch_user()
- 7 masquerade.module \masquerade_switch_user()
Function that 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.
2 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.
File
- ./
masquerade.module, line 668 - masquerade.module
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(referer_uri());
}
$new_user = user_load(array(
'uid' => $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.
if (!user_access($perm) && !isset($_SESSION['masquerading']) && !db_result(db_query("SELECT TRUE FROM {masquerade_users} WHERE uid_from = %d AND uid_to = %d", $user->uid, $new_user->uid))) {
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('site_offline', 0) && !user_access('administer site configuration', $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' => 'administer site configuration',
'@site-maintenance' => url('admin/settings/site-maintenance'),
)));
return FALSE;
}
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', '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', $new_user),
)));
$user->masquerading = $new_user->uid;
$user = $new_user;
return TRUE;
}