[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).
This commit is contained in:
Drak 2012-03-30 22:47:35 +05:45
parent 39141e865b
commit 2f03b31258
7 changed files with 49 additions and 37 deletions

View File

@ -154,19 +154,19 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function invalidate() public function invalidate($lifetime = null)
{ {
$this->storage->clear(); $this->storage->clear();
return $this->storage->regenerate(true); return $this->migrate(true, $lifetime);
} }
/** /**
* {@inheritdoc} * {@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. * {@iheritdoc}
*
* @return MetaBag
*/ */
public function getMeta() public function getMetadata()
{ {
return $this->storage->getMetaBag(); return $this->storage->getMetaBag();
} }
/** /**
* Registers a SessionBagInterface with the session. * {@iheritdoc}
*
* @param SessionBagInterface $bag
*/ */
public function registerBag(SessionBagInterface $bag) public function registerBag(SessionBagInterface $bag)
{ {
@ -230,11 +226,7 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable
} }
/** /**
* Get's a bag instance. * {@iheritdoc}
*
* @param string $name
*
* @return SessionBagInterface
*/ */
public function getBag($name) public function getBag($name)
{ {

View File

@ -71,23 +71,32 @@ interface SessionInterface
* Clears all session attributes and flashes and regenerates the * Clears all session attributes and flashes and regenerates the
* session and deletes the old session from persistence. * 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. * @return Boolean True if session invalidated, false if error.
* *
* @api * @api
*/ */
function invalidate(); function invalidate($lifetime = null);
/** /**
* Migrates the current session to a new session id while maintaining all * Migrates the current session to a new session id while maintaining all
* session attributes. * 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. * @return Boolean True if session migrated, false if error.
* *
* @api * @api
*/ */
function migrate($destroy = false); function migrate($destroy = false, $lifetime = null);
/** /**
* Force the session to be saved and closed. * Force the session to be saved and closed.
@ -173,7 +182,7 @@ interface SessionInterface
public function registerBag(SessionBagInterface $bag); public function registerBag(SessionBagInterface $bag);
/** /**
* Get's a bag instance. * Gets a bag instance by name.
* *
* @param string $name * @param string $name
* *

View File

@ -16,7 +16,7 @@ use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
/** /**
* Metadata container. * Metadata container.
* *
* Adds standard meta data to the session. * Adds meta data to the session.
* *
* @author Drak <drak@zikula.org> * @author Drak <drak@zikula.org>
*/ */
@ -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 * @return integer
*/ */
@ -82,6 +82,11 @@ class MetaBag implements SessionBagInterface
/** /**
* Stamps a new session's meta. * 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) public function stampNew($lifetime = null)
{ {
@ -132,6 +137,11 @@ class MetaBag implements SessionBagInterface
return $this->name; return $this->name;
} }
/**
* Sets name.
*
* @param string $name
*/
public function setName($name) public function setName($name)
{ {
$this->name = $name; $this->name = $name;

View File

@ -98,20 +98,16 @@ class MockArraySessionStorage implements SessionStorageInterface
return true; return true;
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function regenerate($destroy = false) public function regenerate($destroy = false, $lifetime = null)
{ {
if (!$this->started) { if (!$this->started) {
$this->start(); $this->start();
} }
if ($destroy) { $this->metaBag->stampNew($lifetime);
$this->metaBag->stampNew();
}
$this->id = $this->generateId(); $this->id = $this->generateId();
return true; return true;

View File

@ -73,20 +73,17 @@ class MockFileSessionStorage extends MockArraySessionStorage
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function regenerate($destroy = false) public function regenerate($destroy = false, $lifetime = null)
{ {
if (!$this->started) { if (!$this->started) {
$this->start(); $this->start();
} }
if ($destroy) { if ($destroy) {
$this->destroy(); $this->destroy();
$this->metaBag->stampNew();
} }
$this->id = $this->generateId(); return parent::regenerate($destroy, $lifetime);
return true;
} }
/** /**

View File

@ -195,8 +195,12 @@ class NativeSessionStorage implements SessionStorageInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function regenerate($destroy = false) public function regenerate($destroy = false, $lifetime = null)
{ {
if (null !== $lifetime) {
ini_set('session.cookie_lifetime', $lifetime);
}
if ($destroy) { if ($destroy) {
$this->metaBag->stampNew(); $this->metaBag->stampNew();
} }

View File

@ -82,7 +82,11 @@ interface SessionStorageInterface
* Note regenerate+destroy should not clear the session data in memory * Note regenerate+destroy should not clear the session data in memory
* only delete the session data from persistent storage. * 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 * @return Boolean True if session regenerated, false if error
* *
@ -90,7 +94,7 @@ interface SessionStorageInterface
* *
* @api * @api
*/ */
function regenerate($destroy = false); function regenerate($destroy = false, $lifetime = null);
/** /**
* Force the session to be saved and closed. * Force the session to be saved and closed.