function _masquerade_drush_start_masquerade in Masquerade Extras 6.2
Same name and namespace in other branches
- 7.2 masquerade_drush/masquerade_drush.drush.inc \_masquerade_drush_start_masquerade()
Initializes a masquerade on the user's behalf. This is useful when developing and you want the user to start masquerading as soon as they login to the site.
Parameters
string $account: The (source) username.
string $target: The (target) username.
1 call to _masquerade_drush_start_masquerade()
- drush_masquerade_drush_masquerade in masquerade_drush/
masquerade_drush.drush.inc - Implements drush_HOOK_COMMAND().
File
- masquerade_drush/
masquerade_drush.drush.inc, line 253 - Provides some drush commands for masquerade.
Code
function _masquerade_drush_start_masquerade($account, $target) {
session_start();
$account = _masquerade_drush_get_user($account);
$target = _masquerade_drush_get_user($target);
// Ensure the user specified the account they want to begin the masquerade with.
if (empty($account)) {
return drush_log(dt("You must specify the source user. You can provide an email address, user ID, or username."), 'error');
}
// Ensure the user specified the account they want to masquerade as.
if (empty($target)) {
return drush_log(dt("You must specify the target user. You can provide an email address, user ID, or username."), 'error');
}
// Lookup the requested account's session.
$query_session = db_query('SELECT `sid`, `uid`
FROM {sessions} AS s
WHERE s.`uid` = %d', $account->uid);
$session = db_fetch_object($query_session);
// If the requested user doesn't have a session, this won't work...just exit.
if (empty($session)) {
return drush_log(dt("The requested masquerader is not currently logged into the site."), 'error');
}
// Take over our own session with the session ID of the real user.
session_id($session->sid);
// Change ownership of the session to the target user.
$query_session_update = db_query("UPDATE {sessions}\n SET `uid` = %d,\n `session` = '%s',\n `cache` = %d\n WHERE `uid` = %d\n LIMIT 1", $target->uid, serialize($_SESSION), 0, $account->uid);
// If no update was performed, there's something amiss.
if (0 == db_affected_rows($query_session_update)) {
return drush_log(dt('There was a problem initializing a masquerade for: "@name".', array(
'@name' => $account->name,
)), 'error');
}
// Update the masquerade table.
$query_new_masquerader = db_query("INSERT INTO {masquerade}\n (uid_from, uid_as, sid)\n VALUES (%d, %d, '%s');", $account->uid, $target->uid, $session->sid);
// Inform the user the masquerade has begun.
if (0 < db_affected_rows($query_new_masquerader)) {
return drush_log(dt('Initialized masquerade for "@user" as "@target"', array(
'@user' => $account->name,
'@target' => $target->name,
)), 'success');
}
// There was a problem initializing the masquerade.
return drush_log(dt('There was a problem initalizing the masquerade.'), 'error');
}