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