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

Skip to content

[Security] use current request attributes to generate redirect url? #7325

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/Symfony/Component/Security/Http/HttpUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,25 @@ public function generateUri($request, $path)
return $request->getUriForPath($path);
}

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

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

return $this->urlGenerator->generate($route, array(), $absolute);
$url = $this->urlGenerator->generate($route, $attributes, $absolute);

// unnecessary query string parameters must be removed from url
// (ie. query parameters that are presents in $attributes)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bad English

// fortunately, they all are, so we have to remove entire query string
$position = strpos($url, '?');
if (false !== $position) {
$url = substr($url, 0, $position);
}

return $url;
}
}
16 changes: 14 additions & 2 deletions src/Symfony/Component/Security/Tests/Http/HttpUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,25 @@ public function testCheckRequestPathWithUrlMatcherLoadingException()
$utils->checkRequestPath($this->getRequest(), 'foobar');
}

private function getUrlGenerator()
public function testGenerateUrlRemovesQueryString()
{
$method = new \ReflectionMethod('Symfony\Component\Security\Http\HttpUtils', 'generateUrl');
$method->setAccessible(true);

$utils = new HttpUtils($this->getUrlGenerator());
$this->assertEquals('/foo/bar', $method->invoke($utils, 'route_name'));

$utils = new HttpUtils($this->getUrlGenerator('/foo/bar?param=value'));
$this->assertEquals('/foo/bar', $method->invoke($utils, 'route_name'));
}

private function getUrlGenerator($generatedUrl = '/foo/bar')
{
$urlGenerator = $this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface');
$urlGenerator
->expects($this->any())
->method('generate')
->will($this->returnValue('/foo/bar'))
->will($this->returnValue($generatedUrl))
;

return $urlGenerator;
Expand Down