2
0

user-group.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import mongoose from 'mongoose';
  2. import { IUser } from '~/interfaces/user';
  3. import { ObjectIdLike } from '~/server/interfaces/mongoose-utils';
  4. import UserGroup from '~/server/models/user-group';
  5. import { excludeTestIdsFromTargetIds, isIncludesObjectId } from '~/server/util/compare-objectId';
  6. import loggerFactory from '~/utils/logger';
  7. const logger = loggerFactory('growi:service:UserGroupService'); // eslint-disable-line no-unused-vars
  8. const UserGroupRelation = mongoose.model('UserGroupRelation') as any; // TODO: Typescriptize model
  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(deleteRootGroupId, action, transferToUserGroupId, user) {
  92. const rootGroup = await UserGroup.findById(deleteRootGroupId);
  93. if (rootGroup == null) {
  94. throw new Error(`UserGroup data does not exist. id: ${deleteRootGroupId}`);
  95. }
  96. const groupsToDelete = await UserGroup.findGroupsWithDescendantsRecursively([rootGroup]);
  97. // 1. update page & remove all groups
  98. await this.crowi.pageService.handlePrivatePagesForGroupsToDelete(groupsToDelete, action, transferToUserGroupId, user);
  99. // 2. remove all groups
  100. const deletedGroups = await UserGroup.deleteMany({ _id: { $in: groupsToDelete.map(g => g._id) } });
  101. // 3. remove all relations
  102. await UserGroupRelation.removeAllByUserGroups(groupsToDelete);
  103. return deletedGroups;
  104. }
  105. async removeUserByUsername(userGroupId: ObjectIdLike, username: string): Promise<{user: IUser, deletedGroupsCount: number}> {
  106. const User = this.crowi.model('User');
  107. const [userGroup, user] = await Promise.all([
  108. UserGroup.findById(userGroupId),
  109. User.findUserByUsername(username),
  110. ]);
  111. const groupsOfRelationsToDelete = await UserGroup.findGroupsWithDescendantsRecursively([userGroup]);
  112. const relatedGroupIdsToDelete = groupsOfRelationsToDelete.map(g => g._id);
  113. const deleteManyRes = await UserGroupRelation.deleteMany({ relatedUser: user._id, relatedGroup: { $in: relatedGroupIdsToDelete } });
  114. return { user, deletedGroupsCount: deleteManyRes.deletedCount };
  115. }
  116. }
  117. module.exports = UserGroupService;