]> BookStack Code Mirror - bookstack/blob - app/Entities/Controllers/ChapterController.php
Perms: Added transactions around permission effecting actions
[bookstack] / app / Entities / Controllers / ChapterController.php
1 <?php
2
3 namespace BookStack\Entities\Controllers;
4
5 use BookStack\Activity\Models\View;
6 use BookStack\Activity\Tools\UserEntityWatchOptions;
7 use BookStack\Entities\Models\Book;
8 use BookStack\Entities\Queries\ChapterQueries;
9 use BookStack\Entities\Queries\EntityQueries;
10 use BookStack\Entities\Repos\ChapterRepo;
11 use BookStack\Entities\Tools\BookContents;
12 use BookStack\Entities\Tools\Cloner;
13 use BookStack\Entities\Tools\HierarchyTransformer;
14 use BookStack\Entities\Tools\NextPreviousContentLocator;
15 use BookStack\Exceptions\MoveOperationException;
16 use BookStack\Exceptions\NotFoundException;
17 use BookStack\Exceptions\NotifyException;
18 use BookStack\Exceptions\PermissionsException;
19 use BookStack\Http\Controller;
20 use BookStack\References\ReferenceFetcher;
21 use BookStack\Util\DatabaseTransaction;
22 use Illuminate\Http\Request;
23 use Illuminate\Validation\ValidationException;
24 use Throwable;
25
26 class ChapterController extends Controller
27 {
28     public function __construct(
29         protected ChapterRepo $chapterRepo,
30         protected ChapterQueries $queries,
31         protected EntityQueries $entityQueries,
32         protected ReferenceFetcher $referenceFetcher,
33     ) {
34     }
35
36     /**
37      * Show the form for creating a new chapter.
38      */
39     public function create(string $bookSlug)
40     {
41         $book = $this->entityQueries->books->findVisibleBySlugOrFail($bookSlug);
42         $this->checkOwnablePermission('chapter-create', $book);
43
44         $this->setPageTitle(trans('entities.chapters_create'));
45
46         return view('chapters.create', [
47             'book' => $book,
48             'current' => $book,
49         ]);
50     }
51
52     /**
53      * Store a newly created chapter in storage.
54      *
55      * @throws ValidationException
56      */
57     public function store(Request $request, string $bookSlug)
58     {
59         $validated = $this->validate($request, [
60             'name'                => ['required', 'string', 'max:255'],
61             'description_html'    => ['string', 'max:2000'],
62             'tags'                => ['array'],
63             'default_template_id' => ['nullable', 'integer'],
64         ]);
65
66         $book = $this->entityQueries->books->findVisibleBySlugOrFail($bookSlug);
67         $this->checkOwnablePermission('chapter-create', $book);
68
69         $chapter = $this->chapterRepo->create($validated, $book);
70
71         return redirect($chapter->getUrl());
72     }
73
74     /**
75      * Display the specified chapter.
76      */
77     public function show(string $bookSlug, string $chapterSlug)
78     {
79         $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
80         $this->checkOwnablePermission('chapter-view', $chapter);
81
82         $sidebarTree = (new BookContents($chapter->book))->getTree();
83         $pages = $this->entityQueries->pages->visibleForChapterList($chapter->id)->get();
84
85         $nextPreviousLocator = new NextPreviousContentLocator($chapter, $sidebarTree);
86         View::incrementFor($chapter);
87
88         $this->setPageTitle($chapter->getShortName());
89
90         return view('chapters.show', [
91             'book'           => $chapter->book,
92             'chapter'        => $chapter,
93             'current'        => $chapter,
94             'sidebarTree'    => $sidebarTree,
95             'watchOptions'   => new UserEntityWatchOptions(user(), $chapter),
96             'pages'          => $pages,
97             'next'           => $nextPreviousLocator->getNext(),
98             'previous'       => $nextPreviousLocator->getPrevious(),
99             'referenceCount' => $this->referenceFetcher->getReferenceCountToEntity($chapter),
100         ]);
101     }
102
103     /**
104      * Show the form for editing the specified chapter.
105      */
106     public function edit(string $bookSlug, string $chapterSlug)
107     {
108         $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
109         $this->checkOwnablePermission('chapter-update', $chapter);
110
111         $this->setPageTitle(trans('entities.chapters_edit_named', ['chapterName' => $chapter->getShortName()]));
112
113         return view('chapters.edit', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
114     }
115
116     /**
117      * Update the specified chapter in storage.
118      *
119      * @throws NotFoundException
120      */
121     public function update(Request $request, string $bookSlug, string $chapterSlug)
122     {
123         $validated = $this->validate($request, [
124             'name'                => ['required', 'string', 'max:255'],
125             'description_html'    => ['string', 'max:2000'],
126             'tags'                => ['array'],
127             'default_template_id' => ['nullable', 'integer'],
128         ]);
129
130         $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
131         $this->checkOwnablePermission('chapter-update', $chapter);
132
133         $this->chapterRepo->update($chapter, $validated);
134
135         return redirect($chapter->getUrl());
136     }
137
138     /**
139      * Shows the page to confirm deletion of this chapter.
140      *
141      * @throws NotFoundException
142      */
143     public function showDelete(string $bookSlug, string $chapterSlug)
144     {
145         $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
146         $this->checkOwnablePermission('chapter-delete', $chapter);
147
148         $this->setPageTitle(trans('entities.chapters_delete_named', ['chapterName' => $chapter->getShortName()]));
149
150         return view('chapters.delete', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
151     }
152
153     /**
154      * Remove the specified chapter from storage.
155      *
156      * @throws NotFoundException
157      * @throws Throwable
158      */
159     public function destroy(string $bookSlug, string $chapterSlug)
160     {
161         $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
162         $this->checkOwnablePermission('chapter-delete', $chapter);
163
164         $this->chapterRepo->destroy($chapter);
165
166         return redirect($chapter->book->getUrl());
167     }
168
169     /**
170      * Show the page for moving a chapter.
171      *
172      * @throws NotFoundException
173      */
174     public function showMove(string $bookSlug, string $chapterSlug)
175     {
176         $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
177         $this->setPageTitle(trans('entities.chapters_move_named', ['chapterName' => $chapter->getShortName()]));
178         $this->checkOwnablePermission('chapter-update', $chapter);
179         $this->checkOwnablePermission('chapter-delete', $chapter);
180
181         return view('chapters.move', [
182             'chapter' => $chapter,
183             'book'    => $chapter->book,
184         ]);
185     }
186
187     /**
188      * Perform the move action for a chapter.
189      *
190      * @throws NotFoundException|NotifyException
191      */
192     public function move(Request $request, string $bookSlug, string $chapterSlug)
193     {
194         $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
195         $this->checkOwnablePermission('chapter-update', $chapter);
196         $this->checkOwnablePermission('chapter-delete', $chapter);
197
198         $entitySelection = $request->get('entity_selection', null);
199         if ($entitySelection === null || $entitySelection === '') {
200             return redirect($chapter->getUrl());
201         }
202
203         try {
204             $this->chapterRepo->move($chapter, $entitySelection);
205         } catch (PermissionsException $exception) {
206             $this->showPermissionError();
207         } catch (MoveOperationException $exception) {
208             $this->showErrorNotification(trans('errors.selected_book_not_found'));
209
210             return redirect($chapter->getUrl('/move'));
211         }
212
213         return redirect($chapter->getUrl());
214     }
215
216     /**
217      * Show the view to copy a chapter.
218      *
219      * @throws NotFoundException
220      */
221     public function showCopy(string $bookSlug, string $chapterSlug)
222     {
223         $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
224         $this->checkOwnablePermission('chapter-view', $chapter);
225
226         session()->flashInput(['name' => $chapter->name]);
227
228         return view('chapters.copy', [
229             'book'    => $chapter->book,
230             'chapter' => $chapter,
231         ]);
232     }
233
234     /**
235      * Create a copy of a chapter within the requested target destination.
236      *
237      * @throws NotFoundException
238      * @throws Throwable
239      */
240     public function copy(Request $request, Cloner $cloner, string $bookSlug, string $chapterSlug)
241     {
242         $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
243         $this->checkOwnablePermission('chapter-view', $chapter);
244
245         $entitySelection = $request->get('entity_selection') ?: null;
246         $newParentBook = $entitySelection ? $this->entityQueries->findVisibleByStringIdentifier($entitySelection) : $chapter->getParent();
247
248         if (!$newParentBook instanceof Book) {
249             $this->showErrorNotification(trans('errors.selected_book_not_found'));
250
251             return redirect($chapter->getUrl('/copy'));
252         }
253
254         $this->checkOwnablePermission('chapter-create', $newParentBook);
255
256         $newName = $request->get('name') ?: $chapter->name;
257         $chapterCopy = $cloner->cloneChapter($chapter, $newParentBook, $newName);
258         $this->showSuccessNotification(trans('entities.chapters_copy_success'));
259
260         return redirect($chapterCopy->getUrl());
261     }
262
263     /**
264      * Convert the chapter to a book.
265      */
266     public function convertToBook(HierarchyTransformer $transformer, string $bookSlug, string $chapterSlug)
267     {
268         $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
269         $this->checkOwnablePermission('chapter-update', $chapter);
270         $this->checkOwnablePermission('chapter-delete', $chapter);
271         $this->checkPermission('book-create-all');
272
273         $book = (new DatabaseTransaction(function () use ($chapter, $transformer) {
274             return $transformer->transformChapterToBook($chapter);
275         }))->run();
276
277         return redirect($book->getUrl());
278     }
279 }