Move secure URL generation to the Router

This commit is contained in:
Dmitriy Simushev 2014-08-22 13:11:17 +00:00
parent 96df4dc857
commit 21c0ee9134
9 changed files with 157 additions and 20 deletions

View File

@ -32,12 +32,12 @@ use Mibew\Http\Exception\HttpException;
use Mibew\Http\Exception\MethodNotAllowedException as MethodNotAllowedHttpException; use Mibew\Http\Exception\MethodNotAllowedException as MethodNotAllowedHttpException;
use Mibew\Http\Exception\NotFoundException as NotFoundHttpException; use Mibew\Http\Exception\NotFoundException as NotFoundHttpException;
use Mibew\Routing\RouterAwareInterface; use Mibew\Routing\RouterAwareInterface;
use Mibew\Routing\RouterInterface;
use Mibew\Routing\Exception\AccessDeniedException as AccessDeniedRoutingException; use Mibew\Routing\Exception\AccessDeniedException as AccessDeniedRoutingException;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\RequestContext; use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Routing\Exception\MethodNotAllowedException as MethodNotAllowedRoutingException; use Symfony\Component\Routing\Exception\MethodNotAllowedException as MethodNotAllowedRoutingException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException as ResourceNotFoundRoutingException; use Symfony\Component\Routing\Exception\ResourceNotFoundException as ResourceNotFoundRoutingException;

View File

@ -24,10 +24,10 @@ use Mibew\Asset\AssetUrlGeneratorInterface;
use Mibew\Authentication\AuthenticationManagerAwareInterface; use Mibew\Authentication\AuthenticationManagerAwareInterface;
use Mibew\Authentication\AuthenticationManagerInterface; use Mibew\Authentication\AuthenticationManagerInterface;
use Mibew\Routing\RouterAwareInterface; use Mibew\Routing\RouterAwareInterface;
use Mibew\Routing\RouterInterface;
use Mibew\Style\StyleInterface; use Mibew\Style\StyleInterface;
use Mibew\Style\PageStyle; use Mibew\Style\PageStyle;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\RedirectResponse;
/** /**
@ -111,16 +111,16 @@ abstract class AbstractController implements
* *
* @param string $route The name of the route. * @param string $route The name of the route.
* @param mixed $parameters An array of parameters. * @param mixed $parameters An array of parameters.
* @param bool|string $referenceType The type of reference (one of the * @param bool|string $reference_type The type of reference (one of the
* constants in UrlGeneratorInterface). * constants in UrlGeneratorInterface).
* *
* @return string The generated URL. * @return string The generated URL.
* *
* @see UrlGeneratorInterface * @see UrlGeneratorInterface
*/ */
public function generateUrl($route, $parameters = array(), $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH) public function generateUrl($route, $parameters = array(), $reference_type = UrlGeneratorInterface::ABSOLUTE_PATH)
{ {
return $this->getRouter()->generate($route, $parameters, $referenceType); return $this->getRouter()->generate($route, $parameters, $reference_type);
} }
/** /**
@ -128,19 +128,14 @@ abstract class AbstractController implements
* *
* @param string $route The name of the route. * @param string $route The name of the route.
* @param mixed $parameters An array of parameters. * @param mixed $parameters An array of parameters.
* @param bool|string $reference_type The type of reference (one of the
* constants in UrlGeneratorInterface).
* *
* @return string The generated URL. * @return string The generated URL.
*/ */
public function generateSecureUrl($route, $parameters = array()) public function generateSecureUrl($route, $parameters = array(), $reference_type = UrlGeneratorInterface::ABSOLUTE_PATH)
{ {
$context = $this->getRouter()->getContext(); return $this->getRouter()->generateSecure($route, $parameters, $reference_type);
return 'https://' . $context->getHost()
. $this->getRouter()->generate(
$route,
$parameters,
UrlGeneratorInterface::ABSOLUTE_PATH
);
} }
/** /**

View File

@ -237,7 +237,7 @@ class ButtonCodeController extends AbstractController
$link_params['group'] = $group; $link_params['group'] = $group;
} }
$link = ($show_host && $force_secure) $link = ($show_host && $force_secure)
? $this->generateSecureUrl('chat_user_start', $link_params) ? $this->generateSecureUrl('chat_user_start', $link_params, $url_type)
: $this->generateUrl('chat_user_start', $link_params, $url_type); : $this->generateUrl('chat_user_start', $link_params, $url_type);
$modsecfix = $mod_security ? ".replace('http://','').replace('https://','')" : ""; $modsecfix = $mod_security ? ".replace('http://','').replace('https://','')" : "";

View File

@ -24,7 +24,7 @@ use Mibew\Asset\AssetUrlGeneratorInterface;
use Mibew\Authentication\AuthenticationManagerAwareInterface; use Mibew\Authentication\AuthenticationManagerAwareInterface;
use Mibew\Authentication\AuthenticationManagerInterface; use Mibew\Authentication\AuthenticationManagerInterface;
use Mibew\Routing\RouterAwareInterface; use Mibew\Routing\RouterAwareInterface;
use Symfony\Component\Routing\RouterInterface; use Mibew\Routing\RouterInterface;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
class ControllerResolver implements class ControllerResolver implements

View File

@ -0,0 +1,48 @@
<?php
/*
* This file is a part of Mibew Messenger.
*
* Copyright 2005-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Mibew\Routing\Generator;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\Exception\InvalidParameterException;
use Symfony\Component\Routing\Exception\MissingMandatoryParametersException;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
interface SecureUrlGeneratorInterface extends UrlGeneratorInterface
{
/**
* Generates a URL or path for a specific route based on the given
* parameters. Unlike {@link UrlGeneratorInterface::generate()} the URLs its
* generate uses HTTPS no matter what scheme is used in a route defenition.
*
* @param string $name The name of the route
* @param mixed $parameters An array of parameters
* @param bool|string $referenceType The type of reference to be generated
* (one of the constants from {@link UrlGeneratorInterface}).
*
* @return string The generated URL
*
* @throws RouteNotFoundException If the named route doesn't exist
* @throws MissingMandatoryParametersException When some parameters are
* missing that are mandatory for the route
* @throws InvalidParameterException When a parameter value for a
* placeholder is not correct because it does not match the requirement
*/
public function generateSecure($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH);
}

View File

@ -0,0 +1,59 @@
<?php
/*
* This file is a part of Mibew Messenger.
*
* Copyright 2005-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Mibew\Routing\Generator;
use Symfony\Component\Routing\Generator\UrlGenerator as BaseUrlGenerator;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
/**
* UrlGenerator can generate a secure URL or a path for any route in the
* RouteCollection based on the passed parameters.
*/
class UrlGenerator extends BaseUrlGenerator implements SecureUrlGeneratorInterface
{
/**
* {@inheritdoc}
*/
public function generateSecure($name, $parameters = array(), $reference_type = self::ABSOLUTE_PATH)
{
if (null === $route = $this->routes->get($name)) {
throw new RouteNotFoundException(sprintf('Unable to generate a URL for the named route "%s" as such route does not exist.', $name));
}
// The Route has a cache of its own and is not recompiled as long as it
// does not get modified.
$compiled_route = $route->compile();
// Force a route to use HTTPS for this time.
$requirements = $route->getRequirements();
$requirements['_scheme'] = 'https';
return $this->doGenerate(
$compiled_route->getVariables(),
$route->getDefaults(),
$requirements,
$compiled_route->getTokens(),
$parameters,
$name,
$reference_type,
$compiled_route->getHostTokens()
);
}
}

View File

@ -21,11 +21,10 @@ namespace Mibew\Routing;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\RequestContext; use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Generator\UrlGenerator;
use Symfony\Component\Routing\Matcher\RequestMatcherInterface; use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
use Symfony\Component\Routing\Matcher\UrlMatcher; use Symfony\Component\Routing\Matcher\UrlMatcher;
use Mibew\Routing\Generator\UrlGenerator;
use Mibew\Routing\RouteCollectionLoader; use Mibew\Routing\RouteCollectionLoader;
class Router implements RouterInterface, RequestMatcherInterface class Router implements RouterInterface, RequestMatcherInterface
@ -93,6 +92,14 @@ class Router implements RouterInterface, RequestMatcherInterface
return $this->getGenerator()->generate($name, $parameters, $referenceType); return $this->getGenerator()->generate($name, $parameters, $referenceType);
} }
/**
* {@inheritdoc}
*/
public function generateSecure($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH)
{
return $this->getGenerator()->generateSecure($name, $parameters, $referenceType);
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */

View File

@ -19,8 +19,6 @@
namespace Mibew\Routing; namespace Mibew\Routing;
use Symfony\Component\Routing\RouterInterface;
/** /**
* An interface for all router aware objects. * An interface for all router aware objects.
*/ */

View File

@ -0,0 +1,30 @@
<?php
/*
* This file is a part of Mibew Messenger.
*
* Copyright 2005-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Mibew\Routing;
use Mibew\Routing\Generator\SecureUrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface as BaseRouterInterface;
/**
* RouterInterface is the interface that all Router classes must implement.
*/
interface RouterInterface extends BaseRouterInterface, SecureUrlGeneratorInterface
{
}