user-group.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import mongoose from 'mongoose';
  2. import loggerFactory from '~/utils/logger';
  3. import UserGroup, { UserGroupDocument } from '~/server/models/user-group';
  4. import { excludeTestIdsFromTargetIds, isIncludesObjectId } from '~/server/util/compare-objectId';
  5. const logger = loggerFactory('growi:service:UserGroupService'); // eslint-disable-line no-unused-vars
  6. const UserGroupRelation = mongoose.model('UserGroupRelation') as any; // TODO: Typescriptize model
  7. /**
  8. * the service class of UserGroupService
  9. */
  10. class UserGroupService {
  11. crowi: any;
  12. constructor(crowi) {
  13. this.crowi = crowi;
  14. }
  15. async init() {
  16. logger.debug('removing all invalid relations');
  17. return UserGroupRelation.removeAllInvalidRelations();
  18. }
  19. // ref: https://dev.growi.org/61b2cdabaa330ce7d8152844
  20. async updateGroup(id, name?: string, description?: string, parentId?: string | null, forceUpdateParents = false) {
  21. const userGroup = await UserGroup.findById(id);
  22. if (userGroup == null) {
  23. throw new Error('The group does not exist');
  24. }
  25. // check if the new group name is available
  26. const isExist = (await UserGroup.countDocuments({ name })) > 0;
  27. if (userGroup.name !== name && isExist) {
  28. throw new Error('The group name is already taken');
  29. }
  30. if (name != null) {
  31. userGroup.name = name;
  32. }
  33. if (description != null) {
  34. userGroup.description = description;
  35. }
  36. // return when not update parent
  37. if (userGroup.parent === parentId) {
  38. return userGroup.save();
  39. }
  40. /*
  41. * Update parent
  42. */
  43. if (parentId === undefined) { // undefined will be ignored
  44. return userGroup.save();
  45. }
  46. // set parent to null and return when parentId is null
  47. if (parentId == null) {
  48. userGroup.parent = null;
  49. return userGroup.save();
  50. }
  51. const parent = await UserGroup.findById(parentId);
  52. if (parent == null) { // it should not be null
  53. throw Error('Parent group does not exist.');
  54. }
  55. /*
  56. * check if able to update parent or not
  57. */
  58. // throw if parent was in self and its descendants
  59. const descendantsWithTarget = await UserGroup.findGroupsWithDescendantsRecursively([userGroup]);
  60. if (isIncludesObjectId(descendantsWithTarget, parent._id)) {
  61. throw Error('It is not allowed to choose parent from descendant groups.');
  62. }
  63. // find users for comparison
  64. const [targetGroupUsers, parentGroupUsers] = await Promise.all(
  65. [UserGroupRelation.findUserIdsByGroupId(userGroup._id), UserGroupRelation.findUserIdsByGroupId(parent._id)],
  66. );
  67. const usersBelongsToTargetButNotParent = excludeTestIdsFromTargetIds(targetGroupUsers, parentGroupUsers);
  68. // save if no users exist in both target and parent groups
  69. if (targetGroupUsers.length === 0 && parentGroupUsers.length === 0) {
  70. userGroup.parent = parent._id;
  71. return userGroup.save();
  72. }
  73. // add the target group's users to all ancestors
  74. if (forceUpdateParents) {
  75. const ancestorGroups = await UserGroup.findGroupsWithAncestorsRecursively(parent);
  76. const ancestorGroupIds = ancestorGroups.map(group => group._id);
  77. await UserGroupRelation.createByGroupIdsAndUserIds(ancestorGroupIds, usersBelongsToTargetButNotParent);
  78. }
  79. // throw if any of users in the target group is NOT included in the parent group
  80. else {
  81. const isUpdatable = usersBelongsToTargetButNotParent.length === 0;
  82. if (!isUpdatable) {
  83. throw Error('The parent group does not contain the users in this group.');
  84. }
  85. }
  86. userGroup.parent = parent._id;
  87. return userGroup.save();
  88. }
  89. async removeCompletelyByRootGroupId(deleteRootGroupId, action, transferToUserGroupId, user) {
  90. const rootGroup = await UserGroup.findById(deleteRootGroupId);
  91. if (rootGroup == null) {
  92. throw new Error(`UserGroup data does not exist. id: ${deleteRootGroupId}`);
  93. }
  94. const groupsToDelete = await UserGroup.findGroupsWithDescendantsRecursively([rootGroup]);
  95. // 1. update page & remove all groups
  96. await this.crowi.pageService.handlePrivatePagesForGroupsToDelete(groupsToDelete, action, transferToUserGroupId, user);
  97. // 2. remove all groups
  98. const deletedGroups = await UserGroup.deleteMany({ _id: { $in: groupsToDelete.map(g => g._id) } });
  99. // 3. remove all relations
  100. await UserGroupRelation.removeAllByUserGroups(groupsToDelete);
  101. return deletedGroups;
  102. }
  103. }
  104. module.exports = UserGroupService;