3 namespace BookStack\Http\Controllers;
5 use BookStack\Auth\Permissions\PermissionFormData;
6 use BookStack\Entities\Models\Book;
7 use BookStack\Entities\Models\Bookshelf;
8 use BookStack\Entities\Models\Chapter;
9 use BookStack\Entities\Models\Page;
10 use BookStack\Entities\Tools\PermissionsUpdater;
11 use Illuminate\Http\Request;
13 class PermissionsController extends Controller
15 protected PermissionsUpdater $permissionsUpdater;
17 public function __construct(PermissionsUpdater $permissionsUpdater)
19 $this->permissionsUpdater = $permissionsUpdater;
23 * Show the Permissions view for a page.
25 public function showForPage(string $bookSlug, string $pageSlug)
27 $page = Page::getBySlugs($bookSlug, $pageSlug);
28 $this->checkOwnablePermission('restrictions-manage', $page);
30 return view('pages.permissions', [
32 'data' => new PermissionFormData($page),
37 * Set the permissions for a page.
39 public function updateForPage(Request $request, string $bookSlug, string $pageSlug)
41 $page = Page::getBySlugs($bookSlug, $pageSlug);
42 $this->checkOwnablePermission('restrictions-manage', $page);
44 $this->permissionsUpdater->updateFromPermissionsForm($page, $request);
46 $this->showSuccessNotification(trans('entities.pages_permissions_success'));
48 return redirect($page->getUrl());
52 * Show the Restrictions view for a chapter.
54 public function showForChapter(string $bookSlug, string $chapterSlug)
56 $chapter = Chapter::getBySlugs($bookSlug, $chapterSlug);
57 $this->checkOwnablePermission('restrictions-manage', $chapter);
59 return view('chapters.permissions', [
60 'chapter' => $chapter,
61 'data' => new PermissionFormData($chapter),
66 * Set the restrictions for a chapter.
68 public function updateForChapter(Request $request, string $bookSlug, string $chapterSlug)
70 $chapter = Chapter::getBySlugs($bookSlug, $chapterSlug);
71 $this->checkOwnablePermission('restrictions-manage', $chapter);
73 $this->permissionsUpdater->updateFromPermissionsForm($chapter, $request);
75 $this->showSuccessNotification(trans('entities.chapters_permissions_success'));
77 return redirect($chapter->getUrl());
81 * Show the permissions view for a book.
83 public function showForBook(string $slug)
85 $book = Book::getBySlug($slug);
86 $this->checkOwnablePermission('restrictions-manage', $book);
88 return view('books.permissions', [
90 'data' => new PermissionFormData($book),
95 * Set the restrictions for a book.
97 public function updateForBook(Request $request, string $slug)
99 $book = Book::getBySlug($slug);
100 $this->checkOwnablePermission('restrictions-manage', $book);
102 $this->permissionsUpdater->updateFromPermissionsForm($book, $request);
104 $this->showSuccessNotification(trans('entities.books_permissions_updated'));
106 return redirect($book->getUrl());
110 * Show the permissions view for a shelf.
112 public function showForShelf(string $slug)
114 $shelf = Bookshelf::getBySlug($slug);
115 $this->checkOwnablePermission('restrictions-manage', $shelf);
117 return view('shelves.permissions', [
119 'data' => new PermissionFormData($shelf),
124 * Set the permissions for a shelf.
126 public function updateForShelf(Request $request, string $slug)
128 $shelf = Bookshelf::getBySlug($slug);
129 $this->checkOwnablePermission('restrictions-manage', $shelf);
131 $this->permissionsUpdater->updateFromPermissionsForm($shelf, $request);
133 $this->showSuccessNotification(trans('entities.shelves_permissions_updated'));
135 return redirect($shelf->getUrl());
139 * Copy the permissions of a bookshelf to the child books.
141 public function copyShelfPermissionsToBooks(string $slug)
143 $shelf = Bookshelf::getBySlug($slug);
144 $this->checkOwnablePermission('restrictions-manage', $shelf);
146 $updateCount = $this->permissionsUpdater->updateBookPermissionsFromShelf($shelf);
147 $this->showSuccessNotification(trans('entities.shelves_copy_permission_success', ['count' => $updateCount]));
149 return redirect($shelf->getUrl());