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

refactor models/page.js, routes/page.js

Yuki Takei 6 лет назад
Родитель
Сommit
2fb44c787f
2 измененных файлов с 19 добавлено и 18 удалено
  1. 8 12
      src/server/models/page.js
  2. 11 6
      src/server/routes/page.js

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

@@ -468,12 +468,12 @@ module.exports = function(crowi) {
   };
   };
 
 
   pageSchema.methods.applyScope = function(user, grant, grantUserGroupId) {
   pageSchema.methods.applyScope = function(user, grant, grantUserGroupId) {
-    this.grant = grant;
-
     // reset
     // reset
     this.grantedUsers = [];
     this.grantedUsers = [];
     this.grantedGroup = null;
     this.grantedGroup = null;
 
 
+    this.grant = grant || GRANT_PUBLIC;
+
     if (grant !== GRANT_PUBLIC && grant !== GRANT_USER_GROUP) {
     if (grant !== GRANT_PUBLIC && grant !== GRANT_USER_GROUP) {
       this.grantedUsers.push(user._id);
       this.grantedUsers.push(user._id);
     }
     }
@@ -481,10 +481,6 @@ module.exports = function(crowi) {
     if (grant === GRANT_USER_GROUP) {
     if (grant === GRANT_USER_GROUP) {
       this.grantedGroup = grantUserGroupId;
       this.grantedGroup = grantUserGroupId;
     }
     }
-
-    if (grant === GRANT_PUBLIC && grantUserGroupId !== null) {
-      this.grantedGroup = null;
-    }
   };
   };
 
 
 
 
@@ -938,7 +934,7 @@ module.exports = function(crowi) {
       .cursor();
       .cursor();
   };
   };
 
 
-  async function pushRevision(pageData, newRevision, user, grant, grantUserGroupId) {
+  async function pushRevision(pageData, newRevision, user) {
     await newRevision.save();
     await newRevision.save();
     debug('Successfully saved new revision', newRevision);
     debug('Successfully saved new revision', newRevision);
 
 
@@ -977,7 +973,7 @@ module.exports = function(crowi) {
     // sanitize path
     // sanitize path
     path = crowi.xss.process(path); // eslint-disable-line no-param-reassign
     path = crowi.xss.process(path); // eslint-disable-line no-param-reassign
 
 
-    let grant = options.grant || GRANT_PUBLIC;
+    let grant = options.grant;
     // force public
     // force public
     if (isPortalPath(path)) {
     if (isPortalPath(path)) {
       grant = GRANT_PUBLIC;
       grant = GRANT_PUBLIC;
@@ -1001,7 +997,7 @@ module.exports = function(crowi) {
 
 
     let savedPage = await page.save();
     let savedPage = await page.save();
     const newRevision = Revision.prepareRevision(savedPage, body, null, user, { format });
     const newRevision = Revision.prepareRevision(savedPage, body, null, user, { format });
-    const revision = await pushRevision(savedPage, newRevision, user, grant, grantUserGroupId);
+    const revision = await pushRevision(savedPage, newRevision, user);
     savedPage = await this.findByPath(revision.path)
     savedPage = await this.findByPath(revision.path)
       .populate('revision')
       .populate('revision')
       .populate('creator');
       .populate('creator');
@@ -1016,8 +1012,8 @@ module.exports = function(crowi) {
     validateCrowi();
     validateCrowi();
 
 
     const Revision = crowi.model('Revision');
     const Revision = crowi.model('Revision');
-    const grant = options.grant || GRANT_PUBLIC;
-    const grantUserGroupId = options.grantUserGroupId || null;
+    const grant = options.grant || pageData.grant; //                                  use the previous data if absence
+    const grantUserGroupId = options.grantUserGroupId || pageData.grantUserGroupId; // use the previous data if absence
     const isSyncRevisionToHackmd = options.isSyncRevisionToHackmd;
     const isSyncRevisionToHackmd = options.isSyncRevisionToHackmd;
     const socketClientId = options.socketClientId || null;
     const socketClientId = options.socketClientId || null;
 
 
@@ -1027,7 +1023,7 @@ module.exports = function(crowi) {
     // update existing page
     // update existing page
     let savedPage = await pageData.save();
     let savedPage = await pageData.save();
     const newRevision = await Revision.prepareRevision(pageData, body, previousBody, user);
     const newRevision = await Revision.prepareRevision(pageData, body, previousBody, user);
-    const revision = await pushRevision(savedPage, newRevision, user, grant, grantUserGroupId);
+    const revision = await pushRevision(savedPage, newRevision, user);
     savedPage = await this.findByPath(revision.path)
     savedPage = await this.findByPath(revision.path)
       .populate('revision')
       .populate('revision')
       .populate('creator');
       .populate('creator');

+ 11 - 6
src/server/routes/page.js

@@ -567,9 +567,12 @@ module.exports = function(crowi, app) {
       return res.json(ApiResponse.error('Page exists', 'already_exists'));
       return res.json(ApiResponse.error('Page exists', 'already_exists'));
     }
     }
 
 
-    const options = {
-      grant, grantUserGroupId, overwriteScopesOfDescendants, socketClientId, pageTags,
-    };
+    const options = { socketClientId };
+    if (grant != null) {
+      options.grant = grant;
+      options.grantUserGroupId = grantUserGroupId;
+    }
+
     const createdPage = await Page.create(pagePath, body, req.user, options);
     const createdPage = await Page.create(pagePath, body, req.user, options);
 
 
     let savedTags;
     let savedTags;
@@ -645,11 +648,11 @@ module.exports = function(crowi, app) {
       return res.json(ApiResponse.error('Posted param "revisionId" is outdated.', 'outdated'));
       return res.json(ApiResponse.error('Posted param "revisionId" is outdated.', 'outdated'));
     }
     }
 
 
-    const options = { isSyncRevisionToHackmd, socketClientId };
+    const options = {
+      socketClientId, isSyncRevisionToHackmd,
+    };
     if (grant != null) {
     if (grant != null) {
       options.grant = grant;
       options.grant = grant;
-    }
-    if (grantUserGroupId != null) {
       options.grantUserGroupId = grantUserGroupId;
       options.grantUserGroupId = grantUserGroupId;
     }
     }
 
 
@@ -1116,6 +1119,8 @@ module.exports = function(crowi, app) {
     req.body.path = newPagePath;
     req.body.path = newPagePath;
     req.body.body = page.revision.body;
     req.body.body = page.revision.body;
     req.body.grant = page.grant;
     req.body.grant = page.grant;
+    req.body.grantedUsers = page.grantedUsers;
+    req.body.grantedGroup = page.grantedGroup;
     req.body.pageTags = originTags;
     req.body.pageTags = originTags;
 
 
     return api.create(req, res);
     return api.create(req, res);