Skip to content

Commit 078d425

Browse files
committed
Merge branch '2.7' into 2.8
* 2.7: [HttpFoundation] Improve phpdoc [Logging] Add support for firefox in ChromePhpHandler Windows 10 version check in just one line Detect CLI color support for Windows 10 build 10586 [Security] Fixed SwitchUserListener when exiting an impersonication with AnonymousToken [EventDispatcher] Try first if the event is Stopped [FrameworkBundle] fixes grammar in container:debug command manual. [Form] fix "prototype" not required when parent form is not required
2 parents c2d1f6b + 93914bd commit 078d425

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

Firewall/SwitchUserListener.php

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

1414
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
15+
use Symfony\Component\Security\Core\User\UserInterface;
1516
use Symfony\Component\Security\Core\User\UserProviderInterface;
1617
use Symfony\Component\Security\Core\User\UserCheckerInterface;
1718
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
@@ -161,7 +162,7 @@ private function attemptExitUser(Request $request)
161162
throw new AuthenticationCredentialsNotFoundException('Could not find original Token object.');
162163
}
163164

164-
if (null !== $this->dispatcher) {
165+
if (null !== $this->dispatcher && $original->getUser() instanceof UserInterface) {
165166
$user = $this->provider->refreshUser($original->getUser());
166167
$switchEvent = new SwitchUserEvent($request, $user);
167168
$this->dispatcher->dispatch(SecurityEvents::SWITCH_USER, $switchEvent);

Tests/Firewall/SwitchUserListenerTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,54 @@ public function testExitUserDispatchesEventWithRefreshedUser()
158158
$listener->handle($this->event);
159159
}
160160

161+
public function testExitUserDoesNotDispatchEventWithStringUser()
162+
{
163+
$originalUser = 'anon.';
164+
$refreshedUser = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
165+
$this
166+
->userProvider
167+
->expects($this->never())
168+
->method('refreshUser');
169+
$originalToken = $this->getToken();
170+
$originalToken
171+
->expects($this->any())
172+
->method('getUser')
173+
->willReturn($originalUser);
174+
$role = $this
175+
->getMockBuilder('Symfony\Component\Security\Core\Role\SwitchUserRole')
176+
->disableOriginalConstructor()
177+
->getMock();
178+
$role
179+
->expects($this->any())
180+
->method('getSource')
181+
->willReturn($originalToken);
182+
$this
183+
->tokenStorage
184+
->expects($this->any())
185+
->method('getToken')
186+
->willReturn($this->getToken(array($role)));
187+
$this
188+
->request
189+
->expects($this->any())
190+
->method('all')
191+
->with('_switch_user')
192+
->willReturn('_exit');
193+
$this
194+
->request
195+
->expects($this->any())
196+
->method('getUri')
197+
->willReturn('/');
198+
199+
$dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
200+
$dispatcher
201+
->expects($this->never())
202+
->method('dispatch')
203+
;
204+
205+
$listener = new SwitchUserListener($this->tokenStorage, $this->userProvider, $this->userChecker, 'provider123', $this->accessDecisionManager, null, '_switch_user', 'ROLE_ALLOWED_TO_SWITCH', $dispatcher);
206+
$listener->handle($this->event);
207+
}
208+
161209
/**
162210
* @expectedException \Symfony\Component\Security\Core\Exception\AccessDeniedException
163211
*/

0 commit comments

Comments
 (0)