Role-Based Access Control (RBAC): Hierarchies, Permissions & Preventing Role Explosion
Role-Based Access Control (RBAC) groups granular permissions into administrative roles (e.g. Viewer, Editor, Admin, Owner) and assigns roles to users. Production systems enforce role hierarchies, map operations to discrete permission strings ('doc:read', 'doc:edit') rather than role names, and defend against 'role explosion'.
Step 1: Application defines discrete permission strings for every operation (e.g. 'project:create', 'project:delete').
Assigning permissions directly to individual users across thousands of corporate employees is an operational nightmare. When an employee switches departments or changes jobs, updating hundreds of individual access flags manually inevitably causes privilege accumulation and security breaches.
The Hospital Keycard Badges: Nurses get Blue Badges (access to patient rooms and basic medical charts). Surgeons get Red Badges (patient rooms + operating rooms + pharmacy). Hospital Directors get Gold Badges (all facilities + financial records). Nobody programs individual door access for each nurse.
[ User ] ──(Assigned)──► [ Role: 'Editor' ] ──(Inherits)──► [ Role: 'Viewer' ]
│ │
├─► Permission: 'document:create' ├─► Permission: 'document:read'
└─► Permission: 'document:update' └─► Permission: 'comment:create'
│
┌──────────────────────────────┘
▼
[ Enforcement Point ] ── checks: hasPermission('document:read') ──► ALLOWED!CosmosApp organizes corporate team spaces using RBAC. Rather than hardcoding 'if (user.role === "admin")' in backend code, CosmosApp maps roles to fine-grained permissions (documents:write, billing:manage). When CosmosApp introduces a new 'BillingManager' role, developers don't have to touch document editing code.
Step 1: Application defines discrete permission strings for every operation (e.g. 'project:create', 'project:delete').
Step 2: Administrative roles are established, each grouping a collection of specific permissions.
Step 3: Role hierarchies are configured where higher roles inherit all permissions of lower roles (Owner > Admin > Member > Viewer).
Step 4: Users are assigned one or more roles within a tenant or organization context.
Step 5: Incoming requests hit API route guards that evaluate hasPermission(requiredPermission).
Step 6: Policy engine resolves user's assigned roles, expands inherited permissions, and grants or denies access (403 Forbidden).
// CosmosApp Permission-Based RBAC Architecture:
const ROLE_PERMISSIONS: Record<string, string[]> = {
viewer: ['doc:read', 'comment:create'],
editor: ['doc:read', 'comment:create', 'doc:create', 'doc:update'],
admin: ['doc:read', 'comment:create', 'doc:create', 'doc:update', 'doc:delete', 'user:invite'],
};
export function hasPermission(userRoles: string[], requiredPermission: string): boolean {
// Aggregate all permissions across assigned roles:
const permissions = new Set<string>();
for (const role of userRoles) {
(ROLE_PERMISSIONS[role] || []).forEach((p) => permissions.add(p));
}
return permissions.has(requiredPermission);
}- ✕Checking role names directly in controllers is fine (Checking 'if role == admin' creates rigid code; always check specific permissions like 'canDeleteDocument').
- ✕Creating a new role for every edge case is scalable (Creating 'BillingEditorWithReadOnlyReports' leads to role explosion; use ABAC for contextual modifiers).
Key Architectural Takeaways
- ✓Check granular permissions in application code, never hardcoded role names.
- ✓Implement role hierarchies to keep role definitions clean and maintainable.
- ✓Guard against role explosion by combining RBAC with ABAC for contextual parameters.
Writing 'if (user.role === "admin")' throughout the codebase. Introducing a new 'Manager' role requires modifying dozens of files.
Check permissions (e.g. 'can_delete_project') and define which roles possess that permission in a central mapping.
What architectural problem is referred to as 'Role Explosion' in RBAC systems?