]> BookStack Code Mirror - bookstack/blob - app/Permissions/PermissionsController.php
Queries: Updated all app book static query uses
[bookstack] / app / Permissions / PermissionsController.php
1 <?php
2
3 namespace BookStack\Permissions;
4
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;
11
12 class PermissionsController extends Controller
13 {
14     public function __construct(
15         protected PermissionsUpdater $permissionsUpdater,
16         protected EntityQueries $queries,
17     ) {
18     }
19
20     /**
21      * Show the permissions view for a page.
22      */
23     public function showForPage(string $bookSlug, string $pageSlug)
24     {
25         $page = $this->queries->pages->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
26         $this->checkOwnablePermission('restrictions-manage', $page);
27
28         $this->setPageTitle(trans('entities.pages_permissions'));
29         return view('pages.permissions', [
30             'page' => $page,
31             'data' => new PermissionFormData($page),
32         ]);
33     }
34
35     /**
36      * Set the permissions for a page.
37      */
38     public function updateForPage(Request $request, string $bookSlug, string $pageSlug)
39     {
40         $page = $this->queries->pages->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
41         $this->checkOwnablePermission('restrictions-manage', $page);
42
43         $this->permissionsUpdater->updateFromPermissionsForm($page, $request);
44
45         $this->showSuccessNotification(trans('entities.pages_permissions_success'));
46
47         return redirect($page->getUrl());
48     }
49
50     /**
51      * Show the permissions view for a chapter.
52      */
53     public function showForChapter(string $bookSlug, string $chapterSlug)
54     {
55         $chapter = $this->queries->chapters->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
56         $this->checkOwnablePermission('restrictions-manage', $chapter);
57
58         $this->setPageTitle(trans('entities.chapters_permissions'));
59         return view('chapters.permissions', [
60             'chapter' => $chapter,
61             'data' => new PermissionFormData($chapter),
62         ]);
63     }
64
65     /**
66      * Set the permissions for a chapter.
67      */
68     public function updateForChapter(Request $request, string $bookSlug, string $chapterSlug)
69     {
70         $chapter = $this->queries->chapters->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
71         $this->checkOwnablePermission('restrictions-manage', $chapter);
72
73         $this->permissionsUpdater->updateFromPermissionsForm($chapter, $request);
74
75         $this->showSuccessNotification(trans('entities.chapters_permissions_success'));
76
77         return redirect($chapter->getUrl());
78     }
79
80     /**
81      * Show the permissions view for a book.
82      */
83     public function showForBook(string $slug)
84     {
85         $book = $this->queries->books->findVisibleBySlugOrFail($slug);
86         $this->checkOwnablePermission('restrictions-manage', $book);
87
88         $this->setPageTitle(trans('entities.books_permissions'));
89         return view('books.permissions', [
90             'book' => $book,
91             'data' => new PermissionFormData($book),
92         ]);
93     }
94
95     /**
96      * Set the permissions for a book.
97      */
98     public function updateForBook(Request $request, string $slug)
99     {
100         $book = $this->queries->books->findVisibleBySlugOrFail($slug);
101         $this->checkOwnablePermission('restrictions-manage', $book);
102
103         $this->permissionsUpdater->updateFromPermissionsForm($book, $request);
104
105         $this->showSuccessNotification(trans('entities.books_permissions_updated'));
106
107         return redirect($book->getUrl());
108     }
109
110     /**
111      * Show the permissions view for a shelf.
112      */
113     public function showForShelf(string $slug)
114     {
115         $shelf = $this->queries->shelves->findVisibleBySlugOrFail($slug);
116         $this->checkOwnablePermission('restrictions-manage', $shelf);
117
118         $this->setPageTitle(trans('entities.shelves_permissions'));
119         return view('shelves.permissions', [
120             'shelf' => $shelf,
121             'data' => new PermissionFormData($shelf),
122         ]);
123     }
124
125     /**
126      * Set the permissions for a shelf.
127      */
128     public function updateForShelf(Request $request, string $slug)
129     {
130         $shelf = $this->queries->shelves->findVisibleBySlugOrFail($slug);
131         $this->checkOwnablePermission('restrictions-manage', $shelf);
132
133         $this->permissionsUpdater->updateFromPermissionsForm($shelf, $request);
134
135         $this->showSuccessNotification(trans('entities.shelves_permissions_updated'));
136
137         return redirect($shelf->getUrl());
138     }
139
140     /**
141      * Copy the permissions of a bookshelf to the child books.
142      */
143     public function copyShelfPermissionsToBooks(string $slug)
144     {
145         $shelf = $this->queries->shelves->findVisibleBySlugOrFail($slug);
146         $this->checkOwnablePermission('restrictions-manage', $shelf);
147
148         $updateCount = $this->permissionsUpdater->updateBookPermissionsFromShelf($shelf);
149         $this->showSuccessNotification(trans('entities.shelves_copy_permission_success', ['count' => $updateCount]));
150
151         return redirect($shelf->getUrl());
152     }
153
154     /**
155      * Get an empty entity permissions form row for the given role.
156      */
157     public function formRowForRole(string $entityType, string $roleId)
158     {
159         $this->checkPermissionOr('restrictions-manage-all', fn() => userCan('restrictions-manage-own'));
160
161         $role = Role::query()->findOrFail($roleId);
162
163         return view('form.entity-permissions-row', [
164             'role' => $role,
165             'permission' => new EntityPermission(),
166             'entityType' => $entityType,
167             'inheriting' => false,
168         ]);
169     }
170 }