Przeglądaj źródła

retrieve grant from the closest ancestor

Yuki Takei 2 lat temu
rodzic
commit
7821923a13
1 zmienionych plików z 14 dodań i 7 usunięć
  1. 14 7
      apps/app/src/server/service/page/index.ts

+ 14 - 7
apps/app/src/server/service/page/index.ts

@@ -3743,21 +3743,28 @@ class PageService implements IPageService {
    * Create a page
    * Create a page
    * Set options.isSynchronously to true to await all process when you want to run this method multiple times at short intervals.
    * Set options.isSynchronously to true to await all process when you want to run this method multiple times at short intervals.
    */
    */
-  async create(path: string, body: string, user, options: IOptionsForCreate = {}): Promise<PageDocument> {
+  async create(_path: string, body: string, user, options: IOptionsForCreate = {}): Promise<PageDocument> {
     // Switch method
     // Switch method
     const isV5Compatible = this.crowi.configManager.getConfig('crowi', 'app:isV5Compatible');
     const isV5Compatible = this.crowi.configManager.getConfig('crowi', 'app:isV5Compatible');
     if (!isV5Compatible) {
     if (!isV5Compatible) {
-      return this.createV4(path, body, user, options);
+      return this.createV4(_path, body, user, options);
     }
     }
 
 
     // Values
     // Values
-    // eslint-disable-next-line no-param-reassign
-    path = this.crowi.xss.process(path); // sanitize path
-    const { grantUserGroupIds } = options;
-    const grant = isTopPage(path) ? PageGrant.GRANT_PUBLIC : options.grant;
+    const path = this.crowi.xss.process(_path); // sanitize path
+
+    // Retrieve closest ancestor document
+    const Page = mongoose.model<PageDocument, PageModel>('Page');
+    const closestAncestor = await Page.findNonEmptyClosestAncestor(path);
+
+    // Determine grantData
+    const grant = options.grant ?? closestAncestor?.grant ?? PageGrant.GRANT_PUBLIC;
+    const grantedUserIds = grant === PageGrant.GRANT_OWNER ? [user._id] : undefined;
+    const grantUserGroupIds = options.grantUserGroupIds
+      ?? await this.pageGrantService.getUserRelatedGrantedGroups(closestAncestor, user);
     const grantData = {
     const grantData = {
       grant,
       grant,
-      grantedUserIds: grant === PageGrant.GRANT_OWNER ? [user._id] : undefined,
+      grantedUserIds,
       grantUserGroupIds,
       grantUserGroupIds,
     };
     };