Skip to content
This repository was archived by the owner on May 31, 2024. It is now read-only.

Commit c46b28d

Browse files
Merge branch '2.6' into 2.7
* 2.6: [2.6] Towards 100% HHVM compat [Security/Http] Fix test [Stopwatch] Fix test Minor fixes Towards 100% HHVM compat unify default AccessDeniedExeption message trigger event with right user (add test) [Security] Initialize SwitchUserEvent::targetUser on attemptExitUser [Form] Fixed: Data mappers always receive forms indexed by their names Conflicts: src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php src/Symfony/Component/VarDumper/Tests/CliDumperTest.php src/Symfony/Component/VarDumper/Tests/HtmlDumperTest.php
2 parents a98ce20 + 5e1c369 commit c46b28d

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

Core/Exception/AccessDeniedException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
class AccessDeniedException extends \RuntimeException
2020
{
21-
public function __construct($message = 'Access Denied', \Exception $previous = null)
21+
public function __construct($message = 'Access Denied.', \Exception $previous = null)
2222
{
2323
parent::__construct($message, 403, $previous);
2424
}

Http/Firewall/SwitchUserListener.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ private function attemptExitUser(Request $request)
162162
}
163163

164164
if (null !== $this->dispatcher) {
165-
$switchEvent = new SwitchUserEvent($request, $original->getUser());
165+
$user = $this->provider->refreshUser($original->getUser());
166+
$switchEvent = new SwitchUserEvent($request, $user);
166167
$this->dispatcher->dispatch(SecurityEvents::SWITCH_USER, $switchEvent);
167168
}
168169

Http/Tests/Firewall/SwitchUserListenerTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111

1212
namespace Symfony\Component\Security\Http\Tests\Firewall;
1313

14+
use Symfony\Component\Security\Http\Event\SwitchUserEvent;
1415
use Symfony\Component\Security\Http\Firewall\SwitchUserListener;
16+
use Symfony\Component\Security\Http\SecurityEvents;
1517

1618
class SwitchUserListenerTest extends \PHPUnit_Framework_TestCase
1719
{
@@ -100,6 +102,62 @@ public function testExitUserUpdatesToken()
100102
$listener->handle($this->event);
101103
}
102104

105+
public function testExitUserDispatchesEventWithRefreshedUser()
106+
{
107+
$originalUser = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
108+
$refreshedUser = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
109+
$this
110+
->userProvider
111+
->expects($this->any())
112+
->method('refreshUser')
113+
->with($originalUser)
114+
->willReturn($refreshedUser);
115+
$originalToken = $this->getToken();
116+
$originalToken
117+
->expects($this->any())
118+
->method('getUser')
119+
->willReturn($originalUser);
120+
$role = $this
121+
->getMockBuilder('Symfony\Component\Security\Core\Role\SwitchUserRole')
122+
->disableOriginalConstructor()
123+
->getMock();
124+
$role->expects($this->any())->method('getSource')->willReturn($originalToken);
125+
$this
126+
->securityContext
127+
->expects($this->any())
128+
->method('getToken')
129+
->willReturn($this->getToken(array($role)));
130+
$this
131+
->request
132+
->expects($this->any())
133+
->method('get')
134+
->with('_switch_user')
135+
->willReturn('_exit');
136+
$this
137+
->request
138+
->expects($this->any())
139+
->method('getUri')
140+
->willReturn('/');
141+
$this
142+
->request
143+
->query
144+
->expects($this->any())
145+
->method('all')
146+
->will($this->returnValue(array()));
147+
148+
$dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
149+
$dispatcher
150+
->expects($this->once())
151+
->method('dispatch')
152+
->with(SecurityEvents::SWITCH_USER, $this->callback(function (SwitchUserEvent $event) use ($refreshedUser) {
153+
return $event->getTargetUser() === $refreshedUser;
154+
}))
155+
;
156+
157+
$listener = new SwitchUserListener($this->securityContext, $this->userProvider, $this->userChecker, 'provider123', $this->accessDecisionManager, null, '_switch_user', 'ROLE_ALLOWED_TO_SWITCH', $dispatcher);
158+
$listener->handle($this->event);
159+
}
160+
103161
/**
104162
* @expectedException \Symfony\Component\Security\Core\Exception\AccessDeniedException
105163
*/

0 commit comments

Comments
 (0)