SessionManager: Save 'persisted' flag in session metadata
authorBrad Jorsch <bjorsch@wikimedia.org>
Wed, 27 Jan 2016 22:13:35 +0000 (17:13 -0500)
committerBrad Jorsch <bjorsch@wikimedia.org>
Wed, 27 Jan 2016 22:13:35 +0000 (17:13 -0500)
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
includes/session/SessionManager.php
tests/phpunit/includes/session/SessionManagerTest.php

index 95c6f0c..67cbc6d 100644 (file)
@@ -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 ) );
index ecc4e54..d84a2d6 100644 (file)
@@ -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;
index 083223e..f5bb07d 100644 (file)
@@ -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,