|
@@ -2,14 +2,13 @@ import pathlib from 'path';
|
|
|
import { Readable, Writable } from 'stream';
|
|
import { Readable, Writable } from 'stream';
|
|
|
|
|
|
|
|
import type {
|
|
import type {
|
|
|
- Ref, HasObjectId, IUserHasId,
|
|
|
|
|
|
|
+ Ref, HasObjectId, IUserHasId, IUser,
|
|
|
IPage, IPageInfo, IPageInfoAll, IPageInfoForEntity, IPageWithMeta,
|
|
IPage, IPageInfo, IPageInfoAll, IPageInfoForEntity, IPageWithMeta,
|
|
|
} from '@growi/core';
|
|
} from '@growi/core';
|
|
|
import { PageGrant, PageStatus, getIdForRef } from '@growi/core';
|
|
import { PageGrant, PageStatus, getIdForRef } from '@growi/core';
|
|
|
import {
|
|
import {
|
|
|
pagePathUtils, pathUtils,
|
|
pagePathUtils, pathUtils,
|
|
|
} from '@growi/core/dist/utils';
|
|
} from '@growi/core/dist/utils';
|
|
|
-import { collectAncestorPaths, isUsersHomepage } from '@growi/core/dist/utils/page-path-utils';
|
|
|
|
|
import escapeStringRegexp from 'escape-string-regexp';
|
|
import escapeStringRegexp from 'escape-string-regexp';
|
|
|
import mongoose, { ObjectId, Cursor } from 'mongoose';
|
|
import mongoose, { ObjectId, Cursor } from 'mongoose';
|
|
|
import streamToPromise from 'stream-to-promise';
|
|
import streamToPromise from 'stream-to-promise';
|
|
@@ -40,12 +39,14 @@ import ShareLink from '../models/share-link';
|
|
|
import Subscription from '../models/subscription';
|
|
import Subscription from '../models/subscription';
|
|
|
import { V5ConversionError } from '../models/vo/v5-conversion-error';
|
|
import { V5ConversionError } from '../models/vo/v5-conversion-error';
|
|
|
|
|
|
|
|
|
|
+import { configManager } from './config-manager';
|
|
|
|
|
+
|
|
|
const debug = require('debug')('growi:services:page');
|
|
const debug = require('debug')('growi:services:page');
|
|
|
|
|
|
|
|
const logger = loggerFactory('growi:services:page');
|
|
const logger = loggerFactory('growi:services:page');
|
|
|
const {
|
|
const {
|
|
|
- isTrashPage, isTopPage, omitDuplicateAreaPageFromPages,
|
|
|
|
|
- isMovablePage, canMoveByPath, isUsersProtectedPages, hasSlash, generateChildrenRegExp,
|
|
|
|
|
|
|
+ isTrashPage, isTopPage, omitDuplicateAreaPageFromPages, getUsernameByPath, collectAncestorPaths,
|
|
|
|
|
+ canMoveByPath, isUsersTopPage, isMovablePage, isUsersHomepage, hasSlash, generateChildrenRegExp,
|
|
|
} = pagePathUtils;
|
|
} = pagePathUtils;
|
|
|
|
|
|
|
|
const { addTrailingSlash } = pathUtils;
|
|
const { addTrailingSlash } = pathUtils;
|
|
@@ -164,7 +165,7 @@ class PageService {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
canDeleteCompletely(path: string, creatorId: ObjectIdLike, operator: any | null, isRecursively: boolean): boolean {
|
|
canDeleteCompletely(path: string, creatorId: ObjectIdLike, operator: any | null, isRecursively: boolean): boolean {
|
|
|
- if (operator == null || isTopPage(path) || isUsersProtectedPages(path)) return false;
|
|
|
|
|
|
|
+ if (operator == null || isTopPage(path) || isUsersTopPage(path)) return false;
|
|
|
|
|
|
|
|
const pageCompleteDeletionAuthority = this.crowi.configManager.getConfig('crowi', 'security:pageCompleteDeletionAuthority');
|
|
const pageCompleteDeletionAuthority = this.crowi.configManager.getConfig('crowi', 'security:pageCompleteDeletionAuthority');
|
|
|
const pageRecursiveCompleteDeletionAuthority = this.crowi.configManager.getConfig('crowi', 'security:pageRecursiveCompleteDeletionAuthority');
|
|
const pageRecursiveCompleteDeletionAuthority = this.crowi.configManager.getConfig('crowi', 'security:pageRecursiveCompleteDeletionAuthority');
|
|
@@ -175,7 +176,7 @@ class PageService {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
canDelete(path: string, creatorId: ObjectIdLike, operator: any | null, isRecursively: boolean): boolean {
|
|
canDelete(path: string, creatorId: ObjectIdLike, operator: any | null, isRecursively: boolean): boolean {
|
|
|
- if (operator == null || isUsersProtectedPages(path) || isTopPage(path)) return false;
|
|
|
|
|
|
|
+ if (operator == null || isTopPage(path) || isUsersTopPage(path)) return false;
|
|
|
|
|
|
|
|
const pageDeletionAuthority = this.crowi.configManager.getConfig('crowi', 'security:pageDeletionAuthority');
|
|
const pageDeletionAuthority = this.crowi.configManager.getConfig('crowi', 'security:pageDeletionAuthority');
|
|
|
const pageRecursiveDeletionAuthority = this.crowi.configManager.getConfig('crowi', 'security:pageRecursiveDeletionAuthority');
|
|
const pageRecursiveDeletionAuthority = this.crowi.configManager.getConfig('crowi', 'security:pageRecursiveDeletionAuthority');
|
|
@@ -185,6 +186,20 @@ class PageService {
|
|
|
return this.canDeleteLogic(creatorId, operator, isRecursively, singleAuthority, recursiveAuthority);
|
|
return this.canDeleteLogic(creatorId, operator, isRecursively, singleAuthority, recursiveAuthority);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ canDeleteUserHomepageByConfig(): boolean {
|
|
|
|
|
+ return configManager.getConfig('crowi', 'security:user-homepage-deletion:isEnabled') ?? false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ async isUsersHomepageOwnerAbsent(path: string): Promise<boolean> {
|
|
|
|
|
+ const User = mongoose.model('User');
|
|
|
|
|
+ const username = getUsernameByPath(path);
|
|
|
|
|
+ if (username == null) {
|
|
|
|
|
+ throw new Error('Cannot found username by path');
|
|
|
|
|
+ }
|
|
|
|
|
+ const ownerExists = await User.exists({ username });
|
|
|
|
|
+ return ownerExists === null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
private canDeleteLogic(
|
|
private canDeleteLogic(
|
|
|
creatorId: ObjectIdLike,
|
|
creatorId: ObjectIdLike,
|
|
|
operator,
|
|
operator,
|
|
@@ -217,12 +232,58 @@ class PageService {
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- filterPagesByCanDeleteCompletely(pages, user, isRecursively: boolean) {
|
|
|
|
|
- return pages.filter(p => p.isEmpty || this.canDeleteCompletely(p.path, p.creator, user, isRecursively));
|
|
|
|
|
|
|
+ private async getAbsenseUserHomeList(pages: PageDocument[]): Promise<string[]> {
|
|
|
|
|
+ const userHomepages = pages.filter(p => isUsersHomepage(p.path));
|
|
|
|
|
+
|
|
|
|
|
+ const User = mongoose.model<IUser>('User');
|
|
|
|
|
+ const usernames = userHomepages
|
|
|
|
|
+ .map(page => getUsernameByPath(page.path))
|
|
|
|
|
+ // see: https://zenn.dev/kimuson/articles/filter_safety_type_guard
|
|
|
|
|
+ .filter((username): username is Exclude<typeof username, null> => username !== null);
|
|
|
|
|
+ const existingUsernames = await User.distinct<string>('username', { username: { $in: usernames } });
|
|
|
|
|
+
|
|
|
|
|
+ return userHomepages.filter((page) => {
|
|
|
|
|
+ const username = getUsernameByPath(page.path);
|
|
|
|
|
+ if (username == null) {
|
|
|
|
|
+ throw new Error('Cannot found username by path');
|
|
|
|
|
+ }
|
|
|
|
|
+ return !existingUsernames.includes(username);
|
|
|
|
|
+ }).map(p => p.path);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private async filterPages(
|
|
|
|
|
+ pages: PageDocument[],
|
|
|
|
|
+ user: IUserHasId,
|
|
|
|
|
+ isRecursively: boolean,
|
|
|
|
|
+ canDeleteFunction: (path: string, creatorId: ObjectIdLike, operator: any, isRecursively: boolean) => boolean,
|
|
|
|
|
+ ): Promise<PageDocument[]> {
|
|
|
|
|
+ const filteredPages = pages.filter(p => p.isEmpty || canDeleteFunction(p.path, p.creator, user, isRecursively));
|
|
|
|
|
+
|
|
|
|
|
+ if (!this.canDeleteUserHomepageByConfig()) {
|
|
|
|
|
+ return filteredPages;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // Confirmation of deletion of user homepages is an asynchronous process,
|
|
|
|
|
+ // so it is processed separately for performance optimization.
|
|
|
|
|
+ const absenseUserHomeList = await this.getAbsenseUserHomeList(filteredPages);
|
|
|
|
|
+
|
|
|
|
|
+ const excludeActiveUserHomepage = (path: string) => {
|
|
|
|
|
+ if (!isUsersHomepage(path)) {
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+ return absenseUserHomeList.includes(path);
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ return filteredPages
|
|
|
|
|
+ .filter(p => excludeActiveUserHomepage(p.path));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ async filterPagesByCanDeleteCompletely(pages: PageDocument[], user: IUserHasId, isRecursively: boolean): Promise<PageDocument[]> {
|
|
|
|
|
+ return this.filterPages(pages, user, isRecursively, this.canDeleteCompletely);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- filterPagesByCanDelete(pages, user, isRecursively: boolean) {
|
|
|
|
|
- return pages.filter(p => p.isEmpty || this.canDelete(p.path, p.creator, user, isRecursively));
|
|
|
|
|
|
|
+ async filterPagesByCanDelete(pages: PageDocument[], user: IUserHasId, isRecursively: boolean): Promise<PageDocument[]> {
|
|
|
|
|
+ return this.filterPages(pages, user, isRecursively, this.canDelete);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
@@ -250,7 +311,6 @@ class PageService {
|
|
|
meta: {
|
|
meta: {
|
|
|
isV5Compatible: isTopPage(page.path) || page.parent != null,
|
|
isV5Compatible: isTopPage(page.path) || page.parent != null,
|
|
|
isEmpty: page.isEmpty,
|
|
isEmpty: page.isEmpty,
|
|
|
- isMovable: false,
|
|
|
|
|
isDeletable: false,
|
|
isDeletable: false,
|
|
|
isAbleToDeleteCompletely: false,
|
|
isAbleToDeleteCompletely: false,
|
|
|
isRevertible: false,
|
|
isRevertible: false,
|
|
@@ -1385,10 +1445,19 @@ class PageService {
|
|
|
throw new Error('This method does NOT support deleting trashed pages.');
|
|
throw new Error('This method does NOT support deleting trashed pages.');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if (!isMovablePage(page.path)) {
|
|
|
|
|
|
|
+ if (isTopPage(page.path) || isUsersTopPage(page.path)) {
|
|
|
throw new Error('Page is not deletable.');
|
|
throw new Error('Page is not deletable.');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ if (pagePathUtils.isUsersHomepage(page.path)) {
|
|
|
|
|
+ if (!this.crowi.pageService.canDeleteUserHomepageByConfig()) {
|
|
|
|
|
+ throw new Error('User Homepage is not deletable.');
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!await this.crowi.pageService.isUsersHomepageOwnerAbsent(page.path)) {
|
|
|
|
|
+ throw new Error('User Homepage is not deletable.');
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
const newPath = Page.getDeletedPageName(page.path);
|
|
const newPath = Page.getDeletedPageName(page.path);
|
|
|
|
|
|
|
|
const canOperate = await this.crowi.pageOperationService.canOperate(isRecursively, page.path, newPath);
|
|
const canOperate = await this.crowi.pageOperationService.canOperate(isRecursively, page.path, newPath);
|
|
@@ -2398,13 +2467,12 @@ class PageService {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
constructBasicPageInfo(page: PageDocument, isGuestUser?: boolean): IPageInfo | IPageInfoForEntity {
|
|
constructBasicPageInfo(page: PageDocument, isGuestUser?: boolean): IPageInfo | IPageInfoForEntity {
|
|
|
- const isMovable = isGuestUser ? false : isMovablePage(page.path);
|
|
|
|
|
|
|
+ const isDeletable = !(isGuestUser || isTopPage(page.path) || isUsersTopPage(page.path));
|
|
|
|
|
|
|
|
if (page.isEmpty) {
|
|
if (page.isEmpty) {
|
|
|
return {
|
|
return {
|
|
|
isV5Compatible: true,
|
|
isV5Compatible: true,
|
|
|
isEmpty: true,
|
|
isEmpty: true,
|
|
|
- isMovable,
|
|
|
|
|
isDeletable: false,
|
|
isDeletable: false,
|
|
|
isAbleToDeleteCompletely: false,
|
|
isAbleToDeleteCompletely: false,
|
|
|
isRevertible: false,
|
|
isRevertible: false,
|
|
@@ -2421,8 +2489,7 @@ class PageService {
|
|
|
likerIds: this.extractStringIds(likers),
|
|
likerIds: this.extractStringIds(likers),
|
|
|
seenUserIds: this.extractStringIds(seenUsers),
|
|
seenUserIds: this.extractStringIds(seenUsers),
|
|
|
sumOfSeenUsers: page.seenUsers.length,
|
|
sumOfSeenUsers: page.seenUsers.length,
|
|
|
- isMovable,
|
|
|
|
|
- isDeletable: isMovable,
|
|
|
|
|
|
|
+ isDeletable,
|
|
|
isAbleToDeleteCompletely: false,
|
|
isAbleToDeleteCompletely: false,
|
|
|
isRevertible: isTrashPage(page.path),
|
|
isRevertible: isTrashPage(page.path),
|
|
|
contentAge: page.getContentAge(),
|
|
contentAge: page.getContentAge(),
|