users.admin.controller.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import {
  2. Controller,
  3. Put,
  4. Get,
  5. Body,
  6. Param,
  7. HttpStatus,
  8. NotFoundException,
  9. Delete,
  10. BadRequestException,
  11. Req,
  12. Post,
  13. ForbiddenException
  14. } from '@nestjs/common'
  15. import { UsersService } from './users.service'
  16. import { UserProfileDto } from './dto/user-profile.dto'
  17. import { UserUpdateDto } from './dto/user-update.dto'
  18. import { IUsers } from './interfaces/users.interface'
  19. import { ApiBearerAuth, ApiTags } from '@nestjs/swagger'
  20. import { HasAnyRoles, HasRoles } from '../auth/roles.decorator'
  21. import { Role } from '../model/role.enum'
  22. import { IPaginationOptions } from 'nestjs-typeorm-paginate'
  23. import { PageRequest } from 'src/common/dto/page-request'
  24. import { Users } from './entities/users.entity'
  25. import { UserCreateDto } from './dto/user-create.dto'
  26. import { In, Like } from 'typeorm'
  27. import { OperationLogService } from '../operation-log/operation-log.service'
  28. import { OperationType } from '../operation-log/entities/operation-log.entity'
  29. @ApiTags('users.admin')
  30. @Controller('/admin/users')
  31. @ApiBearerAuth()
  32. export class UsersAdminController {
  33. constructor(
  34. private readonly usersService: UsersService,
  35. private readonly operationLogService: OperationLogService
  36. ) {}
  37. @Post()
  38. public async list(@Req() req, @Body() page: PageRequest<Users>) {
  39. if (req.user.roles.includes('api')) {
  40. ;(page.search as any).where.invitor = req.user.id
  41. } else if (req.user.roles.includes('superApi')) {
  42. if (!(page.search as any).where.invitor) {
  43. const userIds = await this.usersService.getApiInvitesIds(req.user.id)
  44. ;(page.search as any).where.invitor = In(userIds)
  45. }
  46. }
  47. ;(page.search as any).where = (page.search as any).where || {}
  48. if ((page.search as any).where.username) {
  49. ;(page.search as any).where.username = Like(`%${(page.search as any).where.username}%`)
  50. }
  51. return await this.usersService.findAll(page)
  52. }
  53. @Put()
  54. @HasAnyRoles('admin', 'api', 'superApi')
  55. public async create(@Req() req, @Body() user: UserCreateDto) {
  56. if (user.roles) {
  57. for (const role of user.roles) {
  58. if (role === Role.Admin && !req.user.roles.includes((Role.Admin))) {
  59. throw new ForbiddenException('无权限')
  60. }
  61. if (role === Role.Api && !(req.user.roles.includes(Role.Admin) || req.user.roles.includes((Role.SuperApi)))) {
  62. throw new ForbiddenException('无权限')
  63. }
  64. }
  65. }
  66. const users = await this.usersService.create(user)
  67. await this.operationLogService.create(req, users, 'Users', OperationType.INSERT, '新增用户')
  68. return users
  69. }
  70. @Post('/update')
  71. public async update(@Body() userProfileDto: UserProfileDto, @Req() req): Promise<any> {
  72. if (req.user.roles.includes('user') || req.user.roles.includes('api')) {
  73. if (req.user.userId !== userProfileDto.id) {
  74. throw new BadRequestException('Permission denied!')
  75. }
  76. }
  77. try {
  78. const users = await this.usersService.update(userProfileDto)
  79. await this.operationLogService.create(req, users, 'Users', OperationType.UPDATE, '修改用户')
  80. return {
  81. message: 'User Updated successfully!',
  82. status: HttpStatus.OK
  83. }
  84. } catch (err) {
  85. throw new BadRequestException(err, 'Error: User not updated!')
  86. }
  87. }
  88. @Get('/get')
  89. public async get(@Req() req) {
  90. const user = await this.usersService.findById(req.user.userId)
  91. return user
  92. }
  93. @Put('/:userId')
  94. public async updateUser(@Param('userId') userId: string, @Body() userUpdateDto: UserUpdateDto) {
  95. try {
  96. await this.usersService.updateUser(Number(userId), userUpdateDto)
  97. return {
  98. message: 'User Updated successfully!',
  99. status: HttpStatus.OK
  100. }
  101. } catch (err) {
  102. throw new BadRequestException(err, 'Error: User not updated!')
  103. }
  104. }
  105. @Delete('/:userId')
  106. public async deleteUser(@Param('userId') userId: number): Promise<void> {
  107. await this.usersService.deleteUser(userId)
  108. }
  109. }