use BookStack\Entities\Tools\TrashCan;
use BookStack\Exceptions\MoveOperationException;
use BookStack\Exceptions\NotFoundException;
+use BookStack\Exceptions\PermissionsException;
use BookStack\Facades\Activity;
use Exception;
* 'book:<id>' (book:5).
*
* @throws MoveOperationException
+ * @throws PermissionsException
*/
public function move(Chapter $chapter, string $parentIdentifier): Book
{
- /** @var Book $parent */
$parent = $this->findParentByIdentifier($parentIdentifier);
if (is_null($parent)) {
throw new MoveOperationException('Book to move chapter into not found');
}
- // TODO - Check create permissions for new parent?
+ if (!userCan('chapter-create', $parent)) {
+ throw new PermissionsException('User does not have permission to create a chapter within the chosen book');
+ }
$chapter->changeBook($parent->id);
$chapter->rebuildPermissions();
public function move(Page $page, string $parentIdentifier): Entity
{
$parent = $this->findParentByIdentifier($parentIdentifier);
- if ($parent === null) {
+ if (is_null($parent)) {
throw new MoveOperationException('Book or chapter to move page into not found');
}
use BookStack\Entities\Tools\PermissionsUpdater;
use BookStack\Exceptions\MoveOperationException;
use BookStack\Exceptions\NotFoundException;
+use BookStack\Exceptions\PermissionsException;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Throwable;
return redirect($chapter->getUrl());
}
- // TODO - Check permissions against pages
-
try {
$newBook = $this->chapterRepo->move($chapter, $entitySelection);
+ } catch (PermissionsException $exception) {
+ $this->showPermissionError();
} catch (MoveOperationException $exception) {
$this->showErrorNotification(trans('errors.selected_book_not_found'));
try {
$parent = $this->pageRepo->move($page, $entitySelection);
+ } catch (PermissionsException $exception) {
+ $this->showPermissionError();
} catch (Exception $exception) {
- if ($exception instanceof PermissionsException) {
- $this->showPermissionError();
- }
-
$this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
return redirect()->back();
$this->assertTrue($chapter->book->id == $newBook->id, 'Page book is now the new book');
}
+ public function test_chapter_move_requires_create_permissions_in_new_book()
+ {
+ $chapter = Chapter::query()->first();
+ $currentBook = $chapter->book;
+ $newBook = Book::query()->where('id', '!=', $currentBook->id)->first();
+ $editor = $this->getEditor();
+
+ $this->setEntityRestrictions($newBook, ['view', 'update', 'delete'], [$editor->roles->first()]);
+ $this->setEntityRestrictions($chapter, ['view', 'update', 'create', 'delete'], [$editor->roles->first()]);
+
+ $moveChapterResp = $this->actingAs($editor)->put($chapter->getUrl('/move'), [
+ 'entity_selection' => 'book:' . $newBook->id,
+ ]);
+ $this->assertPermissionError($moveChapterResp);
+
+ $this->setEntityRestrictions($newBook, ['view', 'update', 'create', 'delete'], [$editor->roles->first()]);
+ $moveChapterResp = $this->put($chapter->getUrl('/move'), [
+ 'entity_selection' => 'book:' . $newBook->id,
+ ]);
+
+ $chapter = Chapter::query()->find($chapter->id);
+ $moveChapterResp->assertRedirect($chapter->getUrl());
+ $this->assertTrue($chapter->book->id == $newBook->id, 'Page book is now the new book');
+ }
+
public function test_chapter_move_changes_book_for_deleted_pages_within()
{
/** @var Chapter $chapter */