Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5765f69

Browse files
author
Amrouche Hamza
committed
[Security] Dispatch an event when "logout user on change" steps in
1 parent278a7ec commit5765f69

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\Security\Http\Event;
13+
14+
useSymfony\Component\Security\Core\Authentication\Token\TokenInterface;
15+
useSymfony\Component\Security\Core\User\UserInterface;
16+
useSymfony\Contracts\EventDispatcher\Event;
17+
18+
/**
19+
* @author Hamza Amrouche <hamza.simperfit@gmail.com>
20+
*/
21+
class LogoutOnChangeEventextends Event
22+
{
23+
private$token;
24+
private$user;
25+
26+
publicfunction__construct(TokenInterface$token,UserInterface$user)
27+
{
28+
$this->token =$token;
29+
$this->user =$user;
30+
}
31+
32+
/**
33+
* @return TokenInterface The token used to refresh the user
34+
*/
35+
publicfunctiongetToken():TokenInterface
36+
{
37+
return$this->token;
38+
}
39+
40+
/**
41+
* @return UserInterface The user refreshed by the provider
42+
*/
43+
publicfunctiongetUser():UserInterface
44+
{
45+
return$this->user;
46+
}
47+
}

‎src/Symfony/Component/Security/Http/Firewall/ContextListener.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
useSymfony\Component\Security\Core\Role\SwitchUserRole;
2929
useSymfony\Component\Security\Core\User\UserInterface;
3030
useSymfony\Component\Security\Core\User\UserProviderInterface;
31+
useSymfony\Component\Security\Http\Event\LogoutOnChangeEvent;
32+
useSymfony\Component\Security\Http\SecurityEvents;
3133

3234
/**
3335
* ContextListener manages the SecurityContext persistence through a session.
@@ -186,6 +188,10 @@ protected function refreshUser(TokenInterface $token)
186188
$this->logger->debug('Cannot refresh token because user has changed.', ['username' =>$refreshedUser->getUsername(),'provider' =>\get_class($provider)]);
187189
}
188190

191+
if (null !==$this->dispatcher) {
192+
$this->dispatcher->dispatch(newLogoutOnChangeEvent($token,$refreshedUser), SecurityEvents::LOGOUT_ON_CHANGE);
193+
}
194+
189195
continue;
190196
}
191197

‎src/Symfony/Component/Security/Http/SecurityEvents.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,12 @@ final class SecurityEvents
3131
* @Event("Symfony\Component\Security\Http\Event\SwitchUserEvent")
3232
*/
3333
constSWITCH_USER ='security.switch_user';
34+
35+
/**
36+
* The LOGOUT_ON_CHANGE event occurs when a token has been deauthenticated
37+
* because of a user change.
38+
*
39+
* @Event("Symfony\Component\Security\Http\Event\LogoutOnChangeEvent")
40+
*/
41+
constLOGOUT_ON_CHANGE ='security.logout_on_change';
3442
}

‎src/Symfony/Component/Security/Http/Tests/Firewall/ContextListenerTest.php‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
useSymfony\Component\Security\Core\User\UserInterface;
3333
useSymfony\Component\Security\Core\User\UserProviderInterface;
3434
useSymfony\Component\Security\Http\Firewall\ContextListener;
35+
useSymfony\Component\Security\Http\SecurityEvents;
3536

3637
class ContextListenerTestextends TestCase
3738
{
@@ -313,6 +314,30 @@ public function testAcceptsProvidersAsTraversable()
313314
$this->assertSame($refreshedUser,$tokenStorage->getToken()->getUser());
314315
}
315316

317+
publicfunctiontestLogoutOnChangeEventAsBeenSent()
318+
{
319+
$tokenStorage =newTokenStorage();
320+
$refreshedUser =newUser('foobar','baz');
321+
322+
$user =newUser('foo','bar');
323+
$session =newSession(newMockArraySessionStorage());
324+
$session->set('_security_context_key',serialize(newUsernamePasswordToken($user,'','context_key', ['ROLE_USER'])));
325+
326+
$request =newRequest();
327+
$request->setSession($session);
328+
$request->cookies->set('MOCKSESSID',true);
329+
330+
$eventDispatcher =newEventDispatcher();
331+
$eventDispatcher->addListener(SecurityEvents::LOGOUT_ON_CHANGE,function ($event)use ($refreshedUser) {
332+
$this->assertEquals($event->getWrappedEvent()->getUser(),$refreshedUser);
333+
});
334+
335+
$listener =newContextListener($tokenStorage, [newNotSupportingUserProvider(),newSupportingUserProvider($refreshedUser)],'context_key',null,$eventDispatcher);
336+
$listener(newRequestEvent($this->getMockBuilder(HttpKernelInterface::class)->getMock(),$request, HttpKernelInterface::MASTER_REQUEST));
337+
338+
$this->assertNull($tokenStorage->getToken());
339+
}
340+
316341
protectedfunctionrunSessionOnKernelResponse($newToken,$original =null)
317342
{
318343
$session =newSession(newMockArraySessionStorage());

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp