You are here

function _masquerade_drush_sessions in Masquerade Extras 7.2

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

Utility command to dump the active sessions table from the site.

Parameters

string $account: An account name, user ID, or email address to search for.

1 call to _masquerade_drush_sessions()
drush_masquerade_drush_sessions in masquerade_drush/masquerade_drush.drush.inc
Implements drush_HOOK_COMMAND().

File

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

Code

function _masquerade_drush_sessions($account = NULL) {

  // Grab all the sessions!
  $sessions = db_select('sessions', 's')
    ->fields('s')
    ->fields('u', array(
    'name',
  ));
  $sessions
    ->join('users', 'u', 'u.uid = s.uid');

  // If an account was provided, load it.
  $user = _masquerade_drush_get_user($account);

  // If the account was found, limit the results to just that account.
  if (!empty($user)) {
    $sessions
      ->condition('uid', $user->uid, '=');
  }
  else {
    if (empty($user) && !empty($account)) {
      return drush_log(sprintf('The account "%s" could not be found.', $account), 'error');
    }
  }

  // Execute the query.
  $sessions = $sessions
    ->execute()
    ->fetchAll();

  // If there are no active sessions, just quit.
  if (empty($account) && empty($sessions)) {
    return drush_log('No one is logged into the site right now.', 'ok');
  }
  else {
    if (!empty($user) && empty($sessions)) {
      return drush_log('The user "' . $user->name . '" is not logged in right now.', 'ok');
    }
  }

  // Dump a list of headers so the table actually makes sense.
  $list = array(
    array(
      'uid',
      'username',
      'hostname',
      'timestamp',
    ),
    array(
      '----------',
      '-----------',
      '---------------',
      '-----------',
    ),
  );
  foreach ($sessions as $s) {
    $list[] = array(
      $s->uid,
      $s->name,
      $s->hostname,
      $s->timestamp,
    );
  }
  drush_print_table($list, TRUE);
}