From b8bce7a063aba6820b7aede66e68ceae23a6f453 Mon Sep 17 00:00:00 2001 From: maeshinshin Date: Tue, 27 Aug 2024 19:25:34 +0900 Subject: [PATCH 1/5] fix type of grant and query --- apps/app/src/server/models/user-group-relation.ts | 2 +- apps/app/src/server/service/page-grant.ts | 2 +- apps/app/src/server/util/compare-objectId.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/app/src/server/models/user-group-relation.ts b/apps/app/src/server/models/user-group-relation.ts index e7f0a894672..040fa710cad 100644 --- a/apps/app/src/server/models/user-group-relation.ts +++ b/apps/app/src/server/models/user-group-relation.ts @@ -128,7 +128,7 @@ schema.statics.findAllRelationForUserGroups = function(userGroups) { * @memberof UserGroupRelation */ schema.statics.findAllGroupsForUser = async function(user): Promise { - const userGroupRelations = await this.find({ relatedUser: user.id }).populate('relatedGroup'); + const userGroupRelations = await this.find({ relatedUser: user._id }).populate('relatedGroup'); const userGroups = userGroupRelations.map((relation) => { return isPopulated(relation.relatedGroup) ? relation.relatedGroup as UserGroupDocument : null; }); diff --git a/apps/app/src/server/service/page-grant.ts b/apps/app/src/server/service/page-grant.ts index 192ac41c1b1..1050d29a150 100644 --- a/apps/app/src/server/service/page-grant.ts +++ b/apps/app/src/server/service/page-grant.ts @@ -276,7 +276,7 @@ class PageGrantService implements IPageGrantService { ).length === 0; if (!userBelongsToAllPreviousGrantedGroups) { - if (grant !== PageGrant.GRANT_USER_GROUP) { + if (Number(grant) !== PageGrant.GRANT_USER_GROUP) { return false; } const pageGrantIncludesUserRelatedGroup = hasIntersection(grantedGroups?.map(g => getIdForRef(g.item)) || [], userRelatedGroupIds); diff --git a/apps/app/src/server/util/compare-objectId.ts b/apps/app/src/server/util/compare-objectId.ts index a7c5231e4b0..d53c313467c 100644 --- a/apps/app/src/server/util/compare-objectId.ts +++ b/apps/app/src/server/util/compare-objectId.ts @@ -1,6 +1,6 @@ import mongoose from 'mongoose'; -import { ObjectIdLike } from '~/server/interfaces/mongoose-utils'; +import type { ObjectIdLike } from '~/server/interfaces/mongoose-utils'; type IObjectId = mongoose.Types.ObjectId; const ObjectId = mongoose.Types.ObjectId; From 106d2cc510766f8d92dad5c85d0a6122683863a8 Mon Sep 17 00:00:00 2001 From: maeshinshin Date: Wed, 28 Aug 2024 21:53:51 +0900 Subject: [PATCH 2/5] fix: Handle grant values as numbers and update grantedGroups in revisions --- apps/app/src/server/models/obsolete-page.js | 4 ++-- apps/app/src/server/models/page.ts | 8 ++++++++ apps/app/src/server/service/page/index.ts | 2 +- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/apps/app/src/server/models/obsolete-page.js b/apps/app/src/server/models/obsolete-page.js index 1b3a30f981c..50f3f9fef85 100644 --- a/apps/app/src/server/models/obsolete-page.js +++ b/apps/app/src/server/models/obsolete-page.js @@ -268,11 +268,11 @@ export const getPageSchema = (crowi) => { this.grant = grant || GRANT_PUBLIC; - if (grant === GRANT_OWNER) { + if (Number(grant) === GRANT_OWNER) { this.grantedUsers.push(user?._id ?? user); } - if (grant === GRANT_USER_GROUP) { + if (Number(grant) === GRANT_USER_GROUP) { this.grantedGroups = grantUserGroupIds; } }; diff --git a/apps/app/src/server/models/page.ts b/apps/app/src/server/models/page.ts index 881029ddca0..ee43a98948d 100644 --- a/apps/app/src/server/models/page.ts +++ b/apps/app/src/server/models/page.ts @@ -787,6 +787,14 @@ export async function pushRevision(pageData, newRevision, user) { pageData.lastUpdateUser = user?._id ?? user; pageData.updatedAt = Date.now(); + // Set the _id of grantedGroups to revision._id + if (pageData.grantedGroups.length > 0) { + pageData.grantedGroups = pageData.grantedGroups.map(group => ({ + ...group, + _id: newRevision._id, + })); + } + return pageData.save(); } diff --git a/apps/app/src/server/service/page/index.ts b/apps/app/src/server/service/page/index.ts index 3849d587cee..e361183a1b7 100644 --- a/apps/app/src/server/service/page/index.ts +++ b/apps/app/src/server/service/page/index.ts @@ -4182,7 +4182,7 @@ class PageService implements IPageService { : clonedPageData.grantedGroups; const grantedUserIds = clonedPageData.grantedUserIds || [user._id]; - const shouldBeOnTree = grant !== PageGrant.GRANT_RESTRICTED; + const shouldBeOnTree = Number(grant) !== PageGrant.GRANT_RESTRICTED; const isChildrenExist = await Page.count({ path: new RegExp(`^${escapeStringRegexp(addTrailingSlash(clonedPageData.path))}`), parent: { $ne: null } }); const isGrantChangeable = await this.pageGrantService.validateGrantChange(user, pageData.grantedGroups, grant, grantUserGroupIds); From 18efb32a39dd297da6f58113cd91a41a74b35d1e Mon Sep 17 00:00:00 2001 From: Futa Arai Date: Sat, 31 Aug 2024 06:22:28 +0900 Subject: [PATCH 3/5] fix v4 page update --- apps/app/src/server/models/user-group-relation.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/app/src/server/models/user-group-relation.ts b/apps/app/src/server/models/user-group-relation.ts index e7f0a894672..9073436c9d8 100644 --- a/apps/app/src/server/models/user-group-relation.ts +++ b/apps/app/src/server/models/user-group-relation.ts @@ -161,7 +161,7 @@ schema.statics.findAllUserGroupIdsRelatedToUser = async function(user): Promise< schema.statics.countByGroupIdsAndUser = async function(userGroupIds: ObjectIdLike[], userData): Promise { const query = { relatedGroup: { $in: userGroupIds }, - relatedUser: userData.id, + relatedUser: userData._id, }; return this.count(query); From 6e9b361108db261f2063125739fa6fcf51e25fba Mon Sep 17 00:00:00 2001 From: Futa Arai Date: Tue, 3 Sep 2024 02:05:28 +0900 Subject: [PATCH 4/5] remove unnecessary Number cast --- apps/app/src/server/models/obsolete-page.js | 4 ++-- apps/app/src/server/service/page-grant.ts | 2 +- apps/app/src/server/service/page/index.ts | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/apps/app/src/server/models/obsolete-page.js b/apps/app/src/server/models/obsolete-page.js index 50f3f9fef85..1b3a30f981c 100644 --- a/apps/app/src/server/models/obsolete-page.js +++ b/apps/app/src/server/models/obsolete-page.js @@ -268,11 +268,11 @@ export const getPageSchema = (crowi) => { this.grant = grant || GRANT_PUBLIC; - if (Number(grant) === GRANT_OWNER) { + if (grant === GRANT_OWNER) { this.grantedUsers.push(user?._id ?? user); } - if (Number(grant) === GRANT_USER_GROUP) { + if (grant === GRANT_USER_GROUP) { this.grantedGroups = grantUserGroupIds; } }; diff --git a/apps/app/src/server/service/page-grant.ts b/apps/app/src/server/service/page-grant.ts index 1050d29a150..192ac41c1b1 100644 --- a/apps/app/src/server/service/page-grant.ts +++ b/apps/app/src/server/service/page-grant.ts @@ -276,7 +276,7 @@ class PageGrantService implements IPageGrantService { ).length === 0; if (!userBelongsToAllPreviousGrantedGroups) { - if (Number(grant) !== PageGrant.GRANT_USER_GROUP) { + if (grant !== PageGrant.GRANT_USER_GROUP) { return false; } const pageGrantIncludesUserRelatedGroup = hasIntersection(grantedGroups?.map(g => getIdForRef(g.item)) || [], userRelatedGroupIds); diff --git a/apps/app/src/server/service/page/index.ts b/apps/app/src/server/service/page/index.ts index e361183a1b7..3849d587cee 100644 --- a/apps/app/src/server/service/page/index.ts +++ b/apps/app/src/server/service/page/index.ts @@ -4182,7 +4182,7 @@ class PageService implements IPageService { : clonedPageData.grantedGroups; const grantedUserIds = clonedPageData.grantedUserIds || [user._id]; - const shouldBeOnTree = Number(grant) !== PageGrant.GRANT_RESTRICTED; + const shouldBeOnTree = grant !== PageGrant.GRANT_RESTRICTED; const isChildrenExist = await Page.count({ path: new RegExp(`^${escapeStringRegexp(addTrailingSlash(clonedPageData.path))}`), parent: { $ne: null } }); const isGrantChangeable = await this.pageGrantService.validateGrantChange(user, pageData.grantedGroups, grant, grantUserGroupIds); From c420236880719f64804fd5e9b3a581c39e2a4e7b Mon Sep 17 00:00:00 2001 From: Futa Arai Date: Tue, 3 Sep 2024 02:09:51 +0900 Subject: [PATCH 5/5] remove unnecessary assignment of grantedGroups --- apps/app/src/server/models/page.ts | 8 -------- 1 file changed, 8 deletions(-) diff --git a/apps/app/src/server/models/page.ts b/apps/app/src/server/models/page.ts index ee43a98948d..881029ddca0 100644 --- a/apps/app/src/server/models/page.ts +++ b/apps/app/src/server/models/page.ts @@ -787,14 +787,6 @@ export async function pushRevision(pageData, newRevision, user) { pageData.lastUpdateUser = user?._id ?? user; pageData.updatedAt = Date.now(); - // Set the _id of grantedGroups to revision._id - if (pageData.grantedGroups.length > 0) { - pageData.grantedGroups = pageData.grantedGroups.map(group => ({ - ...group, - _id: newRevision._id, - })); - } - return pageData.save(); }