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 BookStack\Util\DatabaseTransaction;
11 use Illuminate\Http\Request;
13 class PermissionsController extends Controller
15 public function __construct(
16 protected PermissionsUpdater $permissionsUpdater,
17 protected EntityQueries $queries,
22 * Show the permissions view for a page.
24 public function showForPage(string $bookSlug, string $pageSlug)
26 $page = $this->queries->pages->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
27 $this->checkOwnablePermission('restrictions-manage', $page);
29 $this->setPageTitle(trans('entities.pages_permissions'));
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 = $this->queries->pages->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
42 $this->checkOwnablePermission('restrictions-manage', $page);
44 (new DatabaseTransaction(function () use ($page, $request) {
45 $this->permissionsUpdater->updateFromPermissionsForm($page, $request);
48 $this->showSuccessNotification(trans('entities.pages_permissions_success'));
50 return redirect($page->getUrl());
54 * Show the permissions view for a chapter.
56 public function showForChapter(string $bookSlug, string $chapterSlug)
58 $chapter = $this->queries->chapters->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
59 $this->checkOwnablePermission('restrictions-manage', $chapter);
61 $this->setPageTitle(trans('entities.chapters_permissions'));
62 return view('chapters.permissions', [
63 'chapter' => $chapter,
64 'data' => new PermissionFormData($chapter),
69 * Set the permissions for a chapter.
71 public function updateForChapter(Request $request, string $bookSlug, string $chapterSlug)
73 $chapter = $this->queries->chapters->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
74 $this->checkOwnablePermission('restrictions-manage', $chapter);
76 (new DatabaseTransaction(function () use ($chapter, $request) {
77 $this->permissionsUpdater->updateFromPermissionsForm($chapter, $request);
80 $this->showSuccessNotification(trans('entities.chapters_permissions_success'));
82 return redirect($chapter->getUrl());
86 * Show the permissions view for a book.
88 public function showForBook(string $slug)
90 $book = $this->queries->books->findVisibleBySlugOrFail($slug);
91 $this->checkOwnablePermission('restrictions-manage', $book);
93 $this->setPageTitle(trans('entities.books_permissions'));
94 return view('books.permissions', [
96 'data' => new PermissionFormData($book),
101 * Set the permissions for a book.
103 public function updateForBook(Request $request, string $slug)
105 $book = $this->queries->books->findVisibleBySlugOrFail($slug);
106 $this->checkOwnablePermission('restrictions-manage', $book);
108 (new DatabaseTransaction(function () use ($book, $request) {
109 $this->permissionsUpdater->updateFromPermissionsForm($book, $request);
112 $this->showSuccessNotification(trans('entities.books_permissions_updated'));
114 return redirect($book->getUrl());
118 * Show the permissions view for a shelf.
120 public function showForShelf(string $slug)
122 $shelf = $this->queries->shelves->findVisibleBySlugOrFail($slug);
123 $this->checkOwnablePermission('restrictions-manage', $shelf);
125 $this->setPageTitle(trans('entities.shelves_permissions'));
126 return view('shelves.permissions', [
128 'data' => new PermissionFormData($shelf),
133 * Set the permissions for a shelf.
135 public function updateForShelf(Request $request, string $slug)
137 $shelf = $this->queries->shelves->findVisibleBySlugOrFail($slug);
138 $this->checkOwnablePermission('restrictions-manage', $shelf);
140 (new DatabaseTransaction(function () use ($shelf, $request) {
141 $this->permissionsUpdater->updateFromPermissionsForm($shelf, $request);
144 $this->showSuccessNotification(trans('entities.shelves_permissions_updated'));
146 return redirect($shelf->getUrl());
150 * Copy the permissions of a bookshelf to the child books.
152 public function copyShelfPermissionsToBooks(string $slug)
154 $shelf = $this->queries->shelves->findVisibleBySlugOrFail($slug);
155 $this->checkOwnablePermission('restrictions-manage', $shelf);
157 $updateCount = (new DatabaseTransaction(function () use ($shelf) {
158 return $this->permissionsUpdater->updateBookPermissionsFromShelf($shelf);
161 $this->showSuccessNotification(trans('entities.shelves_copy_permission_success', ['count' => $updateCount]));
163 return redirect($shelf->getUrl());
167 * Get an empty entity permissions form row for the given role.
169 public function formRowForRole(string $entityType, string $roleId)
171 $this->checkPermissionOr('restrictions-manage-all', fn() => userCan('restrictions-manage-own'));
173 $role = Role::query()->findOrFail($roleId);
175 return view('form.entity-permissions-row', [
177 'permission' => new EntityPermission(),
178 'entityType' => $entityType,
179 'inheriting' => false,