/** * Checks access for the account and route using the custom access checker. * * @param \Drupal\Core\Routing\RouteMatchInterface $route_match * The route match object to be checked. * @param \Drupal\Core\Session\AccountInterface $account * The account being checked. * * @return \Drupal\Core\Access\AccessResultInterface * The access result. */ public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account) { $callable = $this->controllerResolver->getControllerFromDefinition($route->getRequirement('_custom_access')); $arguments_resolver = $this->argumentsResolverFactory->getArgumentsResolver($route_match, $account); $arguments = $arguments_resolver->getArguments($callable); return call_user_func_array($callable, $arguments); }
/** * Add default expectations to the access arguments resolver factory. */ protected function setupAccessArgumentsResolverFactory($constraint = NULL) { if (!isset($constraint)) { $constraint = $this->any(); } return $this->argumentsResolverFactory->expects($constraint)->method('getArgumentsResolver')->will($this->returnCallback(function ($route_match, $account) { $resolver = $this->getMock('Drupal\\Component\\Utility\\ArgumentsResolverInterface'); $resolver->expects($this->any())->method('getArguments')->will($this->returnCallback(function ($callable) use($route_match) { return array($route_match->getRouteObject()); })); return $resolver; })); }
/** * Test the access method. */ public function testAccess() { $route_match = $this->getMock('Drupal\\Core\\Routing\\RouteMatchInterface'); $this->controllerResolver->expects($this->at(0))->method('getControllerFromDefinition')->with('\\Drupal\\Tests\\Core\\Access\\TestController::accessDeny')->will($this->returnValue(array(new TestController(), 'accessDeny'))); $resolver0 = $this->getMock('Drupal\\Component\\Utility\\ArgumentsResolverInterface'); $resolver0->expects($this->once())->method('getArguments')->will($this->returnValue(array())); $this->argumentsResolverFactory->expects($this->at(0))->method('getArgumentsResolver')->will($this->returnValue($resolver0)); $this->controllerResolver->expects($this->at(1))->method('getControllerFromDefinition')->with('\\Drupal\\Tests\\Core\\Access\\TestController::accessAllow')->will($this->returnValue(array(new TestController(), 'accessAllow'))); $resolver1 = $this->getMock('Drupal\\Component\\Utility\\ArgumentsResolverInterface'); $resolver1->expects($this->once())->method('getArguments')->will($this->returnValue(array())); $this->argumentsResolverFactory->expects($this->at(1))->method('getArgumentsResolver')->will($this->returnValue($resolver1)); $this->controllerResolver->expects($this->at(2))->method('getControllerFromDefinition')->with('\\Drupal\\Tests\\Core\\Access\\TestController::accessParameter')->will($this->returnValue(array(new TestController(), 'accessParameter'))); $resolver2 = $this->getMock('Drupal\\Component\\Utility\\ArgumentsResolverInterface'); $resolver2->expects($this->once())->method('getArguments')->will($this->returnValue(array('parameter' => 'TRUE'))); $this->argumentsResolverFactory->expects($this->at(2))->method('getArgumentsResolver')->will($this->returnValue($resolver2)); $route = new Route('/test-route', array(), array('_custom_access' => '\\Drupal\\Tests\\Core\\Access\\TestController::accessDeny')); $account = $this->getMock('Drupal\\Core\\Session\\AccountInterface'); $this->assertEquals(AccessResult::neutral(), $this->accessChecker->access($route, $route_match, $account)); $route = new Route('/test-route', array(), array('_custom_access' => '\\Drupal\\Tests\\Core\\Access\\TestController::accessAllow')); $this->assertEquals(AccessResult::allowed(), $this->accessChecker->access($route, $route_match, $account)); $route = new Route('/test-route', array('parameter' => 'TRUE'), array('_custom_access' => '\\Drupal\\Tests\\Core\\Access\\TestController::accessParameter')); $this->assertEquals(AccessResult::allowed(), $this->accessChecker->access($route, $route_match, $account)); }
/** * {@inheritdoc} */ public function check(RouteMatchInterface $route_match, AccountInterface $account = NULL, Request $request = NULL, $return_as_object = FALSE) { if (!isset($account)) { $account = $this->currentUser; } $route = $route_match->getRouteObject(); $checks = $route->getOption('_access_checks') ?: array(); $conjunction = $route->getOption('_access_mode') ?: static::ACCESS_MODE_ALL; // Filter out checks which require the incoming request. if (!isset($request)) { $checks = array_diff($checks, $this->checkProvider->getChecksNeedRequest()); } $result = AccessResult::neutral(); if (!empty($checks)) { $arguments_resolver = $this->argumentsResolverFactory->getArgumentsResolver($route_match, $account, $request); if ($conjunction == static::ACCESS_MODE_ALL) { $result = $this->checkAll($checks, $arguments_resolver); } else { $result = $this->checkAny($checks, $arguments_resolver); } } return $return_as_object ? $result : $result->isAllowed(); }
/** * {@inheritdoc} */ public function check(RouteMatchInterface $route_match, AccountInterface $account = NULL, Request $request = NULL, $return_as_object = FALSE) { if (!isset($account)) { $account = $this->currentUser; } $route = $route_match->getRouteObject(); $checks = $route->getOption('_access_checks') ?: array(); // Filter out checks which require the incoming request. if (!isset($request)) { $checks = array_diff($checks, $this->checkProvider->getChecksNeedRequest()); } $result = AccessResult::neutral(); if (!empty($checks)) { $arguments_resolver = $this->argumentsResolverFactory->getArgumentsResolver($route_match, $account, $request); if (!$checks) { return AccessResult::neutral(); } $result = AccessResult::allowed(); foreach ($checks as $service_id) { $result = $result->andIf($this->performCheck($service_id, $arguments_resolver)); } } return $return_as_object ? $result : $result->isAllowed(); }