Design an Extensible Role-Based Access Control System
Model how administrators assign roles and applications authorize actions while new role definitions can be added safely.
Design a role-based access control library whose role definitions are extensible and whose permission vocabulary is fixed to READ_REPORT, WRITE_REPORT, DELETE_USER, MAKE_ADMIN, and APPROVE_INVOICE.
The source exercise expected discussion of the Decorator pattern; a coherent decorator, composition-based internal design, or behaviorally equivalent simpler representation is acceptable when justified and when it preserves the same composed-definition contract.
For this exercise, assume administrators define roles from the fixed permissions, assign one or more roles to a user, revoke assignments, and application code asks whether that user may perform a permission. Role definitions are registered atomically as a batch of one or more immutable definitions.
A component reference may target either an existing role or another definition in the same batch, including a forward reference; the complete proposed graph is validated before an all-or-nothing commit, so direct and indirect cycles are reachable invalid inputs.
This role-definition composition graph is an in-scope extension mechanism, not a separately administered priority or seniority hierarchy between assigned roles. A user is allowed when any current assigned role grants the requested permission.
Unknown permissions are rejected, and repeating the same assignment or revocation is idempotent.
One shared versioned immutable PolicyState owns both role definitions and assignments; every successful registration, assignment, or revocation publishes one new state version, and each authorization call captures one PolicyState snapshot reference as its linearization point, so definitions and assignments cannot come from different versions and later mutations cannot change that in-flight decision.
Keep the library in memory and in one process; persistence, distributed policy propagation, request context, resource-specific rules, explicit deny rules, and separate role-priority rules are out of scope.