Issue #2004780 by Ryan Weal, YesCT, tim.plunkett, plach, Gábor Hojtsy: Fixed Unify menu item access checking with mock menu items.

8.0.x
Dries 2013-05-31 14:19:40 -04:00
parent 8e778911dd
commit 6049d2517d
2 changed files with 32 additions and 15 deletions

View File

@ -13,6 +13,7 @@ use Drupal\menu_link\Plugin\Core\Entity\MenuLink;
use Drupal\menu_link\MenuLinkStorageController; use Drupal\menu_link\MenuLinkStorageController;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Route;
/** /**
* @defgroup menu Menu system * @defgroup menu Menu system
@ -778,17 +779,7 @@ function _menu_translate(&$router_item, $map, $to_arg = FALSE) {
if (!empty($router_item['route_name'])) { if (!empty($router_item['route_name'])) {
$route_provider = Drupal::getContainer()->get('router.route_provider'); $route_provider = Drupal::getContainer()->get('router.route_provider');
$route = $route_provider->getRouteByName($router_item['route_name']); $route = $route_provider->getRouteByName($router_item['route_name']);
$request = Request::create('/' . $router_item['href']); $router_item['access'] = menu_item_route_access($route, $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;
}
} }
else { else {
// @todo: Remove once all routes are converted. // @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. // menu_tree_check_access() may set this ahead of time for links to nodes.
if (!isset($item['access'])) { if (!isset($item['access'])) {
if ($route = $item->getRoute()) { if ($route = $item->getRoute()) {
$request = Request::create('/' . $item['path']); $item['access'] = menu_item_route_access($route, $item['href']);
$item['access'] = drupal_container()->get('access_manager')->check($route, $request);
} }
elseif (!empty($item['load_functions']) && !_menu_load_objects($item, $map)) { elseif (!empty($item['load_functions']) && !_menu_load_objects($item, $map)) {
// An error occurred loading an object. // An error occurred loading an object.
@ -956,6 +946,32 @@ function _menu_link_translate(&$item, $translate = FALSE) {
return $map; return $map;
} }
/**
* 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. * Gets a loaded object from a router item.
* *

View File

@ -37,10 +37,11 @@ class MockRouteProvider implements RouteProviderInterface {
/** /**
* Implements \Symfony\Cmf\Component\Routing\RouteProviderInterface::getRouteCollectionForRequest(). * 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) { public function getRouteCollectionForRequest(Request $request) {
return $this->routes;
} }
/** /**