Lesson 42 โข Advanced
RBAC & ACL Permissions ๐
Implement role-based access control with role hierarchies, permission middleware, and database-backed authorization for PHP applications.
What You'll Learn in This Lesson
- โข Difference between RBAC and ACL approaches
- โข Define roles with permissions and inheritance hierarchies
- โข Build permission middleware for route protection
- โข Design the database schema for roles and permissions
- โข Check access with can() and requireAny() patterns
Role-Based Access Control
RBAC assigns permissions to roles (not directly to users), then assigns roles to users. Role hierarchies let admin inherit all editor and viewer permissions automatically. This scales to thousands of users โ change a role's permissions once, and it applies to everyone with that role.
Try It: RBAC System
Define roles with inheritance, assign to users, and check access
// Role-Based Access Control (RBAC) in PHP
console.log("=== RBAC vs ACL โ What's the Difference? ===");
console.log();
console.log(" RBAC (Role-Based):");
console.log(" Users โ Roles โ Permissions");
console.log(" 'Alice is an Editor, Editors can publish posts'");
console.log(" โ
Simple, scalable, most common");
console.log();
console.log(" ACL (Access Control List):");
console.log(" Users โ Specific Resource Permissions");
console.log(" 'Alice can edit Post #42 specifically'");
...Permission Middleware & Schema
Protect routes with middleware that checks permissions before the controller runs. If the user lacks the required permission, return 403 Forbidden. The database schema uses pivot tables (role_permissions, user_roles) for many-to-many relationships.
Try It: Permission Middleware
Protect routes with permission checks and see the database schema
// Permission Middleware & Database Schema
console.log("=== Permission Middleware Pattern ===");
console.log();
class PermissionMiddleware {
constructor(rbac) { this.rbac = rbac; }
require(permission) {
return (request) => {
let userId = request.userId;
let allowed = this.rbac.can(userId, permission);
if (!allowed) {
console.log(" ๐ซ 403 Forbidden: " + request.method + " " + request.path);
console.log(" User " + userId + " lacks permission:
...โ ๏ธ Common Mistakes
if (user.role === 'admin'). Check if (user.can('users.delete')). This way, adding a new role doesn't require changing code.๐ Quick Reference โ Authorization
| Pattern | Description |
|---|---|
| RBAC | Users โ Roles โ Permissions |
| ACL | Users โ Specific resource permissions |
| Pivot Table | Many-to-many relationship table |
| Middleware | Pre-controller permission check |
| Gate/Policy | Laravel's authorization abstractions |
๐ Lesson Complete!
You can now build authorization systems! Next, learn to abstract file storage across local, S3, and cloud providers.
Sign up for free to track which lessons you've completed and get learning reminders.