? .DS_Store ? .cache ? .git ? .project ? .settings ? empty ? logs ? includes/tests/file.test ? sites/all/modules ? sites/default/files ? sites/default/settings.php ? sites/default/test Index: modules/user/user.module =================================================================== RCS file: /cvs/drupal/drupal/modules/user/user.module,v retrieving revision 1.913 diff -u -p -r1.913 user.module --- modules/user/user.module 2 Aug 2008 20:08:24 -0000 1.913 +++ modules/user/user.module 6 Aug 2008 20:16:45 -0000 @@ -2410,3 +2410,54 @@ function _user_forms(&$edit, $account, $ return empty($groups) ? FALSE : $groups; } + +/** + * Set the current user stored in $GLOBALS['user']. + * + * When this function is called for the first time the user is saved, + * subsequent calls that set a user do not overwrite the value of the + * original user. The user is restored when this function is called with + * a NULL value. + * + * @param $new_user User to switch to, either UID or user object. If + * nothing is provided the user will be reset to the original. + * @return Current user object. + */ +function user_switch_user($new_user = NULL) { + global $user; + static $user_original; + + if (empty($new_user)) { + if (isset($user_original)) { + // Restore the original user. + $user = $user_original; + unset($user_original); + session_save_session(TRUE); + } + return $user; + } + + // Backup the user the first time this is called. + if (!isset($user_original)) { + $user_original = $user; + session_save_session(FALSE); + } + + if (is_int($new_user)) { + $user = user_load(array('uid' => $new_user)); + } + else { + $user = (object) $new_user; + } + + return $user; +} + +/** + * Restore the user that was originally loaded. + * + * @return Current user. + */ +function user_restore_user() { + return user_set_user(); +}