Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7f3878e

Browse files
committed
merged branch vierbergenlars/patch-1 (PR #8854)
This PR was merged into the master branch. Discussion ---------- [Security] Keep other query string parameters when switching users | Q | A | ---------------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | Doc PR | n/a When switching users, the whole query string gets dropped when redirecting after the switch. I think only the switch user parameter should be dropped from the query string. Commits ------- 0a338f5 [Security] Keep other query string parameters when switching users
2 parents 042be41 + 0a338f5 commit 7f3878e

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/Symfony/Component/Security/Http/Firewall/SwitchUserListener.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ public function handle(GetResponseEvent $event)
9292
}
9393
}
9494

95-
$request->server->set('QUERY_STRING', '');
95+
$request->query->remove($this->usernameParameter);
96+
$request->server->set('QUERY_STRING', http_build_query($request->query->all()));
97+
9698
$response = new RedirectResponse($request->getUri(), 302);
9799

98100
$event->setResponse($response);

src/Symfony/Component/Security/Tests/Http/Firewall/SwitchUserListenerTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ protected function setUp()
3434
$this->userChecker = $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface');
3535
$this->accessDecisionManager = $this->getMock('Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface');
3636
$this->request = $this->getMock('Symfony\Component\HttpFoundation\Request');
37+
$this->request->query = $this->getMock('Symfony\Component\HttpFoundation\ParameterBag');
3738
$this->request->server = $this->getMock('Symfony\Component\HttpFoundation\ServerBag');
3839
$this->event = $this->getEvent($this->request);
3940
}
@@ -86,6 +87,8 @@ public function testExitUserUpdatesToken()
8687

8788
$this->request->expects($this->any())->method('get')->with('_switch_user')->will($this->returnValue('_exit'));
8889
$this->request->expects($this->any())->method('getUri')->will($this->returnValue('/'));
90+
$this->request->query->expects($this->once())->method('remove','_switch_user');
91+
$this->request->query->expects($this->any())->method('all')->will($this->returnValue(array()));
8992
$this->request->server->expects($this->once())->method('set')->with('QUERY_STRING', '');
9093

9194
$this->securityContext->expects($this->once())
@@ -123,6 +126,9 @@ public function testSwitchUser()
123126

124127
$this->securityContext->expects($this->any())->method('getToken')->will($this->returnValue($token));
125128
$this->request->expects($this->any())->method('get')->with('_switch_user')->will($this->returnValue('kuba'));
129+
$this->request->query->expects($this->once())->method('remove','_switch_user');
130+
$this->request->query->expects($this->any())->method('all')->will($this->returnValue(array()));
131+
126132
$this->request->expects($this->any())->method('getUri')->will($this->returnValue('/'));
127133
$this->request->server->expects($this->once())->method('set')->with('QUERY_STRING', '');
128134

@@ -142,6 +148,35 @@ public function testSwitchUser()
142148
$listener->handle($this->event);
143149
}
144150

151+
public function testSwitchUserKeepsOtherQueryStringParameters()
152+
{
153+
$token = $this->getToken(array($this->getMock('Symfony\Component\Security\Core\Role\RoleInterface')));
154+
$user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
155+
$user->expects($this->any())->method('getRoles')->will($this->returnValue(array()));
156+
157+
$this->securityContext->expects($this->any())->method('getToken')->will($this->returnValue($token));
158+
$this->request->expects($this->any())->method('get')->with('_switch_user')->will($this->returnValue('kuba'));
159+
$this->request->query->expects($this->once())->method('remove','_switch_user');
160+
$this->request->query->expects($this->any())->method('all')->will($this->returnValue(array('page'=>3,'section'=>2)));
161+
$this->request->expects($this->any())->method('getUri')->will($this->returnValue('/'));
162+
$this->request->server->expects($this->once())->method('set')->with('QUERY_STRING', 'page=3&section=2');
163+
164+
$this->accessDecisionManager->expects($this->once())
165+
->method('decide')->with($token, array('ROLE_ALLOWED_TO_SWITCH'))
166+
->will($this->returnValue(true));
167+
168+
$this->userProvider->expects($this->once())
169+
->method('loadUserByUsername')->with('kuba')
170+
->will($this->returnValue($user));
171+
$this->userChecker->expects($this->once())
172+
->method('checkPostAuth')->with($user);
173+
$this->securityContext->expects($this->once())
174+
->method('setToken')->with($this->isInstanceOf('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken'));
175+
176+
$listener = new SwitchUserListener($this->securityContext, $this->userProvider, $this->userChecker, 'provider123', $this->accessDecisionManager);
177+
$listener->handle($this->event);
178+
}
179+
145180
private function getEvent($request)
146181
{
147182
$event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent')

0 commit comments

Comments
 (0)