Roles and Permissions System
This document explains the high-level, declarative syntax for defining roles and permissions in the application.Overview
The system provides a clean, maintainable way to manage roles and permissions through:- Config-based role resolution - Roles are configured in
config/core.php - Declarative permission assignment - Permissions are defined directly in the Role enum
- Automatic synchronization - A single command syncs everything to the database
- Platform-level abstraction - Core code doesn’t depend on specific app Role enums
Configuration
config/core.php
Role Definition
app/Enums/Role.php
Permission Definition
Permissions are defined in two places:Inly\Core\Enums\CorePermission (Platform-level)
App\Enums\Permission (App-specific)
Syncing Roles & Permissions
Thepermissions:generate command handles everything automatically:
- ✅ Discover all permissions from
CorePermissionandapp/Enums/Permission - ✅ Create new permissions in the database
- ✅ Delete permissions that no longer exist in either enum
- ✅ Create all roles from
app/Enums/Role - ✅ Sync permissions to each role based on their
permissions()method
Helper Functions
The platform provides helper functions for working with roles dynamically:Usage in Core Code
Core code uses helper functions instead of directly referencingApp\Enums\Role:
Benefits
1. Declarative Syntax
Permissions are defined right where they belong - in the Role enum:2. Type Safety
All permissions are enum cases, so you get full IDE autocomplete and type checking.3. Single Source of Truth
Thepermissions() method is the only place you need to look to understand what permissions a role has.
4. Automatic Cleanup
Old permissions are automatically deleted when you remove them from the enum.5. Platform Independence
Core code doesn’t depend on specific app implementations, making it reusable across projects.Migration from Old System
If you have an oldGenerateRolesAndPermissions command:
- Add
permissions()method to your Role enum - Move permission assignments from the command to the enum
- Delete the old command file
- Run
php artisan permissions:generate