0
0
Fork 0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-05-19 06:31:30 +00:00

Rolled out reference pages to all entities, added testing

Including testing to check permissions applied to listed references.
This commit is contained in:
Dan Brown 2022-08-19 22:40:44 +01:00
parent d5465726e2
commit d198332d3c
No known key found for this signature in database
GPG key ID: 46D9F943C24A2EF9
9 changed files with 182 additions and 19 deletions
app/Http/Controllers

View file

@ -3,7 +3,12 @@
namespace BookStack\Http\Controllers;
use BookStack\Auth\Permissions\PermissionApplicator;
use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\Bookshelf;
use BookStack\Entities\Models\Chapter;
use BookStack\Entities\Models\Entity;
use BookStack\Entities\Models\Page;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\Relation;
class ReferenceController extends Controller
@ -23,8 +28,64 @@ class ReferenceController extends Controller
{
/** @var Page $page */
$page = Page::visible()->whereSlugs($bookSlug, $pageSlug)->firstOrFail();
$references = $this->getEntityReferences($page);
$baseQuery = $page->referencesTo()
return view('pages.references', [
'page' => $page,
'references' => $references,
]);
}
/**
* Display the references to a given chapter.
*/
public function chapter(string $bookSlug, string $chapterSlug)
{
/** @var Chapter $chapter */
$chapter = Chapter::visible()->whereSlugs($bookSlug, $chapterSlug)->firstOrFail();
$references = $this->getEntityReferences($chapter);
return view('chapters.references', [
'chapter' => $chapter,
'references' => $references,
]);
}
/**
* Display the references to a given book.
*/
public function book(string $slug)
{
$book = Book::visible()->where('slug', '=', $slug)->firstOrFail();
$references = $this->getEntityReferences($book);
return view('books.references', [
'book' => $book,
'references' => $references,
]);
}
/**
* Display the references to a given shelf.
*/
public function shelf(string $slug)
{
$shelf = Bookshelf::visible()->where('slug', '=', $slug)->firstOrFail();
$references = $this->getEntityReferences($shelf);
return view('shelves.references', [
'shelf' => $shelf,
'references' => $references,
]);
}
/**
* Query the references for the given entities.
* Loads the commonly required relations while taking permissions into account.
*/
protected function getEntityReferences(Entity $entity): Collection
{
$baseQuery = $entity->referencesTo()
->where('from_type', '=', (new Page())->getMorphClass())
->with([
'from' => fn(Relation $query) => $query->select(Page::$listAttributes),
@ -39,9 +100,6 @@ class ReferenceController extends Controller
'from_type'
)->get();
return view('pages.references', [
'page' => $page,
'references' => $references,
]);
return $references;
}
}