Problem/Motivation
\Drupal\user\Access\RoleAccessCheck does not provide a reason when it doesn't allow access
Steps to reproduce
Create a route that uses \Drupal\user\Access\RoleAccessCheck for access checking.
Access as a user without that role.
No information is provided in the logs
Proposed resolution
Add a reason when not providing access in \Drupal\user\Access\RoleAccessCheck
Remaining tasks
Patch
Tests
User interface changes
API changes
Data model changes
Release notes snippet
Original report
We have a custom module with a route that uses role-based access requirements. This is an internal-use site and not for public access. The basic site setting for the defaut front page is set to /dashboard.
The error when using role-based access does not indicate the reason.

After changing to permission based for a test, the reason is reported.

I can understand attempted access to a restricted path needs to be logged, but is there a way to exclude or filter out a specific route? We get a lot of users hitting / or /dashboard that are not logged in.
| Comment | File | Size | Author |
|---|---|---|---|
| #31 | interdiff-3174996-29_31.txt | 735 bytes | anchal_gupta |
| #31 | 3174996-31.patch | 7.9 KB | anchal_gupta |
| #29 | interdiff-26-29.txt | 1.88 KB | paulocs |
| #29 | 3174996-29.patch | 7.92 KB | paulocs |
| #26 | 3174996-26.patch | 7.51 KB | andypost |
Issue fork drupal-3174996
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
larowlanUpdated this to be a task.
Updated the issue summary to use the template
Comment #3
amjad1233From the initial looks it looks like : web/core/lib/Drupal/Core/Access/AccessManager.php
Which triggers RoleAccessCheck at core/modules/user/src/Access/RoleAccessCheck.php L:50.
Which is returning AccessResult::neutral(**NO_REASON**) without a reason and thus we not getting anything in the logs.
So I will be digging further to see where it denies we could add $reason or something. So in other words we don't know if it's yet denied.
Comment #4
larowlanI think it would be worth trying to put a value there in place of *NO REASON* and see if that helps
Comment #5
amjad1233@larowlan Could be a good candidate for tomorrow's code sprint hoping to resolve this one.
Comment #6
larowlan💯🙌
Comment #8
amjad1233Adding $reason certainly did help as I was suspecting. Just don't know if it's a good practice to add $reason in Neutral access requests.
Comment #9
fubarhouse commentedI've stepped through the process of testing the proposed change.
1. Set up local environment
2. Create authenticated user without access
3. Create content
4. Attempt to edit content as authenticated user without access
5. Observe error message, log out
6. Log in as admin and observe message log.
Looks great.
Comment #10
fubarhouse commentedAdded screenshot of test outcomes.
Comment #11
larowlanLeft a code review. Will chat with Amjad on zoom about test coverage.
Comment #13
larowlanCrediting quietone who was also on the zoom call at the sprint
Comment #15
larowlanSome great suggestions from longwave
Comment #16
andypostit needs to update second screenshot as messages are about roles not permissions)
Comment #17
andypostfixed feedback
Comment #19
larowlanLeft a question
Comment #22
smustgrave commentedThis issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.
MR should be updated to 10.1
There are also a number of open threads on 1407 that should be addressed
This seems like a super helpful feature. Is there any security implication?
Comment #23
andypostNeeds new patch or maybe @amjad1233 can change target branch to 10.1.x
Comment #24
amjad1233Great suggestion let me get my hands dirty in this one.
Comment #25
smustgrave commentedAround to review again if needed. Just wanted to double check that there’s no security issue announcing permissions?
Comment #26
andypostre-roll and fix for #19
Open questions are
Comment #27
amjad1233@andypost Thanks for the re-roll. 💪💪💪
I tried and patch applies cleanly and site also builds up fine.
Regarding Question 1 : I believe we are doing that already at certain places, but I agree with hiding as much as we can. With that said, I will defer question to @larowlan.
Regarding Question 2: I am not sure if this class will only be extended or there will be places where we create new instance of the class... In the later case Protected would fail. I think we should crack this one in #2266817: Deprecate empty AccessInterface and remove usages. May be we explicitly define it's signature in interface.
Comment #28
larowlan1) is secure exposing roles machine names
I think we'd need to load the role and call $role->access('view label') for each one
2) I still like adding a method to simplify the complexity here, nested if/else can be removed with early returns in a new method
Comment #29
paulocsWhy do we need to use
$role->access('view label')in this case? I tried to use it but every route that the user does not have access,$role->access('view label')returns FALSE. The message is not displayed for the user, maybe we don't need the$role->access('view label'). What do you think?For now I have addressed 28.2 pointed by larowlan.
Comment #30
larowlanNo need for else here, as we return above, even more simplification from the new method 💪
I looked into the permissions question and roles require 'administer permissions' to view their labels, which we're not going to grant here, obviously.
I'll ask around
Comment #31
anchal_gupta commentedI have uploaded the patch
as per #30 comment.