C.A.R. 911 uses the Spatie Laravel Permission package to manage user roles and permissions. This provides a flexible and powerful system for controlling access to different parts of the application.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/exechoko/dashboard_roles/llms.txt
Use this file to discover all available pages before exploring further.
Spatie Permission Package
The application uses Spatie Laravel Permission version 5.5:User Model Configuration
The User model includes theHasRoles trait to enable role and permission functionality:
Role Management
Roles are managed through theRolController, which provides full CRUD operations.
Role Controller Permissions
The controller uses middleware to restrict access based on permissions:Permission Types
The system uses four main permission types for role management:View and list all roles in the system
Create new roles and assign permissions
Edit existing roles and update their permissions
Delete roles from the system
Working with Roles
Listing Roles
Retrieve all roles with pagination:Creating a Role
Load Create Form
The create method retrieves all available permissions:Reference: app/Http/Controllers/RolController.php:36-41
Validate Input
The store method validates that both name and permissions are provided:Reference: app/Http/Controllers/RolController.php:52
Editing a Role
To edit a role, you need to retrieve the role, all permissions, and the role’s current permissions:Updating a Role
Update the role name and sync new permissions:The
syncPermissions() method automatically removes old permissions and assigns new ones, ensuring the role has exactly the permissions specified.Deleting a Role
Delete a role from the database:Permission System
Permissions are managed using Spatie’s Permission model:Database Structure
The permission system uses the following tables:roles- Stores role definitionspermissions- Stores permission definitionsrole_has_permissions- Links roles to permissionsmodel_has_roles- Links users to roles
Routes for Role Management
Role management routes are protected by authentication:GET /roles- List all rolesGET /roles/create- Show create formPOST /roles- Store new roleGET /roles/{id}/edit- Show edit formPUT /roles/{id}- Update roleDELETE /roles/{id}- Delete role
Assigning Roles to Users
Roles are assigned when creating or updating users through theUsuarioController:
When updating user roles, the system first removes all existing role assignments before assigning new ones to ensure consistency.
Checking Permissions in Controllers
You can protect controller methods using permission middleware:Role Display Utilities
The User model includes a helper method to get role colors:Best Practices
Plan Your Permissions
Define clear, granular permissions for each feature (e.g.,
ver-rol, crear-rol, editar-rol, borrar-rol).Use Middleware Protection
Always protect routes and controller methods with appropriate permission checks.
Sync Permissions Carefully
When updating roles, use
syncPermissions() to ensure the role has exactly the permissions you intend.