Commit 195503e2 authored by alexpott's avatar alexpott

Issue #2338571 by znerol: Remove SessionManager::startLazy().

parent 2a0254f9
...@@ -49,7 +49,7 @@ ...@@ -49,7 +49,7 @@
* TRUE if the current user can run authorize.php, and FALSE if not. * TRUE if the current user can run authorize.php, and FALSE if not.
*/ */
function authorize_access_allowed() { function authorize_access_allowed() {
\Drupal::service('session_manager')->startLazy(); \Drupal::service('session_manager')->start();
return Settings::get('allow_authorize_operations', TRUE) && \Drupal::currentUser()->hasPermission('administer software updates'); return Settings::get('allow_authorize_operations', TRUE) && \Drupal::currentUser()->hasPermission('administer software updates');
} }
......
...@@ -1453,7 +1453,7 @@ function install_load_profile(&$install_state) { ...@@ -1453,7 +1453,7 @@ function install_load_profile(&$install_state) {
* An array of information about the current installation state. * An array of information about the current installation state.
*/ */
function install_bootstrap_full() { function install_bootstrap_full() {
\Drupal::service('session_manager')->startLazy(); \Drupal::service('session_manager')->start();
} }
/** /**
......
...@@ -47,7 +47,7 @@ public function applies(Request $request) { ...@@ -47,7 +47,7 @@ public function applies(Request $request) {
public function authenticate(Request $request) { public function authenticate(Request $request) {
// Global $user is deprecated, but the session system is still based on it. // Global $user is deprecated, but the session system is still based on it.
global $user; global $user;
$this->sessionManager->startLazy(); $this->sessionManager->start();
if ($this->sessionManager->isStarted()) { if ($this->sessionManager->isStarted()) {
return $user; return $user;
} }
......
...@@ -672,16 +672,13 @@ protected function getKernelParameters() { ...@@ -672,16 +672,13 @@ protected function getKernelParameters() {
*/ */
protected function initializeContainer($rebuild = FALSE) { protected function initializeContainer($rebuild = FALSE) {
$this->containerNeedsDumping = FALSE; $this->containerNeedsDumping = FALSE;
$session_manager_state = 0; $session_manager_started = FALSE;
if (isset($this->container)) { if (isset($this->container)) {
// If there is a session manager, close and save the session. // If there is a session manager, close and save the session.
if ($this->container->initialized('session_manager')) { if ($this->container->initialized('session_manager')) {
$session_manager = $this->container->get('session_manager'); $session_manager = $this->container->get('session_manager');
if ($session_manager->isStartedLazy()) {
$session_manager_state |= 0x1;
}
if ($session_manager->isStarted()) { if ($session_manager->isStarted()) {
$session_manager_state |= 0x2; $session_manager_started = TRUE;
$session_manager->save(); $session_manager->save();
} }
unset($session_manager); unset($session_manager);
...@@ -712,10 +709,7 @@ protected function initializeContainer($rebuild = FALSE) { ...@@ -712,10 +709,7 @@ protected function initializeContainer($rebuild = FALSE) {
$this->attachSynthetic($container); $this->attachSynthetic($container);
$this->container = $container; $this->container = $container;
if ($session_manager_state & 0x1) { if ($session_manager_started) {
$this->container->get('session_manager')->startLazy();
}
if ($session_manager_state & 0x2) {
$this->container->get('session_manager')->start(); $this->container->get('session_manager')->start();
} }
\Drupal::setContainer($this->container); \Drupal::setContainer($this->container);
......
...@@ -114,7 +114,7 @@ public function __construct(RequestStack $request_stack, Connection $connection, ...@@ -114,7 +114,7 @@ public function __construct(RequestStack $request_stack, Connection $connection,
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function startLazy() { public function start() {
global $user; global $user;
if (($this->started || $this->startedLazy) && !$this->closed) { if (($this->started || $this->startedLazy) && !$this->closed) {
...@@ -129,53 +129,66 @@ public function startLazy() { ...@@ -129,53 +129,66 @@ public function startLazy() {
// session is only started on demand in save(), making // session is only started on demand in save(), making
// anonymous users not use a session cookie unless something is stored in // anonymous users not use a session cookie unless something is stored in
// $_SESSION. This allows HTTP proxies to cache anonymous pageviews. // $_SESSION. This allows HTTP proxies to cache anonymous pageviews.
$result = $this->start(); $result = $this->startNow();
if ($user->isAuthenticated() || !$this->isSessionObsolete()) { if ($user->isAuthenticated() || !$this->isSessionObsolete()) {
drupal_page_is_cacheable(FALSE); drupal_page_is_cacheable(FALSE);
} }
} }
else {
// Set a session identifier for this request. This is necessary because if (empty($result)) {
// we lazily start sessions at the end of this request, and some
// processes (like \Drupal::csrfToken()) needs to know the future
// session ID in advance.
$user = new AnonymousUserSession(); $user = new AnonymousUserSession();
// Randomly generate a session identifier for this request. This is
// necessary because \Drupal\user\TempStoreFactory::get() wants to know
// the future session ID of a lazily started session in advance.
//
// @todo: With current versions of PHP there is little reason to generate
// the session id from within application code. Consider using the
// default php session id instead of generating a custom one:
// https://www.drupal.org/node/2238561
$this->setId(Crypt::randomBytesBase64()); $this->setId(Crypt::randomBytesBase64());
if ($is_https && $this->isMixedMode()) { if ($is_https && $this->isMixedMode()) {
$session_id = Crypt::randomBytesBase64(); $session_id = Crypt::randomBytesBase64();
$cookies->set($insecure_session_name, $session_id); $cookies->set($insecure_session_name, $session_id);
} }
// Initialize the session global and attach the Symfony session bags.
$_SESSION = array();
$this->loadSession();
// NativeSessionStorage::loadSession() sets started to TRUE, reset it to
// FALSE here.
$this->started = FALSE;
$this->startedLazy = TRUE;
$result = FALSE; $result = FALSE;
} }
date_default_timezone_set(drupal_get_user_timezone()); date_default_timezone_set(drupal_get_user_timezone());
$this->startedLazy = TRUE;
return $result; return $result;
} }
/** /**
* {@inheritdoc} * Forcibly start a PHP session.
*/ *
public function isStartedLazy() { * @return boolean
return $this->startedLazy; * TRUE if the session is started.
}
/**
* {@inheritdoc}
*/ */
public function start() { protected function startNow() {
if (!$this->isEnabled() || $this->isCli()) { if (!$this->isEnabled() || $this->isCli()) {
return; return FALSE;
}
if ($this->startedLazy) {
// Save current session data before starting it, as PHP will destroy it.
$session_data = $_SESSION;
} }
// Save current session data before starting it, as PHP will destroy it.
$session_data = isset($_SESSION) ? $_SESSION : NULL;
$result = parent::start(); $result = parent::start();
// Restore session data. // Restore session data.
if (!empty($session_data)) { if ($this->startedLazy) {
$_SESSION += $session_data; $_SESSION = $session_data;
} }
return $result; return $result;
...@@ -203,7 +216,7 @@ public function save() { ...@@ -203,7 +216,7 @@ public function save() {
// There is session data to store. Start the session if it is not already // There is session data to store. Start the session if it is not already
// started. // started.
if (!$this->getSaveHandler()->isActive()) { if (!$this->getSaveHandler()->isActive()) {
$this->start(); $this->startNow();
if ($this->requestStack->getCurrentRequest()->isSecure() && $this->isMixedMode()) { if ($this->requestStack->getCurrentRequest()->isSecure() && $this->isMixedMode()) {
$insecure_session_name = $this->getInsecureName(); $insecure_session_name = $this->getInsecureName();
$params = session_get_cookie_params(); $params = session_get_cookie_params();
...@@ -282,7 +295,7 @@ public function regenerate($destroy = FALSE, $lifetime = NULL) { ...@@ -282,7 +295,7 @@ public function regenerate($destroy = FALSE, $lifetime = NULL) {
// Preserve the logged in user, as it will be reset to anonymous // Preserve the logged in user, as it will be reset to anonymous
// by \Drupal\Core\Session\SessionHandler::read(). // by \Drupal\Core\Session\SessionHandler::read().
$account = $user; $account = $user;
$this->start(); $this->startNow();
$user = $account; $user = $account;
} }
date_default_timezone_set(drupal_get_user_timezone()); date_default_timezone_set(drupal_get_user_timezone());
......
...@@ -14,22 +14,6 @@ ...@@ -14,22 +14,6 @@
*/ */
interface SessionManagerInterface extends SessionStorageInterface { interface SessionManagerInterface extends SessionStorageInterface {
/**
* Starts a session if appropriate cookies are on the request.
*
* @return bool
* TRUE if the session was started.
*/
public function startLazy();
/**
* Determines whether the session was started lazily.
*
* @return bool
* TRUE if the session was started lazily.
*/
public function isStartedLazy();
/** /**
* Ends a specific user's session(s). * Ends a specific user's session(s).
* *
......
...@@ -55,7 +55,7 @@ protected function setUp() { ...@@ -55,7 +55,7 @@ protected function setUp() {
* Checks the order of CRUD hook execution messages. * Checks the order of CRUD hook execution messages.
* *
* entity_crud_hook_test.module implements all core entity CRUD hooks and * entity_crud_hook_test.module implements all core entity CRUD hooks and
* stores a message for each in $_SESSION['entity_crud_hook_test']. * stores a message for each in $GLOBALS['entity_crud_hook_test'].
* *
* @param $messages * @param $messages
* An array of plain-text messages in the order they should appear. * An array of plain-text messages in the order they should appear.
...@@ -64,7 +64,7 @@ protected function assertHookMessageOrder($messages) { ...@@ -64,7 +64,7 @@ protected function assertHookMessageOrder($messages) {
$positions = array(); $positions = array();
foreach ($messages as $message) { foreach ($messages as $message) {
// Verify that each message is found and record its position. // Verify that each message is found and record its position.
$position = array_search($message, $_SESSION['entity_crud_hook_test']); $position = array_search($message, $GLOBALS['entity_crud_hook_test']);
if ($this->assertTrue($position !== FALSE, $message)) { if ($this->assertTrue($position !== FALSE, $message)) {
$positions[] = $position; $positions[] = $position;
} }
...@@ -91,7 +91,7 @@ public function testBlockHooks() { ...@@ -91,7 +91,7 @@ public function testBlockHooks() {
'entity_crud_hook_test_entity_create called for type block', 'entity_crud_hook_test_entity_create called for type block',
)); ));
$_SESSION['entity_crud_hook_test'] = array(); $GLOBALS['entity_crud_hook_test'] = array();
$entity->save(); $entity->save();
$this->assertHookMessageOrder(array( $this->assertHookMessageOrder(array(
...@@ -101,7 +101,7 @@ public function testBlockHooks() { ...@@ -101,7 +101,7 @@ public function testBlockHooks() {
'entity_crud_hook_test_entity_insert called for type block', 'entity_crud_hook_test_entity_insert called for type block',
)); ));
$_SESSION['entity_crud_hook_test'] = array(); $GLOBALS['entity_crud_hook_test'] = array();
$entity = Block::load($entity->id()); $entity = Block::load($entity->id());
$this->assertHookMessageOrder(array( $this->assertHookMessageOrder(array(
...@@ -109,7 +109,7 @@ public function testBlockHooks() { ...@@ -109,7 +109,7 @@ public function testBlockHooks() {
'entity_crud_hook_test_block_load called', 'entity_crud_hook_test_block_load called',
)); ));
$_SESSION['entity_crud_hook_test'] = array(); $GLOBALS['entity_crud_hook_test'] = array();
$entity->label = 'New label'; $entity->label = 'New label';
$entity->save(); $entity->save();
...@@ -120,7 +120,7 @@ public function testBlockHooks() { ...@@ -120,7 +120,7 @@ public function testBlockHooks() {
'entity_crud_hook_test_entity_update called for type block', 'entity_crud_hook_test_entity_update called for type block',
)); ));
$_SESSION['entity_crud_hook_test'] = array(); $GLOBALS['entity_crud_hook_test'] = array();
$entity->delete(); $entity->delete();
$this->assertHookMessageOrder(array( $this->assertHookMessageOrder(array(
...@@ -155,7 +155,7 @@ public function testCommentHooks() { ...@@ -155,7 +155,7 @@ public function testCommentHooks() {
)); ));
$node->save(); $node->save();
$nid = $node->id(); $nid = $node->id();
$_SESSION['entity_crud_hook_test'] = array(); $GLOBALS['entity_crud_hook_test'] = array();
$comment = entity_create('comment', array( $comment = entity_create('comment', array(
'cid' => NULL, 'cid' => NULL,
...@@ -176,7 +176,7 @@ public function testCommentHooks() { ...@@ -176,7 +176,7 @@ public function testCommentHooks() {
'entity_crud_hook_test_entity_create called for type comment', 'entity_crud_hook_test_entity_create called for type comment',
)); ));
$_SESSION['entity_crud_hook_test'] = array(); $GLOBALS['entity_crud_hook_test'] = array();
$comment->save(); $comment->save();
$this->assertHookMessageOrder(array( $this->assertHookMessageOrder(array(
...@@ -186,7 +186,7 @@ public function testCommentHooks() { ...@@ -186,7 +186,7 @@ public function testCommentHooks() {
'entity_crud_hook_test_entity_insert called for type comment', 'entity_crud_hook_test_entity_insert called for type comment',
)); ));
$_SESSION['entity_crud_hook_test'] = array(); $GLOBALS['entity_crud_hook_test'] = array();
$comment = Comment::load($comment->id()); $comment = Comment::load($comment->id());
$this->assertHookMessageOrder(array( $this->assertHookMessageOrder(array(
...@@ -194,7 +194,7 @@ public function testCommentHooks() { ...@@ -194,7 +194,7 @@ public function testCommentHooks() {
'entity_crud_hook_test_comment_load called', 'entity_crud_hook_test_comment_load called',
)); ));
$_SESSION['entity_crud_hook_test'] = array(); $GLOBALS['entity_crud_hook_test'] = array();
$comment->setSubject('New subject'); $comment->setSubject('New subject');
$comment->save(); $comment->save();
...@@ -205,7 +205,7 @@ public function testCommentHooks() { ...@@ -205,7 +205,7 @@ public function testCommentHooks() {
'entity_crud_hook_test_entity_update called for type comment', 'entity_crud_hook_test_entity_update called for type comment',
)); ));
$_SESSION['entity_crud_hook_test'] = array(); $GLOBALS['entity_crud_hook_test'] = array();
$comment->delete(); $comment->delete();
$this->assertHookMessageOrder(array( $this->assertHookMessageOrder(array(
...@@ -241,7 +241,7 @@ public function testFileHooks() { ...@@ -241,7 +241,7 @@ public function testFileHooks() {
'entity_crud_hook_test_entity_create called for type file', 'entity_crud_hook_test_entity_create called for type file',
)); ));
$_SESSION['entity_crud_hook_test'] = array(); $GLOBALS['entity_crud_hook_test'] = array();
$file->save(); $file->save();
$this->assertHookMessageOrder(array( $this->assertHookMessageOrder(array(
...@@ -251,7 +251,7 @@ public function testFileHooks() { ...@@ -251,7 +251,7 @@ public function testFileHooks() {
'entity_crud_hook_test_entity_insert called for type file', 'entity_crud_hook_test_entity_insert called for type file',
)); ));
$_SESSION['entity_crud_hook_test'] = array(); $GLOBALS['entity_crud_hook_test'] = array();
$file = file_load($file->id()); $file = file_load($file->id());
$this->assertHookMessageOrder(array( $this->assertHookMessageOrder(array(
...@@ -259,7 +259,7 @@ public function testFileHooks() { ...@@ -259,7 +259,7 @@ public function testFileHooks() {
'entity_crud_hook_test_file_load called', 'entity_crud_hook_test_file_load called',
)); ));
$_SESSION['entity_crud_hook_test'] = array(); $GLOBALS['entity_crud_hook_test'] = array();
$file->setFilename('new.entity_crud_hook_test.file'); $file->setFilename('new.entity_crud_hook_test.file');
$file->save(); $file->save();
...@@ -270,7 +270,7 @@ public function testFileHooks() { ...@@ -270,7 +270,7 @@ public function testFileHooks() {
'entity_crud_hook_test_entity_update called for type file', 'entity_crud_hook_test_entity_update called for type file',
)); ));
$_SESSION['entity_crud_hook_test'] = array(); $GLOBALS['entity_crud_hook_test'] = array();
$file->delete(); $file->delete();
$this->assertHookMessageOrder(array( $this->assertHookMessageOrder(array(
...@@ -304,7 +304,7 @@ public function testNodeHooks() { ...@@ -304,7 +304,7 @@ public function testNodeHooks() {
'entity_crud_hook_test_entity_create called for type node', 'entity_crud_hook_test_entity_create called for type node',
)); ));
$_SESSION['entity_crud_hook_test'] = array(); $GLOBALS['entity_crud_hook_test'] = array();
$node->save(); $node->save();
$this->assertHookMessageOrder(array( $this->assertHookMessageOrder(array(
...@@ -314,7 +314,7 @@ public function testNodeHooks() { ...@@ -314,7 +314,7 @@ public function testNodeHooks() {
'entity_crud_hook_test_entity_insert called for type node', 'entity_crud_hook_test_entity_insert called for type node',
)); ));
$_SESSION['entity_crud_hook_test'] = array(); $GLOBALS['entity_crud_hook_test'] = array();
$node = node_load($node->id()); $node = node_load($node->id());
$this->assertHookMessageOrder(array( $this->assertHookMessageOrder(array(
...@@ -322,7 +322,7 @@ public function testNodeHooks() { ...@@ -322,7 +322,7 @@ public function testNodeHooks() {
'entity_crud_hook_test_node_load called', 'entity_crud_hook_test_node_load called',
)); ));
$_SESSION['entity_crud_hook_test'] = array(); $GLOBALS['entity_crud_hook_test'] = array();
$node->title = 'New title'; $node->title = 'New title';
$node->save(); $node->save();
...@@ -333,7 +333,7 @@ public function testNodeHooks() { ...@@ -333,7 +333,7 @@ public function testNodeHooks() {
'entity_crud_hook_test_entity_update called for type node', 'entity_crud_hook_test_entity_update called for type node',
)); ));
$_SESSION['entity_crud_hook_test'] = array(); $GLOBALS['entity_crud_hook_test'] = array();
$node->delete(); $node->delete();
$this->assertHookMessageOrder(array( $this->assertHookMessageOrder(array(
...@@ -358,7 +358,7 @@ public function testTaxonomyTermHooks() { ...@@ -358,7 +358,7 @@ public function testTaxonomyTermHooks() {
'module' => 'entity_crud_hook_test', 'module' => 'entity_crud_hook_test',
)); ));
$vocabulary->save(); $vocabulary->save();
$_SESSION['entity_crud_hook_test'] = array(); $GLOBALS['entity_crud_hook_test'] = array();
$term = entity_create('taxonomy_term', array( $term = entity_create('taxonomy_term', array(
'vid' => $vocabulary->id(), 'vid' => $vocabulary->id(),
...@@ -373,7 +373,7 @@ public function testTaxonomyTermHooks() { ...@@ -373,7 +373,7 @@ public function testTaxonomyTermHooks() {
'entity_crud_hook_test_entity_create called for type taxonomy_term', 'entity_crud_hook_test_entity_create called for type taxonomy_term',
)); ));
$_SESSION['entity_crud_hook_test'] = array(); $GLOBALS['entity_crud_hook_test'] = array();
$term->save(); $term->save();
$this->assertHookMessageOrder(array( $this->assertHookMessageOrder(array(
...@@ -383,7 +383,7 @@ public function testTaxonomyTermHooks() { ...@@ -383,7 +383,7 @@ public function testTaxonomyTermHooks() {
'entity_crud_hook_test_entity_insert called for type taxonomy_term', 'entity_crud_hook_test_entity_insert called for type taxonomy_term',
)); ));
$_SESSION['entity_crud_hook_test'] = array(); $GLOBALS['entity_crud_hook_test'] = array();
$term = entity_load('taxonomy_term', $term->id()); $term = entity_load('taxonomy_term', $term->id());
$this->assertHookMessageOrder(array( $this->assertHookMessageOrder(array(
...@@ -391,7 +391,7 @@ public function testTaxonomyTermHooks() { ...@@ -391,7 +391,7 @@ public function testTaxonomyTermHooks() {
'entity_crud_hook_test_taxonomy_term_load called', 'entity_crud_hook_test_taxonomy_term_load called',
)); ));
$_SESSION['entity_crud_hook_test'] = array(); $GLOBALS['entity_crud_hook_test'] = array();
$term->setName('New name'); $term->setName('New name');
$term->save(); $term->save();
...@@ -402,7 +402,7 @@ public function testTaxonomyTermHooks() { ...@@ -402,7 +402,7 @@ public function testTaxonomyTermHooks() {
'entity_crud_hook_test_entity_update called for type taxonomy_term', 'entity_crud_hook_test_entity_update called for type taxonomy_term',
)); ));
$_SESSION['entity_crud_hook_test'] = array(); $GLOBALS['entity_crud_hook_test'] = array();
$term->delete(); $term->delete();
$this->assertHookMessageOrder(array( $this->assertHookMessageOrder(array(
...@@ -432,7 +432,7 @@ public function testTaxonomyVocabularyHooks() { ...@@ -432,7 +432,7 @@ public function testTaxonomyVocabularyHooks() {
'entity_crud_hook_test_entity_create called for type taxonomy_vocabulary', 'entity_crud_hook_test_entity_create called for type taxonomy_vocabulary',
)); ));
$_SESSION['entity_crud_hook_test'] = array(); $GLOBALS['entity_crud_hook_test'] = array();
$vocabulary->save(); $vocabulary->save();
$this->assertHookMessageOrder(array( $this->assertHookMessageOrder(array(
...@@ -442,7 +442,7 @@ public function testTaxonomyVocabularyHooks() { ...@@ -442,7 +442,7 @@ public function testTaxonomyVocabularyHooks() {
'entity_crud_hook_test_entity_insert called for type taxonomy_vocabulary', 'entity_crud_hook_test_entity_insert called for type taxonomy_vocabulary',
)); ));
$_SESSION['entity_crud_hook_test'] = array();