summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Core/AuthenticationEvents.php4
-rw-r--r--Http/Firewall/SwitchUserListener.php3
-rw-r--r--Tests/Http/Firewall/SwitchUserListenerTest.php47
3 files changed, 53 insertions, 1 deletions
diff --git a/Core/AuthenticationEvents.php b/Core/AuthenticationEvents.php
index 90b7142..13bce30 100644
--- a/Core/AuthenticationEvents.php
+++ b/Core/AuthenticationEvents.php
@@ -20,6 +20,8 @@ final class AuthenticationEvents
* The event listener method receives a
* Symfony\Component\Security\Core\Event\AuthenticationEvent instance.
*
+ * @Event
+ *
* @var string
*/
const AUTHENTICATION_SUCCESS = 'security.authentication.success';
@@ -32,6 +34,8 @@ final class AuthenticationEvents
* Symfony\Component\Security\Core\Event\AuthenticationFailureEvent
* instance.
*
+ * @Event
+ *
* @var string
*/
const AUTHENTICATION_FAILURE = 'security.authentication.failure';
diff --git a/Http/Firewall/SwitchUserListener.php b/Http/Firewall/SwitchUserListener.php
index 79b715a..b983b68 100644
--- a/Http/Firewall/SwitchUserListener.php
+++ b/Http/Firewall/SwitchUserListener.php
@@ -13,6 +13,7 @@ namespace Symfony\Component\Security\Http\Firewall;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Core\SecurityContextInterface;
+use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\User\UserCheckerInterface;
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
@@ -162,7 +163,7 @@ class SwitchUserListener implements ListenerInterface
throw new AuthenticationCredentialsNotFoundException('Could not find original Token object.');
}
- if (null !== $this->dispatcher) {
+ if (null !== $this->dispatcher && $original->getUser() instanceof UserInterface) {
$user = $this->provider->refreshUser($original->getUser());
$switchEvent = new SwitchUserEvent($request, $user);
$this->dispatcher->dispatch(SecurityEvents::SWITCH_USER, $switchEvent);
diff --git a/Tests/Http/Firewall/SwitchUserListenerTest.php b/Tests/Http/Firewall/SwitchUserListenerTest.php
index 1852751..4e795c8 100644
--- a/Tests/Http/Firewall/SwitchUserListenerTest.php
+++ b/Tests/Http/Firewall/SwitchUserListenerTest.php
@@ -149,6 +149,53 @@ class SwitchUserListenerTest extends \PHPUnit_Framework_TestCase
$listener->handle($this->event);
}
+ public function testExitUserDoesNotDispatchEventWithStringUser()
+ {
+ $originalUser = 'anon.';
+ $this
+ ->userProvider
+ ->expects($this->never())
+ ->method('refreshUser');
+ $originalToken = $this->getToken();
+ $originalToken
+ ->expects($this->any())
+ ->method('getUser')
+ ->willReturn($originalUser);
+ $role = $this
+ ->getMockBuilder('Symfony\Component\Security\Core\Role\SwitchUserRole')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $role
+ ->expects($this->any())
+ ->method('getSource')
+ ->willReturn($originalToken);
+ $this
+ ->securityContext
+ ->expects($this->any())
+ ->method('getToken')
+ ->willReturn($this->getToken(array($role)));
+ $this
+ ->request
+ ->expects($this->any())
+ ->method('get')
+ ->with('_switch_user')
+ ->willReturn('_exit');
+ $this
+ ->request
+ ->expects($this->any())
+ ->method('getUri')
+ ->willReturn('/');
+
+ $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
+ $dispatcher
+ ->expects($this->never())
+ ->method('dispatch')
+ ;
+
+ $listener = new SwitchUserListener($this->securityContext, $this->userProvider, $this->userChecker, 'provider123', $this->accessDecisionManager, null, '_switch_user', 'ROLE_ALLOWED_TO_SWITCH', $dispatcher);
+ $listener->handle($this->event);
+ }
+
/**
* @expectedException \Symfony\Component\Security\Core\Exception\AccessDeniedException
*/