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

execute isNonUserRelatedGroupsGranted after page existence check

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

+ 17 - 18
apps/app/src/client/services/create-page/use-create-page-and-transit.tsx

@@ -60,24 +60,6 @@ export const useCreatePageAndTransit: UseCreatePageAndTransit = () => {
       onCreationStart, onCreated, onAborted, onTerminated,
     } = opts;
 
-    // If parent page is granted to non-user-related groups, let the user select whether or not to inherit them.
-    // Once selected, the request with same params(+ onlyInheritUserRelatedGrantedGroups) and opts will be sent here.
-    if (params.parentPath != null && params?.onlyInheritUserRelatedGrantedGroups == null) {
-      try {
-        const { isNonUserRelatedGroupsGranted } = await getIsNonUserRelatedGroupsGranted(params.parentPath);
-        if (isNonUserRelatedGroupsGranted) {
-          openGrantedGroupsInheritanceSelectModal(params, opts);
-          return;
-        }
-      }
-      catch (err) {
-        throw err;
-      }
-      finally {
-        onTerminated?.();
-      }
-    }
-
     // check the page existence
     if (shouldCheckPageExists && params.path != null) {
       const pagePath = params.path;
@@ -103,6 +85,23 @@ export const useCreatePageAndTransit: UseCreatePageAndTransit = () => {
       }
     }
 
+    // If parent page is granted to non-user-related groups, let the user select whether or not to inherit them.
+    // Once selected, the request with same params(+ onlyInheritUserRelatedGrantedGroups) and opts will be sent here.
+    if (params.parentPath != null && params?.onlyInheritUserRelatedGrantedGroups == null) {
+      try {
+        const { isNonUserRelatedGroupsGranted } = await getIsNonUserRelatedGroupsGranted(params.parentPath);
+        if (isNonUserRelatedGroupsGranted) {
+          openGrantedGroupsInheritanceSelectModal(params, opts);
+          onAborted?.();
+          return;
+        }
+      }
+      catch (err) {
+        // Do not throw error when the parent page is empty, since empty parent page could be created on create request
+        if (err[0]?.code !== 'page_unreachable_or_empty') throw err;
+      }
+    }
+
     // create and transit
     try {
       setCreating(true);

+ 7 - 4
apps/app/src/components/PageCreateModal.tsx

@@ -95,8 +95,10 @@ const PageCreateModal: React.FC = () => {
   const createTodayPage = useCallback(async() => {
     const joinedPath = [todaysParentPath, todayInput].join('/');
     return createAndTransit(
-      { path: joinedPath, wip: true, origin: Origin.View },
-      { shouldCheckPageExists: true, onTerminated: closeCreateModal },
+      {
+        path: joinedPath, parentPath: todaysParentPath, wip: true, origin: Origin.View,
+      },
+      { shouldCheckPageExists: true, onTerminated: closeCreateModal, onAborted: closeCreateModal },
     );
   }, [closeCreateModal, createAndTransit, todayInput, todaysParentPath]);
 
@@ -107,12 +109,13 @@ const PageCreateModal: React.FC = () => {
     return createAndTransit(
       {
         path: pageNameInput,
+        parentPath: pathname,
         wip: true,
         origin: Origin.View,
       },
-      { shouldCheckPageExists: true, onTerminated: closeCreateModal },
+      { shouldCheckPageExists: true, onTerminated: closeCreateModal, onAborted: closeCreateModal },
     );
-  }, [closeCreateModal, createAndTransit, pageNameInput]);
+  }, [closeCreateModal, createAndTransit, pageNameInput, pathname]);
 
   /**
    * access template page

+ 23 - 20
apps/app/src/server/routes/apiv3/page/index.ts

@@ -22,6 +22,8 @@ import type { IPageGrantService } from '~/server/service/page-grant';
 import { preNotifyService } from '~/server/service/pre-notify';
 import loggerFactory from '~/utils/logger';
 
+import type { ApiV3Response } from '../interfaces/apiv3-response';
+
 import { checkPageExistenceHandlersFactory } from './check-page-existence';
 import { createPageHandlersFactory } from './create-page';
 import { publishPageHandlersFactory } from './publish-page';
@@ -638,29 +640,30 @@ module.exports = (crowi) => {
   });
 
   // Check if non user related groups are granted page access
-  router.get('/non-user-related-groups-granted', loginRequiredStrictly, validator.nonUserRelatedGroupsGranted, apiV3FormValidator, async(req, res) => {
-    const { user } = req;
-    const { path } = req.query;
-    const pageGrantService = crowi.pageGrantService as IPageGrantService;
-    try {
-      const page = await Page.findByPathAndViewer(path, user, null, true);
+  router.get('/non-user-related-groups-granted', loginRequiredStrictly, validator.nonUserRelatedGroupsGranted, apiV3FormValidator,
+    async(req, res: ApiV3Response) => {
+      const { user } = req;
+      const { path } = req.query;
+      const pageGrantService = crowi.pageGrantService as IPageGrantService;
+      try {
+        const page = await Page.findByPathAndViewer(path, user, null, true);
 
-      if (page == null) {
-        return res.apiv3Err(new ErrorV3('Page is unreachable or empty.', 'page_unreachable_or_empty'), 400);
-      }
+        if (page == null) {
+          return res.apiv3Err(new ErrorV3('Page is unreachable or empty.', 'page_unreachable_or_empty'), 400);
+        }
 
-      if (page.grant !== PageGrant.GRANT_USER_GROUP) {
-        return res.apiv3({ isNonUserRelatedGroupsGranted: false });
-      }
+        if (page.grant !== PageGrant.GRANT_USER_GROUP) {
+          return res.apiv3({ isNonUserRelatedGroupsGranted: false });
+        }
 
-      const nonUserRelatedGrantedGroups = await pageGrantService.getNonUserRelatedGrantedGroups(page, user);
-      return res.apiv3({ isNonUserRelatedGroupsGranted: nonUserRelatedGrantedGroups.length > 0 });
-    }
-    catch (err) {
-      logger.error('get-page-failed', err);
-      return res.apiv3Err(err, 500);
-    }
-  });
+        const nonUserRelatedGrantedGroups = await pageGrantService.getNonUserRelatedGrantedGroups(page, user);
+        return res.apiv3({ isNonUserRelatedGroupsGranted: nonUserRelatedGrantedGroups.length > 0 });
+      }
+      catch (err) {
+        logger.error('get-page-failed', err);
+        return res.apiv3Err(err, 500);
+      }
+    });
 
   router.get('/applicable-grant', loginRequiredStrictly, validator.applicableGrant, apiV3FormValidator, async(req, res) => {
     const { pageId } = req.query;