8000 [Security] Fixed SwitchUserListener when exiting an impersonation with AnonymousToken by lyrixx · Pull Request #18425 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension
8000
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

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;
Expand Down Expand Up @@ -162,7 +163,7 @@ private function attemptExitUser(Request $request)
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,54 @@ public function testExitUserDispatchesEventWithRefreshedUser()
$listener->handle($this->event);
}

public function testExitUserDoesNotDispatchEventWithStringUser()
{
$originalUser = 'anon.';
$refreshedUser = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
$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())
8605 ->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
*/
Expand Down
0