You are here

function _sms_user_switch in SMS Framework 7

Internal function to swap user.

Safely Impersonating Another User. https://drupal.org/node/218104 Called once to login, and once to logout. Does not nest.

Parameters

object $account: The account object of the user to log in; or none to log out and restore the previous user and session.

2 calls to _sms_user_switch()
sms_user_register_new_user in modules/sms_user/sms_user.module
Registers a new user via SMS
sms_user_sms_incoming in modules/sms_user/sms_user.module
Implements hook_sms_incoming().

File

modules/sms_user/sms_user.module, line 1018
Provides integration between the SMS Framework and Drupal users.

Code

function _sms_user_switch($account = NULL) {
  global $user;
  $original_user =& drupal_static(__FUNCTION__ . '_user');
  $original_state =& drupal_static(__FUNCTION__ . '_state');
  if ($account) {
    $original_user = $user;
    $original_state = drupal_save_session();
    drupal_save_session(FALSE);
    $user = $account;
    $edit = array();
    user_module_invoke('login', $edit, $account);
    $username = $user->uid > 0 ? $user->name : variable_get('anonymous', t('Anonymous'));
    watchdog('sms_user', '%name was logged in using SMS.', array(
      '%name' => $username,
    ));
  }
  elseif (!empty($original_user)) {
    $user = $original_user;
    drupal_save_session($original_state);
    $username = $user->uid > 0 ? $user->name : variable_get('anonymous', t('Anonymous'));
    watchdog('sms_user', '%name was restored as logged in using SMS.', array(
      '%name' => $username,
    ));
  }
}