[Security] changed the HttpUtils constructor to tak both a UrlGenerator and a UrlMatcher instead of a Router (to make it useable by Silex)

This commit is contained in:
Fabien Potencier 2012-06-26 11:17:51 +02:00
parent d131f9d32f
commit 16a0af1262
4 changed files with 38 additions and 32 deletions

View File

@ -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 -->

View File

@ -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

View File

@ -15,7 +15,8 @@ use Symfony\Component\Security\Core\SecurityContextInterface;
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;
@ -26,16 +27,19 @@ use Symfony\Component\Routing\Exception\ResourceNotFoundException;
*/
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;
}
/**
@ -105,7 +109,7 @@ class HttpUtils
{
if ('/' !== $path[0]) {
try {
$parameters = $this->router->match($request->getPathInfo());
$parameters = $this->urlMatcher->match($request->getPathInfo());
return $path === $parameters['_route'];
} catch (MethodNotAllowedException $e) {
@ -120,10 +124,10 @@ class HttpUtils
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);
}
}

View File

@ -30,7 +30,7 @@ class HttpUtilsTest extends \PHPUnit_Framework_TestCase
public function testCreateRedirectResponse()
{
$utils = new HttpUtils($this->getRouter());
$utils = new HttpUtils($this->getUrlGenerator());
// absolute path
$response = $utils->createRedirectResponse($this->getRequest(), '/foobar');
@ -42,14 +42,14 @@ class HttpUtilsTest extends \PHPUnit_Framework_TestCase
$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')))
@ -60,7 +60,7 @@ class HttpUtilsTest extends \PHPUnit_Framework_TestCase
public function testCreateRequest()
{
$utils = new HttpUtils($this->getRouter());
$utils = new HttpUtils($this->getUrlGenerator());
// absolute path
$request = $this->getRequest();
@ -72,13 +72,13 @@ class HttpUtilsTest extends \PHPUnit_Framework_TestCase
$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')))
@ -93,55 +93,55 @@ class HttpUtilsTest extends \PHPUnit_Framework_TestCase
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 = '/')