Просмотр исходного кода

imprv: User group update parent (#5067)

* CC to FC

* Improved userGroupTable

* SWRized

* Updated table & home page component

* Removed hard code

* Implemented listing api

* Fixed lint error

* Implemented key serializer for swr

* Impl update

* Omit serialize middleware

* Upgraded swr ^1.0.1 => ^1.1.2

* Improved sync

* Fixed lint errors

* Removed unnecessary code

* Improved nukey

* Improved types

* Improved interface

* Improved type validation

* Removed unnecessary code

* Removed unnecessary method

* Added a comment

* Typescriptized

* Omitted crowi

* Fixed

* Fixed ci

* Fixed

* Fixed model import
Haku Mizuki 4 лет назад
Родитель
Сommit
06dae0eafc

+ 4 - 2
packages/app/src/server/crowi/index.js

@@ -23,7 +23,8 @@ import AttachmentService from '../service/attachment';
 import { SlackIntegrationService } from '../service/slack-integration';
 import { UserNotificationService } from '../service/user-notification';
 
-import Actiity from '../models/activity';
+import Activity from '../models/activity';
+import UserGroup from '../models/user-group';
 
 const logger = loggerFactory('growi:crowi');
 const httpErrorHandler = require('../middlewares/http-error-handler');
@@ -314,7 +315,8 @@ Crowi.prototype.setupModels = async function() {
   allModels = models;
 
   // include models that independent from crowi
-  allModels.Activity = Actiity;
+  allModels.Activity = Activity;
+  allModels.UserGroup = UserGroup;
 
   Object.keys(allModels).forEach((key) => {
     return this.model(key, models[key](this));

+ 0 - 1
packages/app/src/server/models/index.js

@@ -7,7 +7,6 @@ module.exports = {
   PageTagRelation: require('./page-tag-relation'),
   User: require('./user'),
   ExternalAccount: require('./external-account'),
-  UserGroup: require('./user-group'),
   UserGroupRelation: require('./user-group-relation'),
   Revision: require('./revision'),
   Tag: require('./tag'),

+ 25 - 0
packages/app/src/server/models/user-group-relation.js

@@ -272,6 +272,31 @@ class UserGroupRelation {
       });
   }
 
+  static async findUserIdsByGroupId(groupId) {
+    const relations = await this.find({ relatedGroup: groupId }, { _id: 0, relatedUser: 1 }).lean().exec(); // .lean() to get not ObjectId but string
+
+    return relations.map(relation => relation.relatedUser);
+  }
+
+  static async createByGroupIdsAndUserIds(groupIds, userIds) {
+    const insertOperations = [];
+
+    groupIds.forEach((groupId) => {
+      userIds.forEach((userId) => {
+        insertOperations.push({
+          insertOne: {
+            document: {
+              relatedGroup: groupId,
+              relatedUser: userId,
+            },
+          },
+        });
+      });
+    });
+
+    await this.bulkWrite(insertOperations);
+  }
+
 }
 
 module.exports = function(crowi) {

+ 0 - 158
packages/app/src/server/models/user-group.js

@@ -1,158 +0,0 @@
-const debug = require('debug')('growi:models:userGroup');
-const mongoose = require('mongoose');
-const mongoosePaginate = require('mongoose-paginate-v2');
-
-
-/*
- * define schema
- */
-const ObjectId = mongoose.Schema.Types.ObjectId;
-
-const schema = new mongoose.Schema({
-  userGroupId: String,
-  name: { type: String, required: true, unique: true },
-  createdAt: { type: Date, default: Date.now },
-  parent: { type: ObjectId, ref: 'UserGroup', index: true },
-  description: { type: String, default: '' },
-});
-schema.plugin(mongoosePaginate);
-
-class UserGroup {
-
-  /**
-   * public fields for UserGroup model
-   *
-   * @readonly
-   * @static
-   * @memberof UserGroup
-   */
-  static get USER_GROUP_PUBLIC_FIELDS() {
-    return '_id name createdAt parent description';
-  }
-
-  /**
-   * limit items num for pagination
-   *
-   * @readonly
-   * @static
-   * @memberof UserGroup
-   */
-  static get PAGE_ITEMS() {
-    return 10;
-  }
-
-  /*
-   * model static methods
-   */
-
-  // Generate image path
-  static createUserGroupPictureFilePath(userGroup, name) {
-    const ext = `.${name.match(/(.*)(?:\.([^.]+$))/)[2]}`;
-
-    return `userGroup/${userGroup._id}${ext}`;
-  }
-
-  /**
-   * find all entities with pagination
-   *
-   * @see https://github.com/edwardhotchkiss/mongoose-paginate
-   *
-   * @static
-   * @param {any} opts mongoose-paginate options object
-   * @returns {Promise<any>} mongoose-paginate result object
-   * @memberof UserGroup
-   */
-  static findUserGroupsWithPagination(opts) {
-    const query = { parent: null };
-    const options = Object.assign({}, opts);
-    if (options.page == null) {
-      options.page = 1;
-    }
-    if (options.limit == null) {
-      options.limit = UserGroup.PAGE_ITEMS;
-    }
-
-    return this.paginate(query, options)
-      .catch((err) => {
-        debug('Error on pagination:', err);
-      });
-  }
-
-  static async findChildUserGroupsByParentIds(parentIds, includeGrandChildren = false) {
-    if (!Array.isArray(parentIds)) {
-      throw Error('parentIds must be an array.');
-    }
-
-    const childUserGroups = await this.find({ parent: { $in: parentIds } });
-
-    let grandChildUserGroups = null;
-    if (includeGrandChildren) {
-      const childUserGroupIds = childUserGroups.map(group => group._id);
-      grandChildUserGroups = await this.find({ parent: { $in: childUserGroupIds } });
-    }
-
-    return {
-      childUserGroups,
-      grandChildUserGroups,
-    };
-  }
-
-  // Check if registerable
-  static isRegisterableName(name) {
-    const query = { name };
-
-    return this.findOne(query)
-      .then((userGroupData) => {
-        return (userGroupData == null);
-      });
-  }
-
-  // Delete completely
-  static async removeCompletelyById(deleteGroupId, action, transferToUserGroupId, user) {
-    const UserGroupRelation = mongoose.model('UserGroupRelation');
-
-    const groupToDelete = await this.findById(deleteGroupId);
-    if (groupToDelete == null) {
-      throw new Error('UserGroup data is not exists. id:', deleteGroupId);
-    }
-    const deletedGroup = await groupToDelete.remove();
-
-    await Promise.all([
-      UserGroupRelation.removeAllByUserGroup(deletedGroup),
-      UserGroup.crowi.pageService.handlePrivatePagesForDeletedGroup(deletedGroup, action, transferToUserGroupId, user),
-    ]);
-
-    return deletedGroup;
-  }
-
-  static countUserGroups() {
-    return this.estimatedDocumentCount();
-  }
-
-  static async createGroup(name, description, parentId) {
-    // create without parent
-    if (parentId == null) {
-      return this.create({ name, description });
-    }
-
-    // create with parent
-    const parent = await this.findOne({ _id: parentId });
-    if (parent == null) {
-      throw Error('Parent does not exist.');
-    }
-    return this.create({ name, description, parent });
-  }
-
-  async updateName(name) {
-    this.name = name;
-    await this.save();
-  }
-
-}
-
-
-module.exports = function(crowi) {
-  UserGroup.crowi = crowi;
-  schema.loadClass(UserGroup);
-  return mongoose.model('UserGroup', schema);
-};

+ 119 - 0
packages/app/src/server/models/user-group.ts

@@ -0,0 +1,119 @@
+import mongoose, {
+  Types, Schema, Model, Document,
+} from 'mongoose';
+import mongoosePaginate from 'mongoose-paginate-v2';
+import { getOrCreateModel } from '@growi/core';
+
+import { IUserGroup } from '~/interfaces/user';
+
+
+export interface UserGroupDocument extends IUserGroup, Document {}
+
+export interface UserGroupModel extends Model<UserGroupDocument> {
+  [x:string]: any, // for old methods
+
+  PAGE_ITEMS: 10,
+}
+
+/*
+ * define schema
+ */
+const ObjectId = mongoose.Schema.Types.ObjectId;
+
+const schema = new Schema<UserGroupDocument, UserGroupModel>({
+  name: { type: String, required: true, unique: true },
+  createdAt: { type: Date, default: new Date() },
+  parent: { type: ObjectId, ref: 'UserGroup', index: true },
+  description: { type: String, default: '' },
+});
+schema.plugin(mongoosePaginate);
+
+const PAGE_ITEMS = 10;
+
+schema.statics.findUserGroupsWithPagination = function(opts) {
+  const query = { parent: null };
+  const options = Object.assign({}, opts);
+  if (options.page == null) {
+    options.page = 1;
+  }
+  if (options.limit == null) {
+    options.limit = PAGE_ITEMS;
+  }
+
+  return this.paginate(query, options)
+    .catch((err) => {
+      // debug('Error on pagination:', err); TODO: add logger
+    });
+};
+
+
+schema.statics.findChildUserGroupsByParentIds = async function(parentIds, includeGrandChildren = false) {
+  if (!Array.isArray(parentIds)) {
+    throw Error('parentIds must be an array.');
+  }
+
+  const childUserGroups = await this.find({ parent: { $in: parentIds } });
+
+  let grandChildUserGroups: UserGroupDocument[] | null = null;
+  if (includeGrandChildren) {
+    const childUserGroupIds = childUserGroups.map(group => group._id);
+    grandChildUserGroups = await this.find({ parent: { $in: childUserGroupIds } });
+  }
+
+  return {
+    childUserGroups,
+    grandChildUserGroups,
+  };
+};
+
+// schema.statics.removeCompletelyById = async function(deleteGroupId, action, transferToUserGroupId, user) { // TODO 85062: move this to the service layer
+//   const UserGroupRelation = mongoose.model('UserGroupRelation') as any; // TODO 85062: Typescriptize model
+
+//   const groupToDelete = await this.findById(deleteGroupId);
+//   if (groupToDelete == null) {
+//     throw Error(`UserGroup data is not exists. id: ${deleteGroupId}`);
+//   }
+//   const deletedGroup = await groupToDelete.remove();
+
+//   await Promise.all([
+//     UserGroupRelation.removeAllByUserGroup(deletedGroup),
+//     crowi.pageService.handlePrivatePagesForDeletedGroup(deletedGroup, action, transferToUserGroupId, user),
+//   ]);
+
+//   return deletedGroup;
+// };
+
+schema.statics.countUserGroups = function() {
+  return this.estimatedDocumentCount();
+};
+
+schema.statics.createGroup = async function(name, description, parentId) {
+  // create without parent
+  if (parentId == null) {
+    return this.create({ name, description });
+  }
+
+  // create with parent
+  const parent = await this.findOne({ _id: parentId });
+  if (parent == null) {
+    throw Error('Parent does not exist.');
+  }
+  return this.create({ name, description, parent });
+};
+
+schema.statics.findAllAncestorGroups = async function(parent, ancestors = [parent]) {
+  if (parent == null) {
+    return ancestors;
+  }
+
+  const nextParent = await this.findOne({ _id: parent.parent });
+  if (nextParent == null) {
+    return ancestors;
+  }
+
+  ancestors.push(nextParent);
+
+  return this.findAllAncestorGroups(nextParent, ancestors);
+};
+
+export default getOrCreateModel<UserGroupDocument, UserGroupModel>('UserGroup', schema);

+ 7 - 13
packages/app/src/server/routes/apiv3/user-group.js

@@ -222,6 +222,9 @@ module.exports = (crowi) => {
 
   validator.update = [
     body('name', 'Group name is required').trim().exists({ checkFalsy: true }),
+    body('description', 'Group description must be a string').optional().isString(),
+    body('parentId', 'parentId must be a string').optional().isString(),
+    body('forceUpdateParents', 'forceUpdateParents must be a boolean').optional().isBoolean(),
   ];
 
   /**
@@ -254,21 +257,12 @@ module.exports = (crowi) => {
    */
   router.put('/:id', loginRequiredStrictly, adminRequired, csrf, validator.update, apiV3FormValidator, async(req, res) => {
     const { id } = req.params;
-    const { name } = req.body;
+    const {
+      name, description, parentId, forceUpdateParents = false,
+    } = req.body;
 
     try {
-      const userGroup = await UserGroup.findById(id);
-      if (userGroup == null) {
-        throw new Error('The group does not exist');
-      }
-
-      // check if the new group name is available
-      const isRegisterableName = await UserGroup.isRegisterableName(name);
-      if (!isRegisterableName) {
-        throw new Error('The group name is already taken');
-      }
-
-      await userGroup.updateName(name);
+      const userGroup = await crowi.userGroupService.updateGroup(id, name, description, parentId, forceUpdateParents);
 
       res.apiv3({ userGroup });
     }

+ 0 - 25
packages/app/src/server/service/user-group.js

@@ -1,25 +0,0 @@
-import loggerFactory from '~/utils/logger';
-
-const logger = loggerFactory('growi:service:UserGroupService'); // eslint-disable-line no-unused-vars
-
-const mongoose = require('mongoose');
-
-const UserGroupRelation = mongoose.model('UserGroupRelation');
-
-/**
- * the service class of UserGroupService
- */
-class UserGroupService {
-
-  constructor(configManager) {
-    this.configManager = configManager;
-  }
-
-  async init() {
-    logger.debug('removing all invalid relations');
-    return UserGroupRelation.removeAllInvalidRelations();
-  }
-
-}
-
-module.exports = UserGroupService;

+ 79 - 0
packages/app/src/server/service/user-group.ts

@@ -0,0 +1,79 @@
+import mongoose from 'mongoose';
+
+import loggerFactory from '~/utils/logger';
+import UserGroup from '~/server/models/user-group';
+
+const logger = loggerFactory('growi:service:UserGroupService'); // eslint-disable-line no-unused-vars
+
+
+const UserGroupRelation = mongoose.model('UserGroupRelation') as any; // TODO: Typescriptize model
+
+/**
+ * the service class of UserGroupService
+ */
+class UserGroupService {
+
+  crowi: any;
+
+  constructor(crowi) {
+    this.crowi = crowi;
+  }
+
+  async init() {
+    logger.debug('removing all invalid relations');
+    return UserGroupRelation.removeAllInvalidRelations();
+  }
+
+  // TODO 85062: write test code
+  // ref: https://dev.growi.org/61b2cdabaa330ce7d8152844
+  async updateGroup(id, name, description, parentId, forceUpdateParents = false) {
+    const userGroup = await UserGroup.findById(id);
+    if (userGroup == null) {
+      throw new Error('The group does not exist');
+    }
+
+    // check if the new group name is available
+    const isExist = (await UserGroup.countDocuments({ name })) > 0;
+    if (userGroup.name !== name && isExist) {
+      throw new Error('The group name is already taken');
+    }
+
+    userGroup.name = name;
+    userGroup.description = description;
+
+    // return when not update parent
+    if (userGroup.parent === parentId) {
+      return userGroup.save();
+    }
+
+    const parent = await UserGroup.findById(parentId);
+
+    // find users for comparison
+    const [targetGroupUsers, parentGroupUsers] = await Promise.all(
+      [UserGroupRelation.findUserIdsByGroupId(userGroup._id), UserGroupRelation.findUserIdsByGroupId(parent?._id)], // TODO 85062: consider when parent is null to update the group as the root
+    );
+
+    const usersBelongsToTargetButNotParent = targetGroupUsers.filter(user => !parentGroupUsers.includes(user));
+    // add the target group's users to all ancestors
+    if (forceUpdateParents) {
+      const ancestorGroups = await UserGroup.findAllAncestorGroups(parent);
+      const ancestorGroupIds = ancestorGroups.map(group => group._id);
+
+      await UserGroupRelation.createByGroupIdsAndUserIds(ancestorGroupIds, usersBelongsToTargetButNotParent);
+
+      userGroup.parent = parent?._id; // TODO 85062: consider when parent is null to update the group as the root
+    }
+    // validate related users
+    else {
+      const isUpdatable = usersBelongsToTargetButNotParent.length === 0;
+      if (!isUpdatable) {
+        throw Error('The parent group does not contain the users in this group.');
+      }
+    }
+
+    return userGroup.save();
+  }
+
+}
+
+module.exports = UserGroupService;