|
|
@@ -454,6 +454,7 @@ module.exports = function(crowi) {
|
|
|
|
|
|
pageSchema.statics.findPageByIdAndGrantedUser = function(id, userData) {
|
|
|
var Page = this;
|
|
|
+ var PageGroupRelation = crowi.model('PageGroupRelation');
|
|
|
var pageData = null;
|
|
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
@@ -461,7 +462,7 @@ module.exports = function(crowi) {
|
|
|
.then(function(result) {
|
|
|
pageData = result;
|
|
|
if (userData && !pageData.isGrantedFor(userData)) {
|
|
|
- return Page.isExistsGrantedGroupFor(pageData, userData);
|
|
|
+ return PageGroupRelation.isExistsGrantedGroupForPageAndUser(pageData, userData);
|
|
|
}
|
|
|
else {
|
|
|
return resolve(true);
|
|
|
@@ -482,6 +483,7 @@ module.exports = function(crowi) {
|
|
|
// find page and check if granted user
|
|
|
pageSchema.statics.findPage = function(path, userData, revisionId, ignoreNotFound) {
|
|
|
var self = this;
|
|
|
+ var PageGroupRelation = crowi.model('PageGroupRelation');
|
|
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
self.findOne({path: path}, function(err, pageData) {
|
|
|
@@ -500,9 +502,8 @@ module.exports = function(crowi) {
|
|
|
}
|
|
|
|
|
|
if (!pageData.isGrantedFor(userData)) {
|
|
|
- self.isExistsGrantedGroupFor(pageData, userData)
|
|
|
+ PageGroupRelation.isExistsGrantedGroupForPageAndUser(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 {
|
|
|
@@ -786,10 +787,9 @@ 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) {
|
|
|
- throw new Error();
|
|
|
+ throw new Error('grant userGroupId is not specified');
|
|
|
}
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
page.grant = grant;
|
|
|
@@ -806,39 +806,37 @@ module.exports = function(crowi) {
|
|
|
return reject(err);
|
|
|
}
|
|
|
|
|
|
- // グループの場合
|
|
|
- if (grant == GRANT_USER_GROUP) {
|
|
|
- debug('grant is usergroup', grantUserGroupId);
|
|
|
- UserGroupRelation.findByGroupIdAndUser(grantUserGroupId, userData)
|
|
|
- .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));
|
|
|
- }
|
|
|
- })
|
|
|
- .then((isAlreadyExists) => {
|
|
|
- debug('pageGroupRelation is exists ', isAlreadyExists);
|
|
|
- if (!isAlreadyExists) {
|
|
|
- debug('create new Page and Group relations', grantUserGroup);
|
|
|
- PageGroupRelation.createRelation(grantUserGroup, page)
|
|
|
- .then((relationData) => {
|
|
|
- return resolve();
|
|
|
- });
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
- else {
|
|
|
- PageGroupRelation.removeAllByPage(page);
|
|
|
- }
|
|
|
- return resolve(data);
|
|
|
+ Page.updateGrantUserGroup(page, grant, grantUserGroupId, userData)
|
|
|
+ .then(() => {
|
|
|
+ return resolve(data);
|
|
|
+ });
|
|
|
});
|
|
|
});
|
|
|
};
|
|
|
|
|
|
+ pageSchema.statics.updateGrantUserGroup = function (page, grant, grantUserGroupId, userData) {
|
|
|
+
|
|
|
+ // グループの場合
|
|
|
+ if (grant == GRANT_USER_GROUP) {
|
|
|
+ debug('grant is usergroup', grantUserGroupId);
|
|
|
+ UserGroupRelation.findByGroupIdAndUser(grantUserGroupId, userData)
|
|
|
+ .then((relation) => {
|
|
|
+ if (relation == null) {
|
|
|
+ reject(new Error('no relations were exist for group and user.'));
|
|
|
+ }
|
|
|
+ return PageGroupRelation.findOrCreateRelationForPageAndGroup(page, relation.relatedGroup);
|
|
|
+
|
|
|
+ })
|
|
|
+ .catch((err) => {
|
|
|
+ return reject(new Error('No UserGroup is exists. userGroupId : ', grantUserGroupId));
|
|
|
+ });
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ PageGroupRelation.removeAllByPage(page);
|
|
|
+ }
|
|
|
+
|
|
|
+ };
|
|
|
+
|
|
|
// Instance method でいいのでは
|
|
|
pageSchema.statics.pushToGrantedUsers = function(page, userData) {
|
|
|
|
|
|
@@ -927,10 +925,9 @@ module.exports = function(crowi) {
|
|
|
}
|
|
|
|
|
|
if (newPage.grant == Page.GRANT_USER_GROUP && grantUserGroupId != null) {
|
|
|
- PageGroupRelation.createRelation(grantUserGroupId, newPage, function (err, relationData) {
|
|
|
- if (err) {
|
|
|
- return reject(err);
|
|
|
- }
|
|
|
+ Page.updateGrantUserGroup(newPage, grant, grantUserGroupId, user)
|
|
|
+ .catch((err) => {
|
|
|
+ return reject(err);
|
|
|
});
|
|
|
}
|
|
|
var newRevision = Revision.prepareRevision(newPage, body, user, {format: format});
|
|
|
@@ -1231,35 +1228,6 @@ module.exports = function(crowi) {
|
|
|
return;
|
|
|
};
|
|
|
|
|
|
- // 指定ページに紐づくユーザグループに、対象のユーザが含まれるかを確認
|
|
|
- pageSchema.statics.isExistsGrantedGroupFor = function(pageData, userData) {
|
|
|
- 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 (pageRelations) {
|
|
|
- debug('PageGroupRelation.findByPage result is ', pageRelations);
|
|
|
- if (pageRelations == null) {
|
|
|
- debug('isExistsGrantedGroupFor is return resolve(false);');
|
|
|
- return resolve(false);
|
|
|
- }
|
|
|
- return pageRelations.map(pageRelation => UserGroupRelation.isRelatedUserForGroup(userData, pageRelation.relatedGroup)
|
|
|
- .then(function (checkResult) {
|
|
|
- return resolve(checkResult);
|
|
|
- })
|
|
|
- .catch((err) => {
|
|
|
- return reject(err);
|
|
|
- }));
|
|
|
- })
|
|
|
- .catch((err) => {
|
|
|
- return reject(err);
|
|
|
- });
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* return path that added slash to the end for specified path
|
|
|
*/
|