diff --git a/app/Http/Controllers/BookshelfController.php b/app/Http/Controllers/BookshelfController.php index 02b6299ce..d1752d180 100644 --- a/app/Http/Controllers/BookshelfController.php +++ b/app/Http/Controllers/BookshelfController.php @@ -189,37 +189,56 @@ class BookshelfController extends Controller $this->entityRepo->destroyBookshelf($bookshelf); return redirect('/shelves'); } -// -// /** -// * Show the Restrictions view. -// * @param $bookSlug -// * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View -// */ -// public function showRestrict($bookSlug) -// { -// $book = $this->entityRepo->getBySlug('book', $bookSlug); -// $this->checkOwnablePermission('restrictions-manage', $book); -// $roles = $this->userRepo->getRestrictableRoles(); -// return view('books/restrictions', [ -// 'book' => $book, -// 'roles' => $roles -// ]); -// } -// -// /** -// * Set the restrictions for this book. -// * @param $bookSlug -// * @param $bookSlug -// * @param Request $request -// * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector -// */ -// public function restrict($bookSlug, Request $request) -// { -// $book = $this->entityRepo->getBySlug('book', $bookSlug); -// $this->checkOwnablePermission('restrictions-manage', $book); -// $this->entityRepo->updateEntityPermissionsFromRequest($request, $book); -// session()->flash('success', trans('entities.books_permissions_updated')); -// return redirect($book->getUrl()); -// } + + /** + * Show the Restrictions view. + * @param $slug + * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View + * @throws \BookStack\Exceptions\NotFoundException + */ + public function showRestrict(string $slug) + { + $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); + $this->checkOwnablePermission('restrictions-manage', $bookshelf); + + $roles = $this->userRepo->getRestrictableRoles(); + return view('shelves.restrictions', [ + 'shelf' => $bookshelf, + 'roles' => $roles + ]); + } + + /** + * Set the restrictions for this bookshelf. + * @param $slug + * @param Request $request + * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector + * @throws \BookStack\Exceptions\NotFoundException + */ + public function restrict(string $slug, Request $request) + { + $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); + $this->checkOwnablePermission('restrictions-manage', $bookshelf); + + $this->entityRepo->updateEntityPermissionsFromRequest($request, $bookshelf); + session()->flash('success', trans('entities.shelves_permissions_updated')); + return redirect($bookshelf->getUrl()); + } + + /** + * Copy the permissions of a bookshelf to the child books. + * @param string $slug + * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector + * @throws \BookStack\Exceptions\NotFoundException + */ + public function copyPermissions(string $slug) + { + $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); + $this->checkOwnablePermission('restrictions-manage', $bookshelf); + + $updateCount = $this->entityRepo->copyBookshelfPermissions($bookshelf); + session()->flash('success', trans('entities.shelves_copy_permission_success', ['count' => $updateCount])); + return redirect($bookshelf->getUrl()); + } } diff --git a/app/Repos/EntityRepo.php b/app/Repos/EntityRepo.php index db9226411..ccccd95f4 100644 --- a/app/Repos/EntityRepo.php +++ b/app/Repos/EntityRepo.php @@ -1282,4 +1282,29 @@ class EntityRepo $this->permissionService->deleteJointPermissionsForEntity($entity); $this->searchService->deleteEntityTerms($entity); } + + /** + * Copy the permissions of a bookshelf to all child books. + * Returns the number of books that had permissions updated. + * @param Bookshelf $bookshelf + * @return int + */ + public function copyBookshelfPermissions(Bookshelf $bookshelf) + { + $shelfPermissions = $bookshelf->permissions()->get(['role_id', 'action'])->toArray(); + $shelfBooks = $bookshelf->books()->get(); + $updatedBookCount = 0; + + foreach ($shelfBooks as $book) { + if (!userCan('restrictions-manage', $book)) continue; + $book->permissions()->delete(); + $book->restricted = $bookshelf->restricted; + $book->permissions()->createMany($shelfPermissions); + $book->save(); + $this->permissionService->buildJointPermissionsForEntity($book); + $updatedBookCount++; + } + + return $updatedBookCount; + } } diff --git a/resources/lang/en/entities.php b/resources/lang/en/entities.php index 2228da2cd..44ab2c9cc 100644 --- a/resources/lang/en/entities.php +++ b/resources/lang/en/entities.php @@ -68,7 +68,7 @@ return [ * Shelves */ 'shelves' => 'Shelves', - 'shelves_long' => 'BookShelves', + 'shelves_long' => 'Bookshelves', 'shelves_empty' => 'No shelves have been created', 'shelves_create' => 'Create New Shelf', 'shelves_popular' => 'Popular Shelves', @@ -87,6 +87,13 @@ return [ 'shelves_delete_named' => 'Delete Bookshelf :name', 'shelves_delete_explain' => "This will delete the bookshelf with the name ':name'. Contained books will not be deleted.", 'shelves_delete_confirmation' => 'Are you sure you want to delete this bookshelf?', + 'shelves_permissions' => 'Bookshelf Permissions', + 'shelves_permissions_updated' => 'Bookshelf Permissions Updated', + 'shelves_permissions_active' => 'Bookshelf Permissions Active', + 'shelves_copy_permissions_to_books' => 'Copy Permissions to Books', + 'shelves_copy_permissions' => 'Copy Permissions', + 'shelves_copy_permissions_explain' => 'This will apply the current permission settings of this bookshelf to all books contained within. Before activating, ensure any changes to the permissions of this bookshelf have been saved.', + 'shelves_copy_permission_success' => 'Bookshelf permissions copied to :count books', /** * Books diff --git a/resources/views/shelves/restrictions.blade.php b/resources/views/shelves/restrictions.blade.php index 2a6eb0bea..472078ad2 100644 --- a/resources/views/shelves/restrictions.blade.php +++ b/resources/views/shelves/restrictions.blade.php @@ -2,18 +2,31 @@ @section('toolbar')
+ +
{{ trans('entities.shelves_copy_permissions_explain') }}
+