Skip to content

Commit 93431d2

Browse files
committed
Merge branch '2.8' into 3.0
* 2.8: (23 commits) [Filesystem] Better error handling in remove() [DependencyInjection] Add coverage for invalid Expression in exportParameters [DependencyInjection] Add coverage for all invalid arguments in exportParameters anonymous services are always private [Console] Correct time formatting. [WebProfilerBundle] Fixed error from unset twig variable Force profiler toolbar svg display [DependencyInjection] Resolve aliases before removing abstract services + add tests Fix Dom Crawler select option with empty value Remove unnecessary option assignment fix tests (use non-deprecated options) remove unused variable mock the proper method [PropertyAccess] Fix regression [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 ...
2 parents 10c937c + dea146b commit 93431d2

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-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: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,59 @@ public function testExitUserDispatchesEventWithRefreshedUser()
158158
$listener->handle($this->event);
159159
}
160160

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

0 commit comments

Comments
 (0)