]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/PermissionsController.php
Centralised handling of permission form data to own class
[bookstack] / app / Http / Controllers / PermissionsController.php
1 <?php
2
3 namespace BookStack\Http\Controllers;
4
5 use BookStack\Auth\Permissions\PermissionFormData;
6 use BookStack\Entities\Models\Book;
7 use BookStack\Entities\Models\Bookshelf;
8 use BookStack\Entities\Models\Chapter;
9 use BookStack\Entities\Models\Page;
10 use BookStack\Entities\Tools\PermissionsUpdater;
11 use Illuminate\Http\Request;
12
13 class PermissionsController extends Controller
14 {
15     protected PermissionsUpdater $permissionsUpdater;
16
17     public function __construct(PermissionsUpdater $permissionsUpdater)
18     {
19         $this->permissionsUpdater = $permissionsUpdater;
20     }
21
22     /**
23      * Show the Permissions view for a page.
24      */
25     public function showForPage(string $bookSlug, string $pageSlug)
26     {
27         $page = Page::getBySlugs($bookSlug, $pageSlug);
28         $this->checkOwnablePermission('restrictions-manage', $page);
29
30         return view('pages.permissions', [
31             'page' => $page,
32             'data' => new PermissionFormData($page),
33         ]);
34     }
35
36     /**
37      * Set the permissions for a page.
38      */
39     public function updateForPage(Request $request, string $bookSlug, string $pageSlug)
40     {
41         $page = Page::getBySlugs($bookSlug, $pageSlug);
42         $this->checkOwnablePermission('restrictions-manage', $page);
43
44         $this->permissionsUpdater->updateFromPermissionsForm($page, $request);
45
46         $this->showSuccessNotification(trans('entities.pages_permissions_success'));
47
48         return redirect($page->getUrl());
49     }
50
51     /**
52      * Show the Restrictions view for a chapter.
53      */
54     public function showForChapter(string $bookSlug, string $chapterSlug)
55     {
56         $chapter = Chapter::getBySlugs($bookSlug, $chapterSlug);
57         $this->checkOwnablePermission('restrictions-manage', $chapter);
58
59         return view('chapters.permissions', [
60             'chapter' => $chapter,
61             'data' => new PermissionFormData($chapter),
62         ]);
63     }
64
65     /**
66      * Set the restrictions for a chapter.
67      */
68     public function updateForChapter(Request $request, string $bookSlug, string $chapterSlug)
69     {
70         $chapter = Chapter::getBySlugs($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 = Book::getBySlug($slug);
86         $this->checkOwnablePermission('restrictions-manage', $book);
87
88         return view('books.permissions', [
89             'book' => $book,
90             'data' => new PermissionFormData($book),
91         ]);
92     }
93
94     /**
95      * Set the restrictions for a book.
96      */
97     public function updateForBook(Request $request, string $slug)
98     {
99         $book = Book::getBySlug($slug);
100         $this->checkOwnablePermission('restrictions-manage', $book);
101
102         $this->permissionsUpdater->updateFromPermissionsForm($book, $request);
103
104         $this->showSuccessNotification(trans('entities.books_permissions_updated'));
105
106         return redirect($book->getUrl());
107     }
108
109     /**
110      * Show the permissions view for a shelf.
111      */
112     public function showForShelf(string $slug)
113     {
114         $shelf = Bookshelf::getBySlug($slug);
115         $this->checkOwnablePermission('restrictions-manage', $shelf);
116
117         return view('shelves.permissions', [
118             'shelf' => $shelf,
119             'data' => new PermissionFormData($shelf),
120         ]);
121     }
122
123     /**
124      * Set the permissions for a shelf.
125      */
126     public function updateForShelf(Request $request, string $slug)
127     {
128         $shelf = Bookshelf::getBySlug($slug);
129         $this->checkOwnablePermission('restrictions-manage', $shelf);
130
131         $this->permissionsUpdater->updateFromPermissionsForm($shelf, $request);
132
133         $this->showSuccessNotification(trans('entities.shelves_permissions_updated'));
134
135         return redirect($shelf->getUrl());
136     }
137
138     /**
139      * Copy the permissions of a bookshelf to the child books.
140      */
141     public function copyShelfPermissionsToBooks(string $slug)
142     {
143         $shelf = Bookshelf::getBySlug($slug);
144         $this->checkOwnablePermission('restrictions-manage', $shelf);
145
146         $updateCount = $this->permissionsUpdater->updateBookPermissionsFromShelf($shelf);
147         $this->showSuccessNotification(trans('entities.shelves_copy_permission_success', ['count' => $updateCount]));
148
149         return redirect($shelf->getUrl());
150     }
151 }