directorytree/authorization
Native, easy role & permission management for Laravel. Adds migrations and an Authorizable trait to your User model for role/permission checks, optional custom migrations/models, caching, gate registration, middleware, and testing support.
can(), authorize(), @can, and middleware (role, permission) ensures minimal disruption to existing authorization workflows.roles/permissions tables, merging schemas could require manual migration adjustments.PermissionRegistrar requirement in tests may introduce flakiness if not properly mocked or initialized. Unit tests for authorization logic may need additional setup.role/permission middleware throws 403 errors by default, which may conflict with existing error-handling middleware (e.g., custom 403 pages).Role, Permission) required, or will the defaults suffice?PermissionRegistrar requirement?user->can('permission') vs. Gate::allows()).hasPermission()).role/permission middleware.// Seed initial roles/permissions
Role::create(['name' => 'admin', 'label' => 'Administrator']);
Permission::create(['name' => 'users.manage', 'label' => 'Manage Users']);
Authorization::disableGateRegistration()). Policies can coexist if they delegate to the package’s methods.spatie/laravel-permission or zizaco/entrust. Audit dependencies for overlapping functionality.AuthServiceProvider (e.g., Authorization::useUserModel()).hasPermission(), hasRole()).role:admin routes).@can, authorize(), or middleware.cacheExpiresIn()) based on performance metrics.roles or permissions) may require manual intervention if the package evolves.user.granted_permission) to track changes in production. Example:
event(new PermissionGranted($user, $permission));
php artisan cache:clear) if permission checks fail after updates.auth middleware runs before role/permission middleware in Kernel.php.cacheExpiresIn() for high-write scenarios (e.g., now()->addMinutes(5)).roles_users, permissions_roles, and permissions_users tables are critical for large-scale applications. Add manually if missing:
Schema::table('roles_users', function (Blueprint $table) {
$table->index(['user_id', 'role_id']);
});
hasAnyPermissions() with large permission lists; use hasPermissions() with a smaller subset.| Scenario | Impact | Mitigation |
|---|---|---|
| Cache invalidation race | Stale permissions served briefly. | Use cacheExpiresIn() shorter durations or implement a cache-warming strategy. |
| Database connection issues | Authorization checks fail silently. | Add retries or fallback to a static "deny" policy. |
| Middleware misconfiguration | 403 errors for valid users. | Test middleware in isolation; log failed checks. |
| Permission registrar missing | Tests fail or Gates not registered. | Mock PermissionRegistrar in tests or use Authorization::disableGateRegistration(). |
grant(), revoke(), hasPermission()) and middleware usage.resource.action format).How can I help you explore Laravel packages today?