Jelajahi Sumber

# Feature/196, 198, 199 Grouping users
* fix nested Promise to go outside.
* fix "function(foo) {" code style to "(foo) => {" style.

Tatsuya Ise 8 tahun lalu
induk
melakukan
86c442c017
1 mengubah file dengan 22 tambahan dan 21 penghapusan
  1. 22 21
      lib/models/page.js

+ 22 - 21
lib/models/page.js

@@ -454,26 +454,25 @@ module.exports = function(crowi) {
 
   pageSchema.statics.findPageByIdAndGrantedUser = function(id, userData) {
     var Page = this;
+    var pageData = null;
 
     return new Promise(function(resolve, reject) {
       Page.findPageById(id)
-      .then(function(pageData) {
+      .then(function(result) {
+        pageData = result;
         if (userData && !pageData.isGrantedFor(userData)) {
-          Page.isExistsGrantedGroupFor(pageData, userData)
-          .then(function (checkResult) {
-            debug('isExistsGrantedGroupFor checkResult is ', checkResult);
-            if (!checkResult) {
-              return reject(new Error('Page is not granted for the user')); //PAGE_GRANT_ERROR, null);
-            } else {
-              return resolve(pageData);
-            }
-          })
-          .catch(function(err) {
-            return reject(err);
-          });
+          return Page.isExistsGrantedGroupFor(pageData, userData);
+        }
+        else {
+          return resolve(true);
+        }
+      }).then((checkResult) => {
+        if (checkResult) {
+          return resolve(pageData);
+        }
+        else  {
+          return reject(new Error('Page is not granted for the user')); //PAGE_GRANT_ERROR, null);
         }
-
-        return resolve(pageData);
       }).catch(function(err) {
         return reject(err);
       });
@@ -790,7 +789,7 @@ module.exports = function(crowi) {
     var grantUserGroup = null;
 
     if (grant == GRANT_USER_GROUP && grantUserGroupId == null) {
-      grant = GRANT_PUBLIC;
+      throw new Error();
     }
     return new Promise(function(resolve, reject) {
       page.grant = grant;
@@ -811,21 +810,23 @@ module.exports = function(crowi) {
         if (grant == GRANT_USER_GROUP) {
           debug('grant is usergroup', grantUserGroupId);
           UserGroupRelation.findByGroupIdAndUser(grantUserGroupId, userData)
-          .then(function(relation) {
+          .then((relation) => {
             debug('userGroupRelation is found : ', relation)
             if (relation != null) {
               grantUserGroup = relation.relatedGroup;
               return PageGroupRelation.isExistsRelationForPageAndGroup(page, grantUserGroup);
             }
-            else { return reject(new Error('No UserGroup is exists. userGroupId : ', grantUserGroupId)); }
+            else {
+              return reject(new Error('No UserGroup is exists. userGroupId : ', grantUserGroupId));
+            }
           })
-          .then(function(isAlreadyExists) {
+          .then((isAlreadyExists) => {
             debug('pageGroupRelation is exists ', isAlreadyExists);
             if (!isAlreadyExists) {
               debug('create new Page and Group relations', grantUserGroup);
               PageGroupRelation.createRelation(grantUserGroup, page)
-                .then(function(relationData) {
-                  return Promise.resolve();
+                .then((relationData) => {
+                  return resolve();
                 });
             }
           });