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

Comments

pbuyle created an issue. See original summary.

pbuyle’s picture

Issue summary: View changes
dawehner’s picture

Yeah for some reason (no idea why) \Drupal\Core\Access\CustomAccessCheck::access just passes along the $route_match as well as $request.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

dawehner’s picture

Status: Active » Needs review
StatusFileSize
new4.65 KB

Here is a bugfix for it.

Status: Needs review » Needs work

The last submitted patch, 5: 2786941-5.patch, failed testing.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

gnuget’s picture

Version: 8.4.x-dev » 8.5.x-dev
Status: Needs work » Needs review
StatusFileSize
new4.61 KB

Rebase of #5

Status: Needs review » Needs work

The last submitted patch, 9: 2786941-9.patch, failed testing. View results

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

littletiger’s picture

Hi, I can confirm this patch works for Drupal 8.5.5.

Funny to be learning something new, run into errors and discover it's a bug to be solved in Drupal 8.7 ;D
Why not 8.6 ?
Note, I didn't run the test, don't know why it's failing in #9 ?

gnuget’s picture

Why not 8.6 ?

All the bugs are fixed first in the development version and later if necessary/possible are backported to the stable version.

So, 8.6 is almost out (The first alpha has been already released) so any new fix first will be commited to 8.7 and later can be backported to 8.6.

I will run again the tests on #9 to see if it was a random error.

littletiger’s picture

Anytime I navigate to /user/# I get the error "The website encountered an unexpected error. Please try again later.".
Watchdog log shows the following error :
RuntimeException: Callable "Drupal\Core\Access\CustomAccessCheck::access" requires a value for the "$request" argument. in Drupal\Component\Utility\ArgumentsResolver->handleUnresolvedArgument() (line 142 of /srv/bindings/37df6945aeee4c2cb52bb8fb431fc585/code/core/lib/Drupal/Component/Utility/ArgumentsResolver.php).

After applying the patch... still no luck :-( This should be related, if not the same, no?

wengerk’s picture

Status: Needs work » Needs review
StatusFileSize
new5.91 KB

Re-roll the patch from #09 for 8.7.x.

Here are my changes the following declaration (impossible to generate an interdiff see below):

1. Make the $request optional to prevent breaking the whole custom Access:

+   * @param \Symfony\Component\HttpFoundation\Request $request
+   *   Optional, a request. Only supply this parameter when checking the
+   *   incoming request.
-  public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account) {
+  public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account, Request $request = NULL) {

PS: I wasn't able to generate the interdiff file sorry guys ...

1 out of 2 hunks FAILED -- saving rejects to file /var/folders/5c/cwjk37zd5xz5qrknkqzfkhgr0000gn/T//interdiff-1.jIEori.rej
interdiff: Error applying patch1 to reconstructed file

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

firfin’s picture

Status: Needs review » Reviewed & tested by the community

Solves the problem for me. Luckily I found this post, I was trying to learn D8 routing, menu and access systems and this bug was driving me crazy for days! My problems are not completely solved. But getting a lot less WSODs now.

Does not apply to D8.8 though. I will try and re-roll an D8.9 version tomorrow

firfin’s picture

Status: Reviewed & tested by the community » Needs review

REstoring status

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

sadashiv’s picture

StatusFileSize
new6 KB
new6 KB

Hi,

I am attaching patch rolled again for 8.9.x and 9.1.x

Thanks,
Sadashiv.

The last submitted patch, 22: 2786941-d9-22.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

Status: Needs review » Needs work

The last submitted patch, 22: 2786941-d8-22.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

sadashiv’s picture

Status: Needs work » Needs review
StatusFileSize
new6 KB
new6 KB

Forgot to fix getMock deprecation, now fixed

Thanks,
Sadashiv.

firfin’s picture

Tested with my D8 project in which I encountered this bug and #25 works for me, thanks @sadashiv.
Will try and test with D9 some time soon.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

ollie222’s picture

Using Drupal 8.9.8 the patch in #25 applies cleanly.

If I use a simple route and access check similar to #1 such as

my_test.test_route:
  path: '/my_test'
  defaults:
    _controller: '\Drupal\my_test\Controller\TestController::test'
  requirements:
    _custom_access:  '\Drupal\my_test\Controller\TestController::access'
namespace Drupal\my_test\Controller;

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Routing\Access\AccessInterface;
use Symfony\Component\HttpFoundation\Request;

class TestController extends ControllerBase implements AccessInterface {

  public function access(Request $request) {
    return AccessResult::allowed();
  }

  public function test(Request $request) {
    return [
      '#type' => 'markup',
      '#title' => t('Testing'),
      '#markup' => $this->t('This is a test page'),
    ];
  }

}

then when I access the route directly $request is available in the access check so the patch looks to work correctly.

Now this is probably outside of the remit of this problem but it's sort of linked.

What should happen if the route is used as a tab on a node view page?

my_test.test_route: # The first plugin ID
  route_name: my_test.test_route
  title: 'My Test'
  base_route: entity.node.canonical
  appears_on:
    - entity.node.canonical
  options:
    parameters:
      node: 'entity:node'

If I do this then when viewing the node view/edit page the access check is called but $request is missing again.

Should $request be available on these pages, is this caused by something else?

firfin’s picture

Status: Needs review » Reviewed & tested by the community

Is good for D8.

catch’s picture

Status: Reviewed & tested by the community » Needs review

#28 looks relevant are we missing something passing request when checking access to a route for a tab?

dawehner’s picture

From my understanding this is already the case for any other access checker on local tasks, see https://git.drupalcode.org/project/drupal/-/blob/9.2.x/core/lib/Drupal/C...

This code is calling \Drupal\Core\Access\AccessManager::checkNamedRoute, which sets $request as null.

Maybe the documentation could be better:

+   *   Optional, a request. The parameter is not always provided. For example checking access for local tasks will not have the request object available.
abhijith s’s picture

StatusFileSize
new5.98 KB

Fixed the custom command fail issue in patch #25

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

joachim’s picture

Status: Needs review » Reviewed & tested by the community
catch’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs reroll

Needs a re-roll, and could also use the comment improvement from #31.

kishor_kolekar’s picture

Status: Needs work » Needs review
StatusFileSize
new7.16 KB
new2.09 KB

please review the patch.

Status: Needs review » Needs work

The last submitted patch, 36: 2786941-36.patch, failed testing. View results

kostyashupenko’s picture

Issue tags: -Needs reroll
sadashiv’s picture

Status: Needs work » Needs review
StatusFileSize
new7.14 KB

Rerolling the patch with all changes.

Thanks,
Sadashiv.

Status: Needs review » Needs work

The last submitted patch, 39: 2786941-39.patch, failed testing. View results

sadashiv’s picture

Status: Needs work » Needs review
StatusFileSize
new7.14 KB

Retrying with minor changes

Status: Needs review » Needs work

The last submitted patch, 41: 2786941-40.patch, failed testing. View results

neclimdul’s picture

Pretty sure this is a soft merge failure caused by #3217717: Replace usages of the at() matcher, which is deprecated. There are now two calls to controllerResolved->expect($this->exactly(3)) which I assume means there are suppose to be... 6? I need to dig into this test more but mocks are kinda inscrutable so it might take a few.

neclimdul’s picture

Status: Needs work » Needs review
StatusFileSize
new2.91 KB
new6.82 KB

yeah, was definitely that issue but I didn't notice the patches here where still using the at() method which is why they weren't working.

This is a reroll of the RTBC'd #32 without the later changes + the at() changes in the interdiff.

neclimdul’s picture

Category: Bug report » Feature request
Issue summary: View changes

Did a review and everything in this is pretty straightforward and looks good to go. Didn't really touch anything other then fixing the tests(feel free to adjust credit accordingly) but probably not appropriate to RTBC.

Touched up the summary into template and document why this is useful and other issue cleanup. Technically this is working by design just missing some very expected functionality so updating this to a feature as well.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

froboy’s picture

Status: Needs review » Reviewed & tested by the community

The changes above have passed tests and I've reconfirmed that this patch applies cleanly and does its job. Look to be RTBC to me.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 44: 2786941-44.patch, failed testing. View results

froboy’s picture

Womp womp. I'll try to take a look at this.

froboy’s picture

Status: Needs work » Needs review

Tests seem to be failing due to #3255836: Test fails due to Composer 2.2. I guess we'll just wait until that's resolved. Not flipping back to RTBC until we can get those to pass.

froboy’s picture

Status: Needs review » Reviewed & tested by the community

New tests passed, marking this RTBC again.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 44: 2786941-44.patch, failed testing. View results

neclimdul’s picture

Status: Needs work » Reviewed & tested by the community

This seems to be a random failure.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 44: 2786941-44.patch, failed testing. View results

neclimdul’s picture

Status: Needs work » Reviewed & tested by the community

Drupal\Tests\layout_builder\FunctionalJavascript\* again

pfrenssen’s picture

RTBC++

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 44: 2786941-44.patch, failed testing. View results

renrhaf’s picture

+1 RTBC

yogeshmpawar’s picture

Status: Needs work » Reviewed & tested by the community

+1 RTBC. Hope the random test failures are resolved now.

geek-merlin’s picture

Ouch, just ran into this DX pita. +1 for commit.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 44: 2786941-44.patch, failed testing. View results

ravi.shankar’s picture

Status: Needs work » Reviewed & tested by the community

Back to RTBC.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 44: 2786941-44.patch, failed testing. View results

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

dieterholvoet’s picture

Status: Needs work » Reviewed & tested by the community

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 44: 2786941-44.patch, failed testing. View results

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

lamp5’s picture

StatusFileSize
new6.79 KB

Reroll 9.5.x-dev

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

frob’s picture

Is there anything stopping this from getting committed? It has been RTBCed and rerolled for over 3 years.

neclimdul’s picture

Status: Needs work » Reviewed & tested by the community

I'm not really sure. While I remember _why_ I got here, but its been so long I honestly don't remember the code at all...

That said, the current patch looks the same as all the other RTBC patches and applies cleanly so putting it back to RTBC.

catch’s picture

Status: Reviewed & tested by the community » Needs work

Doesn't apply to 11.x

roshni27’s picture

Status: Needs work » Needs review
StatusFileSize
new6.68 KB
new78.15 KB
new78.15 KB

Issue #68 is functional; it requires only minor adjustments. The patch has been updated for versions 10.x. However, it wasn't originally applied to 11.x, but after making the necessary changes in the 11.x branch, the patch is now successfully applied. As discussed earlier, the patch modifications resolve the issue effectively, so it functionality remains as is.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Restoring status after reroll

  • catch committed ec84cbe4 on 11.x
    Issue #2786941 by sadashiv, roshnichordiya, neclimdul, kishor_kolekar,...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 11.x, thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

froboy’s picture

Fixing my credit.