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