Role Hierarchy
ADMIN
Full System Access
- Manage all bancas, ventanas, and users
- Access all tickets and sorteos
- Configure system-wide settings
- View global analytics
- Manage commission policies at all levels
- Create and modify restriction rules
VENTANA
Ventana Scope Access
- Manage vendedores in their ventana
- View tickets from their ventana
- Access ventana-level analytics
- Create tickets for their vendedores
- View sorteos and loterias
- Cannot modify system configuration
VENDEDOR
Personal Scope Access
- Create tickets for themselves
- View their own tickets
- Access personal sales analytics
- View active sorteos
- Cannot manage other users
- Cannot modify configuration
Role Enforcement
Roles are enforced using middleware guards insrc/middlewares/roleGuards.middleware.ts:
Basic Role Guards
Usage in Routes
Data Scope Filtering
The system applies automatic data filtering based on user role using theapplyRbacFilters utility:
ADMIN Scope
- No filters applied
- Can access all data across all bancas and ventanas
VENTANA Scope
- Filters applied:
WHERE ventanaId = {user.ventanaId} - Can only access data from their assigned ventana
- This includes tickets, vendedores, and analytics
VENDEDOR Scope
- Filters applied:
WHERE vendedorId = {user.id} - Can only access their own data
- Cannot see other vendedores’ tickets or sales
Implementation Example
Permission Matrix
| Resource | ADMIN | VENTANA | VENDEDOR |
|---|---|---|---|
| Bancas | |||
| Create/Update/Delete | ✅ | ❌ | ❌ |
| View All | ✅ | ❌ | ❌ |
| View Own | ✅ | ✅ | ✅ |
| Ventanas | |||
| Create/Update/Delete | ✅ | ❌ | ❌ |
| View All | ✅ | ❌ | ❌ |
| View Own | ✅ | ✅ | ✅ |
| Users/Vendedores | |||
| Create Any | ✅ | ❌ | ❌ |
| Create in Ventana | ✅ | ✅ | ❌ |
| Update Any | ✅ | ❌ | ❌ |
| Update in Ventana | ✅ | ✅ | ❌ |
| Update Self | ✅ | ✅ | ✅ |
| View All | ✅ | ❌ | ❌ |
| View in Ventana | ✅ | ✅ | ❌ |
| Tickets | |||
| Create for Any Vendedor | ✅ | ❌ | ❌ |
| Create for Ventana Vendedor | ✅ | ✅ | ❌ |
| Create for Self | ✅ | ✅ | ✅ |
| View All | ✅ | ❌ | ❌ |
| View Ventana | ✅ | ✅ | ❌ |
| View Own | ✅ | ✅ | ✅ |
| Cancel Any | ✅ | ❌ | ❌ |
| Cancel Ventana | ✅ | ✅ | ❌ |
| Cancel Own | ✅ | ✅ | ✅ |
| Sorteos | |||
| Create/Update/Delete | ✅ | ❌ | ❌ |
| Open/Close/Evaluate | ✅ | ❌ | ❌ |
| View All | ✅ | ✅ | ✅ |
| Loterias | |||
| Create/Update/Delete | ✅ | ❌ | ❌ |
| View All | ✅ | ✅ | ✅ |
| Commission Policies | |||
| Set Banca Policy | ✅ | ❌ | ❌ |
| Set Ventana Policy | ✅ | ❌ | ❌ |
| Set User Policy | ✅ | ❌ | ❌ |
| View All Policies | ✅ | ❌ | ❌ |
| View Own Policy | ✅ | ✅ | ✅ |
| Restriction Rules | |||
| Create/Update/Delete | ✅ | ❌ | ❌ |
| View All | ✅ | ✅ | ✅ |
| Analytics | |||
| Global Dashboard | ✅ | ❌ | ❌ |
| Ventana Dashboard | ✅ | ✅ | ❌ |
| Personal Dashboard | ✅ | ✅ | ✅ |
Impersonation (Vendedor ID)
VENTANA and ADMIN roles can create tickets on behalf of vendedores:Activity Logging
All role-restricted actions are logged in theActivityLog table with:
userId- Who performed the actionaction- What action was performed (e.g.,TICKET_CREATE,USER_UPDATE)targetTypeandtargetId- What resource was affecteddetails- Additional context (JSON)
Best Practices
Always use role guards on protected routes
Always use role guards on protected routes
Never rely on client-side role checks. Always enforce permissions at the API level using middleware.
Apply RBAC filters to all queries
Apply RBAC filters to all queries
Use
applyRbacFilters consistently to prevent data leakage:Validate vendedorId in impersonation scenarios
Validate vendedorId in impersonation scenarios
When VENTANA creates tickets for vendedores, verify the vendedor belongs to their ventana:
Related Resources
Authentication
Learn about JWT authentication
Activity Logs
View activity log API