forked from wangziqi/gongxue-base
feat(task1): restructure directories for turborepo monorepo
- Move backend/ to apps/server/ via git mv - Move frontend/ to apps/admin/ via git mv - Create packages/typescript-config/ with base, nestjs, and react-vite presets
This commit is contained in:
141
apps/server/src/rbac/rbac.controller.ts
Normal file
141
apps/server/src/rbac/rbac.controller.ts
Normal file
@@ -0,0 +1,141 @@
|
||||
import {
|
||||
Controller, Get, Post, Put, Delete, Body, Param, UseGuards, Request, BadRequestException,
|
||||
} from '@nestjs/common';
|
||||
import { RbacService } from './rbac.service';
|
||||
import { CreateRoleDto, UpdateRoleDto, CreateUserDto, UpdateUserDto, ResetPasswordDto } from './dto/rbac.dto';
|
||||
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
|
||||
import { RequirePermission } from '../auth/decorators/permission.decorator';
|
||||
import { OperationLogsService } from '../operation-logs/operation-logs.service';
|
||||
import { extractRequestInfo } from '../common/request-utils';
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Controller('rbac')
|
||||
export class RbacController {
|
||||
constructor(
|
||||
private rbacService: RbacService,
|
||||
private logService: OperationLogsService,
|
||||
) {}
|
||||
|
||||
// ==================== 角色管理 ====================
|
||||
|
||||
@Get('roles')
|
||||
@RequirePermission('role:view')
|
||||
findAllRoles() {
|
||||
return this.rbacService.findAllRoles();
|
||||
}
|
||||
|
||||
@Get('roles/:id')
|
||||
@RequirePermission('role:view')
|
||||
findRoleById(@Param('id') id: string) {
|
||||
return this.rbacService.findRoleById(+id);
|
||||
}
|
||||
|
||||
@Post('roles')
|
||||
@RequirePermission('role:create')
|
||||
async createRole(@Body() dto: CreateRoleDto, @Request() req: any) {
|
||||
const { ipAddress, userAgent } = extractRequestInfo(req);
|
||||
const result = await this.rbacService.createRole(dto);
|
||||
await this.logService.log({ userId: req.user?.id, username: req.user?.username, module: 'RBAC', action: '创建角色', detail: `角色: ${dto.name}`, ipAddress, userAgent });
|
||||
return result;
|
||||
}
|
||||
|
||||
@Put('roles/:id')
|
||||
@RequirePermission('role:edit')
|
||||
async updateRole(@Param('id') id: string, @Body() dto: UpdateRoleDto, @Request() req: any) {
|
||||
const { ipAddress, userAgent } = extractRequestInfo(req);
|
||||
try {
|
||||
const result = await this.rbacService.updateRole(+id, dto);
|
||||
await this.logService.log({ userId: req.user?.id, username: req.user?.username, module: 'RBAC', action: '编辑角色', targetId: +id, targetType: 'role', detail: JSON.stringify(dto), ipAddress, userAgent });
|
||||
return result;
|
||||
} catch (e: any) {
|
||||
throw new BadRequestException(e.message);
|
||||
}
|
||||
}
|
||||
|
||||
@Delete('roles/:id')
|
||||
@RequirePermission('role:delete')
|
||||
async deleteRole(@Param('id') id: string, @Request() req: any) {
|
||||
const { ipAddress, userAgent } = extractRequestInfo(req);
|
||||
try {
|
||||
const result = await this.rbacService.deleteRole(+id);
|
||||
await this.logService.log({ userId: req.user?.id, username: req.user?.username, module: 'RBAC', action: '删除角色', targetId: +id, targetType: 'role', ipAddress, userAgent });
|
||||
return result;
|
||||
} catch (e: any) {
|
||||
throw new BadRequestException(e.message);
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== 权限管理 ====================
|
||||
|
||||
@Get('permissions')
|
||||
@RequirePermission('role:view')
|
||||
findAllPermissions() {
|
||||
return this.rbacService.findAllPermissions();
|
||||
}
|
||||
|
||||
@Get('permissions/tree')
|
||||
@RequirePermission('role:view')
|
||||
getPermissionTree() {
|
||||
return this.rbacService.getPermissionTree();
|
||||
}
|
||||
|
||||
// ==================== 用户管理 ====================
|
||||
|
||||
@Get('users')
|
||||
@RequirePermission('user:view')
|
||||
getUsers() {
|
||||
return this.rbacService.findAllUsers();
|
||||
}
|
||||
|
||||
@Post('users')
|
||||
@RequirePermission('user:create')
|
||||
async createUser(@Body() dto: CreateUserDto, @Request() req: any) {
|
||||
const { ipAddress, userAgent } = extractRequestInfo(req);
|
||||
try {
|
||||
const result = await this.rbacService.createUser(dto);
|
||||
await this.logService.log({ userId: req.user?.id, username: req.user?.username, module: '账号', action: '创建账号', detail: `用户名: ${dto.username}`, ipAddress, userAgent });
|
||||
return result;
|
||||
} catch (e: any) {
|
||||
throw new BadRequestException(e.message);
|
||||
}
|
||||
}
|
||||
|
||||
@Put('users/:id')
|
||||
@RequirePermission('user:edit')
|
||||
async updateUser(@Param('id') id: string, @Body() dto: UpdateUserDto, @Request() req: any) {
|
||||
const { ipAddress, userAgent } = extractRequestInfo(req);
|
||||
try {
|
||||
const result = await this.rbacService.updateUser(+id, dto);
|
||||
await this.logService.log({ userId: req.user?.id, username: req.user?.username, module: '账号', action: '更新账号', targetId: +id, targetType: 'user', detail: JSON.stringify(dto), ipAddress, userAgent });
|
||||
return result;
|
||||
} catch (e: any) {
|
||||
throw new BadRequestException(e.message);
|
||||
}
|
||||
}
|
||||
|
||||
@Put('users/:id/password')
|
||||
@RequirePermission('user:reset-password')
|
||||
async resetPassword(@Param('id') id: string, @Body() dto: ResetPasswordDto, @Request() req: any) {
|
||||
const { ipAddress, userAgent } = extractRequestInfo(req);
|
||||
try {
|
||||
const result = await this.rbacService.resetPassword(+id, dto.password);
|
||||
await this.logService.log({ userId: req.user?.id, username: req.user?.username, module: '账号', action: '重置密码', targetId: +id, targetType: 'user', ipAddress, userAgent });
|
||||
return result;
|
||||
} catch (e: any) {
|
||||
throw new BadRequestException(e.message);
|
||||
}
|
||||
}
|
||||
|
||||
@Delete('users/:id')
|
||||
@RequirePermission('user:delete')
|
||||
async deleteUser(@Param('id') id: string, @Request() req: any) {
|
||||
const { ipAddress, userAgent } = extractRequestInfo(req);
|
||||
try {
|
||||
const result = await this.rbacService.deleteUser(+id);
|
||||
await this.logService.log({ userId: req.user?.id, username: req.user?.username, module: '账号', action: '删除账号', targetId: +id, targetType: 'user', ipAddress, userAgent });
|
||||
return result;
|
||||
} catch (e: any) {
|
||||
throw new BadRequestException(e.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user