user-group.ts 5.6 KB

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