use BookStack\Entities\Tools\PageEditActivity;
use BookStack\Entities\Tools\PageEditorData;
use BookStack\Exceptions\NotFoundException;
+use BookStack\Exceptions\NotifyException;
use BookStack\Exceptions\PermissionsException;
use BookStack\Http\Controller;
use BookStack\References\ReferenceFetcher;
public function edit(Request $request, string $bookSlug, string $pageSlug)
{
$page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
- $this->checkOwnablePermission('page-update', $page);
+ $this->checkOwnablePermission('page-update', $page, $page->getUrl());
$editorData = new PageEditorData($page, $this->entityQueries, $request->query('editor', ''));
if ($editorData->getWarnings()) {
* On a permission error redirect to home and display.
* the error as a notification.
*
- * @return never
+ * @throws NotifyException
*/
- protected function showPermissionError()
+ protected function showPermissionError(string $redirectLocation = '/'): never
{
$message = request()->wantsJson() ? trans('errors.permissionJson') : trans('errors.permission');
- throw new NotifyException($message, '/', 403);
+ throw new NotifyException($message, $redirectLocation, 403);
}
/**
/**
* Check the current user's permissions against an ownable item otherwise throw an exception.
*/
- protected function checkOwnablePermission(string $permission, Model $ownable): void
+ protected function checkOwnablePermission(string $permission, Model $ownable, string $redirectLocation = '/'): void
{
if (!userCan($permission, $ownable)) {
- $this->showPermissionError();
+ $this->showPermissionError($redirectLocation);
}
}
$resp = $this->get('/');
$this->withHtml($resp)->assertElementContains('#recently-updated-pages', $page->name);
}
+
+ public function test_page_edit_without_update_permissions_but_with_view_redirects_to_page()
+ {
+ $page = $this->entities->page();
+
+ $resp = $this->asViewer()->get($page->getUrl('/edit'));
+ $resp->assertRedirect($page->getUrl());
+
+ $resp->assertSessionHas('error', 'You do not have permission to access the requested page.');
+ }
}