Explorar o código

WIP: modify page routes

Yuki Takei %!s(int64=7) %!d(string=hai) anos
pai
achega
a2455807c3
Modificáronse 2 ficheiros con 38 adicións e 13 borrados
  1. 17 4
      lib/models/page.js
  2. 21 9
      lib/routes/page.js

+ 17 - 4
lib/models/page.js

@@ -1,3 +1,16 @@
+/**
+ * The Exception class thrown when the user has no grant to see the page
+ *
+ * @class UserHasNoGrantException
+ */
+class UserHasNoGrantException {
+  constructor(message, user) {
+    this.name = this.constructor.name;
+    this.message = message;
+    this.user = user;
+  }
+}
+
 module.exports = function(crowi) {
   var debug = require('debug')('growi:models:page')
     , mongoose = require('mongoose')
@@ -511,13 +524,13 @@ module.exports = function(crowi) {
         if (!pageData.isGrantedFor(userData)) {
           PageGroupRelation.isExistsGrantedGroupForPageAndUser(pageData, userData)
             .then(isExists => {
-              if (!isExists) {
-                return reject(new Error('Page is not granted for the user')); //PAGE_GRANT_ERROR, null);
-              }
-              else {
+              if (isExists) {
                 // return resolve(pageData);
                 self.populatePageData(pageData, revisionId || null).then(resolve).catch(reject);
               }
+              else {
+                return reject(new UserHasNoGrantException('Page is not granted for the user', userData));
+              }
             })
             .catch(function(err) {
               return reject(err);

+ 21 - 9
lib/routes/page.js

@@ -330,18 +330,30 @@ module.exports = function(crowi, app) {
         });
       }
     })
-    // look for templates if page not exists
+    // page is not found or user is forbidden
     .catch(function(err) {
-      pageTeamplate = 'customlayout-selector/not_found';
+      let isForbidden = false;
+      if (err.name === 'UserHasNoGrantException') {
+        isForbidden = true;
+      }
 
-      return Page.findTemplate(path)
-        .then(template => {
-          if (template) {
-            template = replacePlaceholders(template, req);
-          }
+      if (isForbidden) {
+        pageTeamplate = 'customlayout-selector/forbidden';
+        return;
+      }
+      else {
+        pageTeamplate = 'customlayout-selector/not_found';
 
-          renderVars.template = template;
-        });
+        // look for templates
+        return Page.findTemplate(path)
+          .then(template => {
+            if (template) {
+              template = replacePlaceholders(template, req);
+            }
+
+            renderVars.template = template;
+          });
+      }
     })
     // get list pages
     .then(function() {