This document summarizes the implementation of Role-Based Access Control (RBAC) for the backend system. The RBAC system provides fine-grained access control based on user roles:
- Admin: Full access to all resources
- Manager: Limited CRUD operations on assets
- Employee: Read-only access to assigned assets
File: backend/src/auth/roles.enum.ts
Defines the available roles in the system:
export enum Role {
Admin = 'admin',
Manager = 'manager',
Employee = 'employee',
}File: backend/src/users/entities/user.entity.ts
Updated the User entity to use the Role enum:
@Column({ type: 'enum', enum: Role, default: Role.Employee })
role: Role;File: backend/src/auth/roles.decorator.ts
A custom decorator that specifies which roles are allowed to access a route:
export const Roles = (...roles: Role[]) => SetMetadata(ROLES_KEY, roles);File: backend/src/auth/roles.guard.ts
An authentication guard that checks if the current user has the required role:
@Injectable()
export class RolesGuard implements CanActivate {
canActivate(context: ExecutionContext): boolean {
const requiredRoles = this.reflector.getAllAndOverride<Role[]>('roles', [
context.getHandler(),
context.getClass(),
]);
if (!requiredRoles) {
return true;
}
const { user } = context.switchToHttp().getRequest();
return requiredRoles.some((role) => user.role === role);
}
}File: backend/src/auth/auth.module.ts
Added the RolesGuard to the AuthModule providers and exports.
Here's how to protect routes using the RBAC system:
@Controller('assets')
@UseGuards(RolesGuard) // Apply the guard to the entire controller
export class AssetsController {
@Post()
@Roles(Role.Admin, Role.Manager) // Only Admin and Manager can create assets
create(@Body() dto: CreateAssetDto) {
// Implementation
}
@Get()
@Roles(Role.Admin, Role.Manager, Role.Employee) // All roles can read assets
findAll() {
// Implementation
}
@Delete(':id')
@Roles(Role.Admin) // Only Admin can delete assets
remove(@Param('id') id: string) {
// Implementation
}
}File: backend/src/auth/roles.enum.spec.ts
Tests to verify the Role enum values:
- Verifies each role has the correct string value
- Ensures all three roles are defined
File: backend/src/auth/roles.decorator.spec.ts
Tests to verify the Roles decorator functionality:
- Ensures decorator can handle single role
- Ensures decorator can handle multiple roles
- Verifies ROLES_KEY constant export
File: backend/src/auth/roles.guard.spec.ts
Comprehensive tests for the RolesGuard:
- Allows access when no roles are required
- Allows access when user has required role
- Allows access when user has one of the required roles
- Denies access when user doesn't have required role
- Handles edge cases like missing user roles
- Tests various role combinations
File: backend/src/auth/rbac.integration.spec.ts
Integration tests that demonstrate how RBAC works:
- Shows role-based access patterns
- Demonstrates the access control logic
- Provides examples of how different roles interact with resources
- ✅ Roles enum with Admin, Manager, and Employee roles
- ✅ Custom Roles decorator for route protection
- ✅ RolesGuard that checks user permissions
- ✅ Integration with existing User entity
- ✅ Comprehensive test coverage for all components
- ✅ Example usage in controllers
- ✅ Proper error handling and edge case coverage
The RBAC system provides the following security features:
- Route-Level Protection: Controllers or individual routes can be protected with specific role requirements
- Flexible Role Assignment: Routes can require one or multiple roles
- Default Open Access: Routes without @Roles decorator remain accessible (following NestJS default behavior)
- Hierarchical Access: Higher-level roles can access lower-level resources
- Clear Authorization Failures: Unauthorized access attempts are properly handled
The comprehensive test suite ensures:
- Unit Testing: Each component (enum, decorator, guard) is tested individually
- Integration Testing: Components work together correctly
- Edge Case Coverage: Handles missing roles, invalid configurations, etc.
- Role Matrix Testing: All role combinations are verified
- Security Validation: Ensures proper access control enforcement
| Role | Create Assets | Read Assets | Update Assets | Delete Assets |
|---|---|---|---|---|
| Admin | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |
| Manager | ✅ Yes | ✅ Yes | ✅ Yes | ❌ No |
| Employee | ❌ No | ✅ Yes | ❌ No | ❌ No |
This implementation provides a robust, secure, and well-tested RBAC system that can be easily extended and maintained.