From 296ccfd4a9a6ad3ae412db7e2408c923aaa61f64 Mon Sep 17 00:00:00 2001 From: Brad Jorsch Date: Wed, 27 Jan 2016 17:13:35 -0500 Subject: [PATCH] SessionManager: Save 'persisted' flag in session metadata This allows SessionManager::getSessionById()->isPersisted() to be reliably set. Otherwise it depends on whether the SessionBackend is still loaded or not. Change-Id: I17733559ac5d8fff13881664333f61d36f610b6d --- includes/session/SessionBackend.php | 2 ++ includes/session/SessionManager.php | 3 ++ .../includes/session/SessionManagerTest.php | 32 +++++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/includes/session/SessionBackend.php b/includes/session/SessionBackend.php index 95c6f0c7f6..67cbc6d388 100644 --- a/includes/session/SessionBackend.php +++ b/includes/session/SessionBackend.php @@ -262,6 +262,7 @@ final class SessionBackend { if ( !$this->persist ) { $this->persist = true; $this->forcePersist = true; + $this->metaDirty = true; $this->logger->debug( "SessionBackend $this->id force-persist due to persist()" ); $this->autosave(); } else { @@ -601,6 +602,7 @@ final class SessionBackend { 'forceHTTPS' => $this->forceHTTPS, 'expires' => time() + $this->lifetime, 'loggedOut' => $this->loggedOut, + 'persisted' => $this->persist, ); \Hooks::run( 'SessionMetadata', array( $this, &$metadata, $this->requests ) ); diff --git a/includes/session/SessionManager.php b/includes/session/SessionManager.php index ecc4e54953..d84a2d6ea0 100644 --- a/includes/session/SessionManager.php +++ b/includes/session/SessionManager.php @@ -801,6 +801,9 @@ final class SessionManager implements SessionManagerInterface { if ( !empty( $metadata['forceHTTPS'] ) && !$info->forceHTTPS() ) { $newParams['forceHTTPS'] = true; } + if ( !empty( $metadata['persisted'] ) && !$info->wasPersisted() ) { + $newParams['persisted'] = true; + } if ( !$info->isIdSafe() ) { $newParams['idIsSafe'] = true; diff --git a/tests/phpunit/includes/session/SessionManagerTest.php b/tests/phpunit/includes/session/SessionManagerTest.php index 083223ebed..f5bb07d1db 100644 --- a/tests/phpunit/includes/session/SessionManagerTest.php +++ b/tests/phpunit/includes/session/SessionManagerTest.php @@ -1604,6 +1604,38 @@ class SessionManagerTest extends MediaWikiTestCase { $this->assertTrue( $info->forceHTTPS() ); $this->assertSame( array(), $logger->getBuffer() ); + // "Persist" flag from session + $this->store->setSessionMeta( $id, $metadata ); + $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array( + 'provider' => $provider, + 'id' => $id, + 'userInfo' => $userInfo + ) ); + $this->assertTrue( $loadSessionInfoFromStore( $info ) ); + $this->assertFalse( $info->wasPersisted() ); + $this->assertSame( array(), $logger->getBuffer() ); + + $this->store->setSessionMeta( $id, array( 'persisted' => true ) + $metadata ); + $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array( + 'provider' => $provider, + 'id' => $id, + 'userInfo' => $userInfo + ) ); + $this->assertTrue( $loadSessionInfoFromStore( $info ) ); + $this->assertTrue( $info->wasPersisted() ); + $this->assertSame( array(), $logger->getBuffer() ); + + $this->store->setSessionMeta( $id, array( 'persisted' => false ) + $metadata ); + $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array( + 'provider' => $provider, + 'id' => $id, + 'userInfo' => $userInfo, + 'persisted' => true + ) ); + $this->assertTrue( $loadSessionInfoFromStore( $info ) ); + $this->assertTrue( $info->wasPersisted() ); + $this->assertSame( array(), $logger->getBuffer() ); + // Provider refreshSessionInfo() returning false $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array( 'provider' => $provider3, -- 2.20.1