Skip to content

Commit 23aad7b

Browse files
committed
[Security] Add a method in the security helper to ease programmatic logout (#40663)
1 parent 27b1654 commit 23aad7b

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
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: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,19 @@
1111

1212
namespace Symfony\Component\Security\Core\Tests;
1313

14+
use PHPUnit\Framework\MockObject\MockObject;
1415
use PHPUnit\Framework\TestCase;
1516
use Psr\Container\ContainerInterface;
17+
use Symfony\Component\HttpFoundation\Request;
18+
use Symfony\Component\HttpFoundation\RequestStack;
1619
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
1720
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1821
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
1922
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
2023
use Symfony\Component\Security\Core\Security;
2124
use Symfony\Component\Security\Core\User\InMemoryUser;
25+
use Symfony\Component\Security\Http\Event\LogoutEvent;
26+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
2227

2328
class SecurityTest extends TestCase
2429
{
@@ -85,7 +90,50 @@ public function testIsGranted()
8590
$this->assertTrue($security->isGranted('SOME_ATTRIBUTE', 'SOME_SUBJECT'));
8691
}
8792

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

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