77use BookStack \Activity \Tools \UserEntityWatchOptions ;
88use BookStack \Entities \Models \Book ;
99use BookStack \Entities \Models \Chapter ;
10+ use BookStack \Entities \Models \Page ;
1011use BookStack \Entities \Queries \EntityQueries ;
1112use BookStack \Entities \Queries \PageQueries ;
1213use BookStack \Entities \Repos \PageRepo ;
@@ -95,14 +96,15 @@ public function createAsGuest(Request $request, string $bookSlug, ?string $chapt
9596 }
9697
9798 /**
98- * Show form to continue editing a draft page.
99+ * Show a form to continue editing a draft page.
99100 *
100101 * @throws NotFoundException
101102 */
102103 public function editDraft (Request $ request , string $ bookSlug , int $ pageId )
103104 {
104105 $ draft = $ this ->queries ->findVisibleByIdOrFail ($ pageId );
105106 $ this ->checkOwnablePermission (Permission::PageCreate, $ draft ->getParent ());
107+ $ this ->ensureDraftAccess ($ draft );
106108
107109 $ editorData = new PageEditorData ($ draft , $ this ->entityQueries , $ request ->query ('editor ' , '' ));
108110 $ this ->setPageTitle (trans ('entities.pages_edit_draft ' ));
@@ -124,6 +126,7 @@ public function store(Request $request, string $bookSlug, int $pageId)
124126
125127 $ draftPage = $ this ->queries ->findVisibleByIdOrFail ($ pageId );
126128 $ this ->checkOwnablePermission (Permission::PageCreate, $ draftPage ->getParent ());
129+ $ this ->ensureDraftAccess ($ draftPage );
127130
128131 $ page = $ this ->pageRepo ->publishDraft ($ draftPage , $ request ->all ());
129132
@@ -235,6 +238,7 @@ public function update(Request $request, string $bookSlug, string $pageSlug)
235238 * Save a draft update as a revision.
236239 *
237240 * @throws NotFoundException
241+ * @throws PermissionsException
238242 */
239243 public function saveDraft (Request $ request , int $ pageId )
240244 {
@@ -245,6 +249,10 @@ public function saveDraft(Request $request, int $pageId)
245249 return $ this ->jsonError (trans ('errors.guests_cannot_save_drafts ' ), 500 );
246250 }
247251
252+ if ($ page ->draft ) {
253+ $ this ->ensureDraftAccess ($ page );
254+ }
255+
248256 $ draft = $ this ->pageRepo ->updatePageDraft ($ page , $ request ->only (['name ' , 'html ' , 'markdown ' ]));
249257 $ warnings = (new PageEditActivity ($ page ))->getWarningMessagesForDraft ($ draft );
250258
@@ -294,11 +302,14 @@ public function showDelete(string $bookSlug, string $pageSlug)
294302 * Show the deletion page for the specified page.
295303 *
296304 * @throws NotFoundException
305+ * @throws PermissionsException
297306 */
298307 public function showDeleteDraft (string $ bookSlug , int $ pageId )
299308 {
300309 $ page = $ this ->queries ->findVisibleByIdOrFail ($ pageId );
301310 $ this ->checkOwnablePermission (Permission::PageUpdate, $ page );
311+ $ this ->ensureDraftAccess ($ page );
312+
302313 $ this ->setPageTitle (trans ('entities.pages_delete_draft_named ' , ['pageName ' => $ page ->getShortName ()]));
303314 $ usedAsTemplate =
304315 $ this ->entityQueries ->books ->start ()->where ('default_template_id ' , '= ' , $ page ->id )->count () > 0 ||
@@ -340,7 +351,9 @@ public function destroyDraft(string $bookSlug, int $pageId)
340351 $ page = $ this ->queries ->findVisibleByIdOrFail ($ pageId );
341352 $ book = $ page ->book ;
342353 $ chapter = $ page ->chapter ;
354+
343355 $ this ->checkOwnablePermission (Permission::PageUpdate, $ page );
356+ $ this ->ensureDraftAccess ($ page );
344357
345358 $ this ->pageRepo ->destroy ($ page );
346359
@@ -470,4 +483,14 @@ public function copy(Request $request, Cloner $cloner, string $bookSlug, string
470483
471484 return redirect ($ pageCopy ->getUrl ());
472485 }
486+
487+ /**
488+ * @throws PermissionsException
489+ */
490+ protected function ensureDraftAccess (Page $ draft ): void
491+ {
492+ if (!$ draft ->draft || $ draft ->created_by !== user ()->id ) {
493+ throw new PermissionsException ('This page is already published or does not belong to you. ' );
494+ }
495+ }
473496}
0 commit comments