ROLE_SYSTEM.md
Especificación técnica desde la carpeta docs del proyecto
Role-Based Access Control System
Overview
This application implements a comprehensive role-based access control (RBAC) system with 5 distinct user roles. The system includes special developer tools for testing different user experiences.
User Roles
1. MASTER (Developer)
agustinaramac@gmail.com / madmin1232. ADMIN (Administrator)
admin@test.com / admin1233. PROFESOR (Teacher)
profesor@test.com / teacher1234. PARENT (Guardian)
parent@test.com / parent1235. PUBLIC (Anonymous)
Role Switching Feature (MASTER Only)
Purpose
Allows developers to test the application from different user perspectives without creating multiple accounts or logging in/out repeatedly.
How to Use
`bash
Email: agustinaramac@gmail.com
Password: madmin123
`
- Located in the bottom-left user section of the sidebar
- Shows current role with crown icon
- Dropdown menu with all available roles
- Click "Cambiar" button
- Select desired role from dropdown
- Page will refresh automatically
- Navigation and permissions update immediately
- "Volver a MASTER" button appears when switched
- Returns to original MASTER role instantly
Visual Indicators
Technical Implementation
Core Components
1. Database Schema (prisma/schema.prisma)
`prisma
enum UserRole {
MASTER
ADMIN
PROFESOR
PARENT
PUBLIC
}`
2. Authorization System (src/lib/authorization.ts)
3. Proxy Protection (src/proxy.ts)
4. Role Utilities (src/lib/role-utils.ts)
5. Role Switcher Component (src/components/auth/RoleSwitcher.tsx)
6. Role Switching Hook (src/hooks/useRoleSwitching.ts)
7. API Endpoint (src/app/api/role-switch/route.ts)
Security Features
Route Protection
Routes are automatically protected based on user roles:
`
/admin/* → MASTER, ADMIN only
/profesor/* → MASTER, ADMIN, PROFESOR
/parent/* → MASTER, ADMIN, PROFESOR, PARENT
/public/* → All users (including anonymous)`
Testing
Automated Tests
Run role switching tests:
`bash
npm run test:role-switching`
Manual Testing
`bash
npm run dev
`
- Login as MASTER
- Switch to different roles
- Verify navigation changes
- Test permissions
- Reset to MASTER
`bash
npm run create:test-users
`
Best Practices
For Developers
For Security
For UX
Troubleshooting
Common Issues
- Check if user is MASTER
- Verify API endpoint is accessible
- Check browser console for errors
- Page refresh may be required
- Check session storage
- Verify middleware configuration
- Check authorization configuration
- Verify database user role
- Test with different accounts
Debug Mode
Enable debug logging:
`typescript
const logger = Logger.getInstance("RoleSwitchAPI");
// Logs are available in server console`
Future Enhancements
---
Last Updated: December 2024
Maintainer: Agustín (MASTER)
Archivo: ROLE_SYSTEM.md