Browse Source

feat(permissions): export roles to config for production persistence

alvesantos 1 week ago
parent
commit
4b9a5cf1e5
2 changed files with 33 additions and 15 deletions
  1. 23 1
      app/Console/Commands/SyncAndreiaPermissions.php
  2. 10 14
      app/Services/UnitService.php

+ 23 - 1
app/Console/Commands/SyncAndreiaPermissions.php

@@ -34,11 +34,33 @@ public function handle()
             ->with('permissions')
             ->get();
 
-        if ($templateRoles->isEmpty()) {
+                if ($templateRoles->isEmpty()) {
             $this->error("A unidade da Andreia não possui perfis customizados para replicar.");
             return;
         }
 
+        // NOVIDADE: Exporta a configuração para um arquivo fixo no código!
+        $configArray = [];
+        foreach ($templateRoles as $templateRole) {
+            $perms = [];
+            foreach ($templateRole->permissions as $p) {
+                $perms[] = [
+                    'permission_id' => $p->permission_id,
+                    'bits' => $p->bits,
+                ];
+            }
+            $configArray[] = [
+                'label' => $templateRole->label,
+                'system_key' => $templateRole->system_key,
+                'slug_prefix' => \Illuminate\Support\Str::slug($templateRole->label),
+                'permissions' => $perms,
+            ];
+        }
+        
+        $configContent = "<?php\n\nreturn " . var_export($configArray, true) . ";\n";
+        file_put_contents(config_path('default_franchisee_roles.php'), $configContent);
+        $this->info("Arquivo de configuração config/default_franchisee_roles.php gerado com sucesso! Isso garante o funcionamento em Produção.");
+
         $this->info("Encontrados {$templateRoles->count()} perfis na unidade da Andreia. Replicando para todas as outras unidades...");
 
         $units = Unit::where('id', '!=', $andreiaUnitId)->get();

+ 10 - 14
app/Services/UnitService.php

@@ -61,31 +61,27 @@ public function create(array $data): Unit
             'access_scope' => 'unit',
         ]);
 
-        // Clona os perfis customizados (Neurotrainer, Financeiro, Gestor) baseados na unidade da Andreia
-        $andreiaUnitId = \App\Models\User::find(11)?->units()->value('units.id');
-        if ($andreiaUnitId) {
-            $templateRoles = UserType::where('unit_id', $andreiaUnitId)
-                ->where('is_system', false)
-                ->with('permissions')
-                ->get();
-
+                // Clona os perfis customizados a partir do arquivo de configuração gerado
+        $templateRoles = config('default_franchisee_roles', []);
+        
+        if (!empty($templateRoles)) {
             foreach ($templateRoles as $templateRole) {
                 $newRole = UserType::create([
                     'unit_id' => $unit->id,
-                    'system_key' => $templateRole->system_key,
+                    'system_key' => $templateRole['system_key'],
                     'scope_key' => "unit:{$unit->id}",
-                    'slug' => \Illuminate\Support\Str::slug($templateRole->label . '-' . $unit->id),
-                    'label' => $templateRole->label,
+                    'slug' => \Illuminate\Support\Str::slug($templateRole['label'] . '-' . $unit->id),
+                    'label' => $templateRole['label'],
                     'is_system' => false,
                     'access_scope' => 'unit',
                 ]);
 
-                foreach ($templateRole->permissions as $p) {
+                foreach ($templateRole['permissions'] as $p) {
                     \App\Models\UserTypePermission::create([
                         'user_type' => $newRole->slug,
                         'user_type_id' => $newRole->id,
-                        'permission_id' => $p->permission_id,
-                        'bits' => $p->bits,
+                        'permission_id' => $p['permission_id'],
+                        'bits' => $p['bits'],
                     ]);
                 }
             }