- users: GET/PUT /users/:id/roles(事务化替换 + 过滤无效角色 + 清权限缓存)
- auth: GET /auth/permissions 返回 {super, permissions}
- permissions/seed: 新增 user:assign-role 权限码与资源
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
64 lines
1.9 KiB
TypeScript
64 lines
1.9 KiB
TypeScript
import { Body, Controller, Delete, Get, Param, Patch, Post, Put, Query } from '@nestjs/common';
|
|
import { Permissions } from '../common/permissions.js';
|
|
import { AuditLog, RequirePermissions } from '../auth/decorators.js';
|
|
import { AssignRolesDto, CreateUserDto, ListUserQuery, ResetPasswordDto, UpdateUserDto } from './dto.js';
|
|
import { UsersService } from './users.service.js';
|
|
|
|
@Controller('users')
|
|
export class UsersController {
|
|
constructor(private readonly service: UsersService) {}
|
|
|
|
@Get()
|
|
@RequirePermissions(Permissions.user.list)
|
|
list(@Query() q: ListUserQuery) {
|
|
return this.service.listUsers(q);
|
|
}
|
|
|
|
@Get(':id')
|
|
@RequirePermissions(Permissions.user.list)
|
|
get(@Param('id') id: string) {
|
|
return this.service.findById(id);
|
|
}
|
|
|
|
@Post()
|
|
@RequirePermissions(Permissions.user.create)
|
|
@AuditLog('user', 'create')
|
|
create(@Body() dto: CreateUserDto) {
|
|
return this.service.createUser(dto);
|
|
}
|
|
|
|
@Patch(':id')
|
|
@RequirePermissions(Permissions.user.update)
|
|
@AuditLog('user', 'update')
|
|
update(@Param('id') id: string, @Body() dto: UpdateUserDto) {
|
|
return this.service.updateUser(id, dto);
|
|
}
|
|
|
|
@Patch(':id/password')
|
|
@RequirePermissions(Permissions.user.resetPwd)
|
|
@AuditLog('user', 'reset-pwd')
|
|
resetPassword(@Param('id') id: string, @Body() dto: ResetPasswordDto) {
|
|
return this.service.resetPassword(id, dto);
|
|
}
|
|
|
|
@Get(':id/roles')
|
|
@RequirePermissions(Permissions.user.list)
|
|
roles(@Param('id') id: string) {
|
|
return this.service.assignedRoles(id);
|
|
}
|
|
|
|
@Put(':id/roles')
|
|
@RequirePermissions(Permissions.user.assignRole)
|
|
@AuditLog('user', 'assign-role')
|
|
assignRoles(@Param('id') id: string, @Body() dto: AssignRolesDto) {
|
|
return this.service.assignRoles(id, dto);
|
|
}
|
|
|
|
@Delete(':id')
|
|
@RequirePermissions(Permissions.user.delete)
|
|
@AuditLog('user', 'delete')
|
|
remove(@Param('id') id: string) {
|
|
return this.service.softRemove(id);
|
|
}
|
|
}
|