From a108bf0f76e1c28788461ac7616daf49c07279ad Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Fri, 5 Jun 2015 22:03:12 +0100
Subject: [PATCH] Issue #2478119 by znerol, andypost: Replace references to
 Session Manager with references to Session

---
 core/includes/errors.inc                      |  2 +-
 .../Drupal/Core/Access/CsrfTokenGenerator.php |  2 +-
 core/lib/Drupal/Core/DrupalKernel.php         | 20 ++++++++--------
 .../simpletest/src/BrowserTestBase.php        | 12 ++++++----
 core/modules/simpletest/src/WebTestBase.php   | 20 ++++++----------
 core/modules/system/core.api.php              |  6 ++---
 .../src/Controller/SessionTestController.php  | 23 +++++++++++++------
 7 files changed, 44 insertions(+), 41 deletions(-)

diff --git a/core/includes/errors.inc b/core/includes/errors.inc
index 5e38a973cca8..6933dc8690c9 100644
--- a/core/includes/errors.inc
+++ b/core/includes/errors.inc
@@ -220,7 +220,7 @@ function _drupal_log_error($error, $fatal = FALSE) {
         // Generate a backtrace containing only scalar argument values.
         $message .= '<pre class="backtrace">' . Error::formatBacktrace($backtrace) . '</pre>';
       }
-      if (\Drupal::hasService('session_manager')) {
+      if (\Drupal::hasService('session')) {
         // Message display is dependent on sessions being available.
         drupal_set_message(SafeMarkup::set($message), $class, TRUE);
       }
diff --git a/core/lib/Drupal/Core/Access/CsrfTokenGenerator.php b/core/lib/Drupal/Core/Access/CsrfTokenGenerator.php
index 083dd6cff4fc..c0b111566eb1 100644
--- a/core/lib/Drupal/Core/Access/CsrfTokenGenerator.php
+++ b/core/lib/Drupal/Core/Access/CsrfTokenGenerator.php
@@ -63,7 +63,7 @@ public function __construct(PrivateKey $private_key, MetadataBag $session_metada
    *   'drupal_private_key' configuration variable.
    *
    * @see \Drupal\Core\Site\Settings::getHashSalt()
-   * @see \Drupal\Core\Session\SessionManager::start()
+   * @see \Symfony\Component\HttpFoundation\Session\SessionInterface::start()
    */
   public function get($value = '') {
     $seed = $this->sessionMetadata->getCsrfTokenSeed();
diff --git a/core/lib/Drupal/Core/DrupalKernel.php b/core/lib/Drupal/Core/DrupalKernel.php
index 8202cd6077c4..d21a8fa3f618 100644
--- a/core/lib/Drupal/Core/DrupalKernel.php
+++ b/core/lib/Drupal/Core/DrupalKernel.php
@@ -746,21 +746,21 @@ protected function getKernelParameters() {
    */
   protected function initializeContainer($rebuild = FALSE) {
     $this->containerNeedsDumping = FALSE;
-    $session_manager_started = FALSE;
+    $session_started = FALSE;
     if (isset($this->container)) {
       // Save the id of the currently logged in user.
       if ($this->container->initialized('current_user')) {
         $current_user_id = $this->container->get('current_user')->id();
       }
 
-      // If there is a session manager, close and save the session.
-      if ($this->container->initialized('session_manager')) {
-        $session_manager = $this->container->get('session_manager');
-        if ($session_manager->isStarted()) {
-          $session_manager_started = TRUE;
-          $session_manager->save();
+      // If there is a session, close and save it.
+      if ($this->container->initialized('session')) {
+        $session = $this->container->get('session');
+        if ($session->isStarted()) {
+          $session_started = TRUE;
+          $session->save();
         }
-        unset($session_manager);
+        unset($session);
       }
     }
 
@@ -786,8 +786,8 @@ protected function initializeContainer($rebuild = FALSE) {
     $this->attachSynthetic($container);
 
     $this->container = $container;
-    if ($session_manager_started) {
-      $this->container->get('session_manager')->start();
+    if ($session_started) {
+      $this->container->get('session')->start();
     }
 
     // The request stack is preserved across container rebuilds. Reinject the
diff --git a/core/modules/simpletest/src/BrowserTestBase.php b/core/modules/simpletest/src/BrowserTestBase.php
index 75a548442dbe..06ae369b9da2 100644
--- a/core/modules/simpletest/src/BrowserTestBase.php
+++ b/core/modules/simpletest/src/BrowserTestBase.php
@@ -1326,12 +1326,14 @@ protected function refreshVariables() {
    *   Return TRUE if the user is logged in, FALSE otherwise.
    */
   protected function drupalUserIsLoggedIn(UserInterface $account) {
-    if (!isset($account->sessionId)) {
-      return FALSE;
+    $logged_in = FALSE;
+
+    if (isset($account->sessionId)) {
+      $session_handler = $this->container->get('session_handler.storage');
+      $logged_in = (bool) $session_handler->read($account->sessionId);
     }
-    // The session ID is hashed before being stored in the database.
-    // @see \Drupal\Core\Session\SessionHandler::read()
-    return (bool) db_query("SELECT sid FROM {users_field_data} u INNER JOIN {sessions} s ON u.uid = s.uid AND u.default_langcode = 1 WHERE s.sid = :sid", array(':sid' => Crypt::hashBase64($account->sessionId)))->fetchField();
+
+    return $logged_in;
   }
 
 }
diff --git a/core/modules/simpletest/src/WebTestBase.php b/core/modules/simpletest/src/WebTestBase.php
index a5dd73ece6e1..2cf859af2b53 100644
--- a/core/modules/simpletest/src/WebTestBase.php
+++ b/core/modules/simpletest/src/WebTestBase.php
@@ -573,19 +573,13 @@ protected function drupalLogin(AccountInterface $account) {
    *   The user account object to check.
    */
   protected function drupalUserIsLoggedIn($account) {
-    if (!isset($account->session_id)) {
-      return FALSE;
-    }
-    $session_id = $account->session_id;
-    $request_stack = $this->container->get('request_stack');
-    $request = $request_stack->getCurrentRequest();
-    $cookies = $request->cookies->all();
-    foreach ($this->cookies as $name => $value) {
-      $cookies[$name] = $value['value'];
-    }
-    $request_stack->push($request->duplicate(NULL, NULL, NULL, $cookies));
-    $logged_in = (bool) $this->container->get('session_manager')->getSaveHandler()->read($session_id);
-    $request_stack->pop();
+    $logged_in = FALSE;
+
+    if (isset($account->session_id)) {
+      $session_handler = $this->container->get('session_handler.storage');
+      $logged_in = (bool) $session_handler->read($account->session_id);
+    }
+
     return $logged_in;
   }
 
diff --git a/core/modules/system/core.api.php b/core/modules/system/core.api.php
index a156905f4d7a..145d7664d089 100644
--- a/core/modules/system/core.api.php
+++ b/core/modules/system/core.api.php
@@ -972,10 +972,8 @@
  * - Session: Information about individual users' interactions with the site,
  *   such as whether they are logged in. This is really "state" information, but
  *   it is not stored the same way so it's a separate type here. Session
- *   information is managed via the session_manager service in Drupal, which
- *   implements \Drupal\Core\Session\SessionManagerInterface. See the
- *   @link container Services topic @endlink for more information about
- *   services.
+ *   information is available from the Request object. The session implements
+ *   \Symfony\Component\HttpFoundation\Session\SessionInterface.
  * - State: Information of a temporary nature, generally machine-generated and
  *   not human-edited, about the current state of your site. Examples: the time
  *   when Cron was last run, whether node access permissions need rebuilding,
diff --git a/core/modules/system/tests/modules/session_test/src/Controller/SessionTestController.php b/core/modules/system/tests/modules/session_test/src/Controller/SessionTestController.php
index b17d9a0684cc..510b36f9994c 100644
--- a/core/modules/system/tests/modules/session_test/src/Controller/SessionTestController.php
+++ b/core/modules/system/tests/modules/session_test/src/Controller/SessionTestController.php
@@ -33,11 +33,14 @@ public function get() {
   /**
    * Prints the stored session value to the screen.
    *
+   * @param \Symfony\Component\HttpFoundation\Request $request
+   *   The incoming request.
+   *
    * @return string
    *   A notification message.
    */
-  public function getFromSessionObject() {
-    $value = \Drupal::request()->getSession()->get("session_test_key");
+  public function getFromSessionObject(Request $request) {
+    $value = $request->getSession()->get("session_test_key");
     return empty($value)
       ? []
       : ['#markup' => $this->t('The current value of the stored session variable is: %val', array('%val' => $value))];
@@ -46,15 +49,18 @@ public function getFromSessionObject() {
   /**
    * Print the current session ID.
    *
+   * @param \Symfony\Component\HttpFoundation\Request $request
+   *   The incoming request.
+   *
    * @return string
    *   A notification message with session ID.
    */
-  public function getId() {
+  public function getId(Request $request) {
     // Set a value in $_SESSION, so that SessionManager::save() will start
     // a session.
     $_SESSION['test'] = 'test';
 
-    \Drupal::service('session_manager')->save();
+    $request->getSession()->save();
 
     return ['#markup' => 'session_id:' . session_id() . "\n"];
   }
@@ -142,19 +148,22 @@ public function isLoggedIn() {
   /**
    * Returns the trace recorded by test proxy session handlers as JSON.
    *
+   * @param \Symfony\Component\HttpFoundation\Request $request
+   *   The incoming request.
+   *
    * @return \Symfony\Component\HttpFoundation\JsonResponse
    *   The response.
    */
-  public function traceHandler() {
+  public function traceHandler(Request $request) {
     // Start a session if necessary, set a value and then save and close it.
-    \Drupal::service('session_manager')->start();
+    $request->getSession()->start();
     if (empty($_SESSION['trace-handler'])) {
       $_SESSION['trace-handler'] = 1;
     }
     else {
       $_SESSION['trace-handler']++;
     }
-    \Drupal::service('session_manager')->save();
+    $request->getSession()->save();
 
     // Collect traces and return them in JSON format.
     $trace = \Drupal::service('session_test.session_handler_proxy_trace')->getArrayCopy();
-- 
GitLab