Skip to content

Commit c30ebae

Browse files
committed
fix user context logout handler auto enabled to not load it when the proxy client is not ban capable
1 parent 849954c commit c30ebae

File tree

8 files changed

+117
-10
lines changed

8 files changed

+117
-10
lines changed

src/DependencyInjection/Configuration.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,18 +105,34 @@ public function getConfigTreeBuilder()
105105
->validate()
106106
->ifTrue(
107107
function ($v) {
108-
return $v['user_context']['logout_handler']['enabled']
109-
&& !isset($v['proxy_client']);
108+
return false !== $v['user_context']['logout_handler']['enabled'];
110109
}
111110
)
112111
->then(function ($v) {
112+
if (isset($v['cache_manager']['custom_proxy_client'])) {
113+
$v['user_context']['logout_handler']['enabled'] = true;
114+
115+
return $v;
116+
}
117+
118+
if (isset($v['proxy_client']['default']) && in_array($v['proxy_client']['default'], ['varnish', 'noop'])) {
119+
$v['user_context']['logout_handler']['enabled'] = true;
120+
121+
return $v;
122+
}
123+
if (isset($v['proxy_client']['varnish']) || isset($v['proxy_client']['noop'])) {
124+
$v['user_context']['logout_handler']['enabled'] = true;
125+
126+
return $v;
127+
}
128+
113129
if ('auto' === $v['user_context']['logout_handler']['enabled']) {
114130
$v['user_context']['logout_handler']['enabled'] = false;
115131

116132
return $v;
117133
}
118134

119-
throw new InvalidConfigurationException('You need to configure a proxy_client for the logout_handler.');
135+
throw new InvalidConfigurationException('To enable the user context logout handler, you need to configure a ban capable proxy_client.');
120136
})
121137
;
122138

@@ -414,7 +430,7 @@ private function addProxyClientSection(ArrayNodeDefinition $rootNode)
414430

415431
if ('symfony' === $proxyName) {
416432
if (!$serversConfigured && false === $proxyConfig['use_kernel_dispatcher']) {
417-
throw new \InvalidArgumentException(sprintf('Either configure the "http.servers" section or enable "use_kernel_dispatcher" the proxy "%s"', $proxyName));
433+
throw new \InvalidArgumentException('Either configure the "http.servers" section or enable "proxy_client.symfony.use_kernel_dispatcher"');
418434
}
419435
}
420436
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fos_http_cache:
2+
3+
cache_manager:
4+
enabled: true
5+
custom_proxy_client: acme.proxy_client
6+
7+
user_context: ~
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fos_http_cache:
2+
3+
user_context: ~
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fos_http_cache:
2+
3+
proxy_client:
4+
nginx:
5+
http:
6+
servers: [ localhost ]
7+
8+
user_context: ~
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fos_http_cache:
2+
3+
cache_manager:
4+
enabled: true
5+
custom_proxy_client: acme.proxy_client
6+
7+
user_context:
8+
logout_handler:
9+
enabled: true
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fos_http_cache:
2+
3+
user_context:
4+
logout_handler:
5+
enabled: true
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
fos_http_cache:
2+
3+
proxy_client:
4+
nginx:
5+
http:
6+
servers: [ localhost ]
7+
8+
user_context:
9+
logout_handler:
10+
enabled: true

tests/Unit/DependencyInjection/ConfigurationTest.php

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public function testSupportsAllConfigFormats()
168168
'user_hash_header' => 'FOS-User-Context-Hash',
169169
'role_provider' => true,
170170
'logout_handler' => [
171-
'enabled' => 'auto',
171+
'enabled' => true,
172172
],
173173
],
174174
'flash_message' => [
@@ -207,6 +207,7 @@ public function testCustomProxyClient()
207207
];
208208
$expectedConfiguration['tags']['enabled'] = 'auto';
209209
$expectedConfiguration['invalidation']['enabled'] = 'auto';
210+
$expectedConfiguration['user_context']['logout_handler']['enabled'] = true;
210211

211212
$formats = array_map(function ($path) {
212213
return __DIR__.'/../../Resources/Fixtures/'.$path;
@@ -238,7 +239,7 @@ public function testSupportsNginx()
238239
$expectedConfiguration['cache_manager']['generate_url_type'] = 'auto';
239240
$expectedConfiguration['tags']['enabled'] = 'auto';
240241
$expectedConfiguration['invalidation']['enabled'] = 'auto';
241-
$expectedConfiguration['user_context']['logout_handler']['enabled'] = 'auto';
242+
$expectedConfiguration['user_context']['logout_handler']['enabled'] = false;
242243

243244
$formats = array_map(function ($path) {
244245
return __DIR__.'/../../Resources/Fixtures/'.$path;
@@ -274,7 +275,7 @@ public function testSupportsSymfony()
274275
$expectedConfiguration['cache_manager']['generate_url_type'] = 'auto';
275276
$expectedConfiguration['tags']['enabled'] = 'auto';
276277
$expectedConfiguration['invalidation']['enabled'] = 'auto';
277-
$expectedConfiguration['user_context']['logout_handler']['enabled'] = 'auto';
278+
$expectedConfiguration['user_context']['logout_handler']['enabled'] = false;
278279

279280
$formats = array_map(function ($path) {
280281
return __DIR__.'/../../Resources/Fixtures/'.$path;
@@ -291,7 +292,7 @@ public function testSupportsSymfony()
291292

292293
/**
293294
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
294-
* @expectedExceptionMessage Either configure the "http.servers" section or enable "use_kernel_dispatcher" the proxy "symfony"
295+
* @expectedExceptionMessage Either configure the "http.servers" section or enable "proxy_client.symfony.use_kernel_dispatcher
295296
*/
296297
public function testEmptyServerConfigurationIsNotAllowed()
297298
{
@@ -333,7 +334,7 @@ public function testSupportsNoop()
333334
$expectedConfiguration['cache_manager']['generate_url_type'] = 'auto';
334335
$expectedConfiguration['tags']['enabled'] = 'auto';
335336
$expectedConfiguration['invalidation']['enabled'] = 'auto';
336-
$expectedConfiguration['user_context']['logout_handler']['enabled'] = 'auto';
337+
$expectedConfiguration['user_context']['logout_handler']['enabled'] = true;
337338

338339
$formats = array_map(function ($path) {
339340
return __DIR__.'/../../Resources/Fixtures/'.$path;
@@ -398,7 +399,7 @@ public function testSplitOptions()
398399
$expectedConfiguration['cache_manager']['generate_url_type'] = 'auto';
399400
$expectedConfiguration['tags']['enabled'] = 'auto';
400401
$expectedConfiguration['invalidation']['enabled'] = 'auto';
401-
$expectedConfiguration['user_context']['logout_handler']['enabled'] = 'auto';
402+
$expectedConfiguration['user_context']['logout_handler']['enabled'] = true;
402403

403404
$formats = array_map(function ($path) {
404405
return __DIR__.'/../../Resources/Fixtures/'.$path;
@@ -594,6 +595,54 @@ public function testInvalidationNoCacheManager()
594595
}
595596
}
596597

598+
public function userContextLogoutHandlerProvider()
599+
{
600+
return [
601+
'auto no client' => ['config/user_context_auto_noclient.yml', false, false, false, false, null],
602+
'auto ban client' => ['config/user_context_auto_banclient.yml', true, true, 'custom', 'auto', null],
603+
'auto non ban client' => ['config/user_context_auto_notbanclient.yml', false, 'auto', 'nginx', 'auto', null],
604+
'true no client' => ['config/user_context_true_noclient.yml', null, false, 'auto', false, 'you need to configure a ban capable proxy_client'],
605+
'true ban client' => ['config/user_context_true_banclient.yml', true, true, 'custom', 'auto', null],
606+
'true non ban client' => ['config/user_context_true_notbanclient.yml', null, true, 'nginx', 'auto', 'you need to configure a ban capable proxy_client'],
607+
];
608+
}
609+
610+
/**
611+
* @dataProvider userContextLogoutHandlerProvider
612+
*/
613+
public function testUserContextLogoutHandler(string $configFile, $expected, $cacheManager, $proxyClient, $tags, $exception)
614+
{
615+
$configFile = __DIR__.'/../../Resources/Fixtures/'.$configFile;
616+
if ($exception) {
617+
try {
618+
$this->assertProcessedConfigurationEquals([], [$configFile]);
619+
$this->fail('No exception thrown on invalid configuration');
620+
} catch (InvalidConfigurationException $e) {
621+
$this->assertContains($exception, $e->getMessage());
622+
}
623+
624+
return;
625+
}
626+
627+
$expectedConfiguration = $this->getEmptyConfig();
628+
$expectedConfiguration['cache_manager']['enabled'] = $cacheManager;
629+
if ('custom' === $proxyClient) {
630+
$expectedConfiguration['cache_manager']['custom_proxy_client'] = 'acme.proxy_client';
631+
} elseif ('nginx' === $proxyClient) {
632+
$expectedConfiguration['proxy_client'][$proxyClient]['http'] = [
633+
'servers' => ['localhost'],
634+
'base_url' => null,
635+
'http_client' => null,
636+
];
637+
$expectedConfiguration['proxy_client'][$proxyClient]['purge_location'] = false;
638+
}
639+
$expectedConfiguration['tags']['enabled'] = $tags;
640+
$expectedConfiguration['invalidation']['enabled'] = $tags;
641+
$expectedConfiguration['user_context']['enabled'] = true;
642+
$expectedConfiguration['user_context']['logout_handler']['enabled'] = $expected;
643+
$this->assertProcessedConfigurationEquals($expectedConfiguration, [$configFile]);
644+
}
645+
597646
public function testInvalidDate()
598647
{
599648
$formats = array_map(function ($path) {

0 commit comments

Comments
 (0)