Skip to content

Commit 6c8a7ce

Browse files
committed
[Security] Add a method in the security helper to ease programmatic logout (#40663)
1 parent af77943 commit 6c8a7ce

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

src/Symfony/Bundle/SecurityBundle/Resources/config/security.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@
9090
->args([service_locator([
9191
'security.token_storage' => service('security.token_storage'),
9292
'security.authorization_checker' => service('security.authorization_checker'),
93+
'request_stack' => service('request_stack'),
94+
'event_dispatcher' => service('event_dispatcher')
9395
])])
9496
->alias(Security::class, 'security.helper')
9597

src/Symfony/Component/Security/Core/Security.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1616
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
1717
use Symfony\Component\Security\Core\User\UserInterface;
18+
use Symfony\Component\Security\Http\Event\LogoutEvent;
1819

1920
/**
2021
* Helper class for commonly-needed security tasks.
@@ -69,4 +70,15 @@ public function getToken(): ?TokenInterface
6970
{
7071
return $this->container->get('security.token_storage')->getToken();
7172
}
73+
74+
/**
75+
* Logout the current user automatically. Dispatch the logout event.
76+
*/
77+
public function autoLogout(): void
78+
{
79+
$request = $this->container->get('request_stack')->getCurrentRequest();
80+
$logoutEvent = new LogoutEvent($request, $this->container->get('security.token_storage')->getToken());
81+
$this->container->get('event_dispatcher')->dispatch($logoutEvent);
82+
$this->container->get('security.token_storage')->setToken();
83+
}
7284
}

src/Symfony/Component/Security/Core/Tests/SecurityTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Psr\Container\ContainerInterface;
16+
use Symfony\Component\HttpFoundation\Request;
17+
use Symfony\Component\HttpFoundation\RequestStack;
1618
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
1719
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1820
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
1921
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
2022
use Symfony\Component\Security\Core\Security;
2123
use Symfony\Component\Security\Core\User\InMemoryUser;
24+
use Symfony\Component\Security\Http\Event\LogoutEvent;
25+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
2226

2327
class SecurityTest extends TestCase
2428
{
@@ -85,6 +89,52 @@ public function testIsGranted()
8589
$this->assertTrue($security->isGranted('SOME_ATTRIBUTE', 'SOME_SUBJECT'));
8690
}
8791

92+
public function testAutoLogout(): void
93+
{
94+
$request = new Request();
95+
$requestStack = $this->createMock(RequestStack::class);
96+
$requestStack
97+
->expects($this->once())
98+
->method('getCurrentRequest')
99+
->willReturn($request)
100+
;
101+
102+
$token = $this->createMock(TokenInterface::class);
103+
$tokenStorage = $this->createMock(TokenStorageInterface::class);
104+
$tokenStorage
105+
->expects($this->once())
106+
->method('getToken')
107+
->willReturn($token)
108+
;
109+
$tokenStorage
110+
->expects($this->once())
111+
->method('setToken')
112+
;
113+
114+
$eventDispatcher = $this->createMock(EventDispatcherInterface::class);
115+
$eventDispatcher
116+
->expects($this->once())
117+
->method('dispatch')
118+
->with(new LogoutEvent($request, $token))
119+
;
120+
121+
$container = $this->createMock(ContainerInterface::class);
122+
$container
123+
->expects($this->atLeastOnce())
124+
->method('get')
125+
->willReturnMap([
126+
['request_stack', $requestStack],
127+
['security.token_storage', $tokenStorage],
128+
['event_dispatcher', $eventDispatcher],
129+
])
130+
;
131+
132+
133+
134+
$security = new Security($container);
135+
$security->autoLogout();
136+
}
137+
88138
private function createContainer($serviceId, $serviceObject)
89139
{
90140
$container = $this->createMock(ContainerInterface::class);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy