diff --git a/core/includes/menu.inc b/core/includes/menu.inc index a58a674..bf94563 100644 --- a/core/includes/menu.inc +++ b/core/includes/menu.inc @@ -13,6 +13,7 @@ use Drupal\menu_link\MenuLinkStorageController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; +use Symfony\Component\Routing\Route; /** * @defgroup menu Menu system @@ -778,17 +779,7 @@ function _menu_translate(&$router_item, $map, $to_arg = FALSE) { if (!empty($router_item['route_name'])) { $route_provider = Drupal::getContainer()->get('router.route_provider'); $route = $route_provider->getRouteByName($router_item['route_name']); - $request = Request::create('/' . $router_item['href']); - $request->attributes->set('system_path', $router_item['href']); - // Attempt to match this path to provide a fully built request to the - // acccess checker. - try { - $request->attributes->add(Drupal::service('router.dynamic')->matchRequest($request)); - $router_item['access'] = Drupal::service('access_manager')->check($route, $request); - } - catch (NotFoundHttpException $e) { - $router_item['access'] = FALSE; - } + $router_item['access'] = menu_item_route_access($route, $router_item['href']); } else { // @todo: Remove once all routes are converted. @@ -926,8 +917,7 @@ function _menu_link_translate(&$item, $translate = FALSE) { // menu_tree_check_access() may set this ahead of time for links to nodes. if (!isset($item['access'])) { if ($route = $item->getRoute()) { - $request = Request::create('/' . $item['path']); - $item['access'] = drupal_container()->get('access_manager')->check($route, $request); + $item['access'] = menu_item_route_access($route, $item['href']); } elseif (!empty($item['load_functions']) && !_menu_load_objects($item, $map)) { // An error occurred loading an object. @@ -957,6 +947,32 @@ function _menu_link_translate(&$item, $translate = FALSE) { } /** + * Checks access to a menu item by mocking a request for a path. + * + * @param \Symfony\Component\Routing\Route $route + * Router for the given menu item. + * @param string $href + * Menu path as returned by $item['href'] of menu_get_item(). + * + * @return bool + * TRUE if the user has access or FALSE if the user should be presented + * with access denied. + */ +function menu_item_route_access(Route $route, $href) { + $request = Request::create('/' . $href); + $request->attributes->set('system_path', $href); + // Attempt to match this path to provide a fully built request to the + // access checker. + try { + $request->attributes->add(Drupal::service('router.dynamic')->matchRequest($request)); + return Drupal::service('access_manager')->check($route, $request); + } + catch (NotFoundHttpException $e) { + return FALSE; + } +} + +/** * Gets a loaded object from a router item. * * menu_get_object() provides access to objects loaded by the current router diff --git a/core/modules/system/lib/Drupal/system/Tests/Routing/MockRouteProvider.php b/core/modules/system/lib/Drupal/system/Tests/Routing/MockRouteProvider.php index 8cdf57e..3934949 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Routing/MockRouteProvider.php +++ b/core/modules/system/lib/Drupal/system/Tests/Routing/MockRouteProvider.php @@ -37,10 +37,11 @@ public function __construct(RouteCollection $routes) { /** * Implements \Symfony\Cmf\Component\Routing\RouteProviderInterface::getRouteCollectionForRequest(). * - * Not implemented at present as it is not needed. + * Simply return all routes to prevent + * \Symfony\Component\Routing\Exception\ResourceNotFoundException. */ public function getRouteCollectionForRequest(Request $request) { - + return $this->routes; } /**