Skip to content

Commit 454b152

Browse files
committed
Pages: Redirect user to view if they can't edit
For #5568
1 parent b29fe5c commit 454b152

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

app/Entities/Controllers/PageController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use BookStack\Entities\Tools\PageEditActivity;
1818
use BookStack\Entities\Tools\PageEditorData;
1919
use BookStack\Exceptions\NotFoundException;
20+
use BookStack\Exceptions\NotifyException;
2021
use BookStack\Exceptions\PermissionsException;
2122
use BookStack\Http\Controller;
2223
use BookStack\References\ReferenceFetcher;
@@ -196,7 +197,7 @@ public function getPageAjax(int $pageId)
196197
public function edit(Request $request, string $bookSlug, string $pageSlug)
197198
{
198199
$page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
199-
$this->checkOwnablePermission('page-update', $page);
200+
$this->checkOwnablePermission('page-update', $page, $page->getUrl());
200201

201202
$editorData = new PageEditorData($page, $this->entityQueries, $request->query('editor', ''));
202203
if ($editorData->getWarnings()) {

app/Http/Controller.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ public function setPageTitle(string $title)
4949
* On a permission error redirect to home and display.
5050
* the error as a notification.
5151
*
52-
* @return never
52+
* @throws NotifyException
5353
*/
54-
protected function showPermissionError()
54+
protected function showPermissionError(string $redirectLocation = '/'): never
5555
{
5656
$message = request()->wantsJson() ? trans('errors.permissionJson') : trans('errors.permission');
5757

58-
throw new NotifyException($message, '/', 403);
58+
throw new NotifyException($message, $redirectLocation, 403);
5959
}
6060

6161
/**
@@ -81,10 +81,10 @@ protected function preventGuestAccess(): void
8181
/**
8282
* Check the current user's permissions against an ownable item otherwise throw an exception.
8383
*/
84-
protected function checkOwnablePermission(string $permission, Model $ownable): void
84+
protected function checkOwnablePermission(string $permission, Model $ownable, string $redirectLocation = '/'): void
8585
{
8686
if (!userCan($permission, $ownable)) {
87-
$this->showPermissionError();
87+
$this->showPermissionError($redirectLocation);
8888
}
8989
}
9090

tests/Entity/PageTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,4 +356,14 @@ public function test_recently_updated_pages_on_home()
356356
$resp = $this->get('/');
357357
$this->withHtml($resp)->assertElementContains('#recently-updated-pages', $page->name);
358358
}
359+
360+
public function test_page_edit_without_update_permissions_but_with_view_redirects_to_page()
361+
{
362+
$page = $this->entities->page();
363+
364+
$resp = $this->asViewer()->get($page->getUrl('/edit'));
365+
$resp->assertRedirect($page->getUrl());
366+
367+
$resp->assertSessionHas('error', 'You do not have permission to access the requested page.');
368+
}
359369
}

0 commit comments

Comments
 (0)