Browse Source

Moved isGrantNormalized calculation to apiv3

Taichi Masuyama 3 years ago
parent
commit
53d8322d96
2 changed files with 56 additions and 15 deletions
  1. 56 0
      packages/app/src/server/routes/apiv3/page.js
  2. 0 15
      packages/app/src/server/routes/page.js

+ 56 - 0
packages/app/src/server/routes/apiv3/page.js

@@ -179,6 +179,9 @@ module.exports = (crowi) => {
     info: [
       query('pageId').isMongoId().withMessage('pageId is required'),
     ],
+    isGrantNormalized: [
+      query('pageId').isMongoId().withMessage('pageId is required'),
+    ],
     export: [
       query('format').isString().isIn(['md', 'pdf']),
       query('revisionId').isString(),
@@ -379,6 +382,59 @@ module.exports = (crowi) => {
 
   });
 
+  /**
+   * @swagger
+   *
+   *    /page/is-grant-normalized:
+   *      get:
+   *        tags: [Page]
+   *        summary: /page/info
+   *        description: Retrieve current page's isGrantNormalized value
+   *        operationId: getIsGrantNormalized
+   *        parameters:
+   *          - name: pageId
+   *            in: query
+   *            description: page id
+   *            schema:
+   *              $ref: '#/components/schemas/Page/properties/_id'
+   *        responses:
+   *          200:
+   *            description: Successfully retrieved current isGrantNormalized.
+   *            content:
+   *              application/json:
+   *                schema:
+   *                  $ref: '#/components/schemas/isGrantNormalized'
+   *          400:
+   *            description: Bad request. Page is unreachable or empty.
+   *          500:
+   *            description: Internal server error.
+   */
+  router.get('/is-grant-normalized', loginRequiredStrictly, validator.isGrantNormalized, apiV3FormValidator, async(req, res) => {
+    const { pageId } = req.query;
+
+    const Page = crowi.model('Page');
+    const page = await Page.findByIdAndViewer(pageId, req.user, null, false);
+
+    if (page == null) {
+      return res.apiv3Err(new ErrorV3('Page is unreachable or empty.', 'page_unreachable_or_empty'), 400);
+    }
+
+    const {
+      path, grant, grantedUsers, grantedGroup,
+    } = page;
+
+    let isGrantNormalized;
+    try {
+      isGrantNormalized = await crowi.pageGrantService.isGrantNormalized(req.user, path, grant, grantedUsers, grantedGroup, false, false);
+    }
+    catch (err) {
+      logger.error('Error occurred while processing isGrantNormalized.', err);
+      return res.apiv3Err(err, 500);
+    }
+
+    return res.apiv3({ isGrantNormalized });
+  });
+
   /**
   * @swagger
   *

+ 0 - 15
packages/app/src/server/routes/page.js

@@ -269,20 +269,6 @@ module.exports = function(crowi, app) {
     renderVars.targetAndAncestors = { targetAndAncestors, rootPage };
   }
 
-  async function addRenderVarsForGrantValidation(renderVars, user, page) {
-    const {
-      path, grant, grantedUsers, grantedGroup, isEmpty,
-    } = page;
-
-    if (isEmpty) {
-      return;
-    }
-
-    const isGrantNormalized = await crowi.pageGrantService.isGrantNormalized(user, path, grant, grantedUsers, grantedGroup, false, false);
-
-    renderVars.isGrantNormalized = isGrantNormalized;
-  }
-
   async function addRenderVarsWhenNotFound(renderVars, pathOrId) {
     if (pathOrId == null) {
       return;
@@ -479,7 +465,6 @@ module.exports = function(crowi, app) {
     }
 
     await addRenderVarsForPageTree(renderVars, path, req.user);
-    await addRenderVarsForGrantValidation(renderVars, req.user, page);
 
 
     await interceptorManager.process('beforeRenderPage', req, res, renderVars);