Taichi Masuyama 4 yıl önce
ebeveyn
işleme
a8965b6af0

+ 13 - 6
packages/app/src/server/models/obsolete-page.js

@@ -976,7 +976,7 @@ export const getPageSchema = (crowi) => {
     const Page = this;
     const Revision = crowi.model('Revision');
     const {
-      format = 'markdown', redirectTo, grantUserGroupId, parentId,
+      format = 'markdown', redirectTo, grantUserGroupId, parentId: _parentId,
     } = options;
 
     // sanitize path
@@ -988,11 +988,18 @@ export const getPageSchema = (crowi) => {
       grant = GRANT_PUBLIC;
     }
 
-    const isExist = await this.count({ path, isEmpty: false }); // not validate empty page
+    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');
     }
 
+    const parentPath = nodePath.dirname(path);
+    const parent = await this.findOneParentByParentPath(parentPath);
+
+    /*
+     * UserGroup & Owner validation
+     */
+
     /*
      * update empty page if exists, if not, create a new page
      */
@@ -1008,9 +1015,9 @@ export const getPageSchema = (crowi) => {
 
     const isV5Compatible = crowi.configManager.getConfig('crowi', 'app:isV5Compatible');
 
-    let parent = parentId;
-    if (isV5Compatible && parent == null && !isTopPage(path)) {
-      parent = await Page.getParentIdAndFillAncestors(path);
+    let parentId = _parentId;
+    if (isV5Compatible && parentId == null && !isTopPage(path)) {
+      parentId = await Page.getParentIdAndFillAncestors(path, parent);
     }
 
     page.path = path;
@@ -1018,7 +1025,7 @@ export const getPageSchema = (crowi) => {
     page.lastUpdateUser = user;
     page.redirectTo = redirectTo;
     page.status = STATUS_PUBLISHED;
-    page.parent = parent;
+    page.parent = parentId;
 
     await validateAppliedScope(user, grant, grantUserGroupId);
     page.applyScope(user, grant, grantUserGroupId);

+ 6 - 3
packages/app/src/server/models/page.ts

@@ -40,7 +40,7 @@ type TargetAndAncestorsResult = {
 export interface PageModel extends Model<PageDocument> {
   [x: string]: any; // for obsolete methods
   createEmptyPagesByPaths(paths: string[], publicOnly?: boolean): Promise<void>
-  getParentIdAndFillAncestors(path: string): Promise<string | null>
+  getParentIdAndFillAncestors(path: string, parent: PageDocument): Promise<string | null>
   findByPathAndViewer(path: string | null, user, userGroups?, useFindOne?: boolean, includeEmpty?: boolean): Promise<PageDocument[]>
   findTargetAndAncestorsByPathOrId(pathOrId: string): Promise<TargetAndAncestorsResult>
   findChildrenByParentPathOrIdAndViewer(parentPathOrId: string, user, userGroups?): Promise<PageDocument[]>
@@ -164,6 +164,10 @@ schema.statics.createEmptyPagesByPaths = async function(paths: string[], publicO
   }
 };
 
+schema.statics.findOneParentByParentPath = async function(parentPath: string): Promise<PageDocument | null> {
+  return this.findOne({ path: parentPath }); // find the oldest parent which must always be the true parent
+};
+
 /*
  * Find the parent and update if the parent exists.
  * If not,
@@ -171,10 +175,9 @@ schema.statics.createEmptyPagesByPaths = async function(paths: string[], publicO
  *   - second  update ancestor pages' parent
  *   - finally return the target's parent page id
  */
-schema.statics.getParentIdAndFillAncestors = async function(path: string): Promise<Schema.Types.ObjectId> {
+schema.statics.getParentIdAndFillAncestors = async function(path: string, parent: PageDocument): Promise<Schema.Types.ObjectId> {
   const parentPath = nodePath.dirname(path);
 
-  const parent = await this.findOne({ path: parentPath }); // find the oldest parent which must always be the true parent
   if (parent != null) {
     return parent._id;
   }