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

Skip to content

Commit e4896da

Browse files
committed
merged branch jfsimon/security-redirect-attributes (PR #7325)
This PR was squashed before being merged into the 2.1 branch (closes #7325). Commits ------- 6575df6 [Security] use current request attributes to generate redirect url? Discussion ---------- [Security] use current request attributes to generate redirect url? Maybe we should consider to use current request attributes to generate the login/logout redirections URL? | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #5080 --------------------------------------------------------------------------- by Seldaek at 2013-03-11T08:33:37Z Can you explain why? --------------------------------------------------------------------------- by jfsimon at 2013-03-11T09:30:07Z @Seldaek let say I prefixed all my URLs with a `{domain}` var (`_locale` for instance), I'd like it to be passed to my redirected request. I guess it could lead to side effects, that's why I tagged this PR `RFC`. --------------------------------------------------------------------------- by Seldaek at 2013-03-11T09:46:33Z Fair enough. The main issue I see is that you end up with "garbage" query params in the URL. Any params that was needed by the previous page and not needed by the new one ends up as ?foo=bar in the URL. It's usually not harmful, but not very clean either. I'm not sure what it would take to grab all the params that a route can use, and only copy those over. --------------------------------------------------------------------------- by jfsimon at 2013-03-11T10:12:49Z @Seldaek indeed, I didn't think about those query parameters... I'll try to fix this in a simple way this afternoon. --------------------------------------------------------------------------- by jfsimon at 2013-03-11T14:54:31Z @Seldaek tell me if what you think of this, it may look like a hack (which wont be acceptable). --------------------------------------------------------------------------- by Seldaek at 2013-03-11T14:59:39Z Eh I see. I can't say it's the less hacky thing I ever saw, but it might be alright. I don't think I'm the best person to take this call though.. Let's see what @fabpot thinks.
2 parents cefc820 + 6575df6 commit e4896da

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

src/Symfony/Component/Security/Http/HttpUtils.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,25 @@ public function generateUri($request, $path)
136136
return $request->getUriForPath($path);
137137
}
138138

139-
return $this->generateUrl($path, true);
139+
return $this->generateUrl($path, $request->attributes->all(), true);
140140
}
141141

142-
private function generateUrl($route, $absolute = false)
142+
private function generateUrl($route, array $attributes = array(), $absolute = false)
143143
{
144144
if (null === $this->urlGenerator) {
145145
throw new \LogicException('You must provide a UrlGeneratorInterface instance to be able to use routes.');
146146
}
147147

148-
return $this->urlGenerator->generate($route, array(), $absolute);
148+
$url = $this->urlGenerator->generate($route, $attributes, $absolute);
149+
150+
// unnecessary query string parameters must be removed from url
151+
// (ie. query parameters that are presents in $attributes)
152+
// fortunately, they all are, so we have to remove entire query string
153+
$position = strpos($url, '?');
154+
if (false !== $position) {
155+
$url = substr($url, 0, $position);
156+
}
157+
158+
return $url;
149159
}
150160
}

src/Symfony/Component/Security/Tests/Http/HttpUtilsTest.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,25 @@ public function testCheckRequestPathWithUrlMatcherLoadingException()
137137
$utils->checkRequestPath($this->getRequest(), 'foobar');
138138
}
139139

140-
private function getUrlGenerator()
140+
public function testGenerateUrlRemovesQueryString()
141+
{
142+
$method = new \ReflectionMethod('Symfony\Component\Security\Http\HttpUtils', 'generateUrl');
143+
$method->setAccessible(true);
144+
145+
$utils = new HttpUtils($this->getUrlGenerator());
146+
$this->assertEquals('/foo/bar', $method->invoke($utils, 'route_name'));
147+
148+
$utils = new HttpUtils($this->getUrlGenerator('/foo/bar?param=value'));
149+
$this->assertEquals('/foo/bar', $method->invoke($utils, 'route_name'));
150+
}
151+
152+
private function getUrlGenerator($generatedUrl = '/foo/bar')
141153
{
142154
$urlGenerator = $this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface');
143155
$urlGenerator
144156
->expects($this->any())
145157
->method('generate')
146-
->will($this->returnValue('/foo/bar'))
158+
->will($this->returnValue($generatedUrl))
147159
;
148160

149161
return $urlGenerator;

0 commit comments

Comments
 (0)