Documentation Index
Fetch the complete documentation index at: https://mintlify.com/LizandroCanul/back_sdo/llms.txt
Use this file to discover all available pages before exploring further.
Overview
The Yucatan Public Works API implements Role-Based Access Control (RBAC) to manage permissions and restrict access to certain endpoints based on user roles. This ensures that users can only perform actions appropriate to their authorization level.Available Roles
The API currently supports two user roles:Admin
Full access to all API endpoints including create, read, update, and delete operations
User
Limited access - can view public works and manage their own profile
Admin Role
Role value:"admin"
Administrators have full control over the API and can:
- Create, update, and delete public works (obras)
- Create, view, update, and delete user accounts
- Access all user profiles and data
- Perform all operations without restrictions
User Role
Role value:"user"
Regular users have limited permissions and can:
- View all public works (read-only access to obras)
- View and update their own profile
- Manage their own favorites list
- Cannot create, update, or delete obras
- Cannot access other users’ profiles (unless viewing public data)
How Roles Are Enforced
The API enforces role-based access control using three components:1. JWT Authentication Guard
TheJwtAuthGuard validates that the user has a valid JWT token:
2. Roles Guard
TheRolesGuard checks if the authenticated user has the required role(s):
roles.guard.ts:9-21
If no specific role is required (no
@Roles() decorator), any authenticated user can access the endpoint.3. Roles Decorator
The@Roles() decorator specifies which roles are allowed to access an endpoint:
roles.decorator.ts
Applying Role Protection
Controllers use the guards and decorators to protect endpoints:Controller-Level Protection
Apply guards to the entire controller to protect all endpoints:obras.controller.ts:22-23
Endpoint-Level Role Requirements
Use the@Roles() decorator to specify which roles can access specific endpoints:
obras.controller.ts:29-33
Permission Matrix
Here’s a comprehensive overview of permissions for each role:Public Works (Obras) Endpoints
| Endpoint | Method | Admin | User | Description |
|---|---|---|---|---|
/obras | GET | ✅ | ✅ | View all public works |
/obras/:id | GET | ✅ | ✅ | View specific public work |
/obras | POST | ✅ | ❌ | Create new public work |
/obras/:id | PATCH | ✅ | ❌ | Update public work |
/obras/:id | DELETE | ✅ | ❌ | Delete public work |
User Management Endpoints
| Endpoint | Method | Admin | User | Description |
|---|---|---|---|---|
/users | GET | ✅ | ❌ | View all users |
/users | POST | ✅ | ❌ | Create new user |
/users/:id | GET | ✅ | ✅* | View user profile |
/users/:id | PATCH | ✅ | ✅* | Update user profile |
/users/:id | DELETE | ✅ | ❌ | Delete user |
/users/:id/favorites | GET | ✅ | ✅* | View user’s favorites |
/users/:id/favorites/:obraId | POST | ✅ | ✅* | Add favorite obra |
/users/:id/favorites/:obraId | DELETE | ✅ | ✅* | Remove favorite obra |
✅* = Users can only access their own profile data, not other users’ data
Protected Endpoint Examples
Admin-Only Endpoint
Only users with theadmin role can create public works:
obras.controller.ts:28-33
Authenticated Users (Any Role)
Endpoints without the@Roles() decorator allow any authenticated user:
obras.controller.ts:35-40
Custom Access Logic
Some endpoints implement custom logic to allow users to access their own data:users.controller.ts:44-55
Error Responses
401 Unauthorized
Returned when no valid JWT token is provided:403 Forbidden
Returned when the user is authenticated but doesn’t have the required role:Implementation Details
Guard Order Matters
When applying multiple guards, order is important:users.controller.ts:26
- JwtAuthGuard executes first to validate the token
- RolesGuard executes second to check role permissions
Role Information in JWT
Roles are embedded in the JWT token payload and validated on each request:auth.service.ts:27-31
Best Practices
Apply guards at the controller level
Apply guards at the controller level
Protect entire controllers by applying guards at the class level, then use
@Roles() decorator only where needed:Implement custom authorization logic when needed
Implement custom authorization logic when needed
Use descriptive error messages
Use descriptive error messages
Provide clear error messages to help clients understand why access was denied:
Document role requirements
Document role requirements
Clearly document which roles can access each endpoint in your API documentation.
Related Resources
Authentication Overview
Learn about the authentication system
JWT Tokens
How to obtain and use JWT tokens