class CustomAccessCheck

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Access/CustomAccessCheck.php \Drupal\Core\Access\CustomAccessCheck
  2. 8.9.x core/lib/Drupal/Core/Access/CustomAccessCheck.php \Drupal\Core\Access\CustomAccessCheck
  3. 11.x core/lib/Drupal/Core/Access/CustomAccessCheck.php \Drupal\Core\Access\CustomAccessCheck

Defines an access checker that allows specifying a custom method for access.

You should only use it when you are sure that the access callback will not be reused. Good examples in core are Edit or Toolbar module.

The method is called on another instance of the controller class, so you cannot reuse any stored property of your actual controller instance used to generate the output.

Hierarchy

Expanded class hierarchy of CustomAccessCheck

1 file declares its use of CustomAccessCheck
CustomAccessCheckTest.php in core/tests/Drupal/Tests/Core/Access/CustomAccessCheckTest.php
1 string reference to 'CustomAccessCheck'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses CustomAccessCheck
access_check.custom in core/core.services.yml
Drupal\Core\Access\CustomAccessCheck

File

core/lib/Drupal/Core/Access/CustomAccessCheck.php, line 23

Namespace

Drupal\Core\Access
View source
class CustomAccessCheck implements RoutingAccessInterface {
    
    /**
     * The callable resolver.
     *
     * @var \Drupal\Core\Utility\CallableResolver
     */
    protected CallableResolver $callableResolver;
    
    /**
     * The arguments resolver.
     *
     * @var \Drupal\Core\Access\AccessArgumentsResolverFactoryInterface
     */
    protected $argumentsResolverFactory;
    
    /**
     * Constructs a CustomAccessCheck instance.
     *
     * @param \Drupal\Core\Utility\CallableResolver|\Drupal\Core\Controller\ControllerResolverInterface $callable_resolver
     *   The callable resolver.
     * @param \Drupal\Core\Access\AccessArgumentsResolverFactoryInterface $arguments_resolver_factory
     *   The arguments resolver factory.
     */
    public function __construct(ControllerResolverInterface|CallableResolver $callable_resolver, AccessArgumentsResolverFactoryInterface $arguments_resolver_factory) {
        if ($callable_resolver instanceof ControllerResolverInterface) {
            @trigger_error('Calling ' . __METHOD__ . '() with an argument of ControllerResolverInterface is deprecated in drupal:10.3.0 and is removed in drupal:11.0.0. Use \\Drupal\\Core\\Utility\\CallableResolver instead. See https://www.drupal.org/node/3397706', E_USER_DEPRECATED);
            $callable_resolver = \Drupal::service('callable_resolver');
        }
        $this->callableResolver = $callable_resolver;
        $this->argumentsResolverFactory = $arguments_resolver_factory;
    }
    
    /**
     * Checks access for the account and route using the custom access checker.
     *
     * @param \Symfony\Component\Routing\Route $route
     *   The route.
     * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
     *   The route match object to be checked.
     * @param \Drupal\Core\Session\AccountInterface $account
     *   The account being checked.
     * @param \Symfony\Component\HttpFoundation\Request $request
     *   Optional, a request. Only supply this parameter when checking the
     *   incoming request.
     *
     * @return \Drupal\Core\Access\AccessResultInterface
     *   The access result.
     */
    public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account, Request $request = NULL) {
        try {
            $callable = $this->callableResolver
                ->getCallableFromDefinition($route->getRequirement('_custom_access'));
        } catch (\InvalidArgumentException $e) {
            // The custom access controller method was not found.
            throw new \BadMethodCallException(sprintf('The "%s" method is not callable as a _custom_access callback in route "%s"', $route->getRequirement('_custom_access'), $route->getPath()));
        }
        $arguments_resolver = $this->argumentsResolverFactory
            ->getArgumentsResolver($route_match, $account, $request);
        $arguments = $arguments_resolver->getArguments($callable);
        return call_user_func_array($callable, $arguments);
    }

}

Members

Title Sort descending Modifiers Object type Summary
CustomAccessCheck::$argumentsResolverFactory protected property The arguments resolver.
CustomAccessCheck::$callableResolver protected property The callable resolver.
CustomAccessCheck::access public function Checks access for the account and route using the custom access checker.
CustomAccessCheck::__construct public function Constructs a CustomAccessCheck instance.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.