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\Entities\Models\Book;
9 use BookStack\Entities\Models\Bookshelf;
10 use BookStack\Entities\Models\Chapter;
11 use BookStack\Entities\Models\Page;
12 use BookStack\Entities\Tools\PermissionsUpdater;
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 return view('pages.permissions', [
34 'data' => new PermissionFormData($page),
39 * Set the permissions for a page.
41 public function updateForPage(Request $request, string $bookSlug, string $pageSlug)
43 $page = Page::getBySlugs($bookSlug, $pageSlug);
44 $this->checkOwnablePermission('restrictions-manage', $page);
46 $this->permissionsUpdater->updateFromPermissionsForm($page, $request);
48 $this->showSuccessNotification(trans('entities.pages_permissions_success'));
50 return redirect($page->getUrl());
54 * Show the Restrictions view for a chapter.
56 public function showForChapter(string $bookSlug, string $chapterSlug)
58 $chapter = Chapter::getBySlugs($bookSlug, $chapterSlug);
59 $this->checkOwnablePermission('restrictions-manage', $chapter);
61 return view('chapters.permissions', [
62 'chapter' => $chapter,
63 'data' => new PermissionFormData($chapter),
68 * Set the restrictions for a chapter.
70 public function updateForChapter(Request $request, string $bookSlug, string $chapterSlug)
72 $chapter = Chapter::getBySlugs($bookSlug, $chapterSlug);
73 $this->checkOwnablePermission('restrictions-manage', $chapter);
75 $this->permissionsUpdater->updateFromPermissionsForm($chapter, $request);
77 $this->showSuccessNotification(trans('entities.chapters_permissions_success'));
79 return redirect($chapter->getUrl());
83 * Show the permissions view for a book.
85 public function showForBook(string $slug)
87 $book = Book::getBySlug($slug);
88 $this->checkOwnablePermission('restrictions-manage', $book);
90 return view('books.permissions', [
92 'data' => new PermissionFormData($book),
97 * Set the restrictions for a book.
99 public function updateForBook(Request $request, string $slug)
101 $book = Book::getBySlug($slug);
102 $this->checkOwnablePermission('restrictions-manage', $book);
104 $this->permissionsUpdater->updateFromPermissionsForm($book, $request);
106 $this->showSuccessNotification(trans('entities.books_permissions_updated'));
108 return redirect($book->getUrl());
112 * Show the permissions view for a shelf.
114 public function showForShelf(string $slug)
116 $shelf = Bookshelf::getBySlug($slug);
117 $this->checkOwnablePermission('restrictions-manage', $shelf);
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 = Bookshelf::getBySlug($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 = Bookshelf::getBySlug($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', fn() => userCan('restrictions-manage-all'));
161 $role = Role::query()->findOrFail($roleId);
163 return view('form.entity-permissions-row', [
165 'permission' => new EntityPermission(),
166 'entityType' => $entityType,