Skip to content

Narrow admin roles (ROLE_CMS_ADMIN, ROLE_USER_ADMIN) and additive http_cache lists - #611

Merged
silverbackdan merged 3 commits into
mainfrom
feature/narrow-admin-roles
Nov 24, 2026
Merged

silverbackdan merged 3 commits into
mainfrom
feature/narrow-admin-roles

Conversation

@silverbackdan

Copy link
Copy Markdown
Collaborator

Narrows the admin roles and adds the extension points the module's e-commerce design needs. Breaking.

Admin roles

The bundle adds this hierarchy to security.role_hierarchy (Security\AdminRoles::HIERARCHY):

ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_USER_ADMIN, ROLE_ALLOWED_TO_SWITCH]
ROLE_ADMIN:       [ROLE_CMS_ADMIN]
ROLE_CMS_ADMIN:   [ROLE_USER]
ROLE_USER_ADMIN:  [ROLE_USER]
  • How it merges. SecurityBundle never merges two configs' lists for the same role: its role_hierarchy prototype has performNoDeepMerging(), so a prepended ROLE_ADMIN: [ROLE_CMS_ADMIN] is replaced by the site's ROLE_ADMIN: ROLE_USER. AdminRoleHierarchyCompilerPassTest pins that. The bundle still prepends the hierarchy, so that one is always configured (with none configured, SecurityBundle removes the hierarchy voter). Then AdminRoleHierarchyCompilerPass adds the bundle's roles into the final security.role_hierarchy.roles parameter, keeping every role the site or another bundle already lists for that key.
  • A scalar site entry keeps its role. With the site's ROLE_ADMIN: ROLE_USER written as a scalar (as in the template and the test app), ROLE_ADMIN reaches both ROLE_CMS_ADMIN and ROLE_USER, and neither entry hides the other. Pinned in AdminRoleHierarchyCompilerPassTest::test_a_site_role_admin_given_as_a_scalar_keeps_its_role_and_gains_the_cms_admin_role, and on the booted test app in Security\AdminRolesTest. A site role added under ROLE_ADMIN (e.g. ROLE_SHOP_ACCOUNTS) is reachable alongside ROLE_CMS_ADMIN.
  • Every bundle ROLE_ADMIN check is now ROLE_CMS_ADMIN: content writes and publishing, Route.liveAt, isReachableWithoutRoute, collaboration state, realtime, component usage, the orphan reports and deletions, and both purge endpoints. Existing ROLE_ADMIN and ROLE_SUPER_ADMIN users keep exactly their access: every existing admin test passes unchanged. The only exception is two UserPutTest assertions on the output roles, which now also list ROLE_CMS_ADMIN. Security\AdminRolesTest shows that a user with only ROLE_CMS_ADMIN can use each area and a user with only ROLE_USER_ADMIN gets a 403.
  • user:create --admin still creates a ROLE_ADMIN user, and --super-admin still creates a ROLE_SUPER_ADMIN user. Both are unchanged.

Config defaults now is_granted('ROLE_CMS_ADMIN') (the template can drop its copies)

  • publishable.permission now defaults to is_granted('ROLE_CMS_ADMIN'). It keeps cannotBeEmpty(), and the publishable node is no longer isRequired(), which avoids the user.email_verification required config children are never enforced #214 trap.
  • routable_security now defaults to is_granted('ROLE_CMS_ADMIN').
    • Today null means no check at all: RoutableVoter, RoutableExtension and ComponentVoter grant every reader a page or page data that no live route reaches. That contradicts the reachability rule.
    • The new default enforces the rule out of the box. An explicit null still switches it off.
  • The test app no longer sets either value, so the functional suite runs on the defaults.

User management: ROLE_USER_ADMIN

  • User:superAdmin group. UserContextBuilder now adds it for ROLE_USER_ADMIN.
  • Operation security. UserResourceMetadataCollectionFactory sets the security of every AbstractUser operation:
    • GetCollection and Post: is_granted('ROLE_USER_ADMIN')
    • Get: that, or (user and object.getId() == user.getId())
    • Patch and Put: (is_granted('ROLE_USER_ADMIN') and is_granted('manage_user', object)), or the user themselves
    • Delete: the first part only
    • /me keeps IS_AUTHENTICATED_FULLY.
  • Precedence: ANDed, not replaced. This follows the ContentWriteSecurityResourceMetadataCollectionFactory precedent. An operation's own security becomes (<bundle>) and (<own>). The opt-out is extraProperties: ['user_admin_security' => false] (UserResourceMetadataCollectionFactory::OPT_OUT_EXTRA_PROPERTY). Pinned in UserResourceMetadataCollectionFactoryTest.
    • For the template: its App\Entity\User checks are not merely redundant. Its is_granted('ROLE_SUPER_ADMIN') ANDed with the bundle's check would lock out a user admin. Remove the security: from all five operations, as this PR does for the test app's User.

Escalation guard

  • 403 on a user who outranks the caller. UserManagementVoter (manage_user) grants only when the caller reaches every role the target reaches. A ROLE_USER_ADMIN therefore gets a 403 when they edit, PUT, change the roles of or delete a CMS admin, an admin or a super admin. A role outside the hierarchy also outranks them.
  • 422 on a role the caller does not reach. #[AssignableRoles] on AbstractUser is a class constraint in Default. Any added role that the token user does not reach is a 422 on roles. Added means new compared with the unit of work's original roles, or every role of a new user.
    • With no signed-in user (the console, an anonymous registration), it checks nothing.
    • A super admin can still assign anything they reach. They can no longer assign an arbitrary role outside the hierarchy, which they could before.
  • Self-edit. A plain user cannot write roles at all (only User:superAdmin can write it), so a PATCH of their own roles is ignored. A user admin raising their own roles gets a 422.
  • All of this is pinned in User\UserAdminTest.

/me reachable roles

UserNormalizer no longer calls setRoles() on the entity, which broke #389's rule. It now writes the reachable roles into the normalized roles only. MeTest pins that /me lists every reachable role, and that a flush after normalization stores only the original roles.

Extension points

  1. A visitor-dependent resource can be kept out of shared caches. An anonymous GET on a resource that is not CWA content, with cacheHeaders: ['public' => false, 'no_store' => true], responds private, no-store with no s-maxage. This uses a new DummyVisitorDependentResource in the test app, and is in HttpCache\ExtensionResourceCacheTest.
  2. The three http_cache class lists are additive. personalised_resource_classes, scheduled_expiry_resource_classes and purge_rendered_html_classes always keep the CWA defaults (Configuration::*_CLASSES, re-added by validate()->always()). Configured or prepended classes are added to them, and the info text says so. ConfigurationTest pins it with a second bundle's prepended config.
  3. Purges reach resources that are not CWA content. A Doctrine write to an API resource that is not CWA content (DummyResourceWithFilters) purges its IRI.

Items 1 and 3 already worked and are now pinned. CLAUDE.md gets the admin roles entries, an "Extending CWA" section, the singleflight note on markNeverStored(), and the ROLE_CMS_ADMIN renames.

Not covered by the design, decided here

  • ComponentPosition admin groups. ComponentPosition:read:role_admin (pageDataProperty, pageDataClass) is now …:role_cms_admin. These groups are built from reachable roles, so ROLE_ADMIN still gets them.
  • Route group. Route:cwa_resource:read:ROLE_ADMIN is now …:role_cms_admin. No context builder ever adds a group for Route, and Route operations set no groups, so the group never had any effect. associatedResources is serialized for every caller today. That is unchanged here and may be worth a look.
  • New shared test helper. AuthenticationTrait::loginWithRoles() is tested in AuthenticationTraitTest.

Local runs

  • vendor/bin/paratest -p8: 6265 tests green.
  • Functional suite with API_PLATFORM_USE_SYMFONY_LISTENERS=1: green.
  • php-cs-fixer and phpstan: clean.

🤖 Generated with Claude Code

ROLE_CMS_ADMIN replaces ROLE_ADMIN in every bundle check, ROLE_USER_ADMIN
replaces ROLE_SUPER_ADMIN for user management, with an escalation guard;
the bundle merges its role hierarchy into the application's. The
http_cache class lists always keep the CWA defaults, and normalizing a
user no longer writes its reachable roles onto the entity.
@codecov

codecov Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.19%. Comparing base (bee79af) to head (89a0f57).

Additional details and impacted files
@@             Coverage Diff              @@
##               main     #611      +/-   ##
============================================
+ Coverage     97.18%   97.19%   +0.01%     
- Complexity     5475     5513      +38     
============================================
  Files           451      455       +4     
  Lines         14103    14174      +71     
============================================
+ Hits          13706    13777      +71     
  Misses          397      397              
Flag Coverage Δ
phpunit 97.19% <100.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@silverbackdan
silverbackdan merged commit 6068aa8 into main Nov 24, 2026
14 of 15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant