Skip to content

Commit 59e1b50

Browse files
committed
Merge pull request #165 from FriendsOfSymfony/logout-handler-optional
Add configuration for logout handler
2 parents 993912d + 300f487 commit 59e1b50

File tree

5 files changed

+65
-10
lines changed

5 files changed

+65
-10
lines changed

DependencyInjection/Configuration.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,21 @@ public function getConfigTreeBuilder()
121121
throw new InvalidConfigurationException('You need to configure the Nginx proxy_client to use the Nginx test client');
122122
})
123123
->end()
124+
->validate()
125+
->ifTrue(
126+
function ($v) {
127+
return $v['user_context']['logout_handler']['enabled']
128+
&& !isset($v['proxy_client']);
129+
}
130+
)
131+
->then(function ($v) {
132+
if ('auto' === $v['user_context']['logout_handler']['enabled']) {
133+
$v['user_context']['logout_handler']['enabled'] = false;
134+
135+
return $v;
136+
}
137+
throw new InvalidConfigurationException('You need to configure a proxy_client for the logout_handler.');
138+
})
124139
;
125140

126141
$this->addCacheControlSection($rootNode);
@@ -557,6 +572,17 @@ private function addUserContextListenerSection(ArrayNodeDefinition $rootNode)
557572
->defaultFalse()
558573
->info('Whether to enable a provider that automatically adds all roles of the current user to the context.')
559574
->end()
575+
->arrayNode('logout_handler')
576+
->addDefaultsIfNotSet()
577+
->canBeEnabled()
578+
->children()
579+
->enumNode('enabled')
580+
->values(array(true, false, 'auto'))
581+
->defaultValue('auto')
582+
->info('Whether to enable the user context logout handler.')
583+
->end()
584+
->end()
585+
->end()
560586
->end()
561587
->end()
562588
->end()

DependencyInjection/FOSHttpCacheExtension.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,13 @@ private function loadUserContext(ContainerBuilder $container, XmlFileLoader $loa
176176
->replaceArgument(3, $config['user_hash_header'])
177177
->replaceArgument(4, $config['hash_cache_ttl']);
178178

179-
$container->getDefinition($this->getAlias().'.user_context.logout_handler')
180-
->replaceArgument(1, $config['user_identifier_headers'])
181-
->replaceArgument(2, $config['match']['accept']);
179+
if ($config['logout_handler']['enabled']) {
180+
$container->getDefinition($this->getAlias().'.user_context.logout_handler')
181+
->replaceArgument(1, $config['user_identifier_headers'])
182+
->replaceArgument(2, $config['match']['accept']);
183+
} else {
184+
$container->removeDefinition($this->getAlias().'.user_context.logout_handler');
185+
}
182186

183187
if ($config['role_provider']) {
184188
$container->getDefinition($this->getAlias().'.user_context.role_provider')

Resources/doc/reference/configuration/user-context.rst

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,11 @@ request. However, when you decide to cache hash responses, you must invalidate
118118
them when the user context changes, particularly when the user logs in or out.
119119
This bundle provides a logout handler that takes care of this for you.
120120

121-
Logout Handler
122-
""""""""""""""
121+
logout_handler
122+
~~~~~~~~~~~~~~
123+
124+
The logout handler will invalidate any cached user hashes when the user logs
125+
out.
123126

124127
For the handler to work:
125128

@@ -140,6 +143,16 @@ Add the handler to your firewall configuration:
140143
handlers:
141144
- fos_http_cache.user_context.logout_handler
142145
146+
enabled
147+
"""""""
148+
149+
**type**: ``enum`` **default**: ``auto`` **options**: ``true``, ``false``, ``auto``
150+
151+
Defauts to ``auto``, which enables the logout handler service if a
152+
:doc:`proxy client </reference/configuration/proxy-client>` is configured.
153+
Set to ``true`` to explicitly enable the logout handler. This will throw an
154+
exception if no proxy client is configured.
155+
143156
user_identifier_headers
144157
~~~~~~~~~~~~~~~~~~~~~~~
145158

Tests/Unit/DependencyInjection/ConfigurationTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ public function testSupportsAllConfigFormats()
151151
'user_identifier_headers' => array('Cookie', 'Authorization'),
152152
'user_hash_header' => 'FOS-User-Context-Hash',
153153
'role_provider' => true,
154+
'logout_handler' => array(
155+
'enabled' => 'auto',
156+
),
154157
),
155158
'flash_message' => array(
156159
'enabled' => true,
@@ -192,6 +195,7 @@ public function testSupportsNginx()
192195
$expectedConfiguration['cache_manager']['enabled'] = 'auto';
193196
$expectedConfiguration['tags']['enabled'] = 'auto';
194197
$expectedConfiguration['invalidation']['enabled'] = 'auto';
198+
$expectedConfiguration['user_context']['logout_handler']['enabled'] = 'auto';
195199

196200
$formats = array_map(function ($path) {
197201
return __DIR__.'/../../Resources/Fixtures/'.$path;
@@ -248,6 +252,7 @@ public function testSplitOptions()
248252
$expectedConfiguration['cache_manager']['enabled'] = 'auto';
249253
$expectedConfiguration['tags']['enabled'] = 'auto';
250254
$expectedConfiguration['invalidation']['enabled'] = 'auto';
255+
$expectedConfiguration['user_context']['logout_handler']['enabled'] = 'auto';
251256

252257
$formats = array_map(function ($path) {
253258
return __DIR__.'/../../Resources/Fixtures/'.$path;
@@ -436,6 +441,9 @@ private function getEmptyConfig()
436441
'user_identifier_headers' => array('Cookie', 'Authorization'),
437442
'user_hash_header' => 'X-User-Context-Hash',
438443
'role_provider' => false,
444+
'logout_handler' => array(
445+
'enabled' => false
446+
),
439447
),
440448
'flash_message' => array(
441449
'enabled' => false,

Tests/Unit/DependencyInjection/FOSHttpCacheExtensionTest.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ public function testEmptyConfig()
9292

9393
$container = $this->createContainer();
9494
$this->extension->load(array($config), $container);
95+
96+
$this->assertFalse($container->has('fos_http_cache.user_context.logout_handler'));
9597
}
9698

9799
public function testConfigLoadTagRules()
@@ -214,8 +216,8 @@ public function testConfigLoadCacheControlSplit()
214216

215217
public function testConfigUserContext()
216218
{
217-
$config = array(
218-
array('user_context' => array(
219+
$config = $this->getBaseConfig() + array(
220+
'user_context' => array(
219221
'match' => array(
220222
'matcher_service' => 'my_request_matcher_id',
221223
'method' => 'AUTHENTICATE',
@@ -225,17 +227,18 @@ public function testConfigUserContext()
225227
'user_hash_header' => 'X-Bar',
226228
'hash_cache_ttl' => 30,
227229
'role_provider' => true
228-
)),
230+
),
229231
);
230232

231233
$container = $this->createContainer();
232-
$this->extension->load($config, $container);
234+
$this->extension->load(array($config), $container);
233235

234236
$this->assertTrue($container->has('fos_http_cache.event_listener.user_context'));
235237
$this->assertTrue($container->has('fos_http_cache.user_context.hash_generator'));
236238
$this->assertTrue($container->has('fos_http_cache.user_context.request_matcher'));
237239
$this->assertTrue($container->has('fos_http_cache.user_context.role_provider'));
238-
240+
$this->assertTrue($container->has('fos_http_cache.user_context.logout_handler'));
241+
239242
$this->assertEquals(array('fos_http_cache.user_context.role_provider' => array(array())), $container->findTaggedServiceIds('fos_http_cache.user_context_provider'));
240243
}
241244

@@ -263,6 +266,7 @@ public function testConfigWithoutUserContext()
263266
$this->assertFalse($container->has('fos_http_cache.user_context.hash_generator'));
264267
$this->assertFalse($container->has('fos_http_cache.user_context.request_matcher'));
265268
$this->assertFalse($container->has('fos_http_cache.user_context.role_provider'));
269+
$this->assertFalse($container->has('fos_http_cache.user_context.logout_handler'));
266270
}
267271

268272
public function testConfigLoadFlashMessageSubscriber()

0 commit comments

Comments
 (0)