admin-platform/rbac
Laravel RBAC package for building an admin platform with roles and permissions. Define access rules, assign roles to users, and gate routes, controllers, and UI actions to keep admin features secure and manageable.
Installation
composer require admin-platform/rbac
Add to config/app.php under providers:
AdminPlatform\Rbac\RbacServiceProvider::class,
Publish Config & Migrations
php artisan vendor:publish --provider="AdminPlatform\Rbac\RbacServiceProvider" --tag="rbac-config"
php artisan vendor:publish --provider="AdminPlatform\Rbac\RbacServiceProvider" --tag="rbac-migrations"
php artisan migrate
First Use Case: Assigning a Role
use AdminPlatform\Rbac\Models\Role;
// Create a role
$adminRole = Role::create(['name' => 'admin', 'guard_name' => 'web']);
// Assign to a user
$user->roles()->attach($adminRole);
Check Permissions
if ($user->can('access_dashboard')) {
// Grant access
}
parent_id in the roles table:
$superAdmin = Role::create(['name' => 'super_admin']);
$admin = Role::create(['name' => 'admin', 'parent_id' => $superAdmin->id]);
if ($user->can('edit_users', true)) { // `true` enables hierarchy checks
// Logic for hierarchical access
}
$role->permissions()->sync(['create_post', 'edit_post']);
public function handle($request, Closure $next)
{
if (!$request->user()->can('manage_users')) {
abort(403);
}
return $next($request);
}
use AdminPlatform\Rbac\Traits\HasRbac;
class PostPolicy
{
use HasRbac;
public function update(User $user, Post $post)
{
return $user->can('edit_post');
}
}
Route::middleware(['auth:sanctum', 'rbac'])->group(function () {
Route::get('/admin/dashboard', [DashboardController::class, 'index']);
});
Guard Name Mismatch
guard_name in roles matches your auth guard (e.g., web, api). Defaults to web if omitted.Role::where('guard_name', 'null')->update(['guard_name' => 'web']);
Circular Hierarchy
Role A → Role B → Role A).if ($role->isAncestorOf($parentRole)) {
throw new \Exception("Circular hierarchy detected.");
}
Permission Caching
php artisan cache:clear
config/rbac.php for testing:
'cache_permissions' => env('RBAC_CACHE_PERMISSIONS', false),
Log Permission Checks
Add to config/rbac.php:
'debug' => env('RBAC_DEBUG', false),
Check logs for denied permission attempts.
Dump User Permissions
dd($user->getAllPermissions());
Custom Permission Providers
Override AdminPlatform\Rbac\Contracts\PermissionProvider to fetch permissions from external sources (e.g., database views).
Event Listeners Listen for role/permission changes:
Role::created(function ($role) {
// Trigger notifications or sync services
});
Custom Guards
Extend AdminPlatform\Rbac\RbacGuard for non-standard auth setups.
Index Database Columns
Add indexes to roles and role_permission tables for large datasets:
Schema::table('roles', function (Blueprint $table) {
$table->index('guard_name');
$table->index('parent_id');
});
Lazy-Load Permissions
Use with(['permissions']) sparingly; eager-load only when necessary:
$role = Role::with('permissions')->find($id);
How can I help you explore Laravel packages today?