Problem/Motivation
Since #3558459: Menu access and structure, DisplayBuilderUiHooks::menuLinksDiscoveredAlter() points the Display Builder admin menu link to the instances list for a user who can list instances but not manage profiles. The alter runs during the menu link rebuild, which is stored for every user, so the current user there is whoever triggered the rebuild. Its first check also passes a route name to hasPermission(), where no such permission exists.
Steps to reproduce
- Give a role only
view display builder instanceandaccess administration pages. - Rebuild the menu links as a user with that role, as a router rebuild in their request does:
\Drupal::service('account_switcher')->switchTo($user); \Drupal::service('plugin.manager.menu.link')->rebuild(); - As an administrator, the Display Builder link under Structure now incorrectly opens the instances list instead of the profiles list.
Proposed resolution
- Drop
menuLinksDiscoveredAlter()fromDisplayBuilderUiHooks. - Assign the
entity.display_builder_profile.collectionmenu link a custom classDisplayBuilderMenuLinkextendingMenuLinkDefault. - In
DisplayBuilderMenuLink:getRouteName()dynamically selects the route based on the current user's permissions at runtime.getCacheContexts()includesuser.permissions.
- Provide a Kernel regression test covering route resolution across user roles and verifying menu rebuilds do not leak routes between users.
Remaining tasks
- Maintainer review
User interface changes
None.
Introduced terminology
None.
API changes
None.
Data model changes
None.
Release notes snippet
Fixed Display Builder structure menu link to dynamically resolve its target route per user permissions and cache by user.permissions.
Issue fork display_builder-3641335
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
keerthi kumar commentedI would like to work on this!
Comment #4
keerthi kumar commentedOpened MR replacing `menuLinksDiscoveredAlter()` with a dedicated `DisplayBuilderMenuLink` plugin that resolves the route dynamically at runtime based on the current user's permissions, adds the `user.permissions` cache context, and includes a Kernel test verifying route resolution across different user roles and rebuild scenarios.
Comment #5
keerthi kumar commentedComment #6
keerthi kumar commentedComment #7
mogtofu33 commentedThanks for jumping on this. Please only set Needs review once the pipeline is green; until then it stays Needs work and do not forget to assign to yourself while working.
Also, please don't rewrite the issue summary: it is HTML, and the Markdown version lost the LLM line and the #3558459: Menu access and structure link. I restored it.
Comment #8
keerthi kumar commentedThanks for the advice @mogtofu33.
All linters (PHPCS, PHPStan, PHP-CS-Fixer) and the new `DisplayBuilderMenuLinkTest` in MR 603 are passing without any issues.
The last failure in PHPUnit is ComponentValidatorTest::testComponents caused by the new sdc_devel release tagged yesterday that raised an error for required props without defaults (#3551361). It's currently failing on 1.0.x too (pipeline #1065584).
In this MR, should I add `default: primary` into `test_defaults.component.yml` to make the pipeline fully green, or leave this MR to focus only on the menu link?
Comment #9
mogtofu33 commentedThanks, the sdc devel error is resolved on 1.0.x, will rebase here, so I put in review for me as you've done the work for the issue.
Comment #11
mogtofu33 commented