From 2f03b31258bacff81142a8a1b9ee373d2bcefa33 Mon Sep 17 00:00:00 2001 From: Drak Date: Fri, 30 Mar 2012 22:47:35 +0545 Subject: [PATCH] [HttpFoundation] Added the ability to change the session cookie lifetime on migrate(). This is a very important option which allows the cookie lifetime to be changed on migrate. For example when a user converts from an anonymous session to a logged in session one might wish to change from a persistent cookie to browser session (e.g. a banking application). --- .../HttpFoundation/Session/Session.php | 24 +++++++------------ .../Session/SessionInterface.php | 17 +++++++++---- .../Session/Storage/MetaBag.php | 14 +++++++++-- .../Storage/MockArraySessionStorage.php | 8 ++----- .../Storage/MockFileSessionStorage.php | 9 +++---- .../Session/Storage/NativeSessionStorage.php | 6 ++++- .../Storage/SessionStorageInterface.php | 8 +++++-- 7 files changed, 49 insertions(+), 37 deletions(-) diff --git a/src/Symfony/Component/HttpFoundation/Session/Session.php b/src/Symfony/Component/HttpFoundation/Session/Session.php index a049622ea4..c68bd30fce 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Session.php +++ b/src/Symfony/Component/HttpFoundation/Session/Session.php @@ -154,19 +154,19 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable /** * {@inheritdoc} */ - public function invalidate() + public function invalidate($lifetime = null) { $this->storage->clear(); - return $this->storage->regenerate(true); + return $this->migrate(true, $lifetime); } /** * {@inheritdoc} */ - public function migrate($destroy = false) + public function migrate($destroy = false, $lifetime = null) { - return $this->storage->regenerate($destroy); + return $this->storage->regenerate($destroy, $lifetime); } /** @@ -210,19 +210,15 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable } /** - * Gets session meta. - * - * @return MetaBag + * {@iheritdoc} */ - public function getMeta() + public function getMetadata() { return $this->storage->getMetaBag(); } /** - * Registers a SessionBagInterface with the session. - * - * @param SessionBagInterface $bag + * {@iheritdoc} */ public function registerBag(SessionBagInterface $bag) { @@ -230,11 +226,7 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable } /** - * Get's a bag instance. - * - * @param string $name - * - * @return SessionBagInterface + * {@iheritdoc} */ public function getBag($name) { diff --git a/src/Symfony/Component/HttpFoundation/Session/SessionInterface.php b/src/Symfony/Component/HttpFoundation/Session/SessionInterface.php index 5fe264eb65..bfd7f6abb8 100644 --- a/src/Symfony/Component/HttpFoundation/Session/SessionInterface.php +++ b/src/Symfony/Component/HttpFoundation/Session/SessionInterface.php @@ -71,23 +71,32 @@ interface SessionInterface * Clears all session attributes and flashes and regenerates the * session and deletes the old session from persistence. * + * @param integer $lifetime Sets the cookie lifetime for the session cookie. A null value + * will leave the system settings unchanged, 0 sets the cookie + * to expire with browser session. Time is in seconds, and is + * not a Unix timestamp. + * * @return Boolean True if session invalidated, false if error. * * @api */ - function invalidate(); + function invalidate($lifetime = null); /** * Migrates the current session to a new session id while maintaining all * session attributes. * - * @param Boolean $destroy Whether to delete the old session or leave it to garbage collection. + * @param Boolean $destroy Whether to delete the old session or leave it to garbage collection. + * @param integer $lifetime Sets the cookie lifetime for the session cookie. A null value + * will leave the system settings unchanged, 0 sets the cookie + * to expire with browser session. Time is in seconds, and is + * not a Unix timestamp. * * @return Boolean True if session migrated, false if error. * * @api */ - function migrate($destroy = false); + function migrate($destroy = false, $lifetime = null); /** * Force the session to be saved and closed. @@ -173,7 +182,7 @@ interface SessionInterface public function registerBag(SessionBagInterface $bag); /** - * Get's a bag instance. + * Gets a bag instance by name. * * @param string $name * diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/MetaBag.php b/src/Symfony/Component/HttpFoundation/Session/Storage/MetaBag.php index b2abcd8d68..0bb160f843 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/MetaBag.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/MetaBag.php @@ -16,7 +16,7 @@ use Symfony\Component\HttpFoundation\Session\SessionBagInterface; /** * Metadata container. * - * Adds standard meta data to the session. + * Adds meta data to the session. * * @author Drak */ @@ -71,7 +71,7 @@ class MetaBag implements SessionBagInterface } /** - * Gets the lifetime that this cooke was set with. + * Gets the lifetime that the session cookie was set with. * * @return integer */ @@ -82,6 +82,11 @@ class MetaBag implements SessionBagInterface /** * Stamps a new session's meta. + * + * @param integer $lifetime Sets the cookie lifetime for the session cookie. A null value + * will leave the system settings unchanged, 0 sets the cookie + * to expire with browser session. Time is in seconds, and is + * not a Unix timestamp. */ public function stampNew($lifetime = null) { @@ -132,6 +137,11 @@ class MetaBag implements SessionBagInterface return $this->name; } + /** + * Sets name. + * + * @param string $name + */ public function setName($name) { $this->name = $name; diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php index 3dc8fa1a87..f77116a014 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php @@ -98,20 +98,16 @@ class MockArraySessionStorage implements SessionStorageInterface return true; } - /** * {@inheritdoc} */ - public function regenerate($destroy = false) + public function regenerate($destroy = false, $lifetime = null) { if (!$this->started) { $this->start(); } - if ($destroy) { - $this->metaBag->stampNew(); - } - + $this->metaBag->stampNew($lifetime); $this->id = $this->generateId(); return true; diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/MockFileSessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/MockFileSessionStorage.php index f39d4e9c9f..56e6513800 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/MockFileSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/MockFileSessionStorage.php @@ -73,20 +73,17 @@ class MockFileSessionStorage extends MockArraySessionStorage /** * {@inheritdoc} */ - public function regenerate($destroy = false) + public function regenerate($destroy = false, $lifetime = null) { if (!$this->started) { $this->start(); } - + if ($destroy) { $this->destroy(); - $this->metaBag->stampNew(); } - $this->id = $this->generateId(); - - return true; + return parent::regenerate($destroy, $lifetime); } /** diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php index fd3fb8a99c..72b3f5c79a 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php @@ -195,8 +195,12 @@ class NativeSessionStorage implements SessionStorageInterface /** * {@inheritdoc} */ - public function regenerate($destroy = false) + public function regenerate($destroy = false, $lifetime = null) { + if (null !== $lifetime) { + ini_set('session.cookie_lifetime', $lifetime); + } + if ($destroy) { $this->metaBag->stampNew(); } diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/SessionStorageInterface.php b/src/Symfony/Component/HttpFoundation/Session/Storage/SessionStorageInterface.php index 058d017380..1c1014b89f 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/SessionStorageInterface.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/SessionStorageInterface.php @@ -82,7 +82,11 @@ interface SessionStorageInterface * Note regenerate+destroy should not clear the session data in memory * only delete the session data from persistent storage. * - * @param Boolean $destroy Destroy session when regenerating? + * @param Boolean $destroy Destroy session when regenerating? + * @param integer $lifetime Sets the cookie lifetime for the session cookie. A null value + * will leave the system settings unchanged, 0 sets the cookie + * to expire with browser session. Time is in seconds, and is + * not a Unix timestamp. * * @return Boolean True if session regenerated, false if error * @@ -90,7 +94,7 @@ interface SessionStorageInterface * * @api */ - function regenerate($destroy = false); + function regenerate($destroy = false, $lifetime = null); /** * Force the session to be saved and closed.