Taichi Masuyama 4 лет назад
Родитель
Сommit
f6d1c24bdd

+ 7 - 2
packages/app/src/server/models/page.ts

@@ -398,12 +398,18 @@ export default (crowi: Crowi): any => {
       throw new Error('Cannot create new page to existed path');
     }
 
+    // find existing empty page at target path
+    const emptyPage = await Page.findOne({ path, isEmpty: true });
+
     /*
      * UserGroup & Owner validation
      */
     let isGrantNormalized = false;
     try {
-      isGrantNormalized = await crowi.pageGrantService.isGrantNormalized(path, grant, grantedUserIds, grantUserGroupId);
+      // It must check descendants as well if emptyTarget is not null
+      const shouldCheckDescendants = emptyPage != null;
+
+      isGrantNormalized = await crowi.pageGrantService.isGrantNormalized(path, grant, grantedUserIds, grantUserGroupId, shouldCheckDescendants);
     }
     catch (err) {
       logger.error(`Failed to validate grant of page at "${path}" of grant ${grant}:`, err);
@@ -418,7 +424,6 @@ export default (crowi: Crowi): any => {
      * update empty page if exists, if not, create a new page
      */
     let page;
-    const emptyPage = await Page.findOne({ path, isEmpty: true });
     if (emptyPage != null) {
       page = emptyPage;
       page.isEmpty = false;

+ 2 - 7
packages/app/src/server/service/page-grant.ts

@@ -242,19 +242,14 @@ class PageGrantService {
    * About the rule of validation, see: https://dev.growi.org/61b2cdabaa330ce7d8152844
    * @returns Promise<boolean>
    */
-  async isGrantNormalized(targetPath: string, grant, grantedUserIds: ObjectId[], grantedGroupId: ObjectId): Promise<boolean> {
+  async isGrantNormalized(targetPath: string, grant, grantedUserIds: ObjectId[], grantedGroupId: ObjectId, shouldCheckDescendants = false): Promise<boolean> {
     if (isTopPage(targetPath)) {
       return true;
     }
 
-    const Page = mongoose.model('Page') as PageModel;
-
     const comparableAncestor = await this.generateComparableAncestor(targetPath);
 
-    // find existing empty page at target path
-    // it will be unnecessary to check the descendant if emptyTarget is null
-    const emptyTarget = await Page.findOne({ path: targetPath });
-    if (emptyTarget == null) { // checking the parent is enough
+    if (!shouldCheckDescendants) { // checking the parent is enough
       const comparableTarget = await this.generateComparableTarget(grant, grantedUserIds, grantedGroupId, false);
       return this.processValidation(comparableTarget, comparableAncestor);
     }