Spaces:
Sleeping
Sleeping
| import { | |
| Controller, | |
| Get, | |
| Post, | |
| Put, | |
| Delete, | |
| Body, | |
| Param, | |
| Query, | |
| Res, | |
| ParseIntPipe, | |
| HttpCode, | |
| HttpStatus, | |
| UseGuards, | |
| UseInterceptors, | |
| UploadedFile, | |
| BadRequestException, | |
| } from '@nestjs/common'; | |
| import type { Response } from 'express'; | |
| import { FileInterceptor } from '@nestjs/platform-express'; | |
| import { | |
| ApiTags, | |
| ApiOperation, | |
| ApiResponse, | |
| ApiBearerAuth, | |
| ApiParam, | |
| ApiQuery, | |
| ApiBody, | |
| ApiConsumes, | |
| } from '@nestjs/swagger'; | |
| import { JwtAuthGuard } from './guards/jwt-auth.guard'; | |
| import { RolesGuard } from './guards/roles.guard'; | |
| import { Roles } from './roles.decorator'; | |
| import { RoleName } from './entities/role.entity'; | |
| import { UserManagementService } from './user-management.service'; | |
| import { AdminDashboardService } from './admin-dashboard.service'; | |
| import type { AdminDashboardSummary } from './admin-dashboard.service'; | |
| import { | |
| UserUpdateDto, | |
| UserStatusUpdateDto, | |
| RoleAssignmentDto, | |
| RoleCreateDto, | |
| RoleUpdateDto, | |
| PermissionCreateDto, | |
| PermissionUpdateDto, | |
| PermissionAssignmentDto, | |
| UserFilterDto, | |
| BulkPermissionsDto, | |
| BulkStatusDto, | |
| } from './dto/user-management.dto'; | |
| import { | |
| UserResponseDto, | |
| UserListResponseDto, | |
| RoleResponseDto, | |
| PermissionResponseDto, | |
| PaginatedResponseDto, | |
| } from './dto/user-response.dto'; | |
| ('π₯ User Management') | |
| ('api/admin') | |
| (JwtAuthGuard) | |
| ('JWT-auth') | |
| export class UserManagementController { | |
| constructor( | |
| private readonly userManagementService: UserManagementService, | |
| private readonly adminDashboardService: AdminDashboardService, | |
| ) {} | |
| // ============ USER MANAGEMENT ENDPOINTS ============ | |
| ('users') | |
| ({ | |
| summary: 'List all users', | |
| description: ` | |
| ## List All Users (Paginated) | |
| Retrieves a paginated list of all users in the system with optional filters. | |
| ### Access Control | |
| - **Authentication Required**: β Yes (Bearer Token) | |
| - **Roles Required**: ADMIN, IT_ADMIN | |
| ### Query Parameters | |
| - \`page\`: Page number (default: 1) | |
| - \`size\`: Items per page (default: 10, max: 100) | |
| - \`sort\`: Sort field (default: createdAt) | |
| - \`status\`: Filter by user status (active, inactive, suspended) | |
| - \`role\`: Filter by role name | |
| - \`campusId\`: Filter by campus | |
| ### Notes | |
| - Results are sorted by creation date descending by default | |
| - Includes user roles and basic profile information | |
| `, | |
| }) | |
| ({ name: 'page', required: false, type: Number, example: 1 }) | |
| ({ name: 'size', required: false, type: Number, example: 10 }) | |
| ({ name: 'sort', required: false, type: String, example: 'createdAt' }) | |
| ({ name: 'status', required: false, enum: ['active', 'inactive', 'suspended'] }) | |
| ({ name: 'role', required: false, type: String, example: 'student' }) | |
| ({ name: 'campusId', required: false, type: Number }) | |
| ({ status: 200, description: 'Paginated list of users' }) | |
| ({ status: 401, description: 'Unauthorized' }) | |
| ({ status: 403, description: 'Forbidden - Admin access required' }) | |
| async getUsers( | |
| ('page', new ParseIntPipe({ optional: true })) page: number = 1, | |
| ('size', new ParseIntPipe({ optional: true })) size: number = 10, | |
| ('sort') sort: string = 'createdAt', | |
| ('status') status?: string, | |
| ('role') role?: string, | |
| ('campusId', new ParseIntPipe({ optional: true })) campusId?: number, | |
| ): Promise<PaginatedResponseDto<UserListResponseDto>> { | |
| const filters: UserFilterDto = {}; | |
| if (status) filters.status = status as any; | |
| if (role) filters.role = role; | |
| if (campusId) filters.campusId = campusId; | |
| return this.userManagementService.getUsers(page, size, sort, filters); | |
| } | |
| ('users/bulk-import') | |
| (RoleName.ADMIN, RoleName.IT_ADMIN) | |
| (RolesGuard) | |
| (FileInterceptor('file')) | |
| ({ summary: 'Bulk import users from CSV' }) | |
| ('multipart/form-data') | |
| ({ | |
| schema: { | |
| type: 'object', | |
| properties: { file: { type: 'string', format: 'binary' } }, | |
| }, | |
| }) | |
| ({ status: 201, description: 'Import results' }) | |
| async bulkImportUsers(() file: Express.Multer.File) { | |
| if (!file) throw new BadRequestException('CSV file is required'); | |
| return this.userManagementService.bulkImportUsers(file.buffer); | |
| } | |
| ('users/search') | |
| ({ | |
| summary: 'Search users', | |
| description: ` | |
| ## Search Users | |
| Search for users by name, email, or other criteria. | |
| ### Access Control | |
| - **Authentication Required**: β Yes (Bearer Token) | |
| - **Roles Required**: ADMIN, IT_ADMIN | |
| ### Search Fields | |
| - Email address | |
| - First name | |
| - Last name | |
| - Combined full name | |
| ### Notes | |
| - Search is case-insensitive | |
| - Partial matches are supported | |
| - Returns up to 50 results | |
| `, | |
| }) | |
| ({ name: 'query', description: 'Search term', type: String, example: 'john' }) | |
| ({ status: 200, description: 'Search results' }) | |
| ({ status: 401, description: 'Unauthorized' }) | |
| ({ status: 403, description: 'Forbidden - Admin access required' }) | |
| async searchUsers(('query') query: string): Promise<UserListResponseDto[]> { | |
| return this.userManagementService.searchUsers(query); | |
| } | |
| // ============ BULK STATUS UPDATE ============ | |
| ('users/bulk-status') | |
| (RoleName.ADMIN, RoleName.IT_ADMIN) | |
| (RolesGuard) | |
| ({ summary: 'Bulk update user status' }) | |
| ({ status: 200, description: 'Users status updated' }) | |
| ({ status: 401, description: 'Unauthorized' }) | |
| ({ status: 403, description: 'Forbidden - Admin access required' }) | |
| async bulkUpdateStatus(() dto: BulkStatusDto) { | |
| return this.userManagementService.bulkUpdateStatus(dto); | |
| } | |
| // ============ USER STATISTICS ============ | |
| ('users/statistics') | |
| (RoleName.ADMIN, RoleName.IT_ADMIN) | |
| (RolesGuard) | |
| ({ summary: 'Get user statistics' }) | |
| ({ status: 200, description: 'User statistics' }) | |
| ({ status: 401, description: 'Unauthorized' }) | |
| ({ status: 403, description: 'Forbidden - Admin access required' }) | |
| async getUserStatistics() { | |
| return this.userManagementService.getUserStatistics(); | |
| } | |
| ('dashboard/summary') | |
| (RoleName.ADMIN, RoleName.IT_ADMIN, RoleName.DEPARTMENT_HEAD) | |
| (RolesGuard) | |
| ({ | |
| summary: 'Admin dashboard summary', | |
| description: | |
| 'Returns DB-backed analytics (user sign-ups by month, course enrollment counts) and recent audit log activity.', | |
| }) | |
| ({ status: 200, description: 'Dashboard summary' }) | |
| ({ status: 401, description: 'Unauthorized' }) | |
| ({ status: 403, description: 'Forbidden' }) | |
| async getAdminDashboardSummary(): Promise<AdminDashboardSummary> { | |
| return this.adminDashboardService.getDashboardSummary(); | |
| } | |
| // ============ USER EXPORT ============ | |
| ('users/export') | |
| (RoleName.ADMIN, RoleName.IT_ADMIN) | |
| (RolesGuard) | |
| ({ summary: 'Export users' }) | |
| ({ name: 'format', required: false, enum: ['csv', 'json'], example: 'json' }) | |
| ({ status: 200, description: 'Exported user data' }) | |
| ({ status: 401, description: 'Unauthorized' }) | |
| ({ status: 403, description: 'Forbidden - Admin access required' }) | |
| async exportUsers( | |
| ('format') format: string = 'json', | |
| ({ passthrough: true }) res: Response, | |
| ) { | |
| const data = await this.userManagementService.exportUsers(format); | |
| if (format === 'csv') { | |
| res.setHeader('Content-Type', 'text/csv'); | |
| res.setHeader('Content-Disposition', 'attachment; filename=users.csv'); | |
| return data; | |
| } | |
| return data; | |
| } | |
| ('users/:id') | |
| ({ | |
| summary: 'Get user by ID', | |
| description: ` | |
| ## Get User Details | |
| Retrieves complete details of a specific user by their ID. | |
| ### Access Control | |
| - **Authentication Required**: β Yes (Bearer Token) | |
| - **Roles Required**: ADMIN, IT_ADMIN | |
| ### Response Includes | |
| - User profile information | |
| - All assigned roles | |
| - Associated permissions | |
| - Account status and activity | |
| `, | |
| }) | |
| ({ name: 'id', description: 'User ID', type: Number }) | |
| ({ status: 200, description: 'User details' }) | |
| ({ status: 401, description: 'Unauthorized' }) | |
| ({ status: 403, description: 'Forbidden - Admin access required' }) | |
| ({ status: 404, description: 'User not found' }) | |
| async getUserById(('id', ParseIntPipe) userId: number): Promise<UserResponseDto> { | |
| return this.userManagementService.getUserById(userId); | |
| } | |
| ('users/:id') | |
| ({ | |
| summary: 'Update user', | |
| description: ` | |
| ## Update User Profile | |
| Updates a user's profile information. | |
| ### Access Control | |
| - **Authentication Required**: β Yes (Bearer Token) | |
| - **Roles Required**: ADMIN, IT_ADMIN | |
| ### Updatable Fields | |
| - firstName, lastName | |
| - phone number | |
| - Campus assignment | |
| - Profile settings | |
| ### Notes | |
| - Email cannot be changed through this endpoint | |
| - Password changes use separate endpoint | |
| - Role changes use the role assignment endpoints | |
| `, | |
| }) | |
| ({ name: 'id', description: 'User ID', type: Number }) | |
| ({ type: UserUpdateDto }) | |
| ({ status: 200, description: 'User updated successfully' }) | |
| ({ status: 400, description: 'Invalid input data' }) | |
| ({ status: 401, description: 'Unauthorized' }) | |
| ({ status: 403, description: 'Forbidden - Admin access required' }) | |
| ({ status: 404, description: 'User not found' }) | |
| async updateUser( | |
| ('id', ParseIntPipe) userId: number, | |
| () updateDto: UserUpdateDto, | |
| ): Promise<UserResponseDto> { | |
| return this.userManagementService.updateUser(userId, updateDto); | |
| } | |
| ('users/:id') | |
| (HttpStatus.NO_CONTENT) | |
| ({ | |
| summary: 'Delete user', | |
| description: ` | |
| ## Delete User Account | |
| Permanently deletes a user account from the system. | |
| ### Access Control | |
| - **Authentication Required**: β Yes (Bearer Token) | |
| - **Roles Required**: ADMIN, IT_ADMIN | |
| ### β οΈ Warning | |
| This action is **irreversible**. Consider using status update to deactivate instead. | |
| ### Process Flow | |
| 1. Removes all user sessions | |
| 2. Removes role assignments | |
| 3. Removes user record | |
| 4. Related data may be orphaned or cascade deleted | |
| `, | |
| }) | |
| ({ name: 'id', description: 'User ID', type: Number }) | |
| ({ status: 204, description: 'User deleted successfully' }) | |
| ({ status: 401, description: 'Unauthorized' }) | |
| ({ status: 403, description: 'Forbidden - Admin access required' }) | |
| ({ status: 404, description: 'User not found' }) | |
| async deleteUser(('id', ParseIntPipe) userId: number): Promise<void> { | |
| return this.userManagementService.deleteUser(userId); | |
| } | |
| ('users/:id/status') | |
| ({ | |
| summary: 'Update user status', | |
| description: ` | |
| ## Update User Account Status | |
| Changes the status of a user account (activate, deactivate, suspend). | |
| ### Access Control | |
| - **Authentication Required**: β Yes (Bearer Token) | |
| - **Roles Required**: ADMIN, IT_ADMIN | |
| ### Available Statuses | |
| - \`active\`: User can access the system normally | |
| - \`inactive\`: User cannot login (soft disable) | |
| - \`suspended\`: User is temporarily blocked (can include reason) | |
| ### Notes | |
| - Suspended users' active sessions are terminated | |
| - Reactivating a user requires a new login | |
| `, | |
| }) | |
| ({ name: 'id', description: 'User ID', type: Number }) | |
| ({ type: UserStatusUpdateDto }) | |
| ({ status: 200, description: 'User status updated' }) | |
| ({ status: 401, description: 'Unauthorized' }) | |
| ({ status: 403, description: 'Forbidden - Admin access required' }) | |
| ({ status: 404, description: 'User not found' }) | |
| async updateUserStatus( | |
| ('id', ParseIntPipe) userId: number, | |
| () statusDto: UserStatusUpdateDto, | |
| ): Promise<UserResponseDto> { | |
| return this.userManagementService.updateUserStatus(userId, statusDto); | |
| } | |
| // ============ USER ROLE MANAGEMENT ============ | |
| ('users/:id/roles') | |
| (HttpStatus.CREATED) | |
| ({ | |
| summary: 'Assign role to user', | |
| description: ` | |
| ## Assign Role to User | |
| Assigns an additional role to a user. | |
| ### Access Control | |
| - **Authentication Required**: β Yes (Bearer Token) | |
| - **Roles Required**: ADMIN, IT_ADMIN | |
| ### Available Roles | |
| - \`student\`: Regular student access | |
| - \`instructor\`: Faculty/teaching access | |
| - \`ta\`: Teaching assistant access | |
| - \`admin\`: Administrative access | |
| - \`it_admin\`: Full system access | |
| ### Notes | |
| - Users can have multiple roles | |
| - Duplicate role assignments are ignored | |
| - New permissions take effect immediately | |
| `, | |
| }) | |
| ({ name: 'id', description: 'User ID', type: Number }) | |
| ({ type: RoleAssignmentDto }) | |
| ({ status: 201, description: 'Role assigned successfully' }) | |
| ({ status: 400, description: 'Invalid role or already assigned' }) | |
| ({ status: 401, description: 'Unauthorized' }) | |
| ({ status: 403, description: 'Forbidden - Admin access required' }) | |
| ({ status: 404, description: 'User or role not found' }) | |
| async assignRoleToUser( | |
| ('id', ParseIntPipe) userId: number, | |
| () roleDto: RoleAssignmentDto, | |
| ): Promise<UserResponseDto> { | |
| return this.userManagementService.assignRoleToUser(userId, roleDto); | |
| } | |
| ('users/:id/roles/:roleId') | |
| (HttpStatus.NO_CONTENT) | |
| ({ | |
| summary: 'Remove role from user', | |
| description: ` | |
| ## Remove Role from User | |
| Removes a role from a user. | |
| ### Access Control | |
| - **Authentication Required**: β Yes (Bearer Token) | |
| - **Roles Required**: ADMIN, IT_ADMIN | |
| ### Notes | |
| - Users must have at least one role | |
| - Removing all roles will fail | |
| - Permission changes take effect on next request | |
| `, | |
| }) | |
| ({ name: 'id', description: 'User ID', type: Number }) | |
| ({ name: 'roleId', description: 'Role ID to remove', type: Number }) | |
| ({ status: 204, description: 'Role removed successfully' }) | |
| ({ status: 400, description: 'Cannot remove last role' }) | |
| ({ status: 401, description: 'Unauthorized' }) | |
| ({ status: 403, description: 'Forbidden - Admin access required' }) | |
| ({ status: 404, description: 'User or role assignment not found' }) | |
| async removeRoleFromUser( | |
| ('id', ParseIntPipe) userId: number, | |
| ('roleId', ParseIntPipe) roleId: number, | |
| ): Promise<void> { | |
| return this.userManagementService.removeRoleFromUser(userId, roleId); | |
| } | |
| ('users/:id/permissions') | |
| ({ | |
| summary: 'Get user permissions', | |
| description: ` | |
| ## Get User Effective Permissions | |
| Returns all permissions a user has through their assigned roles. | |
| ### Access Control | |
| - **Authentication Required**: β Yes (Bearer Token) | |
| - **Roles Required**: ADMIN, IT_ADMIN | |
| ### Response Includes | |
| - Direct permissions from each role | |
| - Aggregated unique permissions | |
| - Permission module groupings | |
| `, | |
| }) | |
| ({ name: 'id', description: 'User ID', type: Number }) | |
| ({ status: 200, description: 'User permissions list' }) | |
| ({ status: 401, description: 'Unauthorized' }) | |
| ({ status: 403, description: 'Forbidden - Admin access required' }) | |
| ({ status: 404, description: 'User not found' }) | |
| async getUserPermissions( | |
| ('id', ParseIntPipe) userId: number, | |
| ): Promise<PermissionResponseDto[]> { | |
| return this.userManagementService.getUserPermissions(userId); | |
| } | |
| // ============ ROLE MANAGEMENT ENDPOINTS ============ | |
| ('roles') | |
| ({ | |
| summary: 'List all roles', | |
| description: ` | |
| ## List All Roles | |
| Returns all available roles in the system. | |
| ### Access Control | |
| - **Authentication Required**: β Yes (Bearer Token) | |
| - **Roles Required**: ADMIN, IT_ADMIN | |
| ### Response Includes | |
| - Role ID and name | |
| - Role description | |
| - Associated permissions count | |
| `, | |
| }) | |
| ({ status: 200, description: 'List of all roles' }) | |
| ({ status: 401, description: 'Unauthorized' }) | |
| ({ status: 403, description: 'Forbidden - Admin access required' }) | |
| async getAllRoles(): Promise<RoleResponseDto[]> { | |
| return this.userManagementService.getAllRoles(); | |
| } | |
| ('roles/with-users') | |
| (RoleName.ADMIN, RoleName.IT_ADMIN) | |
| (RolesGuard) | |
| ({ | |
| summary: 'List roles with user counts', | |
| description: ` | |
| ## Roles with User Counts | |
| Returns all roles along with the number of users assigned to each role. | |
| ### Access Control | |
| - **Authentication Required**: β Yes (Bearer Token) | |
| - **Roles Required**: ADMIN, IT_ADMIN | |
| `, | |
| }) | |
| ({ status: 200, description: 'List of roles with user counts' }) | |
| ({ status: 401, description: 'Unauthorized' }) | |
| ({ status: 403, description: 'Forbidden - Admin access required' }) | |
| async getRolesWithUserCounts() { | |
| return this.userManagementService.getRolesWithUserCounts(); | |
| } | |
| ('roles/:id') | |
| ({ | |
| summary: 'Get role by ID', | |
| description: ` | |
| ## Get Role Details | |
| Returns details of a specific role including its permissions. | |
| ### Access Control | |
| - **Authentication Required**: β Yes (Bearer Token) | |
| - **Roles Required**: ADMIN, IT_ADMIN | |
| `, | |
| }) | |
| ({ name: 'id', description: 'Role ID', type: Number }) | |
| ({ status: 200, description: 'Role details' }) | |
| ({ status: 401, description: 'Unauthorized' }) | |
| ({ status: 403, description: 'Forbidden - Admin access required' }) | |
| ({ status: 404, description: 'Role not found' }) | |
| async getRoleById(('id', ParseIntPipe) roleId: number): Promise<RoleResponseDto> { | |
| return this.userManagementService.getRoleById(roleId); | |
| } | |
| ('roles') | |
| (HttpStatus.CREATED) | |
| ({ | |
| summary: 'Create new role', | |
| description: ` | |
| ## Create New Role | |
| Creates a new role in the system. | |
| ### Access Control | |
| - **Authentication Required**: β Yes (Bearer Token) | |
| - **Roles Required**: IT_ADMIN only | |
| ### Notes | |
| - Role names must be unique | |
| - New roles start with no permissions | |
| - Use permission assignment endpoints to add permissions | |
| `, | |
| }) | |
| ({ type: RoleCreateDto }) | |
| ({ status: 201, description: 'Role created successfully' }) | |
| ({ status: 400, description: 'Invalid input or duplicate role name' }) | |
| ({ status: 401, description: 'Unauthorized' }) | |
| ({ status: 403, description: 'Forbidden - IT Admin access required' }) | |
| async createRole(() createDto: RoleCreateDto): Promise<RoleResponseDto> { | |
| return this.userManagementService.createRole(createDto); | |
| } | |
| ('roles/:id') | |
| ({ | |
| summary: 'Update role', | |
| description: ` | |
| ## Update Role | |
| Updates an existing role's name or description. | |
| ### Access Control | |
| - **Authentication Required**: β Yes (Bearer Token) | |
| - **Roles Required**: IT_ADMIN only | |
| ### Notes | |
| - Built-in roles (student, instructor, admin, etc.) should not be renamed | |
| - Permission changes use separate endpoints | |
| `, | |
| }) | |
| ({ name: 'id', description: 'Role ID', type: Number }) | |
| ({ type: RoleUpdateDto }) | |
| ({ status: 200, description: 'Role updated successfully' }) | |
| ({ status: 400, description: 'Invalid input' }) | |
| ({ status: 401, description: 'Unauthorized' }) | |
| ({ status: 403, description: 'Forbidden - IT Admin access required' }) | |
| ({ status: 404, description: 'Role not found' }) | |
| async updateRole( | |
| ('id', ParseIntPipe) roleId: number, | |
| () updateDto: RoleUpdateDto, | |
| ): Promise<RoleResponseDto> { | |
| return this.userManagementService.updateRole(roleId, updateDto); | |
| } | |
| ('roles/:id') | |
| (HttpStatus.NO_CONTENT) | |
| ({ | |
| summary: 'Delete role', | |
| description: ` | |
| ## Delete Role | |
| Deletes a role from the system. | |
| ### Access Control | |
| - **Authentication Required**: β Yes (Bearer Token) | |
| - **Roles Required**: IT_ADMIN only | |
| ### β οΈ Warning | |
| - Built-in roles cannot be deleted | |
| - Roles with assigned users cannot be deleted | |
| - This action is irreversible | |
| `, | |
| }) | |
| ({ name: 'id', description: 'Role ID', type: Number }) | |
| ({ status: 204, description: 'Role deleted successfully' }) | |
| ({ status: 400, description: 'Cannot delete built-in or assigned role' }) | |
| ({ status: 401, description: 'Unauthorized' }) | |
| ({ status: 403, description: 'Forbidden - IT Admin access required' }) | |
| ({ status: 404, description: 'Role not found' }) | |
| async deleteRole(('id', ParseIntPipe) roleId: number): Promise<void> { | |
| return this.userManagementService.deleteRole(roleId); | |
| } | |
| // ============ ROLE PERMISSION MANAGEMENT ============ | |
| ('roles/:id/permissions') | |
| (HttpStatus.CREATED) | |
| ({ | |
| summary: 'Add permission to role', | |
| description: ` | |
| ## Add Permission to Role | |
| Assigns a permission to a role. | |
| ### Access Control | |
| - **Authentication Required**: β Yes (Bearer Token) | |
| - **Roles Required**: IT_ADMIN only | |
| ### Notes | |
| - All users with this role will gain the permission | |
| - Duplicate assignments are ignored | |
| `, | |
| }) | |
| ({ name: 'id', description: 'Role ID', type: Number }) | |
| ({ type: PermissionAssignmentDto }) | |
| ({ status: 201, description: 'Permission added to role' }) | |
| ({ status: 400, description: 'Invalid permission or already assigned' }) | |
| ({ status: 401, description: 'Unauthorized' }) | |
| ({ status: 403, description: 'Forbidden - IT Admin access required' }) | |
| ({ status: 404, description: 'Role or permission not found' }) | |
| async addPermissionToRole( | |
| ('id', ParseIntPipe) roleId: number, | |
| () permDto: PermissionAssignmentDto, | |
| ): Promise<RoleResponseDto> { | |
| return this.userManagementService.addPermissionToRole(roleId, permDto); | |
| } | |
| ('roles/:id/permissions/:permId') | |
| (HttpStatus.NO_CONTENT) | |
| ({ | |
| summary: 'Remove permission from role', | |
| description: ` | |
| ## Remove Permission from Role | |
| Removes a permission from a role. | |
| ### Access Control | |
| - **Authentication Required**: β Yes (Bearer Token) | |
| - **Roles Required**: IT_ADMIN only | |
| ### Notes | |
| - All users with this role will lose the permission | |
| - Changes take effect on next request | |
| `, | |
| }) | |
| ({ name: 'id', description: 'Role ID', type: Number }) | |
| ({ name: 'permId', description: 'Permission ID', type: Number }) | |
| ({ status: 204, description: 'Permission removed from role' }) | |
| ({ status: 401, description: 'Unauthorized' }) | |
| ({ status: 403, description: 'Forbidden - IT Admin access required' }) | |
| ({ status: 404, description: 'Role or permission assignment not found' }) | |
| async removePermissionFromRole( | |
| ('id', ParseIntPipe) roleId: number, | |
| ('permId', ParseIntPipe) permissionId: number, | |
| ): Promise<void> { | |
| return this.userManagementService.removePermissionFromRole(roleId, permissionId); | |
| } | |
| ('roles/:id/permissions/bulk') | |
| (RoleName.IT_ADMIN) | |
| (RolesGuard) | |
| ({ | |
| summary: 'Bulk replace role permissions', | |
| description: ` | |
| ## Bulk Replace Role Permissions | |
| Replaces all permissions for a role with the provided set. | |
| ### Access Control | |
| - **Authentication Required**: β Yes (Bearer Token) | |
| - **Roles Required**: IT_ADMIN only | |
| ### Notes | |
| - This replaces ALL existing permissions β any not included will be removed | |
| - Pass an empty array to remove all permissions | |
| `, | |
| }) | |
| ({ name: 'id', description: 'Role ID', type: Number }) | |
| ({ type: BulkPermissionsDto }) | |
| ({ status: 200, description: 'Permissions replaced successfully' }) | |
| ({ status: 400, description: 'Invalid input' }) | |
| ({ status: 401, description: 'Unauthorized' }) | |
| ({ status: 403, description: 'Forbidden - IT Admin access required' }) | |
| ({ status: 404, description: 'Role not found' }) | |
| async bulkSetPermissions( | |
| ('id', ParseIntPipe) roleId: number, | |
| () bulkDto: BulkPermissionsDto, | |
| ): Promise<RoleResponseDto> { | |
| return this.userManagementService.bulkSetPermissions(roleId, bulkDto.permissionIds); | |
| } | |
| // ============ PERMISSION MANAGEMENT ENDPOINTS ============ | |
| ('permissions') | |
| ({ | |
| summary: 'List all permissions', | |
| description: ` | |
| ## List All Permissions | |
| Returns all available permissions in the system. | |
| ### Access Control | |
| - **Authentication Required**: β Yes (Bearer Token) | |
| - **Roles Required**: ADMIN, IT_ADMIN | |
| `, | |
| }) | |
| ({ status: 200, description: 'List of all permissions' }) | |
| ({ status: 401, description: 'Unauthorized' }) | |
| ({ status: 403, description: 'Forbidden - Admin access required' }) | |
| async getAllPermissions(): Promise<PermissionResponseDto[]> { | |
| return this.userManagementService.getAllPermissions(); | |
| } | |
| ('permissions/matrix') | |
| (RoleName.ADMIN, RoleName.IT_ADMIN) | |
| (RolesGuard) | |
| ({ | |
| summary: 'Get permission matrix', | |
| description: ` | |
| ## Permission Matrix | |
| Returns a matrix showing which permissions are assigned to which roles. | |
| ### Access Control | |
| - **Authentication Required**: β Yes (Bearer Token) | |
| - **Roles Required**: ADMIN, IT_ADMIN | |
| ### Response Structure | |
| - **roles**: List of all roles (id, name, description) | |
| - **permissions**: List of all permissions (id, name, description, module) sorted by module | |
| - **matrix**: Object mapping roleId β array of permissionIds | |
| `, | |
| }) | |
| ({ status: 200, description: 'Permission matrix' }) | |
| ({ status: 401, description: 'Unauthorized' }) | |
| ({ status: 403, description: 'Forbidden - Admin access required' }) | |
| async getPermissionMatrix() { | |
| return this.userManagementService.getPermissionMatrix(); | |
| } | |
| ('permissions/module/:module') | |
| ({ | |
| summary: 'Get permissions by module', | |
| description: ` | |
| ## Get Permissions by Module | |
| Returns all permissions belonging to a specific module. | |
| ### Access Control | |
| - **Authentication Required**: β Yes (Bearer Token) | |
| - **Roles Required**: ADMIN, IT_ADMIN | |
| ### Available Modules | |
| - auth, users, courses, enrollments, files, campus, etc. | |
| `, | |
| }) | |
| ({ name: 'module', description: 'Module name', type: String, example: 'courses' }) | |
| ({ status: 200, description: 'Permissions for the module' }) | |
| ({ status: 401, description: 'Unauthorized' }) | |
| ({ status: 403, description: 'Forbidden - Admin access required' }) | |
| async getPermissionsByModule(('module') module: string): Promise<PermissionResponseDto[]> { | |
| return this.userManagementService.getPermissionsByModule(module); | |
| } | |
| ('permissions') | |
| (HttpStatus.CREATED) | |
| ({ | |
| summary: 'Create permission', | |
| description: ` | |
| ## Create New Permission | |
| Creates a new permission in the system. | |
| ### Access Control | |
| - **Authentication Required**: β Yes (Bearer Token) | |
| - **Roles Required**: IT_ADMIN only | |
| ### Notes | |
| - Permission names should follow pattern: module:action | |
| - Example: courses:create, users:delete | |
| `, | |
| }) | |
| ({ type: PermissionCreateDto }) | |
| ({ status: 201, description: 'Permission created successfully' }) | |
| ({ status: 400, description: 'Invalid input or duplicate permission' }) | |
| ({ status: 401, description: 'Unauthorized' }) | |
| ({ status: 403, description: 'Forbidden - IT Admin access required' }) | |
| async createPermission(() createDto: PermissionCreateDto): Promise<PermissionResponseDto> { | |
| return this.userManagementService.createPermission(createDto); | |
| } | |
| ('permissions/:id') | |
| ({ | |
| summary: 'Update permission', | |
| description: ` | |
| ## Update Permission | |
| Updates an existing permission. | |
| ### Access Control | |
| - **Authentication Required**: β Yes (Bearer Token) | |
| - **Roles Required**: IT_ADMIN only | |
| `, | |
| }) | |
| ({ name: 'id', description: 'Permission ID', type: Number }) | |
| ({ type: PermissionUpdateDto }) | |
| ({ status: 200, description: 'Permission updated successfully' }) | |
| ({ status: 400, description: 'Invalid input' }) | |
| ({ status: 401, description: 'Unauthorized' }) | |
| ({ status: 403, description: 'Forbidden - IT Admin access required' }) | |
| ({ status: 404, description: 'Permission not found' }) | |
| async updatePermission( | |
| ('id', ParseIntPipe) permissionId: number, | |
| () updateDto: PermissionUpdateDto, | |
| ): Promise<PermissionResponseDto> { | |
| return this.userManagementService.updatePermission(permissionId, updateDto); | |
| } | |
| ('permissions/:id') | |
| (HttpStatus.NO_CONTENT) | |
| ({ | |
| summary: 'Delete permission', | |
| description: ` | |
| ## Delete Permission | |
| Deletes a permission from the system. | |
| ### Access Control | |
| - **Authentication Required**: β Yes (Bearer Token) | |
| - **Roles Required**: IT_ADMIN only | |
| ### β οΈ Warning | |
| - Permissions assigned to roles will be removed | |
| - This action is irreversible | |
| `, | |
| }) | |
| ({ name: 'id', description: 'Permission ID', type: Number }) | |
| ({ status: 204, description: 'Permission deleted successfully' }) | |
| ({ status: 401, description: 'Unauthorized' }) | |
| ({ status: 403, description: 'Forbidden - IT Admin access required' }) | |
| ({ status: 404, description: 'Permission not found' }) | |
| async deletePermission(('id', ParseIntPipe) permissionId: number): Promise<void> { | |
| return this.userManagementService.deletePermission(permissionId); | |
| } | |
| } | |