Skip to content

Commit c1e056a

Browse files
committed
Passing the newly generated security token to the event during user switching.
Event allows listeners to easily switch out the token if custom token updates are required
1 parent fe995a3 commit c1e056a

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

src/Symfony/Component/Security/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
3.3.0
5+
-----
6+
7+
* added `TokenInterface` to `\Symfony\Component\Security\Http\Event\SwitchUserEvent` to allow listeners to switch out
8+
the token when custom token generation is required by application.
9+
410
3.2.0
511
-----
612

src/Symfony/Component/Security/Http/Event/SwitchUserEvent.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Security\Http\Event;
1313

1414
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1516
use Symfony\Component\Security\Core\User\UserInterface;
1617
use Symfony\Component\EventDispatcher\Event;
1718

@@ -24,11 +25,13 @@ class SwitchUserEvent extends Event
2425
{
2526
private $request;
2627
private $targetUser;
28+
private $token;
2729

28-
public function __construct(Request $request, UserInterface $targetUser)
30+
public function __construct(Request $request, UserInterface $targetUser, TokenInterface $token = null)
2931
{
3032
$this->request = $request;
3133
$this->targetUser = $targetUser;
34+
$this->token = $token;
3235
}
3336

3437
/**
@@ -46,4 +49,20 @@ public function getTargetUser()
4649
{
4750
return $this->targetUser;
4851
}
52+
53+
/**
54+
* @return TokenInterface
55+
*/
56+
public function getToken()
57+
{
58+
return $this->token;
59+
}
60+
61+
/**
62+
* @param TokenInterface $token
63+
*/
64+
public function setToken(TokenInterface $token)
65+
{
66+
$this->token = $token;
67+
}
4968
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,10 @@ private function attemptSwitchUser(Request $request)
143143
$token = new UsernamePasswordToken($user, $user->getPassword(), $this->providerKey, $roles);
144144

145145
if (null !== $this->dispatcher) {
146-
$switchEvent = new SwitchUserEvent($request, $token->getUser());
146+
$switchEvent = new SwitchUserEvent($request, $token->getUser(), $token);
147147
$this->dispatcher->dispatch(SecurityEvents::SWITCH_USER, $switchEvent);
148+
//use the token from the event in case any listeners have replaced it.
149+
$token = $switchEvent->getToken();
148150
}
149151

150152
return $token;
@@ -167,8 +169,9 @@ private function attemptExitUser(Request $request)
167169

168170
if (null !== $this->dispatcher && $original->getUser() instanceof UserInterface) {
169171
$user = $this->provider->refreshUser($original->getUser());
170-
$switchEvent = new SwitchUserEvent($request, $user);
172+
$switchEvent = new SwitchUserEvent($request, $user, $original);
171173
$this->dispatcher->dispatch(SecurityEvents::SWITCH_USER, $switchEvent);
174+
$original = $switchEvent->getToken();
172175
}
173176

174177
return $original;

0 commit comments

Comments
 (0)