X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/5283919d2493a396f588043b68bf9a4b3d5876a2..refs/pull/139/head:/app/Repos/BookRepo.php diff --git a/app/Repos/BookRepo.php b/app/Repos/BookRepo.php index b74821d31..a11ed2763 100644 --- a/app/Repos/BookRepo.php +++ b/app/Repos/BookRepo.php @@ -1,5 +1,6 @@ restrictionService->enforceBookRestrictions($this->book, 'view'); + return $this->permissionService->enforceBookRestrictions($this->book, 'view'); } /** @@ -123,21 +124,43 @@ class BookRepo extends EntityRepo /** * Get a new book instance from request input. + * @param array $input + * @return Book + */ + public function createFromInput($input) + { + $book = $this->book->newInstance($input); + $book->slug = $this->findSuitableSlug($book->name); + $book->created_by = auth()->user()->id; + $book->updated_by = auth()->user()->id; + $book->save(); + $this->permissionService->buildJointPermissionsForEntity($book); + return $book; + } + + /** + * Update the given book from user input. + * @param Book $book * @param $input * @return Book */ - public function newFromInput($input) + public function updateFromInput(Book $book, $input) { - return $this->book->newInstance($input); + $book->fill($input); + $book->slug = $this->findSuitableSlug($book->name, $book->id); + $book->updated_by = auth()->user()->id; + $book->save(); + $this->permissionService->buildJointPermissionsForEntity($book); + return $book; } /** - * Destroy a book identified by the given slug. - * @param $bookSlug + * Destroy the given book. + * @param Book $book + * @throws \Exception */ - public function destroyBySlug($bookSlug) + public function destroy(Book $book) { - $book = $this->getBySlug($bookSlug); foreach ($book->pages as $page) { $this->pageRepo->destroy($page); } @@ -145,10 +168,20 @@ class BookRepo extends EntityRepo $this->chapterRepo->destroy($chapter); } $book->views()->delete(); - $book->restrictions()->delete(); + $book->permissions()->delete(); + $this->permissionService->deleteJointPermissionsForEntity($book); $book->delete(); } + /** + * Alias method to update the book jointPermissions in the PermissionService. + * @param Book $book + */ + public function updateBookPermissions(Book $book) + { + $this->permissionService->buildJointPermissionsForEntity($book); + } + /** * Get the next child element priority. * @param Book $book @@ -198,20 +231,30 @@ class BookRepo extends EntityRepo * Returns a sorted collection of Pages and Chapters. * Loads the bookslug onto child elements to prevent access database access for getting the slug. * @param Book $book + * @param bool $filterDrafts * @return mixed */ - public function getChildren(Book $book) + public function getChildren(Book $book, $filterDrafts = false) { $pageQuery = $book->pages()->where('chapter_id', '=', 0); - $pageQuery = $this->restrictionService->enforcePageRestrictions($pageQuery, 'view'); + $pageQuery = $this->permissionService->enforcePageRestrictions($pageQuery, 'view'); + + if ($filterDrafts) { + $pageQuery = $pageQuery->where('draft', '=', false); + } + $pages = $pageQuery->get(); - $chapterQuery = $book->chapters()->with(['pages' => function($query) { - $this->restrictionService->enforcePageRestrictions($query, 'view'); + $chapterQuery = $book->chapters()->with(['pages' => function($query) use ($filterDrafts) { + $this->permissionService->enforcePageRestrictions($query, 'view'); + if ($filterDrafts) $query->where('draft', '=', false); }]); - $chapterQuery = $this->restrictionService->enforceChapterRestrictions($chapterQuery, 'view'); + $chapterQuery = $this->permissionService->enforceChapterRestrictions($chapterQuery, 'view'); $chapters = $chapterQuery->get(); - $children = $pages->merge($chapters); + $children = $pages->values(); + foreach ($chapters as $chapter) { + $children->push($chapter); + } $bookSlug = $book->slug; $children->each(function ($child) use ($bookSlug) { @@ -246,8 +289,9 @@ class BookRepo extends EntityRepo public function getBySearch($term, $count = 20, $paginationAppends = []) { $terms = $this->prepareSearchTerms($term); - $books = $this->restrictionService->enforceBookRestrictions($this->book->fullTextSearchQuery(['name', 'description'], $terms)) - ->paginate($count)->appends($paginationAppends); + $bookQuery = $this->permissionService->enforceBookRestrictions($this->book->fullTextSearchQuery(['name', 'description'], $terms)); + $bookQuery = $this->addAdvancedSearchQueries($bookQuery, $term); + $books = $bookQuery->paginate($count)->appends($paginationAppends); $words = join('|', explode(' ', preg_quote(trim($term), '/'))); foreach ($books as $book) { //highlight