Skip to content

Commit 799d8ae

Browse files
committed
Allow disabling redirect on login
1 parent aad3e78 commit 799d8ae

File tree

6 files changed

+35
-12
lines changed

6 files changed

+35
-12
lines changed

src/Symfony/Bundle/SecurityBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* The `security.access_control` now accepts a `RequestMatcherInterface` under the `request_matcher` option as scope configuration
88
* Display the inherited roles of the logged-in user in the Web Debug Toolbar
9+
* Allow disabling the redirection on successful logout by passing `null` to the `target` option
910

1011
6.0
1112
---

src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -400,10 +400,12 @@ private function createFirewall(ContainerBuilder $container, string $id, array $
400400
'logout_path' => $firewall['logout']['path'],
401401
]);
402402

403-
$logoutSuccessListenerId = 'security.logout.listener.default.'.$id;
404-
$container->setDefinition($logoutSuccessListenerId, new ChildDefinition('security.logout.listener.default'))
405-
->replaceArgument(1, $firewall['logout']['target'])
406-
->addTag('kernel.event_subscriber', ['dispatcher' => $firewallEventDispatcherId]);
403+
if (null !== $firewall['logout']['target']) {
404+
$logoutSuccessListenerId = 'security.logout.listener.default.'.$id;
405+
$container->setDefinition($logoutSuccessListenerId, new ChildDefinition('security.logout.listener.default'))
406+
->replaceArgument(1, $firewall['logout']['target'])
407+
->addTag('kernel.event_subscriber', ['dispatcher' => $firewallEventDispatcherId]);
408+
}
407409

408410
// add CSRF provider
409411
if (isset($firewall['logout']['csrf_token_generator'])) {

src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/SecurityExtensionTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,25 @@ public function testConfigureCustomFirewallListener()
780780
$this->assertContains('custom_firewall_listener_id', $firewallListeners);
781781
}
782782

783+
public function testDisableLogoutTarget()
784+
{
785+
$container = $this->getRawContainer();
786+
787+
$container->loadFromExtension('security', [
788+
'firewalls' => [
789+
'main' => [
790+
'logout' => [
791+
'target' => null,
792+
],
793+
],
794+
],
795+
]);
796+
797+
$container->compile();
798+
799+
$this->assertFalse($container->hasDefinition('security.logout.listener.default.main'));
800+
}
801+
783802
protected function getRawContainer()
784803
{
785804
$container = new ContainerBuilder();

src/Symfony/Bundle/SecurityBundle/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"symfony/password-hasher": "^5.4|^6.0",
2828
"symfony/security-core": "^5.4|^6.0",
2929
"symfony/security-csrf": "^5.4|^6.0",
30-
"symfony/security-http": "^5.4|^6.0"
30+
"symfony/security-http": "^5.4|^6.1"
3131
},
3232
"require-dev": {
3333
"doctrine/annotations": "^1.10.4",

src/Symfony/Component/Security/Http/Firewall/LogoutListener.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,12 @@ public function authenticate(RequestEvent $event)
8787
$this->eventDispatcher->dispatch($logoutEvent);
8888

8989
$response = $logoutEvent->getResponse();
90-
if (!$response instanceof Response) {
91-
throw new \RuntimeException('No logout listener set the Response, make sure at least the DefaultLogoutListener is registered.');
92-
}
9390

9491
$this->tokenStorage->setToken(null);
9592

96-
$event->setResponse($response);
93+
if ($response) {
94+
$event->setResponse($response);
95+
}
9796
}
9897

9998
/**

src/Symfony/Component/Security/Http/Tests/Firewall/LogoutListenerTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,6 @@ public function testHandleMatchedPathWithoutCsrfValidation()
122122

123123
public function testNoResponseSet()
124124
{
125-
$this->expectException(\RuntimeException::class);
126-
127125
[$listener, , $httpUtils, $options] = $this->getListener();
128126

129127
$request = new Request();
@@ -133,7 +131,11 @@ public function testNoResponseSet()
133131
->with($request, $options['logout_path'])
134132
->willReturn(true);
135133

136-
$listener(new RequestEvent($this->createMock(HttpKernelInterface::class), $request, HttpKernelInterface::MAIN_REQUEST));
134+
$event = new RequestEvent($this->createMock(HttpKernelInterface::class), $request, HttpKernelInterface::MAIN_REQUEST);
135+
136+
$listener($event);
137+
138+
$this->assertNull($event->getResponse());
137139
}
138140

139141
/**

0 commit comments

Comments
 (0)