Commit 291b9981 authored by alexpott's avatar alexpott

Issue #2302331 by kim.pepper: Move drupal_valid_path to PathValidator service.

parent 90fc3fa7
......@@ -435,6 +435,9 @@ services:
path.matcher:
class: Drupal\Core\Path\PathMatcher
arguments: ['@config.factory']
path.validator:
class: Drupal\Core\Path\PathValidator
arguments: ['@router', '@router.route_provider', '@request_stack', '@access_manager', '@current_user']
# The argument to the hashing service defined in services.yml, to the
# constructor of PhpassHashedPassword is the log2 number of iterations for
......
......@@ -5,9 +5,7 @@
* Functions to handle paths in Drupal.
*/
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\ParamConverter\ParamNotConvertedException;
use Drupal\Core\Routing\RequestHelper;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
/**
......@@ -108,36 +106,10 @@ function path_is_admin($path) {
* @return bool
* TRUE if it is a valid path AND the current user has access permission,
* FALSE otherwise.
*
* @deprecated Use \Drupal::service('path.validator')->isValid($path) service
* instead.
*/
function drupal_valid_path($path) {
// External URLs and the front page are always valid.
if ($path == '<front>' || UrlHelper::isExternal($path)) {
return TRUE;
}
// Check the routing system.
$collection = \Drupal::service('router.route_provider')->getRoutesByPattern('/' . $path);
if ($collection->count() == 0) {
return FALSE;
}
$request = RequestHelper::duplicate(\Drupal::request(), '/' . $path);
$request->attributes->set('_system_path', $path);
// We indicate that a menu administrator is running the menu access check.
$request->attributes->set('_menu_admin', TRUE);
// Attempt to match this path to provide a fully built request to the
// access checker.
try {
$request->attributes->add(\Drupal::service('router')->matchRequest($request));
}
catch (ParamNotConvertedException $e) {
return FALSE;
}
// Consult the accsss manager.
$routes = $collection->all();
$route = reset($routes);
return \Drupal::service('access_manager')->check($route, $request, \Drupal::currentUser());
return \Drupal::service('path.validator')->isValid($path);
}
<?php
/**
* @file
* Contains Drupal\Core\Path\PathValidator
*/
namespace Drupal\Core\Path;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Access\AccessManager;
use Drupal\Core\ParamConverter\ParamNotConvertedException;
use Drupal\Core\Routing\RequestHelper;
use Drupal\Core\Routing\RouteProviderInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
/**
* Provides a default path validator and access checker.
*/
class PathValidator implements PathValidatorInterface {
/**
* The request matcher.
*
* @var \Symfony\Component\Routing\Matcher\RequestMatcherInterface
*/
protected $requestMatcher;
/**
* The route provider.
*
* @var \Drupal\Core\Routing\RouteProviderInterface
*/
protected $routeProvider;
/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;
/**
* The access manager.
*
* @var \Drupal\Core\Access\AccessManager
*/
protected $accessManager;
/**
* The user account.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $account;
/**
* Creates a new PathValidator.
*
* @param \Symfony\Component\Routing\Matcher\RequestMatcherInterface $request_matcher
* The request matcher.
* @param \Drupal\Core\Routing\RouteProviderInterface $route_provider
* The route provider.
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The request stack.
* @param \Drupal\Core\Access\AccessManager $access_manager
* The access manager.
* @param \Drupal\Core\Session\AccountInterface $account
* The user account.
*/
public function __construct(RequestMatcherInterface $request_matcher, RouteProviderInterface $route_provider, RequestStack $request_stack, AccessManager $access_manager, AccountInterface $account) {
$this->requestMatcher = $request_matcher;
$this->routeProvider = $route_provider;
$this->requestStack = $request_stack;
$this->accessManager = $access_manager;
$this->account = $account;
}
/**
* {@inheritdoc}
*/
public function isValid($path) {
// External URLs and the front page are always valid.
if ($path == '<front>' || UrlHelper::isExternal($path)) {
return TRUE;
}
// Check the routing system.
$collection = $this->routeProvider->getRoutesByPattern('/' . $path);
if ($collection->count() == 0) {
return FALSE;
}
$request = RequestHelper::duplicate($this->requestStack->getCurrentRequest(), '/' . $path);
$request->attributes->set('_system_path', $path);
// We indicate that a menu administrator is running the menu access check.
$request->attributes->set('_menu_admin', TRUE);
// Attempt to match this path to provide a fully built request to the
// access checker.
try {
$request->attributes->add($this->requestMatcher->matchRequest($request));
}
catch (ParamNotConvertedException $e) {
return FALSE;
}
// Consult the access manager.
$routes = $collection->all();
$route = reset($routes);
return $this->accessManager->check($route, $request, $this->account);
}
}
<?php
/**
* @file
* Contains Drupal\Core\Path\PathValidatorInterface
*/
namespace Drupal\Core\Path;
/**
* Provides an interface for url path validators.
*/
interface PathValidatorInterface {
/**
* Checks if the URL path is valid and accessible by the current user.
*
* @param string $path
* The path to check.
*
* @return bool
* TRUE if the path is valid.
*/
public function isValid($path);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment