/** * Test the access method. */ public function testAccess() { $request = new Request(array()); $this->controllerResolver->expects($this->at(0))->method('getControllerFromDefinition')->with('\\Drupal\\Tests\\Core\\Access\\TestController::accessDeny')->will($this->returnValue(array(new TestController(), 'accessDeny'))); $this->argumentsResolver->expects($this->at(0))->method('getArguments')->will($this->returnValue(array())); $this->controllerResolver->expects($this->at(1))->method('getControllerFromDefinition')->with('\\Drupal\\Tests\\Core\\Access\\TestController::accessAllow')->will($this->returnValue(array(new TestController(), 'accessAllow'))); $this->argumentsResolver->expects($this->at(1))->method('getArguments')->will($this->returnValue(array())); $this->controllerResolver->expects($this->at(2))->method('getControllerFromDefinition')->with('\\Drupal\\Tests\\Core\\Access\\TestController::accessParameter')->will($this->returnValue(array(new TestController(), 'accessParameter'))); $this->argumentsResolver->expects($this->at(2))->method('getArguments')->will($this->returnValue(array('parameter' => 'TRUE'))); $route = new Route('/test-route', array(), array('_custom_access' => '\\Drupal\\Tests\\Core\\Access\\TestController::accessDeny')); $account = $this->getMock('Drupal\\Core\\Session\\AccountInterface'); $this->assertSame(AccessInterface::DENY, $this->accessChecker->access($route, $request, $account)); $route = new Route('/test-route', array(), array('_custom_access' => '\\Drupal\\Tests\\Core\\Access\\TestController::accessAllow')); $this->assertSame(AccessInterface::ALLOW, $this->accessChecker->access($route, $request, $account)); $route = new Route('/test-route', array('parameter' => 'TRUE'), array('_custom_access' => '\\Drupal\\Tests\\Core\\Access\\TestController::accessParameter')); $this->assertSame(AccessInterface::ALLOW, $this->accessChecker->access($route, $request, $account)); }
/** * Performs the specified access check. * * @param string $service_id * The access check service ID to use. * @param \Symfony\Component\Routing\Route $route * The route to check access to. * @param \Symfony\Component\HttpFoundation\Request $request * The incoming request object. * @param \Drupal\Core\Session\AccountInterface $account * The current user. * * @throws \Drupal\Core\Access\AccessException * Thrown when the access check returns an invalid value. * * @return string * A \Drupal\Core\Access\AccessInterface constant value. */ protected function performCheck($service_id, $route, $request, $account) { $callable = array($this->checks[$service_id], $this->checkMethods[$service_id]); $arguments = $this->argumentsResolver->getArguments($callable, $route, $request, $account); $service_access = call_user_func_array($callable, $arguments); if (!in_array($service_access, array(AccessInterface::ALLOW, AccessInterface::DENY, AccessInterface::KILL), TRUE)) { throw new AccessException("Access error in {$service_id}. Access services can only return AccessInterface::ALLOW, AccessInterface::DENY, or AccessInterface::KILL constants."); } return $service_access; }
/** * Tests that an access checker throws an exception for not allowed values. * * @dataProvider providerCheckException * * @expectedException \Drupal\Core\Access\AccessException */ public function testCheckException($return_value, $access_mode) { $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_mode' => $access_mode, '_access_checks' => array('test_incorrect_value')); $route = new Route('', array(), $requirements, $options); $route_provider->expects($this->any())->method('getRouteByName')->will($this->returnValue($route)); $this->argumentsResolver->expects($this->any())->method('getArguments')->will($this->returnCallback(function ($callable, $route, $request, $account) { return array($route); })); $request = new Request(); $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->urlGenerator, $this->paramConverter, $this->argumentsResolver, $this->requestStack); $access_manager->setContainer($container); $access_manager->addCheckService('test_incorrect_value', 'access'); $access_manager->checkNamedRoute('test_incorrect_value', array(), $this->account, $request); }
/** * Checks access for the account and route using the custom access checker. * * @param \Symfony\Component\Routing\Route $route * The route to check against. * @param \Symfony\Component\HttpFoundation\Request $request * The request object. * @param \Drupal\Core\Session\AccountInterface $account * The currently logged in account. * * @return string * A \Drupal\Core\Access\AccessInterface constant value. */ public function access(Route $route, Request $request, AccountInterface $account) { $callable = $this->controllerResolver->getControllerFromDefinition($route->getRequirement('_custom_access')); $arguments = $this->argumentsResolver->getArguments($callable, $route, $request, $account); return call_user_func_array($callable, $arguments); }