Index: modules/user/user.module
===================================================================
--- modules/user/user.module	(revision 89)
+++ modules/user/user.module	(working copy)
@@ -2447,3 +2447,53 @@
   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);
+      drupal_save_session(TRUE);
+    }
+    return $user;
+  }
+
+  // Backup the user the first time this is called.
+  if (!isset($user_original)) {
+    $user_original = $user;
+    drupal_save_session(FALSE);
+  }
+
+  if (is_numeric($new_user)) {
+    $user = user_load(array('uid' => $new_user));
+  }
+  else {
+    $user = is_object($new_user) ? $new_user : (object) $new_user;
+  }
+
+  return $user;
+}
+
+/**
+ * Restore the user that was originally loaded.
+ *
+ * @return Current user.
+ */
+function user_restore_user() {
+  return user_switch_user();
+}
Index: modules/user/user.test
===================================================================
--- modules/user/user.test	(revision 89)
+++ modules/user/user.test	(working copy)
@@ -570,3 +570,39 @@
     $this->assertRaw($this->unprivileged_user->name, t('User name found in autocompletion results.'));
   }
 }
+
+
+class UserSwitchUserTestCase extends DrupalWebTestCase {
+  /**
+   * Implementation of getInfo().
+   */
+  function getInfo() {
+    return array(
+      'name' => t('Switch User'),
+      'description' => t('Temporarily switch the current user, and then restore the original user.'),
+      'group' => t('User'),
+    );
+  }
+
+  function setUp() {
+    parent::setUp();
+
+    $this->original_user = $this->drupalCreateUser();
+  }
+
+  function testUserSwitchUser() {
+    global $user;
+
+    $this->drupalLogin($this->original_user);
+    $this->assertEqual($user->uid, $this->original_user->uid, t('Using original user. This user: !this-uid, original user: !orig-uid.', array('!this-uid' => $user->uid, '!orig-uid' => $this->original_user->uid)));
+
+    // Create a new user and switch to it.
+    $new_user = $this->drupalCreateUser();
+    user_switch_user($new_user->uid);
+
+    $this->assertEqual($user->uid, $new_user->uid, t('Switched to new user. This user: !this-uid, new user: !orig-uid.', array('!this-uid' => $user->uid, '!orig-uid' => $new_user->uid)));
+
+    user_restore_user();
+    $this->assertEqual($user->uid, $this->original_user->uid, t('Switched back to original user. This user: !this-uid, original user: !orig-uid.', array('!this-uid' => $user->uid, '!orig-uid' => $this->original_user->uid)));
+  }
+}
