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

refs 126902: convert grantedGroup to array

Futa Arai 2 лет назад
Родитель
Сommit
8e21753266

+ 0 - 50
apps/app/src/migrations/20230717141157-set-granted-group-model-value.js

@@ -1,50 +0,0 @@
-import mongoose from 'mongoose';
-
-import { getMongoUri, mongoOptions } from '~/server/util/mongoose-utils';
-import loggerFactory from '~/utils/logger';
-
-const logger = loggerFactory('growi:remove-basic-auth-related-config');
-
-module.exports = {
-  async up(db, client) {
-    logger.info('Apply migration');
-
-    mongoose.connect(getMongoUri(), mongoOptions);
-    const pageCollection = await db.collection('pages');
-    const pageOperationCollection = await db.collection('pageoperations');
-
-    // Set the model type of grantedGroup to UserGroup
-    // for Pages that were created before ExternalUserGroup was introduced
-    pageCollection.updateMany(
-      { grantedGroupModel: null },
-      { $set: { grantedGroupModel: 'UserGroup' } },
-    );
-    // Set the model type of grantUserGroupIdModel to UserGroup
-    // for PageOperations that were created before ExternalUserGroup was introduced
-    pageOperationCollection.updateMany(
-      { 'options.grantUserGroupIdModel': null },
-      { $set: { 'options.grantUserGroupIdModel': 'UserGroup' } },
-    );
-
-    logger.info('Migration has successfully applied');
-  },
-
-  async down(db, client) {
-    logger.info('Rollback migration');
-
-    mongoose.connect(getMongoUri(), mongoOptions);
-    const pageCollection = await db.collection('pages');
-    const pageOperationCollection = await db.collection('pageoperations');
-
-    pageCollection.updateMany(
-      { grantedGroupModel: 'UserGroup' },
-      { $set: { grantedGroupModel: null } },
-    );
-    pageOperationCollection.updateMany(
-      { 'options.grantUserGroupIdModel': 'UserGroup' },
-      { $set: { 'options.grantUserGroupIdModel': null } },
-    );
-
-    logger.info('Migration has been successfully rollbacked');
-  },
-};

+ 160 - 0
apps/app/src/migrations/20230723061824-granted-group-to-array-of-objects.js

@@ -0,0 +1,160 @@
+import loggerFactory from '~/utils/logger';
+
+const logger = loggerFactory('growi:remove-basic-auth-related-config');
+
+module.exports = {
+  async up(db, client) {
+    logger.info('Apply migration');
+
+    const pageCollection = await db.collection('pages');
+    const pageOperationCollection = await db.collection('pageoperations');
+    // Convert grantedGroup to array
+    // Set the model type of grantedGroup to UserGroup for Pages that were created before ExternalUserGroup was introduced
+    await pageCollection.updateMany(
+      { grantedGroup: { $ne: null } },
+      [
+        {
+          $set: {
+            grantedGroup: [
+              {
+                type: 'UserGroup',
+                item: '$grantedGroup',
+              },
+            ],
+          },
+        },
+      ],
+    );
+
+    await pageOperationCollection.updateMany(
+      { 'options.grantUserGroupId': { $ne: null } },
+      [
+        {
+          $set: {
+            'options.grantUserGroupId': [
+              {
+                type: 'UserGroup',
+                item: '$options.grantUserGroupId',
+              },
+            ],
+          },
+        },
+      ],
+    );
+
+    await pageOperationCollection.updateMany(
+      { 'page.grantedGroup': { $ne: null } },
+      [
+        {
+          $set: {
+            'page.grantedGroup': [
+              {
+                type: 'UserGroup',
+                item: '$page.grantedGroup',
+              },
+            ],
+          },
+        },
+      ],
+    );
+
+    await pageOperationCollection.updateMany(
+      { 'exPage.grantedGroup': { $ne: null } },
+      [
+        {
+          $set: {
+            'exPage.grantedGroup': [
+              {
+                type: 'UserGroup',
+                item: '$exPage.grantedGroup',
+              },
+            ],
+          },
+        },
+      ],
+    );
+
+    // rename fields
+    await pageCollection.updateMany({}, {
+      $rename: {
+        grantedGroup: 'grantedGroups',
+      },
+    });
+    await pageOperationCollection.updateMany({}, {
+      $rename: {
+        'options.grantUserGroupId': 'options.grantUserGroupIds',
+        'page.grantedGroup': 'page.grantedGroups',
+        'exPage.grantedGroup': 'exPage.grantedGroups',
+      },
+    });
+
+    logger.info('Migration has successfully applied');
+  },
+
+  async down(db, client) {
+    logger.info('Rollback migration');
+
+    const pageCollection = await db.collection('pages');
+    const pageOperationCollection = await db.collection('pageoperations');
+
+    await pageCollection.updateMany(
+      { grantedGroups: { $exists: true } },
+      [
+        {
+          $set: {
+            grantedGroups: { $arrayElemAt: ['$grantedGroups.item', 0] },
+          },
+        },
+      ],
+    );
+    await pageOperationCollection.updateMany(
+      { 'options.grantUserGroupIds': { $exists: true } },
+      [
+        {
+          $set: {
+            'options.grantUserGroupIds': { $arrayElemAt: ['options.grantUserGroupIds.item', 0] },
+          },
+        },
+      ],
+    );
+    await pageOperationCollection.updateMany(
+      { 'page.grantedGroups': { $exists: true } },
+      [
+        {
+          $set: {
+            'page.grantedGroups': { $arrayElemAt: ['page.grantedGroups.item', 0] },
+          },
+        },
+      ],
+    );
+    await pageOperationCollection.updateMany(
+      { 'exPage.grantedGroups': { $exists: true } },
+      [
+        {
+          $set: {
+            'exPage.grantedGroups': { $arrayElemAt: ['exPage.grantedGroups.item', 0] },
+          },
+        },
+      ],
+    );
+
+    // rename fields
+    await pageCollection.updateMany(
+      { grantedGroups: { $exists: true } },
+      {
+        $rename: {
+          grantedGroups: 'grantedGroup',
+        },
+      },
+    );
+    await pageOperationCollection.updateMany({}, {
+      $rename: {
+        'options.grantUserGroupIds': 'options.grantUserGroupId',
+        'page.grantedGroups': 'page.grantedGroup',
+        'exPage.grantedGroups': 'exPage.grantedGroup',
+      },
+    });
+
+    logger.info('Migration has been successfully rollbacked');
+  },
+};

+ 14 - 6
apps/app/src/server/models/interfaces/page-operation.ts

@@ -1,3 +1,5 @@
+import { GroupType } from '@growi/core';
+
 import { PageGrant } from '~/interfaces/page';
 
 import { ObjectIdLike } from '../../interfaces/mongoose-utils';
@@ -9,8 +11,10 @@ export type IPageForResuming = {
   parent?: ObjectIdLike,
   grant?: number,
   grantedUsers?: ObjectIdLike[],
-  grantedGroup?: ObjectIdLike,
-  grantedGroupModel?: string,
+  grantedGroups: {
+    type: GroupType,
+    item: ObjectIdLike,
+  }[],
   descendantCount: number,
   status?: number,
   revision?: ObjectIdLike,
@@ -24,16 +28,20 @@ export type IUserForResuming = {
 
 export type IOptionsForUpdate = {
   grant?: PageGrant,
-  grantUserGroupId?: ObjectIdLike,
-  grantUserGroupIdModel?: string,
+  grantUserGroupIds?: {
+    type: GroupType,
+    item: ObjectIdLike,
+  }[],
   isSyncRevisionToHackmd?: boolean,
   overwriteScopesOfDescendants?: boolean,
 };
 
 export type IOptionsForCreate = {
   format?: string,
-  grantUserGroupId?: ObjectIdLike,
-  grantUserGroupIdModel?: string,
+  grantUserGroupIds?: {
+    type: GroupType,
+    item: ObjectIdLike,
+  }[],
   grant?: PageGrant,
   overwriteScopesOfDescendants?: boolean,
   isSynchronously?: boolean,

+ 1 - 1
apps/app/src/server/models/obsolete-page.js

@@ -72,7 +72,7 @@ export const populateDataToShowRevision = (page, userPublicFields, shouldExclude
       { path: 'lastUpdateUser', select: userPublicFields },
       { path: 'creator', select: userPublicFields },
       { path: 'deleteUser', select: userPublicFields },
-      { path: 'grantedGroup' },
+      { path: 'grantedGroups.item' },
       { path: 'revision', select: shouldExcludeBody ? '-body' : undefined, populate: {
         path: 'author', select: userPublicFields,
       } },

+ 20 - 14
apps/app/src/server/models/page-operation.ts

@@ -59,13 +59,16 @@ const pageSchemaForResuming = new Schema<IPageForResuming>({
   status: { type: String },
   grant: { type: Number },
   grantedUsers: [{ type: ObjectId, ref: 'User' }],
-  grantedGroup: { type: ObjectId, refPath: 'grantedGroupModel', index: true },
-  grantedGroupModel: {
-    type: String,
-    enum: ['UserGroup', 'ExternalUserGroup'],
-    required: true,
-    default: 'UserGroup',
-  },
+  grantedGroups: [{
+    type: {
+      type: String,
+      enum: ['UserGroup', 'ExternalUserGroup'],
+      required: true,
+    },
+    item: {
+      type: ObjectId, refPath: 'grantedGroups.type', required: true,
+    },
+  }],
   creator: { type: ObjectId, ref: 'User' },
   lastUpdateUser: { type: ObjectId, ref: 'User' },
 });
@@ -79,13 +82,16 @@ const optionsSchemaForResuming = new Schema<IOptionsForResuming>({
   updateMetadata: { type: Boolean },
   prevDescendantCount: { type: Number },
   grant: { type: Number },
-  grantUserGroupId: { type: ObjectId, refPath: 'grantedGroupModel' },
-  grantUserGroupIdModel: {
-    type: String,
-    enum: ['UserGroup', 'ExternalUserGroup'],
-    required: true,
-    default: 'UserGroup',
-  },
+  grantUserGroupIds: [{
+    type: {
+      type: String,
+      enum: ['UserGroup', 'ExternalUserGroup'],
+      required: true,
+    },
+    item: {
+      type: ObjectId, refPath: 'grantedGroups.type', required: true,
+    },
+  }],
   format: { type: String },
   isSyncRevisionToHackmd: { type: Boolean },
   overwriteScopesOfDescendants: { type: Boolean },

+ 12 - 9
apps/app/src/server/models/page.ts

@@ -97,13 +97,16 @@ const schema = new Schema<PageDocument, PageModel>({
   status: { type: String, default: STATUS_PUBLISHED, index: true },
   grant: { type: Number, default: GRANT_PUBLIC, index: true },
   grantedUsers: [{ type: ObjectId, ref: 'User' }],
-  grantedGroup: { type: ObjectId, refPath: 'grantedGroupModel', index: true },
-  grantedGroupModel: {
-    type: String,
-    enum: ['UserGroup', 'ExternalUserGroup'],
-    required: true,
-    default: 'UserGroup',
-  },
+  grantedGroups: [{
+    type: {
+      type: String,
+      enum: ['UserGroup', 'ExternalUserGroup'],
+      required: true,
+    },
+    item: {
+      type: ObjectId, refPath: 'grantedGroups.type', required: true, index: true,
+    },
+  }],
   creator: { type: ObjectId, ref: 'User', index: true },
   lastUpdateUser: { type: ObjectId, ref: 'User' },
   liker: [{ type: ObjectId, ref: 'User' }],
@@ -331,7 +334,7 @@ export class PageQueryBuilder {
 
     if (userGroups != null && userGroups.length > 0) {
       grantConditions.push(
-        { grant: GRANT_USER_GROUP, grantedGroup: { $in: userGroups } },
+        { grant: GRANT_USER_GROUP, 'grantedGroups.item': { $in: userGroups } },
       );
     }
 
@@ -937,7 +940,7 @@ export function generateGrantCondition(
   }
   else if (userGroups != null && userGroups.length > 0) {
     grantConditions.push(
-      { grant: GRANT_USER_GROUP, grantedGroup: { $in: userGroups } },
+      { grant: GRANT_USER_GROUP, 'grantedGroups.item': { $in: userGroups } },
     );
   }
 

+ 5 - 2
packages/core/src/interfaces/page.ts

@@ -5,6 +5,7 @@ import type { SubscriptionStatusType } from './subscription';
 import type { ITag } from './tag';
 import type { IUser, IUserGroupHasId, IUserHasId } from './user';
 
+export type GroupType = 'UserGroup' | 'ExternalUserGroup'
 
 export type IPage = {
   path: string,
@@ -20,8 +21,10 @@ export type IPage = {
   isEmpty: boolean,
   grant: PageGrant,
   grantedUsers: Ref<IUser>[],
-  grantedGroup: Ref<any>,
-  grantedGroupModel?: string,
+  grantedGroups: {
+    type: GroupType,
+    item: Ref<any>,
+  }[],
   lastUpdateUser: Ref<IUser>,
   liker: Ref<IUser>[],
   commentCount: number