itizawa 5 лет назад
Родитель
Сommit
395d662bf8
2 измененных файлов с 15 добавлено и 11 удалено
  1. 13 9
      src/server/models/page.js
  2. 2 2
      src/test/models/page.test.js

+ 13 - 9
src/server/models/page.js

@@ -144,8 +144,6 @@ class PageQueryBuilder {
    * If top page, return without doing anything.
    */
   addConditionToListWithDescendants(path, option) {
-    const excludeParent = option.excludeParent || false;
-
     // No request is set for the top page
     if (isTopPage(path)) {
       return this;
@@ -156,14 +154,12 @@ class PageQueryBuilder {
 
     const startsPattern = escapeStringRegexp(pathWithTrailingSlash);
 
-    const filters = [{ path: new RegExp(`^${startsPattern}`) }];
-    if (!excludeParent) {
-      filters.push({ path: pathNormalized });
-    }
-
     this.query = this.query
       .and({
-        $or: filters,
+        $or: [
+          { path: pathNormalized },
+          { path: new RegExp(`^${startsPattern}`) },
+        ],
       });
 
     return this;
@@ -721,12 +717,20 @@ module.exports = function(crowi) {
    * find pages that is match with `path` and its descendants whitch user is able to manage
    */
   pageSchema.statics.findManageableListWithDescendants = async function(page, user, option = {}) {
+    const excludeParent = option.excludeParent || false;
+
     if (user == null) {
       return null;
     }
 
     const builder = new PageQueryBuilder(this.find());
-    builder.addConditionToListWithDescendants(page.path, option);
+
+    if (excludeParent) {
+      builder.addConditionToListOnlyDescendants(page.path, option);
+    }
+    else {
+      builder.addConditionToListWithDescendants(page.path, option);
+    }
     builder.addConditionToExcludeRedirect();
 
     // add grant conditions

+ 2 - 2
src/test/models/page.test.js

@@ -333,7 +333,7 @@ describe('Page', () => {
   describe('PageQueryBuilder.addConditionToListWithDescendants', () => {
     test('can retrieve descendants of /page', async() => {
       const builder = new PageQueryBuilder(Page.find());
-      builder.addConditionToListWithDescendants('/page', {});
+      builder.addConditionToListWithDescendants('/page');
 
       const result = await builder.query.exec();
 
@@ -346,7 +346,7 @@ describe('Page', () => {
 
     test('can retrieve descendants of /page1', async() => {
       const builder = new PageQueryBuilder(Page.find());
-      builder.addConditionToListWithDescendants('/page1/', {});
+      builder.addConditionToListWithDescendants('/page1/');
 
       const result = await builder.query.exec();