3 namespace BookStack\Permissions;
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Bookshelf;
7 use BookStack\Entities\Models\Chapter;
8 use BookStack\Entities\Models\Page;
9 use BookStack\Entities\Tools\PermissionsUpdater;
10 use BookStack\Http\Controllers\Controller;
11 use BookStack\Permissions\Models\EntityPermission;
12 use BookStack\Users\Models\Role;
13 use Illuminate\Http\Request;
15 class PermissionsController extends Controller
17 protected PermissionsUpdater $permissionsUpdater;
19 public function __construct(PermissionsUpdater $permissionsUpdater)
21 $this->permissionsUpdater = $permissionsUpdater;
25 * Show the Permissions view for a page.
27 public function showForPage(string $bookSlug, string $pageSlug)
29 $page = Page::getBySlugs($bookSlug, $pageSlug);
30 $this->checkOwnablePermission('restrictions-manage', $page);
32 $this->setPageTitle(trans('entities.pages_permissions'));
33 return view('pages.permissions', [
35 'data' => new PermissionFormData($page),
40 * Set the permissions for a page.
42 public function updateForPage(Request $request, string $bookSlug, string $pageSlug)
44 $page = Page::getBySlugs($bookSlug, $pageSlug);
45 $this->checkOwnablePermission('restrictions-manage', $page);
47 $this->permissionsUpdater->updateFromPermissionsForm($page, $request);
49 $this->showSuccessNotification(trans('entities.pages_permissions_success'));
51 return redirect($page->getUrl());
55 * Show the Restrictions view for a chapter.
57 public function showForChapter(string $bookSlug, string $chapterSlug)
59 $chapter = Chapter::getBySlugs($bookSlug, $chapterSlug);
60 $this->checkOwnablePermission('restrictions-manage', $chapter);
62 $this->setPageTitle(trans('entities.chapters_permissions'));
63 return view('chapters.permissions', [
64 'chapter' => $chapter,
65 'data' => new PermissionFormData($chapter),
70 * Set the restrictions for a chapter.
72 public function updateForChapter(Request $request, string $bookSlug, string $chapterSlug)
74 $chapter = Chapter::getBySlugs($bookSlug, $chapterSlug);
75 $this->checkOwnablePermission('restrictions-manage', $chapter);
77 $this->permissionsUpdater->updateFromPermissionsForm($chapter, $request);
79 $this->showSuccessNotification(trans('entities.chapters_permissions_success'));
81 return redirect($chapter->getUrl());
85 * Show the permissions view for a book.
87 public function showForBook(string $slug)
89 $book = Book::getBySlug($slug);
90 $this->checkOwnablePermission('restrictions-manage', $book);
92 $this->setPageTitle(trans('entities.books_permissions'));
93 return view('books.permissions', [
95 'data' => new PermissionFormData($book),
100 * Set the restrictions for a book.
102 public function updateForBook(Request $request, string $slug)
104 $book = Book::getBySlug($slug);
105 $this->checkOwnablePermission('restrictions-manage', $book);
107 $this->permissionsUpdater->updateFromPermissionsForm($book, $request);
109 $this->showSuccessNotification(trans('entities.books_permissions_updated'));
111 return redirect($book->getUrl());
115 * Show the permissions view for a shelf.
117 public function showForShelf(string $slug)
119 $shelf = Bookshelf::getBySlug($slug);
120 $this->checkOwnablePermission('restrictions-manage', $shelf);
122 $this->setPageTitle(trans('entities.shelves_permissions'));
123 return view('shelves.permissions', [
125 'data' => new PermissionFormData($shelf),
130 * Set the permissions for a shelf.
132 public function updateForShelf(Request $request, string $slug)
134 $shelf = Bookshelf::getBySlug($slug);
135 $this->checkOwnablePermission('restrictions-manage', $shelf);
137 $this->permissionsUpdater->updateFromPermissionsForm($shelf, $request);
139 $this->showSuccessNotification(trans('entities.shelves_permissions_updated'));
141 return redirect($shelf->getUrl());
145 * Copy the permissions of a bookshelf to the child books.
147 public function copyShelfPermissionsToBooks(string $slug)
149 $shelf = Bookshelf::getBySlug($slug);
150 $this->checkOwnablePermission('restrictions-manage', $shelf);
152 $updateCount = $this->permissionsUpdater->updateBookPermissionsFromShelf($shelf);
153 $this->showSuccessNotification(trans('entities.shelves_copy_permission_success', ['count' => $updateCount]));
155 return redirect($shelf->getUrl());
159 * Get an empty entity permissions form row for the given role.
161 public function formRowForRole(string $entityType, string $roleId)
163 $this->checkPermissionOr('restrictions-manage-all', fn() => userCan('restrictions-manage-own'));
165 $role = Role::query()->findOrFail($roleId);
167 return view('form.entity-permissions-row', [
169 'permission' => new EntityPermission(),
170 'entityType' => $entityType,
171 'inheriting' => false,