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

WIP: refactor applyScope method

Yuki Takei 7 лет назад
Родитель
Сommit
2c1af0c006
2 измененных файлов с 48 добавлено и 14 удалено
  1. 44 12
      src/server/models/page.js
  2. 4 2
      src/server/routes/page.js

+ 44 - 12
src/server/models/page.js

@@ -829,20 +829,11 @@ module.exports = function(crowi) {
     return pageData.save();
   }
 
-  async function applyGrant(page, user, grant, grantUserGroupId) {
+  async function validateAppliedScope(user, grant, grantUserGroupId) {
     if (grant == GRANT_USER_GROUP && grantUserGroupId == null) {
       throw new Error('grant userGroupId is not specified');
     }
 
-    page.grant = grant;
-    if (grant == GRANT_PUBLIC || grant == GRANT_USER_GROUP) {
-      page.grantedUsers = [];
-    }
-    else {
-      page.grantedUsers = [];
-      page.grantedUsers.push(user._id);
-    }
-
     if (grant == GRANT_USER_GROUP) {
       const UserGroupRelation = crowi.model('UserGroupRelation');
       const count = await UserGroupRelation.countByGroupIdAndUser(grantUserGroupId, user);
@@ -850,11 +841,47 @@ module.exports = function(crowi) {
       if (count === 0) {
         throw new Error('no relations were exist for group and user.');
       }
+    }
+  }
 
+  function applyScope(page, user, grant, grantUserGroupId) {
+    page.grant = grant;
+
+    // reset
+    page.grantedUsers = [];
+
+    if (grant !== GRANT_PUBLIC && grant !== GRANT_USER_GROUP) {
+      page.grantedUsers.push(user._id);
+    }
+
+    if (grant === GRANT_USER_GROUP) {
       page.grantedGroup = grantUserGroupId;
     }
   }
 
+  async function applyScopesToDescendantsAsyncronously(parentPage, user) {
+    const builder = new PageQueryBuilder(this.find());
+    builder.addConditionToListWithDescendants(parentPage.path);
+
+    builder.addConditionToExcludeRedirect();
+
+    // add grant conditions
+    await addConditionToFilteringByViewerForList(builder, user);
+
+    // get all pages that the specified user can update
+    const pages = builder.query.exec();
+
+    for (const page in pages) {
+      // skip parentPage
+      if (page._id === parentPage._id) {
+        continue;
+      }
+
+      applyScope(page, user, parentPage.grant, parentPage.grantedGroup);
+      page.save();
+    }
+  }
+
   pageSchema.statics.create = function(path, body, user, options = {}) {
     validateCrowi();
 
@@ -891,7 +918,10 @@ module.exports = function(crowi) {
         newPage.updatedAt = Date.now();
         newPage.redirectTo = redirectTo;
         newPage.status = STATUS_PUBLISHED;
-        applyGrant(newPage, user, grant, grantUserGroupId);
+
+        // TODO await
+        validateAppliedScope(user, grant, grantUserGroupId);
+        applyScope(newPage, user, grant, grantUserGroupId);
 
         return newPage.save();
       })
@@ -921,8 +951,10 @@ module.exports = function(crowi) {
       , socketClientId = options.socketClientId || null
       ;
 
+    await validateAppliedScope(user, grant, grantUserGroupId);
+
     // update existing page
-    applyGrant(pageData, user, grant, grantUserGroupId);
+    applyScope(pageData, user, grant, grantUserGroupId);
     let savedPage = await pageData.save();
     const newRevision = await Revision.prepareRevision(pageData, body, previousBody, user);
     const revision = await pushRevision(savedPage, newRevision, user, grant, grantUserGroupId);

+ 4 - 2
src/server/routes/page.js

@@ -516,6 +516,7 @@ module.exports = function(crowi, app) {
     const pagePath = req.body.path || null;
     const grant = req.body.grant || null;
     const grantUserGroupId = req.body.grantUserGroupId || null;
+    const overwriteScopesOfDescendants = req.body.overwriteScopesOfDescendants || null;
     const isSlackEnabled = !!req.body.isSlackEnabled;   // cast to boolean
     const slackChannels = req.body.slackChannels || null;
     const socketClientId = req.body.socketClientId || undefined;
@@ -530,7 +531,7 @@ module.exports = function(crowi, app) {
       return res.json(ApiResponse.error('Page exists', 'already_exists'));
     }
 
-    const options = {grant, grantUserGroupId, socketClientId};
+    const options = {grant, grantUserGroupId, overwriteScopesOfDescendants, socketClientId};
     const createdPage = await Page.create(pagePath, body, req.user, options);
 
     const result = { page: serializeToObj(createdPage) };
@@ -572,6 +573,7 @@ module.exports = function(crowi, app) {
     const revisionId = req.body.revision_id || null;
     const grant = req.body.grant || null;
     const grantUserGroupId = req.body.grantUserGroupId || null;
+    const overwriteScopesOfDescendants = req.body.overwriteScopesOfDescendants || null;
     const isSlackEnabled = !!req.body.isSlackEnabled;                     // cast to boolean
     const slackChannels = req.body.slackChannels || null;
     const isSyncRevisionToHackmd = !!req.body.isSyncRevisionToHackmd;     // cast to boolean
@@ -593,7 +595,7 @@ module.exports = function(crowi, app) {
       return res.json(ApiResponse.error('Posted param "revisionId" is outdated.', 'outdated'));
     }
 
-    const options = {isSyncRevisionToHackmd, socketClientId};
+    const options = {overwriteScopesOfDescendants, isSyncRevisionToHackmd, socketClientId};
     if (grant != null) {
       options.grant = grant;
     }