Lightpack RBAC (Role-Based Access Control)

A minimal, efficient, and modular Role-Based Access Control (RBAC) implementation for the Lightpack PHP framework.


Features


Database Schema

Tables created by the included migration:

Create schema migration file:

php console create:migration --support=rbac

Run migration:

php console migrate:up

Usage

Add RbacTrait to Your User Model:

class User extends Model {
    use RbacTrait;
}

Assign/Remove Roles

   // Attach a role by ID
   $user->roles()->atatch($roleId); 

   // Remove a role by ID
   $user->roles()->detach($roleId); 

Check Roles & Permissions

   // true/false by role name or ID
   $user->hasRole('admin'); 

   // true/false by permission name or ID
   $user->can('edit_post'); 

   // true/false
   $user->cannot('delete_post'); 

   // true if user has 'superadmin' role
   $user->isSuperAdmin(); 

Fetch All Roles or Permissions

// Collection of Role models
$user->roles; 

// Collection of Permission models (via roles)
$user->permissions; 

Filter Users by Role or Permission

// By role name
$admins = User::filters(['role' => 'admin'])->all();

// By role ID
$editors = User::filters(['role' => 2])->all();

// By permission name
$canEdit = User::filters(['permission' => 'edit_post'])->all();

// By permission ID
$canDelete = User::filters(['permission' => 11])->all();

// By role and permission both
$filtered = User::filters(['role' => 'admin', 'permission' => 'edit_post'])->all();

Fetch Role Permissions

$role = new Role(23);

// Collection of permissions assigned to this role
$role->permissions();