Simplified custom access checker does not have access to Request

Created on 19 August 2016, over 8 years ago
Updated 26 September 2023, about 1 year ago

Problem/Motivation

When using a simplified custom access checker (as documented at https://www.drupal.org/node/2122195#access-custom ), the method does not have access to the Request object. According to the documentation"the available arguments are the same as for the service defined method.". But when the method has a argument type-hinted with Symfony\Component\HttpFoundation\Request a null value is received.

This is a very useful feature if you're doing some one off JWT style query access or some session based access control. These are the sort of use cases you might logically use a one off custom access test and it is possible with additional boiler plate but it doesn't really match expectations when writing a custom access check.

Steps to reproduce

When using the following route and controller:

# test_module.routing.yml
test_module.test_route:
  path: '/test'
  defaults:
    _controller: '\Drupal\test_module\Controller\TestController::test'
  requirements:
    _custom_access:  '\Drupal\test_module\Controller\TestController::access'

namespace Drupal\test_module\Controller;

use Symfony\Component\HttpFoundation\Request;

class TestController extends ControllerBase implements AccessInterface {

  public function access(Request $request) {
    // ...
  }

  public function test(Request $request) {
    // ...
  }

}

Accessing the route trigger the following error: Uncaught PHP Exception RuntimeException: "Callable "Drupal\test_module\Controller\TestController::access" requires a value for the "$request" argument.

If the same code is used as a service with the following config:

# test_module.routing.yml
test_module.test_route:
  path: '/test'
  defaults:
    _controller: '\Drupal\test_module\Controller\TestController::test'
  requirements:
    _test_access_check: 'TRUE'
# test_module.services.yml
services:
  test_module.test_access_check:
    class: Drupal\test_module\Controller\TestController
    tags:
      - { name: access_check, applies_to: _test_access_check }

Then the access check works as expected and receive a request object.

Proposed resolution

Add request to injected argument list.

Remaining tasks

Review/Commit

User interface changes

N/A

API changes

No changes to existing API's and fully BC compatible with existing implementations..

Data model changes

n/a

Release notes snippet

TODO

Feature request
Status

Fixed

Version

11.0 🔥

Component
Routing 

Last updated 2 days ago

Created by

🇨🇦Canada pbuyle

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024