From f72ba0a27dbacfb73bc355c207e8ac7990038a20 Mon Sep 17 00:00:00 2001 From: Florin Patan Date: Sat, 9 Jun 2012 22:45:38 +0300 Subject: [PATCH] Fixed detection of an active session --- src/Symfony/Component/HttpFoundation/Request.php | 2 +- .../Component/HttpFoundation/Session/Session.php | 8 ++++++++ .../HttpFoundation/Session/SessionInterface.php | 9 +++++++++ .../Session/Storage/MockArraySessionStorage.php | 10 ++++++++++ .../Session/Storage/NativeSessionStorage.php | 10 ++++++++++ .../Component/HttpFoundation/Tests/RequestTest.php | 2 ++ .../Tests/Http/Firewall/ContextListenerTest.php | 1 + 7 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/HttpFoundation/Request.php b/src/Symfony/Component/HttpFoundation/Request.php index 581b4c999a..4eaa5b5ec2 100644 --- a/src/Symfony/Component/HttpFoundation/Request.php +++ b/src/Symfony/Component/HttpFoundation/Request.php @@ -518,7 +518,7 @@ class Request */ public function hasSession() { - return null !== $this->session; + return null !== $this->session && $this->session->isStarted(); } /** diff --git a/src/Symfony/Component/HttpFoundation/Session/Session.php b/src/Symfony/Component/HttpFoundation/Session/Session.php index b70a334f6a..ee987c67e5 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Session.php +++ b/src/Symfony/Component/HttpFoundation/Session/Session.php @@ -130,6 +130,14 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable $this->storage->getBag($this->attributeName)->clear(); } + /** + * {@inheritdoc} + */ + public function isStarted() + { + return $this->storage->isStarted(); + } + /** * Returns an iterator for attributes. * diff --git a/src/Symfony/Component/HttpFoundation/Session/SessionInterface.php b/src/Symfony/Component/HttpFoundation/Session/SessionInterface.php index 833dd9cf84..d8ff563738 100644 --- a/src/Symfony/Component/HttpFoundation/Session/SessionInterface.php +++ b/src/Symfony/Component/HttpFoundation/Session/SessionInterface.php @@ -174,6 +174,15 @@ interface SessionInterface */ function clear(); + /** + * Check if a session was started + * + * @api + * + * @return boolean + */ + public function isStarted(); + /** * Registers a SessionBagInterface with the session. * diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php index 45725522e8..9a4a26a4cc 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php @@ -199,6 +199,16 @@ class MockArraySessionStorage implements SessionStorageInterface return $this->bags[$name]; } + /** + * Check if the session was started or not + * + * @return boolean + */ + public function isStarted() + { + return $this->started; + } + /** * Sets the MetadataBag. * diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php index 7a9d77531e..0f77a9565e 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php @@ -289,6 +289,16 @@ class NativeSessionStorage implements SessionStorageInterface return $this->metadataBag; } + /** + * Check if the session was started or not + * + * @return boolean + */ + public function isStarted() + { + return $this->started; + } + /** * Sets session.* ini variables. * diff --git a/src/Symfony/Component/HttpFoundation/Tests/RequestTest.php b/src/Symfony/Component/HttpFoundation/Tests/RequestTest.php index 9d943ef0d4..55ad28ecb3 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/RequestTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/RequestTest.php @@ -884,6 +884,7 @@ class RequestTest extends \PHPUnit_Framework_TestCase $this->assertFalse($request->hasSession()); $request->setSession(new Session(new MockArraySessionStorage())); + $request->getSession()->start(); $this->assertTrue($request->hasSession()); } @@ -895,6 +896,7 @@ class RequestTest extends \PHPUnit_Framework_TestCase $request->cookies->set('MOCKSESSID', 'foo'); $this->assertFalse($request->hasPreviousSession()); $request->setSession(new Session(new MockArraySessionStorage())); + $request->getSession()->start(); $this->assertTrue($request->hasPreviousSession()); } diff --git a/src/Symfony/Component/Security/Tests/Http/Firewall/ContextListenerTest.php b/src/Symfony/Component/Security/Tests/Http/Firewall/ContextListenerTest.php index 646ed2357a..d6723785ee 100644 --- a/src/Symfony/Component/Security/Tests/Http/Firewall/ContextListenerTest.php +++ b/src/Symfony/Component/Security/Tests/Http/Firewall/ContextListenerTest.php @@ -85,6 +85,7 @@ class ContextListenerTest extends \PHPUnit_Framework_TestCase protected function runSessionOnKernelResponse($newToken, $original = null) { $session = new Session(new MockArraySessionStorage()); + $session->start(); if ($original !== null) { $session->set('_security_session', $original);