Explorar el Código

# Feature/196, 198, 199 Grouping users
* Fix page accessibility for not granted user.

Tatsuya Ise hace 8 años
padre
commit
af1599c9e2
Se han modificado 3 ficheros con 39 adiciones y 7 borrados
  1. 1 0
      lib/models/page-group-relation.js
  2. 15 6
      lib/models/page.js
  3. 23 1
      lib/models/user-group-relation.js

+ 1 - 0
lib/models/page-group-relation.js

@@ -92,6 +92,7 @@ module.exports = function(crowi) {
     return new Promise(function (resolve, reject) {
       PageGroupRelation
         .findOne({ targetPage: page.id })
+        .populate('relatedGroup')
         .exec(function (err, data) {
           if (err) {
             return reject(err);

+ 15 - 6
lib/models/page.js

@@ -507,15 +507,17 @@ module.exports = function(crowi) {
               if (!checkResult) {
                 return reject(new Error('Page is not granted for the user')); //PAGE_GRANT_ERROR, null);
               } else {
-                return resolve(pageData);
+                // return resolve(pageData);
+                self.populatePageData(pageData, revisionId || null).then(resolve).catch(reject);
               }
             })
             .catch(function (err) {
               return reject(err);
             });
         }
-
-        self.populatePageData(pageData, revisionId || null).then(resolve).catch(reject);
+        else {
+          self.populatePageData(pageData, revisionId || null).then(resolve).catch(reject);
+        }
       });
     });
   };
@@ -785,6 +787,7 @@ module.exports = function(crowi) {
     var PageGroupRelation = crowi.model('PageGroupRelation');
     var UserGroupRelation = crowi.model('UserGroupRelation');
     var provGrant = page.grant;
+    var grantUserGroup = null;
 
     if (grant == GRANT_USER_GROUP && grantUserGroupId == null) {
       grant = GRANT_PUBLIC;
@@ -809,15 +812,18 @@ module.exports = function(crowi) {
           debug('grant is usergroup', grantUserGroupId);
           UserGroupRelation.findByGroupIdAndUser(grantUserGroupId, userData)
           .then(function(relation) {
+            debug('userGroupRelation is found : ', relation)
             if (relation != null) {
-              return PageGroupRelation.checkIsExistsRelationForPageAndGroup(page, relation.relatedGroup);
+              grantUserGroup = relation.relatedGroup;
+              return PageGroupRelation.checkIsExistsRelationForPageAndGroup(page, grantUserGroup);
             }
             else { return reject(new Error('No UserGroup is exists. userGroupId : ', grantUserGroupId)); }
           })
           .then(function(isAlreadyExists) {
+            debug('pageGroupRelation is exists ', isAlreadyExists);
             if (!isAlreadyExists) {
-              debug('create new Page and Group relations', relation.relatedGroup);
-              PageGroupRelation.createRelation(relation.relatedGroup, page, function (err, relationData) {
+              debug('create new Page and Group relations', grantUserGroup);
+              PageGroupRelation.createRelation(grantUserGroup, page, function (err, relationData) {
                 if (err) {
                   return reject(err);
                 }
@@ -1235,9 +1241,12 @@ module.exports = function(crowi) {
     var PageGroupRelation = crowi.model('PageGroupRelation');
     var UserGroupRelation = crowi.model('UserGroupRelation');
 
+    debug('isExistsGrantedGroupFor is called.');
+
     return new Promise(function (resolve, reject) {
       PageGroupRelation.findByPage(pageData)
       .then(function (pageRelation) {
+        debug('PageGroupRelation.findByPage result is ', pageRelation);
         if (pageRelation == null) {
           debug('isExistsGrantedGroupFor is return resolve(false);');
           return resolve(false);

+ 23 - 1
lib/models/user-group-relation.js

@@ -107,13 +107,35 @@ module.exports = function(crowi) {
     });
   };
 
+  // グループのIDとユーザのIDから関係性を取得
+  userGroupRelationSchema.statics.findByGroupIdAndUser = function (userGroupId, userData, callback) {
+    var UserGroupRelation = this;
+
+    return new Promise(function (resolve, reject) {
+      UserGroupRelation
+        .findOne({ relatedGroup: userGroupId, relatedUser: userData.id })
+        .populate('relatedUser')
+        .populate('relatedGroup')
+        .exec(function (err, userGroupRelationData) {
+          if (err) {
+            return reject(err);
+          }
+          debug(userGroupRelationData);
+          return resolve(userGroupRelationData);
+        });
+    });
+  }
+
   // グループとユーザを指定し、関係性が存在するかチェック
-  userGroupRelationSchema.statics.checkIsRelatedUserForGroup = function (userData, userGroup, callback) {
+  userGroupRelationSchema.statics.checkIsRelatedUserForGroup = function (userData, userGroup) {
     var UserGroupRelation = this;
 
+    debug('checkIsRelatedUserForGroup is called.', userData, userGroup);
+
     return new Promise(function (resolve, reject) {
       UserGroupRelation.count( {relatedGroup: userGroup.id, relatedUser: userData.id}, function (err, count) {
       if (err) {
+        debug('An Error occured.', err);
         return reject(err);
       }
       debug('checkIsRelatedUserForGroup count : ', count);