Skip to content

Latest commit

 

History

History
171 lines (132 loc) · 6.21 KB

File metadata and controls

171 lines (132 loc) · 6.21 KB

Role-Based Access Control (RBAC) Implementation Summary

Overview

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

Implementation Details

1. Role Enum

File: backend/src/auth/roles.enum.ts

Defines the available roles in the system:

export enum Role {
  Admin = 'admin',
  Manager = 'manager',
  Employee = 'employee',
}

2. Updated User Entity

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;

3. Roles Decorator

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);

4. Roles Guard

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);
  }
}

5. Updated Auth Module

File: backend/src/auth/auth.module.ts

Added the RolesGuard to the AuthModule providers and exports.

Usage Example

Protecting Routes with RBAC

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
  }
}

Comprehensive Testing

1. Roles Enum Tests

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

2. Roles Decorator Tests

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

3. Roles Guard Tests

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

4. RBAC Integration Tests

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

Features Implemented

  1. ✅ Roles enum with Admin, Manager, and Employee roles
  2. ✅ Custom Roles decorator for route protection
  3. ✅ RolesGuard that checks user permissions
  4. ✅ Integration with existing User entity
  5. ✅ Comprehensive test coverage for all components
  6. ✅ Example usage in controllers
  7. ✅ Proper error handling and edge case coverage

API Security

The RBAC system provides the following security features:

  1. Route-Level Protection: Controllers or individual routes can be protected with specific role requirements
  2. Flexible Role Assignment: Routes can require one or multiple roles
  3. Default Open Access: Routes without @Roles decorator remain accessible (following NestJS default behavior)
  4. Hierarchical Access: Higher-level roles can access lower-level resources
  5. Clear Authorization Failures: Unauthorized access attempts are properly handled

Testing Strategy

The comprehensive test suite ensures:

  1. Unit Testing: Each component (enum, decorator, guard) is tested individually
  2. Integration Testing: Components work together correctly
  3. Edge Case Coverage: Handles missing roles, invalid configurations, etc.
  4. Role Matrix Testing: All role combinations are verified
  5. Security Validation: Ensures proper access control enforcement

Example RBAC Matrix

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.