1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2024-10-30 07:32:39 +01:00

Updated book copy to copy shelf relations

Where permission to edit the shelf is allowed.
For #3699
This commit is contained in:
Dan Brown 2022-09-28 14:14:51 +01:00
parent 8f3430d386
commit 60171b3522
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
3 changed files with 37 additions and 1 deletions

View File

@ -19,6 +19,7 @@ use Illuminate\Support\Collection;
* @property \Illuminate\Database\Eloquent\Collection $chapters * @property \Illuminate\Database\Eloquent\Collection $chapters
* @property \Illuminate\Database\Eloquent\Collection $pages * @property \Illuminate\Database\Eloquent\Collection $pages
* @property \Illuminate\Database\Eloquent\Collection $directPages * @property \Illuminate\Database\Eloquent\Collection $directPages
* @property \Illuminate\Database\Eloquent\Collection $shelves
*/ */
class Book extends Entity implements HasCoverImage class Book extends Entity implements HasCoverImage
{ {

View File

@ -4,6 +4,7 @@ namespace BookStack\Entities\Tools;
use BookStack\Actions\Tag; use BookStack\Actions\Tag;
use BookStack\Entities\Models\Book; use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\Bookshelf;
use BookStack\Entities\Models\Chapter; use BookStack\Entities\Models\Chapter;
use BookStack\Entities\Models\Entity; use BookStack\Entities\Models\Entity;
use BookStack\Entities\Models\Page; use BookStack\Entities\Models\Page;
@ -71,8 +72,10 @@ class Cloner
$bookDetails = $this->entityToInputData($original); $bookDetails = $this->entityToInputData($original);
$bookDetails['name'] = $newName; $bookDetails['name'] = $newName;
// Clone book
$copyBook = $this->bookRepo->create($bookDetails); $copyBook = $this->bookRepo->create($bookDetails);
// Clone contents
$directChildren = $original->getDirectChildren(); $directChildren = $original->getDirectChildren();
foreach ($directChildren as $child) { foreach ($directChildren as $child) {
if ($child instanceof Chapter && userCan('chapter-create', $copyBook)) { if ($child instanceof Chapter && userCan('chapter-create', $copyBook)) {
@ -84,6 +87,14 @@ class Cloner
} }
} }
// Clone bookshelf relationships
/** @var Bookshelf $shelf */
foreach ($original->shelves as $shelf) {
if (userCan('bookshelf-update', $shelf)) {
$shelf->appendBook($copyBook);
}
}
return $copyBook; return $copyBook;
} }

View File

@ -4,6 +4,7 @@ namespace Tests\Entity;
use BookStack\Entities\Models\Book; use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\BookChild; use BookStack\Entities\Models\BookChild;
use BookStack\Entities\Models\Bookshelf;
use BookStack\Entities\Repos\BookRepo; use BookStack\Entities\Repos\BookRepo;
use Tests\TestCase; use Tests\TestCase;
use Tests\Uploads\UsesImages; use Tests\Uploads\UsesImages;
@ -344,11 +345,34 @@ class BookTest extends TestCase
$bookRepo->updateCoverImage($book, $coverImageFile); $bookRepo->updateCoverImage($book, $coverImageFile);
$this->asEditor()->post($book->getUrl('/copy'), ['name' => 'My copy book']); $this->asEditor()->post($book->getUrl('/copy'), ['name' => 'My copy book']);
/** @var Book $copy */ /** @var Book $copy */
$copy = Book::query()->where('name', '=', 'My copy book')->first(); $copy = Book::query()->where('name', '=', 'My copy book')->first();
$this->assertNotNull($copy->cover); $this->assertNotNull($copy->cover);
$this->assertNotEquals($book->cover->id, $copy->cover->id); $this->assertNotEquals($book->cover->id, $copy->cover->id);
} }
public function test_copy_adds_book_to_shelves_if_edit_permissions_allows()
{
/** @var Bookshelf $shelfA */
/** @var Bookshelf $shelfB */
[$shelfA, $shelfB] = Bookshelf::query()->take(2)->get();
/** @var Book $book */
$book = Book::query()->first();
$shelfA->appendBook($book);
$shelfB->appendBook($book);
$viewer = $this->getViewer();
$this->giveUserPermissions($viewer, ['book-update-all', 'book-create-all', 'bookshelf-update-all']);
$this->setEntityRestrictions($shelfB);
$this->asEditor()->post($book->getUrl('/copy'), ['name' => 'My copy book']);
/** @var Book $copy */
$copy = Book::query()->where('name', '=', 'My copy book')->first();
$this->assertTrue($copy->shelves()->where('id', '=', $shelfA->id)->exists());
$this->assertFalse($copy->shelves()->where('id', '=', $shelfB->id)->exists());
}
} }