From 3f3fad7113344ac262111cce049ed3b11d2d7f83 Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Thu, 17 Dec 2020 17:31:18 +0000 Subject: [PATCH] Fixed book-tree-gen page visibility issue When book trees were generated, pages in chapters where ALL pages within were not supposed to be visibile, would be visible due to the code falling back on the raw relation which would not account for permissions. This has now been changed so that a custom 'visible_pages' attribute is set and used by any book tree structures, to ensure it does not fall back to the raw relation. Added an extra test to cover. For #2414 --- app/Entities/Chapter.php | 10 -------- app/Entities/Managers/BookContents.php | 6 ++++- resources/views/books/export.blade.php | 8 +++---- resources/views/books/sort-box.blade.php | 2 +- resources/views/chapters/child-menu.blade.php | 4 ++-- resources/views/chapters/list-item.blade.php | 10 ++++---- resources/views/partials/book-tree.blade.php | 2 +- resources/views/search/book.blade.php | 24 ------------------- tests/Permissions/RestrictionsTest.php | 16 +++++++++++++ 9 files changed, 35 insertions(+), 47 deletions(-) delete mode 100644 resources/views/search/book.blade.php diff --git a/app/Entities/Chapter.php b/app/Entities/Chapter.php index 3290afcfa..5f5509efa 100644 --- a/app/Entities/Chapter.php +++ b/app/Entities/Chapter.php @@ -5,7 +5,6 @@ use Illuminate\Support\Collection; /** * Class Chapter * @property Collection $pages - * @package BookStack\Entities */ class Chapter extends BookChild { @@ -52,15 +51,6 @@ class Chapter extends BookChild return mb_strlen($description) > $length ? mb_substr($description, 0, $length-3) . '...' : $description; } - /** - * Check if this chapter has any child pages. - * @return bool - */ - public function hasChildren() - { - return count($this->pages) > 0; - } - /** * Get the visible pages in this chapter. */ diff --git a/app/Entities/Managers/BookContents.php b/app/Entities/Managers/BookContents.php index 52447e43b..b9844da57 100644 --- a/app/Entities/Managers/BookContents.php +++ b/app/Entities/Managers/BookContents.php @@ -53,12 +53,16 @@ class BookContents $pages->groupBy('chapter_id')->each(function ($pages, $chapter_id) use ($chapterMap, &$lonePages) { $chapter = $chapterMap->get($chapter_id); if ($chapter) { - $chapter->setAttribute('pages', collect($pages)->sortBy($this->bookChildSortFunc())); + $chapter->setAttribute('visible_pages', collect($pages)->sortBy($this->bookChildSortFunc())); } else { $lonePages = $lonePages->concat($pages); } }); + $chapters->whereNull('visible_pages')->each(function (Chapter $chapter) { + $chapter->setAttribute('visible_pages', collect([])); + }); + $all->each(function (Entity $entity) use ($renderPages) { $entity->setRelation('book', $this->book); diff --git a/resources/views/books/export.blade.php b/resources/views/books/export.blade.php index e86a24e81..f62b89582 100644 --- a/resources/views/books/export.blade.php +++ b/resources/views/books/export.blade.php @@ -41,9 +41,9 @@