]> BookStack Code Mirror - bookstack/blob - app/Entities/Tools/Cloner.php
Permissions: Cleanup after review of enum implementation PR
[bookstack] / app / Entities / Tools / Cloner.php
1 <?php
2
3 namespace BookStack\Entities\Tools;
4
5 use BookStack\Activity\Models\Tag;
6 use BookStack\Entities\Models\Book;
7 use BookStack\Entities\Models\Bookshelf;
8 use BookStack\Entities\Models\Chapter;
9 use BookStack\Entities\Models\Entity;
10 use BookStack\Entities\Models\CoverImageInterface;
11 use BookStack\Entities\Models\Page;
12 use BookStack\Entities\Repos\BookRepo;
13 use BookStack\Entities\Repos\ChapterRepo;
14 use BookStack\Entities\Repos\PageRepo;
15 use BookStack\Permissions\Permission;
16 use BookStack\Uploads\Image;
17 use BookStack\Uploads\ImageService;
18 use Illuminate\Http\UploadedFile;
19
20 class Cloner
21 {
22     public function __construct(
23         protected PageRepo $pageRepo,
24         protected ChapterRepo $chapterRepo,
25         protected BookRepo $bookRepo,
26         protected ImageService $imageService,
27     ) {
28     }
29
30     /**
31      * Clone the given page into the given parent using the provided name.
32      */
33     public function clonePage(Page $original, Entity $parent, string $newName): Page
34     {
35         $copyPage = $this->pageRepo->getNewDraftPage($parent);
36         $pageData = $this->entityToInputData($original);
37         $pageData['name'] = $newName;
38
39         return $this->pageRepo->publishDraft($copyPage, $pageData);
40     }
41
42     /**
43      * Clone the given page into the given parent using the provided name.
44      * Clones all child pages.
45      */
46     public function cloneChapter(Chapter $original, Book $parent, string $newName): Chapter
47     {
48         $chapterDetails = $this->entityToInputData($original);
49         $chapterDetails['name'] = $newName;
50
51         $copyChapter = $this->chapterRepo->create($chapterDetails, $parent);
52
53         if (userCan(Permission::PageCreate, $copyChapter)) {
54             /** @var Page $page */
55             foreach ($original->getVisiblePages() as $page) {
56                 $this->clonePage($page, $copyChapter, $page->name);
57             }
58         }
59
60         return $copyChapter;
61     }
62
63     /**
64      * Clone the given book.
65      * Clones all child chapters and pages.
66      */
67     public function cloneBook(Book $original, string $newName): Book
68     {
69         $bookDetails = $this->entityToInputData($original);
70         $bookDetails['name'] = $newName;
71
72         // Clone book
73         $copyBook = $this->bookRepo->create($bookDetails);
74
75         // Clone contents
76         $directChildren = $original->getDirectVisibleChildren();
77         foreach ($directChildren as $child) {
78             if ($child instanceof Chapter && userCan(Permission::ChapterCreate, $copyBook)) {
79                 $this->cloneChapter($child, $copyBook, $child->name);
80             }
81
82             if ($child instanceof Page && !$child->draft && userCan(Permission::PageCreate, $copyBook)) {
83                 $this->clonePage($child, $copyBook, $child->name);
84             }
85         }
86
87         // Clone bookshelf relationships
88         /** @var Bookshelf $shelf */
89         foreach ($original->shelves as $shelf) {
90             if (userCan(Permission::BookshelfUpdate, $shelf)) {
91                 $shelf->appendBook($copyBook);
92             }
93         }
94
95         return $copyBook;
96     }
97
98     /**
99      * Convert an entity to a raw data array of input data.
100      *
101      * @return array<string, mixed>
102      */
103     public function entityToInputData(Entity $entity): array
104     {
105         $inputData = $entity->getAttributes();
106         $inputData['tags'] = $this->entityTagsToInputArray($entity);
107
108         // Add a cover to the data if existing on the original entity
109         if ($entity instanceof CoverImageInterface) {
110             $cover = $entity->cover()->first();
111             if ($cover) {
112                 $inputData['image'] = $this->imageToUploadedFile($cover);
113             }
114         }
115
116         return $inputData;
117     }
118
119     /**
120      * Copy the permission settings from the source entity to the target entity.
121      */
122     public function copyEntityPermissions(Entity $sourceEntity, Entity $targetEntity): void
123     {
124         $permissions = $sourceEntity->permissions()->get(['role_id', 'view', 'create', 'update', 'delete'])->toArray();
125         $targetEntity->permissions()->delete();
126         $targetEntity->permissions()->createMany($permissions);
127         $targetEntity->rebuildPermissions();
128     }
129
130     /**
131      * Convert an image instance to an UploadedFile instance to mimic
132      * a file being uploaded.
133      */
134     protected function imageToUploadedFile(Image $image): ?UploadedFile
135     {
136         $imgData = $this->imageService->getImageData($image);
137         $tmpImgFilePath = tempnam(sys_get_temp_dir(), 'bs_cover_clone_');
138         file_put_contents($tmpImgFilePath, $imgData);
139
140         return new UploadedFile($tmpImgFilePath, basename($image->path));
141     }
142
143     /**
144      * Convert the tags on the given entity to the raw format
145      * that's used for incoming request data.
146      */
147     protected function entityTagsToInputArray(Entity $entity): array
148     {
149         $tags = [];
150
151         /** @var Tag $tag */
152         foreach ($entity->tags as $tag) {
153             $tags[] = ['name' => $tag->name, 'value' => $tag->value];
154         }
155
156         return $tags;
157     }
158 }