7.1 KiB
Role-Based Access Control (RBAC) System
This document describes the comprehensive Role-Based Access Control (RBAC) system implemented in the Duxiter application.
Overview
The RBAC system provides fine-grained access control through a permission-based approach, replacing the previous simple role-based authorization. This system allows for more flexible and secure access management across all application features.
User Roles
The system defines five distinct user roles, each with specific permissions:
1. Superuser
- Description: Full system administrator with unrestricted access
- Permissions: All permissions in the system
- Use Case: System administrators, platform owners
2. Tenant Admin
- Description: Administrator within a specific tenant organization
- Permissions: Full access within their tenant scope
- Use Case: Organization administrators, department heads
3. Evaluator
- Description: Standard user who can perform evaluations and access most features
- Permissions: Evaluation operations, monitoring, basic tenant access
- Use Case: Risk analysts, compliance officers
4. Read Only
- Description: User with view-only access to data
- Permissions: Read access to evaluations, companies, RUT data, monitoring, notifications
- Use Case: Auditors, reporting users, stakeholders
5. Write Only
- Description: User who can create/update data but has limited read access
- Permissions: Create evaluations, update companies, RUT lookup, monitoring operations
- Use Case: Data entry personnel, automated systems
Permission System
The system uses granular permissions organized by functional areas:
User Management
USER_CREATE: Create new usersUSER_READ: View user informationUSER_UPDATE: Modify user detailsUSER_DELETE: Remove users
Evaluation Operations
EVALUATION_CREATE: Create single evaluationsEVALUATION_READ: View evaluation resultsEVALUATION_UPDATE: Modify evaluationsEVALUATION_DELETE: Remove evaluationsEVALUATION_BULK: Perform bulk evaluation operations
Company Data
COMPANY_READ: View company informationCOMPANY_UPDATE: Modify company data
RUT Operations
RUT_LOOKUP: Perform RUT lookupsRUT_READ: View RUT-related data and results
Sheriff Logs
SHERIFF_LOGS_READ: Access Sheriff API logs and data
Monitoring
MONITORING_CREATE: Create monitoring schedulesMONITORING_READ: View monitoring dataMONITORING_UPDATE: Modify monitoring settingsMONITORING_DELETE: Remove monitoring schedulesMONITORING_EXECUTE: Execute monitoring tasks
Tenant Management
TENANT_READ: View tenant informationTENANT_UPDATE: Modify tenant settingsTENANT_USERS_MANAGE: Manage users within tenant
Administrative
ADMIN_DASHBOARD: Access administrative featuresADMIN_SETTINGS: Modify system settingsADMIN_LOGS: Access system logs
Notifications
NOTIFICATION_READ: View notificationsNOTIFICATION_CREATE: Send notifications
Implementation
Backend Middleware
The permission system is implemented through middleware functions:
// Single permission check
requirePermission(Permission.USER_CREATE)
// Multiple permissions (any one required)
requireAnyPermission([Permission.USER_READ, Permission.USER_UPDATE])
// Multiple permissions (all required)
requireAllPermissions([Permission.USER_READ, Permission.TENANT_READ])
Route Protection
All API routes are protected with appropriate permission checks:
// Example: User management route
router.post('/users',
authenticate,
tenantFilter,
requirePermission(Permission.USER_CREATE),
createUser
);
Frontend Integration
The frontend receives user role information through the authentication context and can conditionally render UI elements based on user permissions.
Role-Permission Matrix
| Permission | Superuser | Tenant Admin | Evaluator | Read Only | Write Only |
|---|---|---|---|---|---|
| USER_CREATE | ✓ | ✓ | ✗ | ✗ | ✗ |
| USER_READ | ✓ | ✓ | ✓ | ✓ | ✗ |
| EVALUATION_CREATE | ✓ | ✓ | ✓ | ✗ | ✓ |
| EVALUATION_READ | ✓ | ✓ | ✓ | ✓ | ✓ |
| COMPANY_READ | ✓ | ✓ | ✓ | ✓ | ✓ |
| RUT_LOOKUP | ✓ | ✓ | ✓ | ✗ | ✓ |
| MONITORING_CREATE | ✓ | ✓ | ✓ | ✗ | ✓ |
| ADMIN_DASHBOARD | ✓ | ✗ | ✗ | ✗ | ✗ |
| TENANT_USERS_MANAGE | ✓ | ✓ | ✗ | ✗ | ✗ |
Security Considerations
- Principle of Least Privilege: Users are granted only the minimum permissions necessary for their role
- Tenant Isolation: All operations are scoped to the user's tenant (except superuser)
- Permission Validation: Every API endpoint validates permissions before processing requests
- Role Inheritance: Higher-level roles include permissions from lower-level roles where appropriate
Migration from Previous System
The new RBAC system replaces the previous simple role-based authorization:
- Before:
authorize(['superuser', 'tenant_admin']) - After:
requirePermission(Permission.USER_CREATE)
This change provides:
- More granular control
- Better separation of concerns
- Easier permission management
- Improved security
Usage Examples
Creating a New User Role
- Add the role to the
UserRoleenum in the user model - Define permissions for the role in
ROLE_PERMISSIONSmapping - Update frontend type definitions
- Test the new role's access patterns
Adding a New Permission
- Add the permission to the
Permissionenum - Assign it to appropriate roles in
ROLE_PERMISSIONS - Apply the permission to relevant routes
- Update documentation
Checking Permissions in Controllers
import { hasPermission, Permission } from '../middleware/permissions.middleware';
// In controller function
if (!hasPermission(req.user.role, Permission.USER_DELETE)) {
return res.status(403).json({ error: 'Insufficient permissions' });
}
Troubleshooting
Common Issues
- 403 Forbidden Errors: Check if the user's role has the required permission
- Missing Permissions: Verify the permission is defined and assigned to the role
- Route Access Issues: Ensure the correct permission middleware is applied to routes
Debugging
Enable debug logging to trace permission checks:
console.log('User role:', req.user.role);
console.log('Required permission:', permission);
console.log('Has permission:', hasPermission(req.user.role, permission));
Future Enhancements
- Dynamic Permissions: Allow runtime permission assignment
- Resource-Level Permissions: Permissions on specific resources (e.g., specific evaluations)
- Time-Based Permissions: Temporary permission grants
- Audit Logging: Track all permission checks and access attempts
- Permission Groups: Logical grouping of related permissions
Conclusion
The new RBAC system provides a robust, scalable, and secure foundation for access control in the Duxiter application. It enables fine-grained permission management while maintaining simplicity for common use cases.