Sfoglia il codice sorgente

fix validateGrantChange

Futa Arai 2 anni fa
parent
commit
22129498df

+ 4 - 3
apps/app/src/server/crowi/index.js

@@ -713,12 +713,13 @@ Crowi.prototype.setupGrowiPluginService = async function() {
 };
 };
 
 
 Crowi.prototype.setupPageService = async function() {
 Crowi.prototype.setupPageService = async function() {
-  if (this.pageService == null) {
-    this.pageService = new PageService(this);
-  }
   if (this.pageGrantService == null) {
   if (this.pageGrantService == null) {
     this.pageGrantService = new PageGrantService(this);
     this.pageGrantService = new PageGrantService(this);
   }
   }
+  // initialize after pageGrantService since pageService uses pageGrantService in constructor
+  if (this.pageService == null) {
+    this.pageService = new PageService(this);
+  }
   if (this.pageOperationService == null) {
   if (this.pageOperationService == null) {
     this.pageOperationService = new PageOperationService(this);
     this.pageOperationService = new PageOperationService(this);
     await this.pageOperationService.init();
     await this.pageOperationService.init();

+ 14 - 5
apps/app/src/server/service/page-grant.ts

@@ -220,18 +220,24 @@ class PageGrantService {
    * @param grant The grant to be changed to
    * @param grant The grant to be changed to
    * @param grantedGroupIds The groups to be granted
    * @param grantedGroupIds The groups to be granted
    */
    */
-  async validateGrantChange(user, previousGrantedGroupIds: IGrantedGroup[], grant?: PageGrant, grantedGroupIds?: IGrantedGroup[]): Promise<void> {
+  async validateGrantChange(user, previousGrantedGroupIds: IGrantedGroup[], grant?: PageGrant, grantedGroupIds?: IGrantedGroup[]): Promise<boolean> {
     const userRelatedGroupIds = (await this.getUserRelatedGroups(user)).map(g => g.item._id);
     const userRelatedGroupIds = (await this.getUserRelatedGroups(user)).map(g => g.item._id);
-    const userBelongsToAllPreviousGrantedGroups = excludeTestIdsFromTargetIds(userRelatedGroupIds, previousGrantedGroupIds.map(g => getIdForRef(g.item)));
+    const userBelongsToAllPreviousGrantedGroups = excludeTestIdsFromTargetIds(
+      previousGrantedGroupIds.map(g => getIdForRef(g.item)),
+      userRelatedGroupIds,
+    ).length === 0;
+
     if (!userBelongsToAllPreviousGrantedGroups) {
     if (!userBelongsToAllPreviousGrantedGroups) {
       if (grant !== PageGrant.GRANT_USER_GROUP) {
       if (grant !== PageGrant.GRANT_USER_GROUP) {
-        throw Error("cannot change group grant to other grant if the user doesn't belong to all granted groups");
+        return false;
       }
       }
       const pageGrantIncludesUserRelatedGroup = includesObjectIds(grantedGroupIds?.map(g => getIdForRef(g.item)) || [], userRelatedGroupIds);
       const pageGrantIncludesUserRelatedGroup = includesObjectIds(grantedGroupIds?.map(g => getIdForRef(g.item)) || [], userRelatedGroupIds);
       if (!pageGrantIncludesUserRelatedGroup) {
       if (!pageGrantIncludesUserRelatedGroup) {
-        throw Error("page grant doesn't include user related group");
+        return false;
       }
       }
     }
     }
+
+    return true;
   }
   }
 
 
   /**
   /**
@@ -469,7 +475,10 @@ class PageGrantService {
     }
     }
 
 
     if (previousGrantedGroupIds != null) {
     if (previousGrantedGroupIds != null) {
-      this.validateGrantChange(user, previousGrantedGroupIds, grant, grantedGroupIds);
+      const isGrantChangeable = await this.validateGrantChange(user, previousGrantedGroupIds, grant, grantedGroupIds);
+      if (!isGrantChangeable) {
+        return false;
+      }
     }
     }
 
 
     const comparableAncestor = await this.generateComparableAncestor(targetPath, includeNotMigratedPages);
     const comparableAncestor = await this.generateComparableAncestor(targetPath, includeNotMigratedPages);