Prevented custom homepage being deleted

Fixes #546
This commit is contained in:
Dan Brown 2017-10-15 19:14:46 +01:00
parent a988438946
commit db51cee2d8
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
4 changed files with 35 additions and 2 deletions

View File

@ -324,9 +324,10 @@ class PageController extends Controller
$page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
$book = $page->book;
$this->checkOwnablePermission('page-delete', $page);
$this->entityRepo->destroyPage($page);
Activity::addMessage('page_delete', $book->id, $page->name);
session()->flash('success', trans('entities.pages_delete_success'));
$this->entityRepo->destroyPage($page);
return redirect($book->getUrl());
}

View File

@ -4,6 +4,7 @@ use BookStack\Book;
use BookStack\Chapter;
use BookStack\Entity;
use BookStack\Exceptions\NotFoundException;
use BookStack\Exceptions\NotifyException;
use BookStack\Page;
use BookStack\PageRevision;
use BookStack\Services\AttachmentService;
@ -1073,6 +1074,7 @@ class EntityRepo
/**
* Destroy a given page along with its dependencies.
* @param Page $page
* @throws NotifyException
*/
public function destroyPage(Page $page)
{
@ -1084,6 +1086,12 @@ class EntityRepo
$this->permissionService->deleteJointPermissionsForEntity($page);
$this->searchService->deleteEntityTerms($page);
// Check if set as custom homepage
$customHome = setting('app-homepage', '0:');
if (intval($page->id) === intval(explode(':', $customHome)[0])) {
throw new NotifyException(trans('errors.page_custom_home_deletion'), $page->getUrl());
}
// Delete Attached Files
$attachmentService = app(AttachmentService::class);
foreach ($page->attachments as $attachment) {

View File

@ -41,6 +41,7 @@ return [
// Pages
'page_draft_autosave_fail' => 'Failed to save draft. Ensure you have internet connection before saving this page',
'page_custom_home_deletion' => 'Cannot delete a page while it is set as a homepage',
// Entities
'entity_not_found' => 'Entity not found',

View File

@ -16,7 +16,8 @@ class HomepageTest extends TestCase
$homeVisit->assertSee('Recent Activity');
}
public function test_custom_homepage() {
public function test_custom_homepage()
{
$this->asEditor();
$name = 'My custom homepage';
$content = 'This is the body content of my custom homepage.';
@ -30,4 +31,26 @@ class HomepageTest extends TestCase
$homeVisit->assertSee('Recently Updated Pages');
$homeVisit->assertSee('Recent Activity');
}
public function test_delete_custom_homepage()
{
$this->asEditor();
$name = 'My custom homepage';
$content = 'This is the body content of my custom homepage.';
$customPage = $this->newPage(['name' => $name, 'html' => $content]);
$this->setSettings(['app-homepage' => $customPage->id]);
$homeVisit = $this->get('/');
$homeVisit->assertSee($name);
$pageDeleteReq = $this->delete($customPage->getUrl());
$pageDeleteReq->assertStatus(302);
$pageDeleteReq->assertRedirect($customPage->getUrl());
$pageDeleteReq->assertSessionHas('error');
$pageDeleteReq->assertSessionMissing('success');
$homeVisit = $this->get('/');
$homeVisit->assertSee($name);
$homeVisit->assertStatus(200);
}
}