users.controller.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import { NextFunction, Request, Response, Router } from 'express';
  2. import { dtoValidationMiddleware } from '../middlewares/validation';
  3. import { UserService } from './users.service';
  4. import { milvusService } from '../milvus';
  5. import {
  6. CreateUserDto,
  7. UpdateUserDto,
  8. CreateRoleDto,
  9. AssignUserRoleDto,
  10. UnassignUserRoleDto,
  11. } from './dto';
  12. export class UserController {
  13. private router: Router;
  14. private userService: UserService;
  15. constructor() {
  16. this.userService = new UserService(milvusService);
  17. this.router = Router();
  18. }
  19. generateRoutes() {
  20. this.router.get('/', this.getUsers.bind(this));
  21. this.router.post(
  22. '/',
  23. dtoValidationMiddleware(CreateUserDto),
  24. this.createUsers.bind(this)
  25. );
  26. this.router.put(
  27. '/',
  28. dtoValidationMiddleware(UpdateUserDto),
  29. this.updateUsers.bind(this)
  30. );
  31. this.router.delete('/:username', this.deleteUser.bind(this));
  32. this.router.get('/roles', this.getRoles.bind(this));
  33. this.router.post(
  34. '/roles',
  35. dtoValidationMiddleware(CreateRoleDto),
  36. this.createRole.bind(this)
  37. );
  38. this.router.delete('/roles/:roleName', this.deleteRole.bind(this));
  39. this.router.put(
  40. '/:username/role/update',
  41. dtoValidationMiddleware(AssignUserRoleDto),
  42. this.updateUserRole.bind(this)
  43. );
  44. this.router.put(
  45. '/:username/role/unassign',
  46. dtoValidationMiddleware(UnassignUserRoleDto),
  47. this.unassignUserRole.bind(this)
  48. );
  49. return this.router;
  50. }
  51. async getUsers(req: Request, res: Response, next: NextFunction) {
  52. try {
  53. const result = await this.userService.getUsers();
  54. res.send(result);
  55. } catch (error) {
  56. next(error);
  57. }
  58. }
  59. async createUsers(req: Request, res: Response, next: NextFunction) {
  60. const { username, password } = req.body;
  61. try {
  62. const result = await this.userService.createUser({ username, password });
  63. res.send(result);
  64. } catch (error) {
  65. next(error);
  66. }
  67. }
  68. async updateUsers(req: Request, res: Response, next: NextFunction) {
  69. const { username, oldPassword, newPassword } = req.body;
  70. try {
  71. const result = await this.userService.updateUser({
  72. username,
  73. oldPassword,
  74. newPassword,
  75. });
  76. res.send(result);
  77. } catch (error) {
  78. next(error);
  79. }
  80. }
  81. async deleteUser(req: Request, res: Response, next: NextFunction) {
  82. const { username } = req.params;
  83. try {
  84. const result = await this.userService.deleteUser({ username });
  85. res.send(result);
  86. } catch (error) {
  87. next(error);
  88. }
  89. }
  90. async getRoles(req: Request, res: Response, next: NextFunction) {
  91. try {
  92. const result = await this.userService.getRoles();
  93. res.send(result);
  94. } catch (error) {
  95. next(error);
  96. }
  97. }
  98. async createRole(req: Request, res: Response, next: NextFunction) {
  99. const { roleName } = req.body;
  100. try {
  101. const result = await this.userService.createRole({ roleName });
  102. res.send(result);
  103. } catch (error) {
  104. next(error);
  105. }
  106. }
  107. async deleteRole(req: Request, res: Response, next: NextFunction) {
  108. const { roleName } = req.params;
  109. try {
  110. const result = await this.userService.deleteRole({ roleName });
  111. res.send(result);
  112. } catch (error) {
  113. next(error);
  114. }
  115. }
  116. async updateUserRole(req: Request, res: Response, next: NextFunction) {
  117. const { roles } = req.body;
  118. const { username } = req.params;
  119. const results = [];
  120. try {
  121. // get user existing roles
  122. const selectUser = await this.userService.selectUser({
  123. username,
  124. includeRoleInfo: false,
  125. });
  126. const existingRoles = selectUser.results[0].roles;
  127. // remove user existing roles
  128. for (let i = 0; i < existingRoles.length; i++) {
  129. if (existingRoles[i].name.length > 0) {
  130. await this.userService.unassignUserRole({
  131. username,
  132. roleName: existingRoles[i].name,
  133. });
  134. }
  135. }
  136. // assign new user roles
  137. for (let i = 0; i < roles.length; i++) {
  138. const result = await this.userService.assignUserRole({
  139. username,
  140. roleName: roles[i],
  141. });
  142. results.push(result);
  143. }
  144. res.send(results);
  145. } catch (error) {
  146. next(error);
  147. }
  148. }
  149. async unassignUserRole(req: Request, res: Response, next: NextFunction) {
  150. const { roleName } = req.body;
  151. const { username } = req.params;
  152. try {
  153. const result = await this.userService.unassignUserRole({
  154. username,
  155. roleName,
  156. });
  157. res.send(result);
  158. } catch (error) {
  159. next(error);
  160. }
  161. }
  162. }