Overview
ServITech implements a role-based access control (RBAC) system to manage user permissions and restrict access to specific endpoints. The system uses the Spatie Permission package to assign roles to users and protect routes based on those roles.User Roles
The application defines three distinct user roles inapp/Enums/UserRoles.php:5:
Role Descriptions
User
Default role for registered users. Can access public content and manage their own profile and support requests.
Employee
Extended permissions for staff members. (Currently defined but not actively used in route protection)
Admin
Full system access. Can manage categories, repair requests, and all administrative functions.
Role Assignment
New users are automatically assigned theUSER role upon registration:
Role assignment during registration ensures all users have appropriate base permissions. Admin and Employee roles must be manually assigned through database operations or admin interfaces.
Route Protection
Routes are protected using Laravel middleware defined inroutes/api.php.
Public Routes
Accessible to all users without authentication:Authenticated Routes
Require valid JWT token (auth:api middleware):
Admin-Only Routes
Require both authentication andadmin role:
Middleware Chain
Admin routes use a middleware chain:Spatie Permission Package
The system uses spatie/laravel-permission for role management.Configuration
Key settings fromconfig/permission.php:3:
Key Features
- Role caching: Permissions are cached for 24 hours to improve performance
- Automatic cache invalidation: Cache is flushed when roles/permissions are updated
- Multiple role assignment: Users can have multiple roles (though this API typically assigns one)
Checking Permissions in Code
While the current implementation primarily uses route middleware, you can also check permissions programmatically:Check User Role
Check in Blade Views
Assign/Remove Roles
Authorization Flow
Error Responses
401 Unauthorized - Missing or Invalid Token
403 Forbidden - Insufficient Permissions
Error messages are localized based on the
Accept-Language header. See Localization for details.Protected Endpoints Summary
| Endpoint Pattern | Authentication | Role Required | Purpose |
|---|---|---|---|
/auth/* | No | None | Login, register, password reset |
/articles (GET) | No | None | Browse products |
/user/* | Yes | Any | Profile management |
/support-request/* | Yes | Any | Support tickets |
/category/* | Yes | Admin | Category management |
/repair-request/* | Yes | Admin | Repair orders |
/articles (POST/PUT/DELETE) | Varies | Varies | Article management |
Best Practices
Always use enum values for role names
Always use enum values for role names
Use
UserRoles::ADMIN->value instead of hardcoded strings to prevent typos and enable refactoring.Nest role middleware inside auth middleware
Nest role middleware inside auth middleware
Role checks require an authenticated user. Always place role middleware after authentication.
Cache permissions in production
Cache permissions in production
The Spatie package caches permissions by default. Don’t disable caching in production environments.
Use descriptive role names in translations
Use descriptive role names in translations
Role names are translated using the
__('enums.user_roles.{role}') pattern for user-facing displays.Database Schema
The Spatie Permission package creates these tables:roles- Stores role definitionsmodel_has_roles- Pivot table linking users to rolespermissions- Stores granular permissions (not currently used)role_has_permissions- Links permissions to rolesmodel_has_permissions- Direct user permissions (not currently used)
The current implementation uses role-based routing. The permission system is available for future granular access control if needed.
Next Steps
Authentication
Learn how JWT tokens are generated and validated
Error Handling
Understand authorization error responses