|
17 | 17 | use Symfony\Component\HttpKernel\HttpKernelInterface;
|
18 | 18 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
|
19 | 19 | use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
|
| 20 | +use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; |
20 | 21 | use Symfony\Component\Security\Core\Role\SwitchUserRole;
|
21 | 22 | use Symfony\Component\Security\Core\User\User;
|
22 | 23 | use Symfony\Component\Security\Http\Event\SwitchUserEvent;
|
@@ -191,6 +192,36 @@ public function testSwitchUser()
|
191 | 192 | $this->assertInstanceOf('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken', $this->tokenStorage->getToken());
|
192 | 193 | }
|
193 | 194 |
|
| 195 | + public function testSwitchUserAlreadySwitched() |
| 196 | + { |
| 197 | + $originalToken = new UsernamePasswordToken('original', null, 'key', ['ROLE_FOO']); |
| 198 | + $alreadySwitchedToken = new UsernamePasswordToken('switched_1', null, 'key', [new SwitchUserRole('ROLE_PREVIOUS_ADMIN', $originalToken)]); |
| 199 | + |
| 200 | + $tokenStorage = new TokenStorage(); |
| 201 | + $tokenStorage->setToken($alreadySwitchedToken); |
| 202 | + |
| 203 | + $targetUser = new User('kuba', 'password', ['ROLE_FOO', 'ROLE_BAR']); |
| 204 | + $this->request->query->set('_switch_user', 'kuba'); |
| 205 | + |
| 206 | + $this->accessDecisionManager->expects($this->once()) |
| 207 | + ->method('decide')->with($originalToken, ['ROLE_ALLOWED_TO_SWITCH']) |
| 208 | + ->willReturn(true); |
| 209 | + $this->userProvider->expects($this->once()) |
| 210 | + ->method('loadUserByUsername') |
| 211 | + ->with('kuba') |
| 212 | + ->willReturn($targetUser); |
| 213 | + $this->userChecker->expects($this->once()) |
| 214 | + ->method('checkPostAuth')->with($targetUser); |
| 215 | + |
| 216 | + $listener = new SwitchUserListener($tokenStorage, $this->userProvider, $this->userChecker, 'provider123', $this->accessDecisionManager, null, '_switch_user', 'ROLE_ALLOWED_TO_SWITCH', null, false); |
| 217 | + $listener->handle($
67F4
span>this->event); |
| 218 | + |
| 219 | + $this->assertSame([], $this->request->query->all()); |
| 220 | + $this->assertSame('', $this->request->server->get('QUERY_STRING')); |
| 221 | + $this->assertSame('kuba', $tokenStorage->getToken()->getUsername()); |
| 222 | + $this->assertSame($originalToken, $tokenStorage->getToken()->getRoles()[2]->getSource()); |
| 223 | + } |
| 224 | + |
194 | 225 | public function testSwitchUserWorksWithFalsyUsernames()
|
195 | 226 | {
|
196 | 227 | $token = new UsernamePasswordToken('username', '', 'key', ['ROLE_FOO']);
|
|
0 commit comments