Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / lib / Drupal / Core / Access / CustomAccessCheck.php
1 <?php
2
3 namespace Drupal\Core\Access;
4
5 use Drupal\Core\Controller\ControllerResolverInterface;
6 use Drupal\Core\Routing\Access\AccessInterface as RoutingAccessInterface;
7 use Drupal\Core\Routing\RouteMatchInterface;
8 use Drupal\Core\Session\AccountInterface;
9 use Symfony\Component\Routing\Route;
10
11 /**
12  * Defines an access checker that allows specifying a custom method for access.
13  *
14  * You should only use it when you are sure that the access callback will not be
15  * reused. Good examples in core are Edit or Toolbar module.
16  *
17  * The method is called on another instance of the controller class, so you
18  * cannot reuse any stored property of your actual controller instance used
19  * to generate the output.
20  */
21 class CustomAccessCheck implements RoutingAccessInterface {
22
23   /**
24    * The controller resolver.
25    *
26    * @var \Drupal\Core\Controller\ControllerResolverInterface
27    */
28   protected $controllerResolver;
29
30   /**
31    * The arguments resolver.
32    *
33    * @var \Drupal\Core\Access\AccessArgumentsResolverFactoryInterface
34    */
35   protected $argumentsResolverFactory;
36
37   /**
38    * Constructs a CustomAccessCheck instance.
39    *
40    * @param \Drupal\Core\Controller\ControllerResolverInterface $controller_resolver
41    *   The controller resolver.
42    * @param \Drupal\Core\Access\AccessArgumentsResolverFactoryInterface $arguments_resolver_factory
43    *   The arguments resolver factory.
44    */
45   public function __construct(ControllerResolverInterface $controller_resolver, AccessArgumentsResolverFactoryInterface $arguments_resolver_factory) {
46     $this->controllerResolver = $controller_resolver;
47     $this->argumentsResolverFactory = $arguments_resolver_factory;
48   }
49
50   /**
51    * Checks access for the account and route using the custom access checker.
52    *
53    * @param \Symfony\Component\Routing\Route $route
54    *   The route.
55    * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
56    *   The route match object to be checked.
57    * @param \Drupal\Core\Session\AccountInterface $account
58    *   The account being checked.
59    *
60    * @return \Drupal\Core\Access\AccessResultInterface
61    *   The access result.
62    */
63   public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account) {
64     $callable = $this->controllerResolver->getControllerFromDefinition($route->getRequirement('_custom_access'));
65     $arguments_resolver = $this->argumentsResolverFactory->getArgumentsResolver($route_match, $account);
66     $arguments = $arguments_resolver->getArguments($callable);
67
68     return call_user_func_array($callable, $arguments);
69   }
70
71 }