View source
<?php
namespace Drupal\Core\Access;
use Drupal\Core\ParamConverter\ParamConverterManagerInterface;
use Drupal\Core\ParamConverter\ParamNotConvertedException;
use Drupal\Core\Routing\RouteMatch;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Routing\RouteProviderInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Component\Utility\ArgumentsResolverInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Drupal\Core\Routing\RouteObjectInterface;
class AccessManager implements AccessManagerInterface {
protected $routeProvider;
protected $paramConverterManager;
protected $argumentsResolverFactory;
protected $currentUser;
protected $checkProvider;
public function __construct(RouteProviderInterface $route_provider, ParamConverterManagerInterface $paramconverter_manager, AccessArgumentsResolverFactoryInterface $arguments_resolver_factory, AccountInterface $current_user, CheckProviderInterface $check_provider) {
$this->routeProvider = $route_provider;
$this->paramConverterManager = $paramconverter_manager;
$this->argumentsResolverFactory = $arguments_resolver_factory;
$this->currentUser = $current_user;
$this->checkProvider = $check_provider;
}
public function checkNamedRoute($route_name, array $parameters = [], AccountInterface $account = NULL, $return_as_object = FALSE) {
try {
$route = $this->routeProvider
->getRouteByName($route_name);
$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) {
$result = AccessResult::forbidden()
->addCacheTags([
'config:core.extension',
]);
return $return_as_object ? $result : $result
->isAllowed();
} catch (ParamNotConvertedException $e) {
$result = AccessResult::forbidden()
->setCacheMaxAge(0);
return $return_as_object ? $result : $result
->isAllowed();
}
}
public function checkRequest(Request $request, AccountInterface $account = NULL, $return_as_object = FALSE) {
$route_match = RouteMatch::createFromRequest($request);
return $this
->check($route_match, $account, $request, $return_as_object);
}
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') ?: [];
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);
$result = AccessResult::allowed();
foreach ($checks as $service_id) {
$result = $result
->andIf($this
->performCheck($service_id, $arguments_resolver));
}
}
return $return_as_object ? $result : $result
->isAllowed();
}
protected function performCheck($service_id, ArgumentsResolverInterface $arguments_resolver) {
$callable = $this->checkProvider
->loadCheck($service_id);
$arguments = $arguments_resolver
->getArguments($callable);
$service_access = call_user_func_array($callable, $arguments);
if (!$service_access instanceof AccessResultInterface) {
throw new AccessException("Access error in {$service_id}. Access services must return an object that implements AccessResultInterface.");
}
return $service_access;
}
}