|
|
@@ -46,6 +46,7 @@ import { V5ConversionError } from '../models/vo/v5-conversion-error';
|
|
|
import { divideByType } from '../util/granted-group';
|
|
|
|
|
|
import { configManager } from './config-manager';
|
|
|
+import { IPageGrantService } from './page-grant';
|
|
|
import { preNotifyService } from './pre-notify';
|
|
|
|
|
|
const debug = require('debug')('growi:services:page');
|
|
|
@@ -152,11 +153,14 @@ class PageService {
|
|
|
|
|
|
activityEvent: any;
|
|
|
|
|
|
+ pageGrantService: IPageGrantService;
|
|
|
+
|
|
|
constructor(crowi) {
|
|
|
this.crowi = crowi;
|
|
|
this.pageEvent = crowi.event('page');
|
|
|
this.tagEvent = crowi.event('tag');
|
|
|
this.activityEvent = crowi.event('activity');
|
|
|
+ this.pageGrantService = crowi.pageGrantService;
|
|
|
|
|
|
// init
|
|
|
this.initPageEvent();
|
|
|
@@ -544,7 +548,7 @@ class PageService {
|
|
|
if (grant !== Page.GRANT_RESTRICTED) {
|
|
|
let isGrantNormalized = false;
|
|
|
try {
|
|
|
- isGrantNormalized = await this.crowi.pageGrantService.isGrantNormalized(user, newPagePath, grant, grantedUserIds, grantedGroupIds, false);
|
|
|
+ isGrantNormalized = await this.pageGrantService.isGrantNormalized(user, newPagePath, grant, grantedUserIds, grantedGroupIds, false);
|
|
|
}
|
|
|
catch (err) {
|
|
|
logger.error(`Failed to validate grant of page at "${newPagePath}" when renaming`, err);
|
|
|
@@ -1054,7 +1058,7 @@ class PageService {
|
|
|
if (grant !== Page.GRANT_RESTRICTED) {
|
|
|
let isGrantNormalized = false;
|
|
|
try {
|
|
|
- isGrantNormalized = await this.crowi.pageGrantService.isGrantNormalized(user, newPagePath, grant, grantedUserIds, grantedGroupIds, false);
|
|
|
+ isGrantNormalized = await this.pageGrantService.isGrantNormalized(user, newPagePath, grant, grantedUserIds, grantedGroupIds, false);
|
|
|
}
|
|
|
catch (err) {
|
|
|
logger.error(`Failed to validate grant of page at "${newPagePath}" when duplicating`, err);
|
|
|
@@ -1188,7 +1192,7 @@ class PageService {
|
|
|
|
|
|
newPagePath = this.crowi.xss.process(newPagePath); // eslint-disable-line no-param-reassign
|
|
|
|
|
|
- const createdPage = await this.crowi.pageService.create(
|
|
|
+ const createdPage = await this.create(
|
|
|
newPagePath, page.revision.body, user, options,
|
|
|
);
|
|
|
this.pageEvent.emit('duplicate', page, user);
|
|
|
@@ -1464,10 +1468,10 @@ class PageService {
|
|
|
}
|
|
|
|
|
|
if (pagePathUtils.isUsersHomepage(page.path)) {
|
|
|
- if (!this.crowi.pageService.canDeleteUserHomepageByConfig()) {
|
|
|
+ if (!this.canDeleteUserHomepageByConfig()) {
|
|
|
throw new Error('User Homepage is not deletable.');
|
|
|
}
|
|
|
- if (!await this.crowi.pageService.isUsersHomepageOwnerAbsent(page.path)) {
|
|
|
+ if (!await this.isUsersHomepageOwnerAbsent(page.path)) {
|
|
|
throw new Error('User Homepage is not deletable.');
|
|
|
}
|
|
|
}
|
|
|
@@ -2680,7 +2684,7 @@ class PageService {
|
|
|
try {
|
|
|
const shouldCheckDescendants = true;
|
|
|
|
|
|
- isGrantNormalized = await this.crowi.pageGrantService.isGrantNormalized(user, path, grant, grantedUserIds, grantedGroupIds, shouldCheckDescendants);
|
|
|
+ isGrantNormalized = await this.pageGrantService.isGrantNormalized(user, path, grant, grantedUserIds, grantedGroupIds, shouldCheckDescendants);
|
|
|
}
|
|
|
catch (err) {
|
|
|
logger.error(`Failed to validate grant of page at "${path}"`, err);
|
|
|
@@ -2797,7 +2801,7 @@ class PageService {
|
|
|
try {
|
|
|
const shouldCheckDescendants = true;
|
|
|
|
|
|
- isGrantNormalized = await this.crowi.pageGrantService.isGrantNormalized(user, path, grant, grantedUserIds, grantedGroupIds, shouldCheckDescendants);
|
|
|
+ isGrantNormalized = await this.pageGrantService.isGrantNormalized(user, path, grant, grantedUserIds, grantedGroupIds, shouldCheckDescendants);
|
|
|
}
|
|
|
catch (err) {
|
|
|
logger.error(`Failed to validate grant of page at "${path}"`, err);
|
|
|
@@ -2843,7 +2847,7 @@ class PageService {
|
|
|
let normalizablePages;
|
|
|
let nonNormalizablePages;
|
|
|
try {
|
|
|
- [normalizablePages, nonNormalizablePages] = await this.crowi.pageGrantService.separateNormalizableAndNotNormalizablePages(user, pagesToNormalize);
|
|
|
+ [normalizablePages, nonNormalizablePages] = await this.pageGrantService.separateNormalizableAndNotNormalizablePages(user, pagesToNormalize);
|
|
|
}
|
|
|
catch (err) {
|
|
|
socket.emit(SocketEventName.PageMigrationError);
|
|
|
@@ -3666,7 +3670,7 @@ class PageService {
|
|
|
private async canProcessCreate(
|
|
|
path: string,
|
|
|
grantData: {
|
|
|
- grant: number,
|
|
|
+ grant?: PageGrant,
|
|
|
grantedUserIds?: ObjectIdLike[],
|
|
|
grantUserGroupIds?: IGrantedGroup[],
|
|
|
},
|
|
|
@@ -3703,7 +3707,7 @@ class PageService {
|
|
|
const isEmptyPageAlreadyExist = await Page.count({ path, isEmpty: true }) > 0;
|
|
|
const shouldCheckDescendants = isEmptyPageAlreadyExist && !options?.overwriteScopesOfDescendants;
|
|
|
|
|
|
- isGrantNormalized = await this.crowi.pageGrantService.isGrantNormalized(user, path, grant, grantedUserIds, grantUserGroupIds, shouldCheckDescendants);
|
|
|
+ isGrantNormalized = await this.pageGrantService.isGrantNormalized(user, path, grant, grantedUserIds, grantUserGroupIds, shouldCheckDescendants);
|
|
|
}
|
|
|
catch (err) {
|
|
|
logger.error(`Failed to validate grant of page at "${path}" of grant ${grant}:`, err);
|
|
|
@@ -3714,8 +3718,8 @@ class PageService {
|
|
|
}
|
|
|
|
|
|
if (options?.overwriteScopesOfDescendants) {
|
|
|
- const updateGrantInfo = await this.crowi.pageGrantService.generateUpdateGrantInfoToOverwriteDescendants(user, grant, options.grantUserGroupIds);
|
|
|
- const canOverwriteDescendants = await this.crowi.pageGrantService.canOverwriteDescendants(path, user, updateGrantInfo);
|
|
|
+ const updateGrantInfo = await this.pageGrantService.generateUpdateGrantInfoToOverwriteDescendants(user, grant, options.grantUserGroupIds);
|
|
|
+ const canOverwriteDescendants = await this.pageGrantService.canOverwriteDescendants(path, user, updateGrantInfo);
|
|
|
|
|
|
if (!canOverwriteDescendants) {
|
|
|
throw Error('Cannot overwrite scopes of descendants.');
|
|
|
@@ -3745,14 +3749,14 @@ class PageService {
|
|
|
const {
|
|
|
format = 'markdown', grantUserGroupIds,
|
|
|
} = options;
|
|
|
- const grant = isTopPage(path) ? Page.GRANT_PUBLIC : options.grant;
|
|
|
+ const grant = isTopPage(path) ? PageGrant.GRANT_PUBLIC : options.grant;
|
|
|
const grantData = {
|
|
|
grant,
|
|
|
- grantedUserIds: grant === Page.GRANT_OWNER ? [user._id] : undefined,
|
|
|
+ grantedUserIds: grant === PageGrant.GRANT_OWNER ? [user._id] : undefined,
|
|
|
grantUserGroupIds,
|
|
|
};
|
|
|
|
|
|
- const isGrantRestricted = grant === Page.GRANT_RESTRICTED;
|
|
|
+ const isGrantRestricted = grant === PageGrant.GRANT_RESTRICTED;
|
|
|
|
|
|
// Validate
|
|
|
const shouldValidateGrant = !isGrantRestricted;
|
|
|
@@ -3901,7 +3905,7 @@ class PageService {
|
|
|
private async canProcessForceCreateBySystem(
|
|
|
path: string,
|
|
|
grantData: {
|
|
|
- grant: number,
|
|
|
+ grant: PageGrant,
|
|
|
grantedUserIds?: ObjectIdLike[],
|
|
|
grantUserGroupId?: ObjectIdLike,
|
|
|
},
|
|
|
@@ -4052,7 +4056,7 @@ class PageService {
|
|
|
}
|
|
|
|
|
|
async updatePage(
|
|
|
- pageData,
|
|
|
+ pageData: PageDocument,
|
|
|
body: string | null,
|
|
|
previousBody: string | null,
|
|
|
user,
|
|
|
@@ -4081,14 +4085,12 @@ class PageService {
|
|
|
const shouldBeOnTree = grant !== PageGrant.GRANT_RESTRICTED;
|
|
|
const isChildrenExist = await Page.count({ path: new RegExp(`^${escapeStringRegexp(addTrailingSlash(clonedPageData.path))}`), parent: { $ne: null } });
|
|
|
|
|
|
- const { pageService, pageGrantService } = this.crowi;
|
|
|
-
|
|
|
if (shouldBeOnTree) {
|
|
|
let isGrantNormalized = false;
|
|
|
try {
|
|
|
const shouldCheckDescendants = !options.overwriteScopesOfDescendants;
|
|
|
// eslint-disable-next-line max-len
|
|
|
- isGrantNormalized = await pageGrantService.isGrantNormalized(user, clonedPageData.path, grant, grantedUserIds, grantUserGroupIds, shouldCheckDescendants);
|
|
|
+ isGrantNormalized = await this.pageGrantService.isGrantNormalized(user, clonedPageData.path, grant, grantedUserIds, grantUserGroupIds, shouldCheckDescendants, false, pageData.grantedGroups);
|
|
|
}
|
|
|
catch (err) {
|
|
|
logger.error(`Failed to validate grant of page at "${clonedPageData.path}" of grant ${grant}:`, err);
|
|
|
@@ -4099,8 +4101,8 @@ class PageService {
|
|
|
}
|
|
|
|
|
|
if (options.overwriteScopesOfDescendants) {
|
|
|
- const updateGrantInfo = await pageGrantService.generateUpdateGrantInfoToOverwriteDescendants(user, grant, options.grantUserGroupIds);
|
|
|
- const canOverwriteDescendants = await pageGrantService.canOverwriteDescendants(clonedPageData.path, user, updateGrantInfo);
|
|
|
+ const updateGrantInfo = await this.pageGrantService.generateUpdateGrantInfoToOverwriteDescendants(user, grant, options.grantUserGroupIds);
|
|
|
+ const canOverwriteDescendants = await this.pageGrantService.canOverwriteDescendants(clonedPageData.path, user, updateGrantInfo);
|
|
|
|
|
|
if (!canOverwriteDescendants) {
|
|
|
throw Error('Cannot overwrite scopes of descendants.');
|
|
|
@@ -4108,7 +4110,7 @@ class PageService {
|
|
|
}
|
|
|
|
|
|
if (!wasOnTree) {
|
|
|
- const newParent = await pageService.getParentAndFillAncestorsByUser(user, newPageData.path);
|
|
|
+ const newParent = await this.getParentAndFillAncestorsByUser(user, newPageData.path);
|
|
|
newPageData.parent = newParent._id;
|
|
|
}
|
|
|
}
|
|
|
@@ -4184,7 +4186,7 @@ class PageService {
|
|
|
}
|
|
|
|
|
|
|
|
|
- async updatePageV4(pageData, body, previousBody, user, options: IOptionsForUpdate = {}): Promise<PageDocument> {
|
|
|
+ async updatePageV4(pageData: PageDocument, body, previousBody, user, options: IOptionsForUpdate = {}): Promise<PageDocument> {
|
|
|
const Page = mongoose.model('Page') as unknown as PageModel;
|
|
|
const Revision = mongoose.model('Revision') as any; // TODO: TypeScriptize model
|
|
|
|
|
|
@@ -4192,6 +4194,9 @@ class PageService {
|
|
|
const grantUserGroupIds = options.grantUserGroupIds || pageData.grantUserGroupIds; // use the previous data if absence
|
|
|
const isSyncRevisionToHackmd = options.isSyncRevisionToHackmd;
|
|
|
|
|
|
+ // TODO 136137: validate multiple group grant before save using pageData and options
|
|
|
+ await this.pageGrantService.validateGrantChange(user, pageData.grantedGroups, grant, grantUserGroupIds);
|
|
|
+
|
|
|
await this.validateAppliedScope(user, grant, grantUserGroupIds);
|
|
|
pageData.applyScope(user, grant, grantUserGroupIds);
|
|
|
|