user-group.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. const loggerFactory = require('@alias/logger');
  2. const logger = loggerFactory('growi:routes:apiv3:user-group'); // eslint-disable-line no-unused-vars
  3. const express = require('express');
  4. const router = express.Router();
  5. const { body, param, query } = require('express-validator/check');
  6. const {
  7. csrfVerify,
  8. loginRequired,
  9. adminRequired,
  10. } = require('../../util/middlewares');
  11. const validator = {};
  12. module.exports = (crowi) => {
  13. const { ErrorV3, UserGroup, UserGroupRelation } = crowi.models;
  14. const { ApiV3FormValidator } = crowi.middlewares;
  15. router.get('/', loginRequired(crowi), adminRequired(), async(req, res) => {
  16. // TODO: filter with querystring
  17. try {
  18. const userGroups = await UserGroup.find();
  19. return res.apiv3({ userGroups });
  20. }
  21. catch (err) {
  22. const msg = 'Error occurred in fetching user group list';
  23. logger.error('Error', err);
  24. return res.apiv3Err(new ErrorV3(msg, 'user-group-list-fetch-failed'));
  25. }
  26. });
  27. validator.create = [
  28. body('name', 'Group name is required').trim().exists(),
  29. ];
  30. router.post('/', loginRequired(crowi), adminRequired(), csrfVerify(crowi), validator.create, ApiV3FormValidator, async(req, res) => {
  31. const { name } = req.body;
  32. try {
  33. const userGroupName = crowi.xss.process(name);
  34. const userGroup = await UserGroup.createGroupByName(userGroupName);
  35. return res.apiv3({ userGroup });
  36. }
  37. catch (err) {
  38. const msg = 'Error occurred in creating a user group';
  39. logger.error(msg, err);
  40. return res.apiv3Err(new ErrorV3(msg, 'user-group-create-failed'));
  41. }
  42. });
  43. validator.delete = [
  44. param('id').trim().exists(),
  45. query('actionName').trim().exists(),
  46. query('transferToUserGroupId').trim(),
  47. ];
  48. router.delete('/:id', loginRequired(crowi), adminRequired(), csrfVerify(crowi), validator.delete, ApiV3FormValidator, async(req, res) => {
  49. const { id: deleteGroupId } = req.params;
  50. const { actionName, transferToUserGroupId } = req.query;
  51. try {
  52. const userGroup = await UserGroup.removeCompletelyById(deleteGroupId, actionName, transferToUserGroupId);
  53. return res.apiv3({ userGroup });
  54. }
  55. catch (err) {
  56. const msg = 'Error occurred in deleting a user group';
  57. logger.error(msg, err);
  58. return res.apiv3Err(new ErrorV3(msg, 'user-group-delete-failed'));
  59. }
  60. });
  61. // return one group with the id
  62. // router.get('/:id', async(req, res) => {
  63. // });
  64. // update one group with the id
  65. // router.put('/:id/update', async(req, res) => {
  66. // });
  67. router.get('/:id/users', loginRequired(crowi), adminRequired(), async(req, res) => {
  68. const { id } = req.params;
  69. try {
  70. const userGroup = await UserGroup.findById(id);
  71. const userGroupRelations = await UserGroupRelation.findAllRelationForUserGroup(userGroup);
  72. const users = userGroupRelations.map((userGroupRelation) => {
  73. return userGroupRelation.relatedUser;
  74. });
  75. return res.apiv3({ users });
  76. }
  77. catch (err) {
  78. const msg = `Error occurred in fetching users for group: ${id}`;
  79. logger.error(msg, err);
  80. return res.apiv3Err(new ErrorV3(msg, 'user-group-fetch-failed'));
  81. }
  82. });
  83. return router;
  84. };