From 689cde612200a6cb9f231f6c24f5f87cabc66e16 Mon Sep 17 00:00:00 2001
From: Fabian Franz <github@fabian-franz.de>
Date: Fri, 9 Nov 2012 06:58:04 +0100
Subject: [PATCH] Issue #782672 by sun, David_Rothstein, jhedstrom, franz: Loosen the coupling between the user module and the installer/maintenance theme.

---
 core/includes/install.core.inc   |    2 --
 core/includes/theme.inc          |   21 ++++++---------------
 core/modules/user/user.module    |   25 +++++++++++++++++++++++++
 core/modules/user/user.pages.inc |    3 +++
 4 files changed, 34 insertions(+), 17 deletions(-)

diff --git a/core/includes/install.core.inc b/core/includes/install.core.inc
index 2b117bc..1790594 100644
--- a/core/includes/install.core.inc
+++ b/core/includes/install.core.inc
@@ -331,10 +331,8 @@ function install_begin_request(&$install_state) {
   require_once DRUPAL_ROOT . '/core/includes/ajax.inc';
   // Override the module list with a minimal set of modules.
   $module_list['system']['filename'] = 'core/modules/system/system.module';
-  $module_list['user']['filename']   = 'core/modules/user/user.module';
   module_list(NULL, $module_list);
   drupal_load('module', 'system');
-  drupal_load('module', 'user');
 
   // Load the cache infrastructure using a "fake" cache implementation that
   // does not attempt to write to the database. We need this during the initial
diff --git a/core/includes/theme.inc b/core/includes/theme.inc
index d2fe902..d30927b 100644
--- a/core/includes/theme.inc
+++ b/core/includes/theme.inc
@@ -2444,10 +2444,7 @@ function template_preprocess(&$variables, $hook) {
     $drupal_static_fast['default_variables'] = &drupal_static(__FUNCTION__);
   }
   $default_variables = &$drupal_static_fast['default_variables'];
-  // Global $user object shouldn't change during a page request once rendering
-  // has started, but if there's an edge case where it does, re-fetch the
-  // variables appropriate for the new user.
-  if (!isset($default_variables) || ($user !== $default_variables['user'])) {
+  if (!isset($default_variables)) {
     $default_variables = _template_preprocess_default_variables();
   }
   if (!isset($default_attributes)) {
@@ -2461,32 +2458,26 @@ function template_preprocess(&$variables, $hook) {
 
   // Initialize html class attribute for the current hook.
   $variables['attributes']['class'][] = drupal_html_class($hook);
+
+  // Allow other modules / themes to alter these variables
+  drupal_alter('template_default_variables', $variables);
 }
 
 /**
  * Returns hook-independent variables to template_preprocess().
  */
 function _template_preprocess_default_variables() {
-  global $user;
-
   // Variables that don't depend on a database connection.
   $variables = array(
     'title_prefix' => array(),
     'title_suffix' => array(),
-    'user' => $user,
     'db_is_active' => !defined('MAINTENANCE_MODE'),
+    // User module overrides these when it is loaded.
+    'user' => drupal_anonymous_user(),
     'is_admin' => FALSE,
     'logged_in' => FALSE,
   );
 
-  // The user object has no uid property when the database does not exist during
-  // install. The user_access() check deals with issues when in maintenance mode
-  // as uid is set but the user.module has not been included.
-  if (isset($user->uid) && function_exists('user_access')) {
-    $variables['is_admin'] = user_access('access administration pages');
-    $variables['logged_in'] = ($user->uid > 0);
-  }
-
   // drupal_is_front_page() might throw an exception.
   try {
     $variables['is_front'] = drupal_is_front_page();
diff --git a/core/modules/user/user.module b/core/modules/user/user.module
index f1d3254..4906b5c 100644
--- a/core/modules/user/user.module
+++ b/core/modules/user/user.module
@@ -886,6 +886,25 @@ function user_format_name($account) {
 }
 
 /**
+ * Alter preprocesses variables for all templates.
+ *
+ * @see template_preprocess()
+ * @see theme()
+ */
+function user_template_default_variables_alter(&$variables) {
+  global $user;
+
+  $variables['user'] = clone $user;
+  // Remove password, sid and ssid as themers don't need them
+  unset($variables['user']->pass);
+  unset($variables['user']->sid);
+  unset($variables['user']->ssid);
+
+  $variables['is_admin'] = user_access('access administration pages');
+  $variables['logged_in'] = ($user->uid > 0);
+}
+
+/**
  * Process variables for user-picture.tpl.php.
  *
  * The $variables array contains the following arguments:
@@ -1728,6 +1747,9 @@ function user_login_finalize(&$edit = array()) {
   // or incorrectly does a redirect which would leave the old session in place.
   drupal_session_regenerate();
 
+  // Clear user object in default variables
+  drupal_static_reset('template_preprocess');
+
   module_invoke_all('user_login', $edit, $user);
 }
 
@@ -1944,6 +1966,9 @@ function _user_cancel($edit, $account, $method) {
   if ($account->uid == $user->uid) {
     // Destroy the current session, and reset $user to the anonymous user.
     session_destroy();
+
+    // Clear user object in default variables
+    drupal_static_reset('template_preprocess');
   }
 
   // Clear the cache for anonymous users.
diff --git a/core/modules/user/user.pages.inc b/core/modules/user/user.pages.inc
index 20af863..7e10a5a 100644
--- a/core/modules/user/user.pages.inc
+++ b/core/modules/user/user.pages.inc
@@ -180,6 +180,9 @@ function user_logout() {
   // Destroy the current session, and reset $user to the anonymous user.
   session_destroy();
 
+  // Clear user object in default variables
+  drupal_static_reset('template_preprocess');
+
   drupal_goto();
 }
 
-- 
1.7.4.1

