3 namespace BookStack\Http\Controllers;
5 use BookStack\Auth\Permissions\EntityPermission;
6 use BookStack\Auth\Permissions\PermissionFormData;
7 use BookStack\Auth\Role;
8 use BookStack\Auth\User;
9 use BookStack\Entities\Models\Book;
10 use BookStack\Entities\Models\Bookshelf;
11 use BookStack\Entities\Models\Chapter;
12 use BookStack\Entities\Models\Page;
13 use BookStack\Entities\Tools\PermissionsUpdater;
14 use Illuminate\Http\Request;
16 class PermissionsController extends Controller
18 protected PermissionsUpdater $permissionsUpdater;
20 public function __construct(PermissionsUpdater $permissionsUpdater)
22 $this->permissionsUpdater = $permissionsUpdater;
26 * Show the Permissions view for a page.
28 public function showForPage(string $bookSlug, string $pageSlug)
30 $page = Page::getBySlugs($bookSlug, $pageSlug);
31 $this->checkOwnablePermission('restrictions-manage', $page);
33 $this->setPageTitle(trans('entities.pages_permissions'));
34 return view('pages.permissions', [
36 'data' => new PermissionFormData($page),
41 * Set the permissions for a page.
43 public function updateForPage(Request $request, string $bookSlug, string $pageSlug)
45 $page = Page::getBySlugs($bookSlug, $pageSlug);
46 $this->checkOwnablePermission('restrictions-manage', $page);
48 $this->permissionsUpdater->updateFromPermissionsForm($page, $request);
50 $this->showSuccessNotification(trans('entities.pages_permissions_success'));
52 return redirect($page->getUrl());
56 * Show the Restrictions view for a chapter.
58 public function showForChapter(string $bookSlug, string $chapterSlug)
60 $chapter = Chapter::getBySlugs($bookSlug, $chapterSlug);
61 $this->checkOwnablePermission('restrictions-manage', $chapter);
63 $this->setPageTitle(trans('entities.chapters_permissions'));
64 return view('chapters.permissions', [
65 'chapter' => $chapter,
66 'data' => new PermissionFormData($chapter),
71 * Set the restrictions for a chapter.
73 public function updateForChapter(Request $request, string $bookSlug, string $chapterSlug)
75 $chapter = Chapter::getBySlugs($bookSlug, $chapterSlug);
76 $this->checkOwnablePermission('restrictions-manage', $chapter);
78 $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 = Book::getBySlug($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 restrictions for a book.
103 public function updateForBook(Request $request, string $slug)
105 $book = Book::getBySlug($slug);
106 $this->checkOwnablePermission('restrictions-manage', $book);
108 $this->permissionsUpdater->updateFromPermissionsForm($book, $request);
110 $this->showSuccessNotification(trans('entities.books_permissions_updated'));
112 return redirect($book->getUrl());
116 * Show the permissions view for a shelf.
118 public function showForShelf(string $slug)
120 $shelf = Bookshelf::getBySlug($slug);
121 $this->checkOwnablePermission('restrictions-manage', $shelf);
123 $this->setPageTitle(trans('entities.shelves_permissions'));
124 return view('shelves.permissions', [
126 'data' => new PermissionFormData($shelf),
131 * Set the permissions for a shelf.
133 public function updateForShelf(Request $request, string $slug)
135 $shelf = Bookshelf::getBySlug($slug);
136 $this->checkOwnablePermission('restrictions-manage', $shelf);
138 $this->permissionsUpdater->updateFromPermissionsForm($shelf, $request);
140 $this->showSuccessNotification(trans('entities.shelves_permissions_updated'));
142 return redirect($shelf->getUrl());
146 * Copy the permissions of a bookshelf to the child books.
148 public function copyShelfPermissionsToBooks(string $slug)
150 $shelf = Bookshelf::getBySlug($slug);
151 $this->checkOwnablePermission('restrictions-manage', $shelf);
153 $updateCount = $this->permissionsUpdater->updateBookPermissionsFromShelf($shelf);
154 $this->showSuccessNotification(trans('entities.shelves_copy_permission_success', ['count' => $updateCount]));
156 return redirect($shelf->getUrl());
160 * Get an empty entity permissions form row for the given role.
162 public function formRowForRole(string $entityType, string $roleId)
164 $this->checkPermissionOr('restrictions-manage-all', fn() => userCan('restrictions-manage-own'));
166 /** @var Role $role */
167 $role = Role::query()->findOrFail($roleId);
169 return view('form.entity-permissions-row', [
170 'modelType' => 'role',
171 'modelId' => $role->id,
172 'modelName' => $role->display_name,
173 'modelDescription' => $role->description,
174 'permission' => new EntityPermission(),
175 'entityType' => $entityType,
176 'inheriting' => false,
181 * Get an empty entity permissions form row for the given user.
183 public function formRowForUser(string $entityType, string $userId)
185 $this->checkPermissionOr('restrictions-manage-all', fn() => userCan('restrictions-manage-own'));
187 /** @var User $user */
188 $user = User::query()->findOrFail($userId);
190 return view('form.entity-permissions-row', [
191 'modelType' => 'user',
192 'modelId' => $user->id,
193 'modelName' => $user->name,
194 'modelDescription' => '',
195 'permission' => new EntityPermission(),
196 'entityType' => $entityType,
197 'inheriting' => false,