users.controller.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. // user
  21. this.router.get('/', this.getUsers.bind(this));
  22. this.router.post(
  23. '/',
  24. dtoValidationMiddleware(CreateUserDto),
  25. this.createUsers.bind(this)
  26. );
  27. this.router.put(
  28. '/',
  29. dtoValidationMiddleware(UpdateUserDto),
  30. this.updateUsers.bind(this)
  31. );
  32. this.router.delete('/:username', this.deleteUser.bind(this));
  33. this.router.put(
  34. '/:username/role/update',
  35. dtoValidationMiddleware(AssignUserRoleDto),
  36. this.updateUserRole.bind(this)
  37. );
  38. this.router.put(
  39. '/:username/role/unassign',
  40. dtoValidationMiddleware(UnassignUserRoleDto),
  41. this.unassignUserRole.bind(this)
  42. );
  43. // role
  44. this.router.get('/rbac', this.rbac.bind(this));
  45. this.router.get('/roles', this.getRoles.bind(this));
  46. this.router.post(
  47. '/roles',
  48. dtoValidationMiddleware(CreateRoleDto),
  49. this.createRole.bind(this)
  50. );
  51. this.router.get('/roles/:roleName', this.listGrant.bind(this));
  52. this.router.delete('/roles/:roleName', this.deleteRole.bind(this));
  53. this.router.put(
  54. '/roles/:roleName/updatePrivileges',
  55. this.updateRolePrivileges.bind(this)
  56. );
  57. return this.router;
  58. }
  59. async getUsers(req: Request, res: Response, next: NextFunction) {
  60. try {
  61. const result = await this.userService.getUsers();
  62. res.send(result);
  63. } catch (error) {
  64. next(error);
  65. }
  66. }
  67. async createUsers(req: Request, res: Response, next: NextFunction) {
  68. const { username, password } = req.body;
  69. try {
  70. const result = await this.userService.createUser({ username, password });
  71. res.send(result);
  72. } catch (error) {
  73. next(error);
  74. }
  75. }
  76. async updateUsers(req: Request, res: Response, next: NextFunction) {
  77. const { username, oldPassword, newPassword } = req.body;
  78. try {
  79. const result = await this.userService.updateUser({
  80. username,
  81. oldPassword,
  82. newPassword,
  83. });
  84. res.send(result);
  85. } catch (error) {
  86. next(error);
  87. }
  88. }
  89. async deleteUser(req: Request, res: Response, next: NextFunction) {
  90. const { username } = req.params;
  91. try {
  92. const result = await this.userService.deleteUser({ username });
  93. res.send(result);
  94. } catch (error) {
  95. next(error);
  96. }
  97. }
  98. async getRoles(req: Request, res: Response, next: NextFunction) {
  99. try {
  100. const result = (await this.userService.getRoles()) as any;
  101. for (let i = 0; i < result.results.length; i++) {
  102. const { entities } = await this.userService.listGrants({
  103. roleName: result.results[i].role.name,
  104. });
  105. result.results[i].entities = entities;
  106. }
  107. res.send(result);
  108. } catch (error) {
  109. next(error);
  110. }
  111. }
  112. async createRole(req: Request, res: Response, next: NextFunction) {
  113. const { roleName } = req.body;
  114. try {
  115. const result = await this.userService.createRole({ roleName });
  116. res.send(result);
  117. } catch (error) {
  118. next(error);
  119. }
  120. }
  121. async deleteRole(req: Request, res: Response, next: NextFunction) {
  122. const { roleName } = req.params;
  123. const { force } = req.body;
  124. try {
  125. if (force) {
  126. await this.userService.revokeAllRolePrivileges({ roleName });
  127. }
  128. const result = await this.userService.deleteRole({ roleName });
  129. res.send(result);
  130. } catch (error) {
  131. next(error);
  132. }
  133. }
  134. async updateUserRole(req: Request, res: Response, next: NextFunction) {
  135. const { roles } = req.body;
  136. const { username } = req.params;
  137. const results = [];
  138. try {
  139. // get user existing roles
  140. const selectUser = await this.userService.selectUser({
  141. username,
  142. includeRoleInfo: false,
  143. });
  144. const existingRoles = selectUser.results[0].roles;
  145. // remove user existing roles
  146. for (let i = 0; i < existingRoles.length; i++) {
  147. if (existingRoles[i].name.length > 0) {
  148. await this.userService.unassignUserRole({
  149. username,
  150. roleName: existingRoles[i].name,
  151. });
  152. }
  153. }
  154. // assign new user roles
  155. for (let i = 0; i < roles.length; i++) {
  156. const result = await this.userService.assignUserRole({
  157. username,
  158. roleName: roles[i],
  159. });
  160. results.push(result);
  161. }
  162. res.send(results);
  163. } catch (error) {
  164. next(error);
  165. }
  166. }
  167. async unassignUserRole(req: Request, res: Response, next: NextFunction) {
  168. const { roleName } = req.body;
  169. const { username } = req.params;
  170. try {
  171. const result = await this.userService.unassignUserRole({
  172. username,
  173. roleName,
  174. });
  175. res.send(result);
  176. } catch (error) {
  177. next(error);
  178. }
  179. }
  180. async rbac(req: Request, res: Response, next: NextFunction) {
  181. try {
  182. const result = await this.userService.getRBAC();
  183. res.send(result);
  184. } catch (error) {
  185. next(error);
  186. }
  187. }
  188. async listGrant(req: Request, res: Response, next: NextFunction) {
  189. const { roleName } = req.params;
  190. try {
  191. const result = await this.userService.listGrants({
  192. roleName,
  193. });
  194. res.send(result);
  195. } catch (error) {
  196. next(error);
  197. }
  198. }
  199. async updateRolePrivileges(req: Request, res: Response, next: NextFunction) {
  200. const { privileges } = req.body;
  201. const { roleName } = req.params;
  202. const results = [];
  203. try {
  204. // revoke all
  205. this.userService.revokeAllRolePrivileges({ roleName });
  206. // assign new user roles
  207. for (let i = 0; i < privileges.length; i++) {
  208. const result = await this.userService.grantRolePrivilege(privileges[i]);
  209. results.push(result);
  210. }
  211. res.send(results);
  212. } catch (error) {
  213. next(error);
  214. }
  215. }
  216. }