You are here

function _masquerade_drush_terminate_masquerade in Masquerade Extras 6.2

Same name and namespace in other branches
  1. 7.2 masquerade_drush/masquerade_drush.drush.inc \_masquerade_drush_terminate_masquerade()

Deletes active masquerades from the database. This is exceptionally useful when an active masquerade is causing a development problem and the user can't end the masquerade.

Parameters

string $account: The source account name whose masquerade(s) need to be stopped.

1 call to _masquerade_drush_terminate_masquerade()
drush_masquerade_drush_masquerade in masquerade_drush/masquerade_drush.drush.inc
Implements drush_HOOK_COMMAND().

File

masquerade_drush/masquerade_drush.drush.inc, line 199
Provides some drush commands for masquerade.

Code

function _masquerade_drush_terminate_masquerade($account) {

  // Find the requested user account.
  $account = _masquerade_drush_get_user($account);

  // Make sure the user provided exists.
  if (empty($account)) {
    return drush_print(dt('You must specify a valid user account. You can provide an email address, user ID, or username.'));
  }

  // Lookup the masquerades the current user has active.
  $query_active_masquerades = db_query("SELECT `sid`, `uid_as`, `uid_from`\n       FROM {masquerade}\n      WHERE uid_from = %d", $account->uid);

  // Loop over each masquerade the user currently has active.
  while (FALSE !== ($masquerade = db_fetch_object($query_active_masquerades))) {

    // Restore the session owner to its originator.
    $upd = db_query("UPDATE {sessions}\n          SET `uid` = %d\n        WHERE `sid` = '%s'\n        LIMIT 1;", $masquerade->uid_from, $masquerade->sid);
  }

  // Terminate all masquerades in the masquerade table from this user.
  $query_num_ended = db_query("DELETE FROM {masquerade}\n       WHERE `uid_from` = %d", $account->uid);

  // Alert the user to any # of masquerades we terminated.
  if (db_affected_rows($query_num_ended)) {
    return drush_log(dt('Ended @count masquerades from account @user', array(
      '@count' => $num_ended,
      '@user' => $account->name,
    )), 'success');
  }
  return drush_log(dt('No active masquerades for "@user"', array(
    '@user' => $account->name,
  )), 'warning');
}