Commit 195503e2 authored by alexpott's avatar alexpott

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

parent 2a0254f9
......@@ -49,7 +49,7 @@
* TRUE if the current user can run authorize.php, and FALSE if not.
*/
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');
}
......
......@@ -1453,7 +1453,7 @@ function install_load_profile(&$install_state) {
* An array of information about the current installation state.
*/
function install_bootstrap_full() {
\Drupal::service('session_manager')->startLazy();
\Drupal::service('session_manager')->start();
}
/**
......
......@@ -47,7 +47,7 @@ public function applies(Request $request) {
public function authenticate(Request $request) {
// Global $user is deprecated, but the session system is still based on it.
global $user;
$this->sessionManager->startLazy();
$this->sessionManager->start();
if ($this->sessionManager->isStarted()) {
return $user;
}
......
......@@ -672,16 +672,13 @@ protected function getKernelParameters() {
*/
protected function initializeContainer($rebuild = FALSE) {
$this->containerNeedsDumping = FALSE;
$session_manager_state = 0;
$session_manager_started = FALSE;
if (isset($this->container)) {
// 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->isStartedLazy()) {
$session_manager_state |= 0x1;
}
if ($session_manager->isStarted()) {
$session_manager_state |= 0x2;
$session_manager_started = TRUE;
$session_manager->save();
}
unset($session_manager);
......@@ -712,10 +709,7 @@ protected function initializeContainer($rebuild = FALSE) {
$this->attachSynthetic($container);
$this->container = $container;
if ($session_manager_state & 0x1) {
$this->container->get('session_manager')->startLazy();
}
if ($session_manager_state & 0x2) {
if ($session_manager_started) {
$this->container->get('session_manager')->start();
}
\Drupal::setContainer($this->container);
......
......@@ -114,7 +114,7 @@ public function __construct(RequestStack $request_stack, Connection $connection,
/**
* {@inheritdoc}
*/
public function startLazy() {
public function start() {
global $user;
if (($this->started || $this->startedLazy) && !$this->closed) {
......@@ -129,53 +129,66 @@ public function startLazy() {
// session is only started on demand in save(), making
// anonymous users not use a session cookie unless something is stored in
// $_SESSION. This allows HTTP proxies to cache anonymous pageviews.
$result = $this->start();
$result = $this->startNow();
if ($user->isAuthenticated() || !$this->isSessionObsolete()) {
drupal_page_is_cacheable(FALSE);
}
}
else {
// Set a session identifier for this request. This is necessary because
// 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.
if (empty($result)) {
$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());
if ($is_https && $this->isMixedMode()) {
$session_id = Crypt::randomBytesBase64();
$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;
}
date_default_timezone_set(drupal_get_user_timezone());
$this->startedLazy = TRUE;
return $result;
}
/**
* {@inheritdoc}
*/
public function isStartedLazy() {
return $this->startedLazy;
}
/**
* {@inheritdoc}
* Forcibly start a PHP session.
*
* @return boolean
* TRUE if the session is started.
*/
public function start() {
protected function startNow() {
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();
// Restore session data.
if (!empty($session_data)) {
$_SESSION += $session_data;
if ($this->startedLazy) {
$_SESSION = $session_data;
}
return $result;
......@@ -203,7 +216,7 @@ public function save() {
// There is session data to store. Start the session if it is not already
// started.
if (!$this->getSaveHandler()->isActive()) {
$this->start();
$this->startNow();
if ($this->requestStack->getCurrentRequest()->isSecure() && $this->isMixedMode()) {
$insecure_session_name = $this->getInsecureName();
$params = session_get_cookie_params();
......@@ -282,7 +295,7 @@ public function regenerate($destroy = FALSE, $lifetime = NULL) {
// Preserve the logged in user, as it will be reset to anonymous
// by \Drupal\Core\Session\SessionHandler::read().
$account = $user;
$this->start();
$this->startNow();
$user = $account;
}
date_default_timezone_set(drupal_get_user_timezone());
......
......@@ -14,22 +14,6 @@
*/
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).
*
......
......@@ -55,7 +55,7 @@ protected function setUp() {
* Checks the order of CRUD hook execution messages.
*
* 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
* An array of plain-text messages in the order they should appear.
......@@ -64,7 +64,7 @@ protected function assertHookMessageOrder($messages) {
$positions = array();
foreach ($messages as $message) {
// 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)) {
$positions[] = $position;
}
......@@ -91,7 +91,7 @@ public function testBlockHooks() {
'entity_crud_hook_test_entity_create called for type block',
));
$_SESSION['entity_crud_hook_test'] = array();
$GLOBALS['entity_crud_hook_test'] = array();
$entity->save();
$this->assertHookMessageOrder(array(
......@@ -101,7 +101,7 @@ public function testBlockHooks() {
'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());
$this->assertHookMessageOrder(array(
......@@ -109,7 +109,7 @@ public function testBlockHooks() {
'entity_crud_hook_test_block_load called',
));
$_SESSION['entity_crud_hook_test'] = array();
$GLOBALS['entity_crud_hook_test'] = array();
$entity->label = 'New label';
$entity->save();
......@@ -120,7 +120,7 @@ public function testBlockHooks() {
'entity_crud_hook_test_entity_update called for type block',
));
$_SESSION['entity_crud_hook_test'] = array();
$GLOBALS['entity_crud_hook_test'] = array();
$entity->delete();
$this->assertHookMessageOrder(array(
......@@ -155,7 +155,7 @@ public function testCommentHooks() {
));
$node->save();
$nid = $node->id();
$_SESSION['entity_crud_hook_test'] = array();
$GLOBALS['entity_crud_hook_test'] = array();
$comment = entity_create('comment', array(
'cid' => NULL,
......@@ -176,7 +176,7 @@ public function testCommentHooks() {
'entity_crud_hook_test_entity_create called for type comment',
));
$_SESSION['entity_crud_hook_test'] = array();
$GLOBALS['entity_crud_hook_test'] = array();
$comment->save();
$this->assertHookMessageOrder(array(
......@@ -186,7 +186,7 @@ public function testCommentHooks() {
'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());
$this->assertHookMessageOrder(array(
......@@ -194,7 +194,7 @@ public function testCommentHooks() {
'entity_crud_hook_test_comment_load called',
));
$_SESSION['entity_crud_hook_test'] = array();
$GLOBALS['entity_crud_hook_test'] = array();
$comment->setSubject('New subject');
$comment->save();
......@@ -205,7 +205,7 @@ public function testCommentHooks() {
'entity_crud_hook_test_entity_update called for type comment',
));
$_SESSION['entity_crud_hook_test'] = array();
$GLOBALS['entity_crud_hook_test'] = array();
$comment->delete();
$this->assertHookMessageOrder(array(
......@@ -241,7 +241,7 @@ public function testFileHooks() {
'entity_crud_hook_test_entity_create called for type file',
));
$_SESSION['entity_crud_hook_test'] = array();
$GLOBALS['entity_crud_hook_test'] = array();
$file->save();
$this->assertHookMessageOrder(array(
......@@ -251,7 +251,7 @@ public function testFileHooks() {
'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());
$this->assertHookMessageOrder(array(
......@@ -259,7 +259,7 @@ public function testFileHooks() {
'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->save();
......@@ -270,7 +270,7 @@ public function testFileHooks() {
'entity_crud_hook_test_entity_update called for type file',
));
$_SESSION['entity_crud_hook_test'] = array();
$GLOBALS['entity_crud_hook_test'] = array();
$file->delete();
$this->assertHookMessageOrder(array(
......@@ -304,7 +304,7 @@ public function testNodeHooks() {
'entity_crud_hook_test_entity_create called for type node',
));
$_SESSION['entity_crud_hook_test'] = array();
$GLOBALS['entity_crud_hook_test'] = array();
$node->save();
$this->assertHookMessageOrder(array(
......@@ -314,7 +314,7 @@ public function testNodeHooks() {
'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());
$this->assertHookMessageOrder(array(
......@@ -322,7 +322,7 @@ public function testNodeHooks() {
'entity_crud_hook_test_node_load called',
));
$_SESSION['entity_crud_hook_test'] = array();
$GLOBALS['entity_crud_hook_test'] = array();
$node->title = 'New title';
$node->save();
......@@ -333,7 +333,7 @@ public function testNodeHooks() {
'entity_crud_hook_test_entity_update called for type node',
));
$_SESSION['entity_crud_hook_test'] = array();
$GLOBALS['entity_crud_hook_test'] = array();
$node->delete();
$this->assertHookMessageOrder(array(
......@@ -358,7 +358,7 @@ public function testTaxonomyTermHooks() {
'module' => 'entity_crud_hook_test',
));
$vocabulary->save();
$_SESSION['entity_crud_hook_test'] = array();
$GLOBALS['entity_crud_hook_test'] = array();
$term = entity_create('taxonomy_term', array(
'vid' => $vocabulary->id(),
......@@ -373,7 +373,7 @@ public function testTaxonomyTermHooks() {
'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();
$this->assertHookMessageOrder(array(
......@@ -383,7 +383,7 @@ public function testTaxonomyTermHooks() {
'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());
$this->assertHookMessageOrder(array(
......@@ -391,7 +391,7 @@ public function testTaxonomyTermHooks() {
'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->save();
......@@ -402,7 +402,7 @@ public function testTaxonomyTermHooks() {
'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();
$this->assertHookMessageOrder(array(
......@@ -432,7 +432,7 @@ public function testTaxonomyVocabularyHooks() {
'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();
$this->assertHookMessageOrder(array(
......@@ -442,7 +442,7 @@ public function testTaxonomyVocabularyHooks() {
'entity_crud_hook_test_entity_insert called for type taxonomy_vocabulary',
));
$_SESSION['entity_crud_hook_test'] = array();
$GLOBALS['entity_crud_hook_test'] = array();
$vocabulary = entity_load('taxonomy_vocabulary', $vocabulary->id());
$this->assertHookMessageOrder(array(
......@@ -450,7 +450,7 @@ public function testTaxonomyVocabularyHooks() {
'entity_crud_hook_test_taxonomy_vocabulary_load called',
));
$_SESSION['entity_crud_hook_test'] = array();
$GLOBALS['entity_crud_hook_test'] = array();
$vocabulary->name = 'New name';
$vocabulary->save();
......@@ -461,7 +461,7 @@ public function testTaxonomyVocabularyHooks() {
'entity_crud_hook_test_entity_update called for type taxonomy_vocabulary',
));
$_SESSION['entity_crud_hook_test'] = array();
$GLOBALS['entity_crud_hook_test'] = array();
$vocabulary->delete();
$this->assertHookMessageOrder(array(
......@@ -489,7 +489,7 @@ public function testUserHooks() {
'entity_crud_hook_test_entity_create called for type user',
));
$_SESSION['entity_crud_hook_test'] = array();
$GLOBALS['entity_crud_hook_test'] = array();
$account->save();
$this->assertHookMessageOrder(array(
......@@ -499,7 +499,7 @@ public function testUserHooks() {
'entity_crud_hook_test_entity_insert called for type user',
));
$_SESSION['entity_crud_hook_test'] = array();
$GLOBALS['entity_crud_hook_test'] = array();
user_load($account->id());
$this->assertHookMessageOrder(array(
......@@ -507,7 +507,7 @@ public function testUserHooks() {
'entity_crud_hook_test_user_load called',
));
$_SESSION['entity_crud_hook_test'] = array();
$GLOBALS['entity_crud_hook_test'] = array();
$account->name = 'New name';
$account->save();
......@@ -518,7 +518,7 @@ public function testUserHooks() {
'entity_crud_hook_test_entity_update called for type user',
));
$_SESSION['entity_crud_hook_test'] = array();
$GLOBALS['entity_crud_hook_test'] = array();
user_delete($account->id());
$this->assertHookMessageOrder(array(
......
......@@ -46,7 +46,7 @@ public function onKernelRequestTest(GetResponseEvent $event) {
* An array of event listener definitions.
*/
static function getSubscribedEvents() {
$events[KernelEvents::REQUEST][] = array('onKernelRequestTest', 100);
$events[KernelEvents::REQUEST][] = array('onKernelRequestTest');
return $events;
}
......
services:
session_test.subscriber:
class: Drupal\session_test\EventSubscriber\SessionTestSubscriber
arguments: ['@session_manager']
tags:
- { name: event_subscriber }
......@@ -7,6 +7,7 @@
namespace Drupal\session_test\EventSubscriber;
use Drupal\Core\Session\SessionManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\KernelEvents;
......@@ -18,11 +19,27 @@
*/
class SessionTestSubscriber implements EventSubscriberInterface {
/*
/**
* The session manager.
*
* @var \Drupal\Core\Session\SessionManagerInterface
*/
protected $sessionManager;
/**
* Stores whether $_SESSION is empty at the beginning of the request.
*
* @var bool
*/
protected $emptySession;
/**
* Constructs a new session test subscriber.
*/
public function __construct(SessionManagerInterface $session_manager) {
$this->sessionManager = $session_manager;
}
/**
* Set header for session testing.
*
......@@ -30,12 +47,7 @@ class SessionTestSubscriber implements EventSubscriberInterface {
* The Event to process.
*/
public function onKernelRequestSessionTest(GetResponseEvent $event) {
// Trigger the authentication in the test to ensure that $_SESSION has the
// needed data.
// @todo: On the longrun the session will be lazy initialized, so we no
// longer have to force it here.
\Drupal::currentUser()->getAccount();
$this->emptySession = intval(empty($_SESSION));
$this->emptySession = (int) !$this->sessionManager->start();
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment