|
|
@@ -354,11 +354,113 @@ schema.statics.findAncestors = async function(targetPath: string, includeEmpty =
|
|
|
return ancestors;
|
|
|
};
|
|
|
|
|
|
+/*
|
|
|
+ * Utils from obsolete-page.js
|
|
|
+ */
|
|
|
+async function pushRevision(pageData, newRevision, user) {
|
|
|
+ await newRevision.save();
|
|
|
+
|
|
|
+ pageData.revision = newRevision;
|
|
|
+ pageData.lastUpdateUser = user;
|
|
|
+ pageData.updatedAt = Date.now();
|
|
|
+
|
|
|
+ return pageData.save();
|
|
|
+}
|
|
|
+
|
|
|
|
|
|
/*
|
|
|
* Merge obsolete page model methods and define new methods which depend on crowi instance
|
|
|
*/
|
|
|
export default (crowi: Crowi): any => {
|
|
|
+ const pageEvent = crowi.event('page');
|
|
|
+
|
|
|
+ schema.statics.create = async function(path, body, user, options = {}) {
|
|
|
+ if (crowi.pageGrantService == null || crowi.configManager == null) {
|
|
|
+ throw Error('Crowi is not setup');
|
|
|
+ }
|
|
|
+
|
|
|
+ const isV5Compatible = crowi.configManager.getConfig('crowi', 'app:isV5Compatible');
|
|
|
+ if (!isV5Compatible) {
|
|
|
+ // v4 compatible process
|
|
|
+ return this.createV4(path, body, user, options);
|
|
|
+ }
|
|
|
+
|
|
|
+ const Page = this;
|
|
|
+ const Revision = crowi.model('Revision');
|
|
|
+ const {
|
|
|
+ format = 'markdown', redirectTo, grantedUserIds, grantUserGroupId,
|
|
|
+ } = options;
|
|
|
+
|
|
|
+ // sanitize path
|
|
|
+ path = crowi.xss.process(path); // eslint-disable-line no-param-reassign
|
|
|
+
|
|
|
+ let grant = options.grant;
|
|
|
+ // force public
|
|
|
+ if (isTopPage(path)) {
|
|
|
+ grant = GRANT_PUBLIC;
|
|
|
+ }
|
|
|
+
|
|
|
+ const isExist = (await this.count({ path, isEmpty: false })) > 0; // not validate empty page
|
|
|
+ if (isExist) {
|
|
|
+ throw new Error('Cannot create new page to existed path');
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ * UserGroup & Owner validation
|
|
|
+ */
|
|
|
+ let isGrantNormalized = false;
|
|
|
+ try {
|
|
|
+ isGrantNormalized = await crowi.pageGrantService.isGrantNormalized(path, grant, grantedUserIds, grantUserGroupId);
|
|
|
+ }
|
|
|
+ catch (err) {
|
|
|
+ logger.error(`Failed to validate grant of page at "${path}" of grant ${grant}:`, err);
|
|
|
+ throw err;
|
|
|
+ }
|
|
|
+ if (!isGrantNormalized) {
|
|
|
+ throw Error('The selected grant or grantedGroup is not assignable to this page.');
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /*
|
|
|
+ * update empty page if exists, if not, create a new page
|
|
|
+ */
|
|
|
+ let page;
|
|
|
+ const emptyPage = await Page.findOne({ path, isEmpty: true });
|
|
|
+ if (emptyPage != null) {
|
|
|
+ page = emptyPage;
|
|
|
+ page.isEmpty = false;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ page = new Page();
|
|
|
+ }
|
|
|
+
|
|
|
+ let parentId: string | null = null;
|
|
|
+ const parentPath = nodePath.dirname(path);
|
|
|
+ const parent = await this.findOneParentByParentPath(parentPath);
|
|
|
+ if (!isTopPage(path)) {
|
|
|
+ parentId = await Page.getParentIdAndFillAncestors(path, parent);
|
|
|
+ }
|
|
|
+
|
|
|
+ page.path = path;
|
|
|
+ page.creator = user;
|
|
|
+ page.lastUpdateUser = user;
|
|
|
+ page.redirectTo = redirectTo;
|
|
|
+ page.status = STATUS_PUBLISHED;
|
|
|
+ page.parent = options.grant === GRANT_RESTRICTED ? null : parentId;
|
|
|
+
|
|
|
+ page.applyScope(user, grant, grantUserGroupId);
|
|
|
+
|
|
|
+ let savedPage = await page.save();
|
|
|
+ const newRevision = Revision.prepareRevision(savedPage, body, null, user, { format });
|
|
|
+ const revision = await pushRevision(savedPage, newRevision, user);
|
|
|
+ savedPage = await this.findByPath(revision.path);
|
|
|
+ await savedPage.populateDataToShowRevision();
|
|
|
+
|
|
|
+ pageEvent.emit('create', savedPage, user);
|
|
|
+
|
|
|
+ return savedPage;
|
|
|
+ };
|
|
|
+
|
|
|
// add old page schema methods
|
|
|
const pageSchema = getPageSchema(crowi);
|
|
|
schema.methods = { ...pageSchema.methods, ...schema.methods };
|