user-group.ts 6.3 KB

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