Browse Source

fix: PermissionSeeder

Denis 6 months ago
parent
commit
c41dfec319

+ 2 - 2
app/Broadcasting/DTO/WebsocketEventDataDTO.php → app/Broadcasting/Entity/WebsocketEventData.php

@@ -1,8 +1,8 @@
 <?php
 
-namespace App\Broadcasting\DTO;
+namespace App\Broadcasting\Entity;
 
-final readonly class WebsocketEventDataDTO
+final readonly class WebsocketEventData
 {
     public function __construct(
         public string $room,

+ 2 - 2
app/Broadcasting/Events/WebsocketEvent.php

@@ -3,7 +3,7 @@
 namespace App\Broadcasting\Events;
 
 use App\Broadcasting\Events\WebsocketEventInterface;
-use App\Broadcasting\DTO\WebsocketEventDataDTO;
+use App\Broadcasting\Entity\WebsocketEventData;
 use Illuminate\Broadcasting\Channel;
 use Illuminate\Broadcasting\InteractsWithSockets;
 use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
@@ -15,7 +15,7 @@ final class WebsocketEvent implements WebsocketEventInterface, ShouldBroadcastNo
     use Dispatchable, InteractsWithSockets, SerializesModels;
 
     public function __construct(
-        private readonly WebsocketEventDataDTO $dto
+        private readonly WebsocketEventData $dto
     ) {}
 
     public function broadcastOn(): Channel

+ 2 - 2
app/Broadcasting/Events/WebsocketEventInterface.php

@@ -4,12 +4,12 @@
 
 namespace App\Broadcasting\Events;
 
-use App\Broadcasting\DTO\WebsocketEventDataDTO;
+use App\Broadcasting\Entity\WebsocketEventData;
 use Illuminate\Broadcasting\Channel;
 
 interface WebsocketEventInterface
 {
-    public function __construct(WebsocketEventDataDTO $dto);
+    public function __construct(WebsocketEventData $dto);
     public function broadcastOn(): Channel;
     public function broadcastWith(): array;
 }

+ 56 - 39
database/seeders/PermissionSeeder.php

@@ -2,7 +2,7 @@
 
 namespace Database\Seeders;
 
-use App\DTO\PermissionDTO;
+use App\Models\Permission;
 use Illuminate\Database\Seeder;
 use App\Services\PermissionService;
 
@@ -30,45 +30,45 @@ public function run(): void
         */
         $permissions = [
             [
-                'scope' => 'dashboard',
-                'description' => 'Dashboard',
-                'bits' => 511,
-                'children' => []
+                "scope" => "dashboard",
+                "description" => "Dashboard",
+                "bits" => 511,
+                "children" => [],
             ],
             [
-                'scope' => 'config',
-                'description' => 'Configurações',
-                'bits' => 271,
-                'children' => [
+                "scope" => "config",
+                "description" => "Configurações",
+                "bits" => 271,
+                "children" => [
                     [
-                        'scope' => 'config.user',
-                        'description' => 'Configurações de Usuários',
-                        'bits' => 271,
-                        'children' => []
+                        "scope" => "config.user",
+                        "description" => "Configurações de Usuários",
+                        "bits" => 271,
+                        "children" => [],
                     ],
                     [
-                        'scope' => 'config.permission',
-                        'description' => 'Configurações de Permissões',
-                        'bits' => 271,
-                        'children' => []
+                        "scope" => "config.permission",
+                        "description" => "Configurações de Permissões",
+                        "bits" => 271,
+                        "children" => [],
                     ],
                     [
-                        'scope' => 'config.city',
-                        'description' => 'Configurações de Cidades',
-                        'bits' => 271,
-                        'children' => []
+                        "scope" => "config.city",
+                        "description" => "Configurações de Cidades",
+                        "bits" => 271,
+                        "children" => [],
                     ],
                     [
-                        'scope' => 'config.country',
-                        'description' => 'Configurações de Países',
-                        'bits' => 271,
-                        'children' => []
+                        "scope" => "config.country",
+                        "description" => "Configurações de Países",
+                        "bits" => 271,
+                        "children" => [],
                     ],
                     [
-                        'scope' => 'config.state',
-                        'description' => 'Configurações de Estados',
-                        'bits' => 271,
-                        'children' => []
+                        "scope" => "config.state",
+                        "description" => "Configurações de Estados",
+                        "bits" => 271,
+                        "children" => [],
                     ],
                 ],
             ],
@@ -77,17 +77,34 @@ public function run(): void
         $this->createPermissionsAndChildren(permissions: $permissions);
     }
 
-    private function createPermissionsAndChildren(array $permissions, ?int $parent_id = null): void
-    {
-        foreach ($permissions as $permission) {
-            if ($parent_id != null) {
-                array_merge($permission, [
-                    'parent_id' => $parent_id,
-                ]);
+    /**
+     * Recursively creates or updates permissions and handles nesting.
+     *
+     * @param array $permissions The array of permission data.
+     * @param Permission|null $parent The parent Permission object (for nested sets).
+     */
+    private function createPermissionsAndChildren(
+        array $permissions,
+        ?Permission $parent = null,
+    ): void {
+        foreach ($permissions as $permissionData) {
+            $children = $permissionData["children"];
+            unset($permissionData["children"]);
+
+            $permissionNode = Permission::updateOrCreate(
+                ["scope" => $permissionData["scope"]],
+                $permissionData,
+            );
+
+            if ($parent) {
+                $parent->appendNode($permissionNode);
             }
-            $permissionDb = $this->permissionService->store(permissionDTO: PermissionDTO::fromArray(data: (array) $permission));
-            if (!empty($permission['children'])) {
-                $this->createPermissionsAndChildren(permissions: $permission['children'], parent_id: $permissionDb->id);
+
+            if (!empty($children)) {
+                $this->createPermissionsAndChildren(
+                    permissions: $children,
+                    parent: $permissionNode,
+                );
             }
         }
     }