Permission Hierarchies
You'll learn: How higher-level permissions can automatically include lower-level permissions, reducing complexity in authorization systems.
What Are Permission Hierarchies?
A permission hierarchy is a structure where certain permissions automatically grant other permissions beneath them. Think of it like a company org chart: if you have "CEO" access, you inherently have the permissions of every role below—manager, employee, and intern. You don't need to explicitly check for each level.
In web applications, this means granting someone users:admin permission might automatically include users:edit, users:view, and users:list. Instead of checking all four permissions individually, you only need to check whether the user has any permission in that hierarchy that's sufficient for the action.
How It Works
Hierarchies typically follow a naming convention using separators like colons or dots:
admin:all // Top of hierarchy
→ posts:manage // Can do anything with posts
→ posts:edit // Can edit posts
→ posts:view // Can only view posts
When checking permissions, you test from most specific to most general:
function hasPermission(userPermissions, requiredPermission) {
// Direct match
if (userPermissions.includes(requiredPermission)) return true;
// Check hierarchy: does user have a higher permission?
// e.g., if checking "posts:view", also accept "posts:edit" or "admin:all"
const parts = requiredPermission.split(':');
// Check parent levels like "posts:*" or "admin:all"
return false; // Simplified example
}
This reduces permission sprawl—users don't need dozens of granular permissions when a single higher-level one suffices.
Key Takeaway: Permission hierarchies let higher-level permissions automatically grant lower-level access, simplifying permission management by reducing the number of explicit grants needed.