3 namespace BookStack\Permissions;
5 use BookStack\Entities\Queries\EntityQueries;
6 use BookStack\Entities\Tools\PermissionsUpdater;
7 use BookStack\Http\Controller;
8 use BookStack\Permissions\Models\EntityPermission;
9 use BookStack\Users\Models\Role;
10 use Illuminate\Http\Request;
12 class PermissionsController extends Controller
14 public function __construct(
15 protected PermissionsUpdater $permissionsUpdater,
16 protected EntityQueries $queries,
21 * Show the permissions view for a page.
23 public function showForPage(string $bookSlug, string $pageSlug)
25 $page = $this->queries->pages->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
26 $this->checkOwnablePermission('restrictions-manage', $page);
28 $this->setPageTitle(trans('entities.pages_permissions'));
29 return view('pages.permissions', [
31 'data' => new PermissionFormData($page),
36 * Set the permissions for a page.
38 public function updateForPage(Request $request, string $bookSlug, string $pageSlug)
40 $page = $this->queries->pages->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
41 $this->checkOwnablePermission('restrictions-manage', $page);
43 $this->permissionsUpdater->updateFromPermissionsForm($page, $request);
45 $this->showSuccessNotification(trans('entities.pages_permissions_success'));
47 return redirect($page->getUrl());
51 * Show the permissions view for a chapter.
53 public function showForChapter(string $bookSlug, string $chapterSlug)
55 $chapter = $this->queries->chapters->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
56 $this->checkOwnablePermission('restrictions-manage', $chapter);
58 $this->setPageTitle(trans('entities.chapters_permissions'));
59 return view('chapters.permissions', [
60 'chapter' => $chapter,
61 'data' => new PermissionFormData($chapter),
66 * Set the permissions for a chapter.
68 public function updateForChapter(Request $request, string $bookSlug, string $chapterSlug)
70 $chapter = $this->queries->chapters->findVisibleBySlugsOrFail($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 = $this->queries->books->findVisibleBySlugOrFail($slug);
86 $this->checkOwnablePermission('restrictions-manage', $book);
88 $this->setPageTitle(trans('entities.books_permissions'));
89 return view('books.permissions', [
91 'data' => new PermissionFormData($book),
96 * Set the permissions for a book.
98 public function updateForBook(Request $request, string $slug)
100 $book = $this->queries->books->findVisibleBySlugOrFail($slug);
101 $this->checkOwnablePermission('restrictions-manage', $book);
103 $this->permissionsUpdater->updateFromPermissionsForm($book, $request);
105 $this->showSuccessNotification(trans('entities.books_permissions_updated'));
107 return redirect($book->getUrl());
111 * Show the permissions view for a shelf.
113 public function showForShelf(string $slug)
115 $shelf = $this->queries->shelves->findVisibleBySlugOrFail($slug);
116 $this->checkOwnablePermission('restrictions-manage', $shelf);
118 $this->setPageTitle(trans('entities.shelves_permissions'));
119 return view('shelves.permissions', [
121 'data' => new PermissionFormData($shelf),
126 * Set the permissions for a shelf.
128 public function updateForShelf(Request $request, string $slug)
130 $shelf = $this->queries->shelves->findVisibleBySlugOrFail($slug);
131 $this->checkOwnablePermission('restrictions-manage', $shelf);
133 $this->permissionsUpdater->updateFromPermissionsForm($shelf, $request);
135 $this->showSuccessNotification(trans('entities.shelves_permissions_updated'));
137 return redirect($shelf->getUrl());
141 * Copy the permissions of a bookshelf to the child books.
143 public function copyShelfPermissionsToBooks(string $slug)
145 $shelf = $this->queries->shelves->findVisibleBySlugOrFail($slug);
146 $this->checkOwnablePermission('restrictions-manage', $shelf);
148 $updateCount = $this->permissionsUpdater->updateBookPermissionsFromShelf($shelf);
149 $this->showSuccessNotification(trans('entities.shelves_copy_permission_success', ['count' => $updateCount]));
151 return redirect($shelf->getUrl());
155 * Get an empty entity permissions form row for the given role.
157 public function formRowForRole(string $entityType, string $roleId)
159 $this->checkPermissionOr('restrictions-manage-all', fn() => userCan('restrictions-manage-own'));
161 $role = Role::query()->findOrFail($roleId);
163 return view('form.entity-permissions-row', [
165 'permission' => new EntityPermission(),
166 'entityType' => $entityType,
167 'inheriting' => false,