]> BookStack Code Mirror - bookstack/commitdiff
Added clone of entity permissions on chapter/book promotion
authorDan Brown <redacted>
Tue, 14 Jun 2022 14:55:44 +0000 (15:55 +0100)
committerDan Brown <redacted>
Tue, 14 Jun 2022 14:55:44 +0000 (15:55 +0100)
app/Entities/Tools/Cloner.php
app/Entities/Tools/HierarchyTransformer.php

index 3553a9db3cd569dbd9030439512ba4beb6737fb6..91e10b9be23457ec90d2d238d4dd0b3ec0f56789 100644 (file)
@@ -121,6 +121,18 @@ class Cloner
         return $inputData;
     }
 
+    /**
+     * Copy the permission settings from the source entity to the target entity.
+     */
+    public function copyEntityPermissions(Entity $sourceEntity, Entity $targetEntity): void
+    {
+        $targetEntity->restricted = $sourceEntity->restricted;
+        $permissions = $sourceEntity->permissions()->get(['role_id', 'action'])->toArray();
+        $targetEntity->permissions()->delete();
+        $targetEntity->permissions()->createMany($permissions);
+        $targetEntity->rebuildPermissions();
+    }
+
     /**
      * Convert an image instance to an UploadedFile instance to mimic
      * a file being uploaded.
index 17e153e0562b0b8f2cc1fdbc9c30044f44e64c59..c95d5fa5307e4d4e7d9bd930d8a031f7b15de804 100644 (file)
@@ -16,19 +16,13 @@ class HierarchyTransformer
     protected Cloner $cloner;
     protected TrashCan $trashCan;
 
-    // TODO - Test setting book cover image from API
-    //   Ensure we can update without resetting image accidentally
-    //   Ensure api docs correct.
-    // TODO - As above but for shelves.
-
     public function transformChapterToBook(Chapter $chapter): Book
     {
         // TODO - Check permissions before call
         //   Permissions: edit-chapter, delete-chapter, create-book
         $inputData = $this->cloner->entityToInputData($chapter);
         $book = $this->bookRepo->create($inputData);
-
-        // TODO - Copy permissions
+        $this->cloner->copyEntityPermissions($chapter, $book);
 
         /** @var Page $page */
         foreach ($chapter->pages as $page) {
@@ -48,8 +42,7 @@ class HierarchyTransformer
         //   Permissions: edit-book, delete-book, create-shelf
         $inputData = $this->cloner->entityToInputData($book);
         $shelf = $this->shelfRepo->create($inputData, []);
-
-        // TODO - Copy permissions?
+        $this->cloner->copyEntityPermissions($book, $shelf);
 
         $shelfBookSyncData = [];