Selaa lähdekoodia

[Security] removed a hack

Fabien Potencier 14 vuotta sitten
vanhempi
commit
4f8a98033a

+ 14 - 9
src/Symfony/Component/Security/Http/HttpUtils.php

@@ -13,7 +13,7 @@ namespace Symfony\Component\Security\Http;
 
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\HttpFoundation\RedirectResponse;
-use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
+use Symfony\Component\Routing\RouterInterface;
 
 /**
  * Encapsulates the logic needed to create sub-requests, redirect the user, and match URLs.
@@ -22,16 +22,16 @@ use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  */
 class HttpUtils
 {
-    private $urlGenerator;
+    private $router;
 
     /**
      * Constructor.
      *
-     * @param UrlGeneratorInterface $urlGenerator An UrlGeneratorInterface instance
+     * @param RouterInterface $router An RouterInterface instance
      */
-    public function __construct(UrlGeneratorInterface $urlGenerator = null)
+    public function __construct(RouterInterface $router = null)
     {
-        $this->urlGenerator = $urlGenerator;
+        $this->router = $router;
     }
 
     /**
@@ -82,7 +82,12 @@ class HttpUtils
     public function checkRequestPath(Request $request, $path)
     {
         if ('/' !== $path[0]) {
-            $path = preg_replace('#'.preg_quote($request->getBaseUrl(), '#').'#', '', $this->generateUrl($path));
+            try {
+                $parameters = $this->router->match($request->getPathInfo());
+
+                return $path === $parameters['_route'];
+            } catch (\Exception $e) {
+            }
         }
 
         return $path === $request->getPathInfo();
@@ -90,10 +95,10 @@ class HttpUtils
 
     private function generateUrl($route, $absolute = false)
     {
-        if (null === $this->urlGenerator) {
-            throw new \LogicException('You must provide a UrlGeneratorInterface instance to be able to use routes.');
+        if (null === $this->router) {
+            throw new \LogicException('You must provide a RouterInterface instance to be able to use routes.');
         }
 
-        return $this->urlGenerator->generate($route, array(), $absolute);
+        return $this->router->generate($route, array(), $absolute);
     }
 }

+ 24 - 9
tests/Symfony/Tests/Component/Security/Http/HttpUtilsTest.php

@@ -19,7 +19,7 @@ class HttpUtilsTest extends \PHPUnit_Framework_TestCase
 {
     public function testCreateRedirectResponse()
     {
-        $utils = new HttpUtils($generator = $this->getUrlGenerator());
+        $utils = new HttpUtils($this->getRouter());
 
         // absolute path
         $response = $utils->createRedirectResponse($this->getRequest(), '/foobar');
@@ -31,8 +31,8 @@ class HttpUtilsTest extends \PHPUnit_Framework_TestCase
         $this->assertTrue($response->isRedirect('http://symfony.com/'));
 
         // route name
-        $utils = new HttpUtils($generator = $this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface'));
-        $generator
+        $utils = new HttpUtils($router = $this->getMock('Symfony\Component\Routing\RouterInterface'));
+        $router
             ->expects($this->any())
             ->method('generate')
             ->with('foobar', array(), true)
@@ -43,7 +43,7 @@ class HttpUtilsTest extends \PHPUnit_Framework_TestCase
 
     public function testCreateRequest()
     {
-        $utils = new HttpUtils($this->getUrlGenerator());
+        $utils = new HttpUtils($this->getRouter());
 
         // absolute path
         $request = $this->getRequest();
@@ -65,25 +65,40 @@ class HttpUtilsTest extends \PHPUnit_Framework_TestCase
 
     public function testCheckRequestPath()
     {
-        $utils = new HttpUtils($this->getUrlGenerator());
+        $utils = new HttpUtils($this->getRouter());
 
         $this->assertTrue($utils->checkRequestPath($this->getRequest(), '/'));
         $this->assertFalse($utils->checkRequestPath($this->getRequest(), '/foo'));
 
+        $router = $this->getMock('Symfony\Component\Routing\RouterInterface');
+        $router
+            ->expects($this->any())
+            ->method('match')
+            ->will($this->returnValue(array()))
+        ;
+        $utils = new HttpUtils($router);
         $this->assertFalse($utils->checkRequestPath($this->getRequest(), 'foobar'));
+
+        $router = $this->getMock('Symfony\Component\Routing\RouterInterface');
+        $router
+            ->expects($this->any())
+            ->method('match')
+            ->will($this->returnValue(array('_route' => 'foobar')))
+        ;
+        $utils = new HttpUtils($router);
         $this->assertTrue($utils->checkRequestPath($this->getRequest('/foo/bar'), 'foobar'));
     }
 
-    private function getUrlGenerator()
+    private function getRouter()
     {
-        $generator = $this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface');
-        $generator
+        $router = $this->getMock('Symfony\Component\Routing\RouterInterface');
+        $router
             ->expects($this->any())
             ->method('generate')
             ->will($this->returnValue('/foo/bar'))
         ;
 
-        return $generator;
+        return $router;
     }
 
     private function getRequest($path = '/')