Cache issue with local actions

Created on 6 February 2025, 18 days ago

Problem/Motivation

When I install the module the local actions cache is not revalidated correctly.
In this example I have two roles, "Content Basic" and "Content Admin".

  • Content Basic: You have the "Access the Content overview page" permission
  • Content Admin: Has the permission "Access the Content overview page" and "Basic page: Create new content"

If I create a new user and add the "Content Basic" role and navigate to the "/admin/content" page, he will be able to view it as expected.

Now I change the user's role to "Content Admin" and reload the "/admin/content" page.

The expected result would be that an "Add content" button would be added to the local actions, but I still don't have any local action.

Now if I clear the cache and reload the page the "Add content" button will appear as expected.

The opposite effect also happens, if you change the role to "Content Basic" and reload the page the "Add content" button will continue to appear, it will only be removed after clearing the cache.

Steps to reproduce

  1. Do a standard drupal installation.
  2. Install the micro_site module.
  3. Create two roles: "Content Basic" and "Content Admin"
  4. In the "Content Basic" role add the permissions: "Access the Content overview page", "View the administration theme"
  5. In the "Content Admin" role add the permissions: "Basic page: Create new content", "Access the Content overview page", "View the administration theme"
  6. Create a new user with the "Content Basic" role.
  7. Logging in with the created user.
  8. Navigate to the "/admin/content" page.
  9. Check that the "Add content" button (local action) is not present, as expected.
  10. Changing the user role from "Content Basic" to "Content Admin".
  11. Reload the "/admin/content" page and verify that the "Add Content" button is not appearing, even though the user now has permissions for the action.
  12. Clear the cache, reload the "/admin/content" page and check that the "Add Content" button now appears, as expected.

Proposed resolution

After investigating a bit it seems to me that the problem is being created by replacing the "router.route_provider" service in the src/MicroSiteServiceProvider.php file.

From some quick tests I did it seems the issue will be resolved if I add 'user.roles' as required_cache_contexts.

...
class MicroSiteServiceProvider extends ServiceProviderBase implements ServiceModifierInterface {

  /**
   * {@inheritdoc}
   */
  public function alter(ContainerBuilder $container) {
      ...
      // Permissions related to site context are based on user referenced by site.
      // We need so to add the user as a cache context.
      if (!in_array('user', $renderer_config['required_cache_contexts'])) {
        $renderer_config['required_cache_contexts'][] = 'user';
        $renderer_config['required_cache_contexts'][] = 'user.roles';
      }
      ...
  }
}

Remaining tasks

Check if this solution break any system in the module.

User interface changes

None.

API changes

None.

Data model changes

None.

🐛 Bug report
Status

Active

Version

1.0

Component

Code

Created by

🇵🇹Portugal lolgm

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024