[HttpFoundation] Fixed session migration with custom cookie lifetime

This commit is contained in:
Guite 2020-02-09 17:51:23 +01:00 committed by Nicolas Grekas
parent 3ebe15e0be
commit 3e824de385
2 changed files with 18 additions and 3 deletions

View File

@ -152,7 +152,7 @@ class NativeSessionStorage implements SessionStorageInterface
// ok to try and start the session // ok to try and start the session
if (!session_start()) { if (!session_start()) {
throw new \RuntimeException('Failed to start the session'); throw new \RuntimeException('Failed to start the session.');
} }
if (null !== $this->emulateSameSite) { if (null !== $this->emulateSameSite) {
@ -213,8 +213,10 @@ class NativeSessionStorage implements SessionStorageInterface
return false; return false;
} }
if (null !== $lifetime) { if (null !== $lifetime && $lifetime != ini_get('session.cookie_lifetime')) {
$this->save();
ini_set('session.cookie_lifetime', $lifetime); ini_set('session.cookie_lifetime', $lifetime);
$this->start();
} }
if ($destroy) { if ($destroy) {
@ -314,7 +316,7 @@ class NativeSessionStorage implements SessionStorageInterface
public function getBag($name) public function getBag($name)
{ {
if (!isset($this->bags[$name])) { if (!isset($this->bags[$name])) {
throw new \InvalidArgumentException(sprintf('The SessionBagInterface %s is not registered.', $name)); throw new \InvalidArgumentException(sprintf('The SessionBagInterface "%s" is not registered.', $name));
} }
if (!$this->started && $this->saveHandler->isActive()) { if (!$this->started && $this->saveHandler->isActive()) {

View File

@ -123,6 +123,19 @@ class NativeSessionStorageTest extends TestCase
$this->assertEquals(11, $storage->getBag('attributes')->get('legs')); $this->assertEquals(11, $storage->getBag('attributes')->get('legs'));
} }
public function testRegenerateWithCustomLifetime()
{
$storage = $this->getStorage();
$storage->start();
$id = $storage->getId();
$lifetime = 999999;
$storage->getBag('attributes')->set('legs', 11);
$storage->regenerate(false, $lifetime);
$this->assertNotEquals($id, $storage->getId());
$this->assertEquals(11, $storage->getBag('attributes')->get('legs'));
$this->assertEquals($lifetime, ini_get('session.cookie_lifetime'));
}
public function testSessionGlobalIsUpToDateAfterIdRegeneration() public function testSessionGlobalIsUpToDateAfterIdRegeneration()
{ {
$storage = $this->getStorage(); $storage = $this->getStorage();