Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[Security] changed the HttpUtils constructor to tak both a UrlGenerat…
…or and a UrlMatcher instead of a Router (to make it useable by Silex)
  • Loading branch information
fabpot committed Jun 26, 2012
1 parent d131f9d commit 16a0af1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 32 deletions.
Expand Up @@ -128,6 +128,7 @@

<service id="security.http_utils" class="%security.http_utils.class%" public="false">
<argument type="service" id="router" on-invalid="null" />
<argument type="service" id="router" on-invalid="null" />
</service>

<!-- Validator -->
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Security/CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
2.1.0
-----

* changed the HttpUtils constructor signature to take a UrlGenerator and a UrlMatcher instead of a Router
* EncoderFactoryInterface::getEncoder() can now also take a class name as an argument
* allow switching to the user that is already impersonated
* added support for the remember_me parameter in the query
Expand Down
22 changes: 13 additions & 9 deletions src/Symfony/Component/Security/Http/HttpUtils.php
Expand Up @@ -15,7 +15,8 @@

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;

Expand All @@ -26,16 +27,19 @@
*/
class HttpUtils
{
private $router;
private $urlGenerator;
private $urlMatcher;

/**
* Constructor.
*
* @param RouterInterface $router An RouterInterface instance
* @param UrlGeneratorInterface $urlGenerator A UrlGeneratorInterface instance
* @param UrlMatcherInterface $urlMatcher A UrlMatcherInterface instance
*/
public function __construct(RouterInterface $router = null)
public function __construct(UrlGeneratorInterface $urlGenerator = null, UrlMatcherInterface $urlMatcher = null)
{
$this->router = $router;
$this->urlGenerator = $urlGenerator;
$this->urlMatcher = $urlMatcher;
}

/**
Expand Down Expand Up @@ -105,7 +109,7 @@ public function checkRequestPath(Request $request, $path)
{
if ('/' !== $path[0]) {
try {
$parameters = $this->router->match($request->getPathInfo());
$parameters = $this->urlMatcher->match($request->getPathInfo());

return $path === $parameters['_route'];
} catch (MethodNotAllowedException $e) {
Expand All @@ -120,10 +124,10 @@ public function checkRequestPath(Request $request, $path)

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

return $this->router->generate($route, array(), $absolute);
return $this->urlGenerator->generate($route, array(), $absolute);
}
}
46 changes: 23 additions & 23 deletions src/Symfony/Component/Security/Tests/Http/HttpUtilsTest.php
Expand Up @@ -30,7 +30,7 @@ protected function setUp()

public function testCreateRedirectResponse()
{
$utils = new HttpUtils($this->getRouter());
$utils = new HttpUtils($this->getUrlGenerator());

// absolute path
$response = $utils->createRedirectResponse($this->getRequest(), '/foobar');
Expand All @@ -42,14 +42,14 @@ public function testCreateRedirectResponse()
$this->assertTrue($response->isRedirect('http://symfony.com/'));

// route name
$utils = new HttpUtils($router = $this->getMockBuilder('Symfony\Component\Routing\Router')->disableOriginalConstructor()->getMock());
$router
$utils = new HttpUtils($urlGenerator = $this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface'));
$urlGenerator
->expects($this->any())
->method('generate')
->with('foobar', array(), true)
->will($this->returnValue('http://localhost/foo/bar'))
;
$router
$urlGenerator
->expects($this->any())
->method('getContext')
->will($this->returnValue($this->getMock('Symfony\Component\Routing\RequestContext')))
Expand All @@ -60,7 +60,7 @@ public function testCreateRedirectResponse()

public function testCreateRequest()
{
$utils = new HttpUtils($this->getRouter());
$utils = new HttpUtils($this->getUrlGenerator());

// absolute path
$request = $this->getRequest();
Expand All @@ -72,13 +72,13 @@ public function testCreateRequest()
$this->assertEquals('bar', $subRequest->server->get('Foo'));

// route name
$utils = new HttpUtils($router = $this->getMockBuilder('Symfony\Component\Routing\Router')->disableOriginalConstructor()->getMock());
$router
$utils = new HttpUtils($urlGenerator = $this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface'));
$urlGenerator
->expects($this->once())
->method('generate')
->will($this->returnValue('/foo/bar'))
;
$router
$urlGenerator
->expects($this->any())
->method('getContext')
->will($this->returnValue($this->getMock('Symfony\Component\Routing\RequestContext')))
Expand All @@ -93,55 +93,55 @@ public function testCreateRequest()

public function testCheckRequestPath()
{
$utils = new HttpUtils($this->getRouter());
$utils = new HttpUtils($this->getUrlGenerator());

$this->assertTrue($utils->checkRequestPath($this->getRequest(), '/'));
$this->assertFalse($utils->checkRequestPath($this->getRequest(), '/foo'));

$router = $this->getMock('Symfony\Component\Routing\RouterInterface');
$router
$urlMatcher = $this->getMock('Symfony\Component\Routing\Matcher\UrlMatcherInterface');
$urlMatcher
->expects($this->any())
->method('match')
->will($this->throwException(new ResourceNotFoundException()))
;
$utils = new HttpUtils($router);
$utils = new HttpUtils(null, $urlMatcher);
$this->assertFalse($utils->checkRequestPath($this->getRequest(), 'foobar'));

$router = $this->getMock('Symfony\Component\Routing\RouterInterface');
$router
$urlMatcher = $this->getMock('Symfony\Component\Routing\Matcher\UrlMatcherInterface');
$urlMatcher
->expects($this->any())
->method('match')
->will($this->returnValue(array('_route' => 'foobar')))
;
$utils = new HttpUtils($router);
$utils = new HttpUtils(null, $urlMatcher);
$this->assertTrue($utils->checkRequestPath($this->getRequest('/foo/bar'), 'foobar'));
}

/**
* @expectedException \RuntimeException
*/
public function testCheckRequestPathWithRouterLoadingException()
public function testCheckRequestPathWithUrlMatcherLoadingException()
{
$router = $this->getMock('Symfony\Component\Routing\RouterInterface');
$router
$urlMatcher = $this->getMock('Symfony\Component\Routing\Matcher\UrlMatcherInterface');
$urlMatcher
->expects($this->any())
->method('match')
->will($this->throwException(new \RuntimeException()))
;
$utils = new HttpUtils($router);
$utils = new HttpUtils(null, $urlMatcher);
$utils->checkRequestPath($this->getRequest(), 'foobar');
}

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

return $router;
return $urlGenerator;
}

private function getRequest($path = '/')
Expand Down

0 comments on commit 16a0af1

Please sign in to comment.