/** * {@inheritdoc} */ public function enhance(array $defaults, Request $request) { // Just run the parameter conversion once per request. if (!isset($defaults['_raw_variables'])) { $defaults['_raw_variables'] = $this->copyRawVariables($defaults); $defaults = $this->paramConverterManager->convert($defaults); } return $defaults; }
/** * @covers ::copyRawVariables() */ public function testCopyRawVariables() { $route = new Route('/test/{id}'); $defaults = array(RouteObjectInterface::ROUTE_OBJECT => $route, 'id' => '1'); // Set one default to mirror another by reference. $defaults['bar'] =& $defaults['id']; $this->paramConverterManager->expects($this->any())->method('convert')->with($this->isType('array'), $this->isInstanceOf('Symfony\\Component\\HttpFoundation\\Request'))->will($this->returnCallback(function ($defaults) { // Convert the mirrored default to another value. $defaults['bar'] = '2'; return $defaults; })); $expected = new ParameterBag(array('id' => 1)); $result = $this->paramConversionEnhancer->enhance($defaults, new Request()); $this->assertEquals($result['_raw_variables'], $expected); }
/** * Tests the checkNamedRoute with default values. * * @covers \Drupal\Core\Access\AccessManager::checkNamedRoute() */ public function testCheckNamedRouteWithDefaultValue() { $this->routeCollection = new RouteCollection(); $route = new Route('/test-route-1/{value}', array('value' => 'example'), array('_test_access' => 'TRUE')); $this->routeCollection->add('test_route_1', $route); $this->routeProvider = $this->getMock('Drupal\\Core\\Routing\\RouteProviderInterface'); $this->routeProvider->expects($this->any())->method('getRouteByName')->with('test_route_1', array())->will($this->returnValue($route)); $map = array(); $map[] = array('test_route_1', array('value' => 'example'), '/test-route-1/example'); $this->urlGenerator = $this->getMock('Symfony\\Component\\Routing\\Generator\\UrlGeneratorInterface'); $this->urlGenerator->expects($this->any())->method('generate')->with('test_route_1', array())->will($this->returnValueMap($map)); $this->paramConverter = $this->getMock('Drupal\\Core\\ParamConverter\\ParamConverterManagerInterface'); $this->paramConverter->expects($this->at(0))->method('convert')->with(array('value' => 'example', RouteObjectInterface::ROUTE_OBJECT => $route))->will($this->returnValue(array('value' => 'upcasted_value'))); $this->argumentsResolver->expects($this->atLeastOnce())->method('getArguments')->will($this->returnCallback(function ($callable, $route, $request, $account) { return array($route); })); $subrequest = Request::create('/test-route-1/example'); $this->accessManager = new AccessManager($this->routeProvider, $this->urlGenerator, $this->paramConverter, $this->argumentsResolver, $this->requestStack); $this->accessManager->setContainer($this->container); $this->requestStack->push(new Request()); $access_check = $this->getMock('Drupal\\Tests\\Core\\Access\\TestAccessCheckInterface'); $access_check->expects($this->atLeastOnce())->method('applies')->will($this->returnValue(TRUE)); $access_check->expects($this->atLeastOnce())->method('access')->will($this->returnValue(AccessInterface::KILL)); $subrequest->attributes->set('value', 'upcasted_value'); $this->container->set('test_access', $access_check); $this->accessManager->addCheckService('test_access', 'access'); $this->accessManager->setChecks($this->routeCollection); $this->assertFalse($this->accessManager->checkNamedRoute('test_route_1', array(), $this->account)); }
/** * {@inheritdoc} */ public function checkNamedRoute($route_name, array $parameters = array(), AccountInterface $account = NULL, $return_as_object = FALSE) { try { $route = $this->routeProvider->getRouteByName($route_name, $parameters); // ParamConverterManager relies on the route name and object being // available from the parameters array. $parameters[RouteObjectInterface::ROUTE_NAME] = $route_name; $parameters[RouteObjectInterface::ROUTE_OBJECT] = $route; $upcasted_parameters = $this->paramConverterManager->convert($parameters + $route->getDefaults()); $route_match = new RouteMatch($route_name, $route, $upcasted_parameters, $parameters); return $this->check($route_match, $account, NULL, $return_as_object); } catch (RouteNotFoundException $e) { // Cacheable until extensions change. $result = AccessResult::forbidden()->addCacheTags(['config:core.extension']); return $return_as_object ? $result : $result->isAllowed(); } catch (ParamNotConvertedException $e) { // Uncacheable because conversion of the parameter may not have been // possible due to dynamic circumstances. $result = AccessResult::forbidden()->setCacheMaxAge(0); return $return_as_object ? $result : $result->isAllowed(); } }
/** * Checks a named route with parameters against applicable access check services. * * Determines whether the route is accessible or not. * * @param string $route_name * The route to check access to. * @param array $parameters * Optional array of values to substitute into the route path patern. * @param \Drupal\Core\Session\AccountInterface $account * The current user. * @param \Symfony\Component\HttpFoundation\Request $route_request * Optional incoming request object. If not provided, one will be built * using the route information and the current request from the container. * * @return bool * Returns TRUE if the user has access to the route, otherwise FALSE. */ public function checkNamedRoute($route_name, array $parameters = array(), AccountInterface $account, Request $route_request = NULL) { try { $route = $this->routeProvider->getRouteByName($route_name, $parameters); if (empty($route_request)) { // Create a request and copy the account from the current request. $defaults = $parameters + $route->getDefaults(); $route_request = RequestHelper::duplicate($this->requestStack->getCurrentRequest(), $this->urlGenerator->generate($route_name, $defaults)); $defaults[RouteObjectInterface::ROUTE_OBJECT] = $route; $route_request->attributes->add($this->paramConverterManager->convert($defaults, $route_request)); } return $this->check($route, $route_request, $account); } catch (RouteNotFoundException $e) { return FALSE; } catch (ParamNotConvertedException $e) { return FALSE; } }
/** * Tests that an access checker throws an exception for not allowed values. * * @dataProvider providerCheckException * * @expectedException \Drupal\Core\Access\AccessException */ public function testCheckException($return_value) { $route_provider = $this->getMock('Drupal\\Core\\Routing\\RouteProviderInterface'); // Setup a test route for each access configuration. $requirements = array('_test_incorrect_value' => 'TRUE'); $options = array('_access_checks' => array('test_incorrect_value')); $route = new Route('', array(), $requirements, $options); $route_provider->expects($this->any())->method('getRouteByName')->will($this->returnValue($route)); $this->paramConverter = $this->getMock('Drupal\\Core\\ParamConverter\\ParamConverterManagerInterface'); $this->paramConverter->expects($this->any())->method('convert')->will($this->returnValue(array())); $this->setupAccessArgumentsResolverFactory(); $container = new ContainerBuilder(); // Register a service that will return an incorrect value. $access_check = $this->getMock('Drupal\\Tests\\Core\\Access\\TestAccessCheckInterface'); $access_check->expects($this->any())->method('access')->will($this->returnValue($return_value)); $container->set('test_incorrect_value', $access_check); $access_manager = new AccessManager($route_provider, $this->paramConverter, $this->argumentsResolverFactory, $this->currentUser, $this->checkProvider); $this->checkProvider->setContainer($container); $this->checkProvider->addCheckService('test_incorrect_value', 'access'); $access_manager->checkNamedRoute('test_incorrect_value', array(), $this->account); }
/** * {@inheritdoc} */ public function checkNamedRoute($route_name, array $parameters = array(), AccountInterface $account, Request $route_request = NULL) { try { $route = $this->routeProvider->getRouteByName($route_name, $parameters); if (empty($route_request)) { // Create a cloned request with fresh attributes. $route_request = RequestHelper::duplicate($this->requestStack->getCurrentRequest(), $this->urlGenerator->generate($route_name, $parameters)); $route_request->attributes->replace(array()); // Populate $route_request->attributes with both raw and converted // parameters. $parameters += $route->getDefaults(); $route_request->attributes->set('_raw_variables', new ParameterBag($parameters)); $parameters[RouteObjectInterface::ROUTE_OBJECT] = $route; $route_request->attributes->add($this->paramConverterManager->convert($parameters, $route_request)); } return $this->check($route, $route_request, $account); } catch (RouteNotFoundException $e) { return FALSE; } catch (ParamNotConvertedException $e) { return FALSE; } }
/** * Applies parameter converters to route parameters. * * @param \Drupal\Core\Routing\RouteBuildEvent $event * The event to process. */ public function onRoutingRouteAlterSetParameterConverters(RouteBuildEvent $event) { $this->paramConverterManager->setRouteParameterConverters($event->getRouteCollection()); }
/** * {@inheritdoc} */ public function enhance(array $defaults, Request $request) { $defaults['_raw_variables'] = $this->copyRawVariables($defaults); return $this->paramConverterManager->convert($defaults); }