Przeglądaj źródła

feat(permissions): implement role cloning from configuration for user types

alvesantos 1 tydzień temu
rodzic
commit
45d5dc1f35
2 zmienionych plików z 85 dodań i 0 usunięć
  1. 44 0
      patch_sync.php
  2. 41 0
      patch_unitservice.php

+ 44 - 0
patch_sync.php

@@ -0,0 +1,44 @@
+<?php
+$content = file_get_contents('app/Console/Commands/SyncAndreiaPermissions.php');
+$replacement = <<<'REPLACE'
+        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...");
+REPLACE;
+
+$content = str_replace(
+    'if ($templateRoles->isEmpty()) {
+            $this->error("A unidade da Andreia não possui perfis customizados para replicar.");
+            return;
+        }
+
+        $this->info("Encontrados {$templateRoles->count()} perfis na unidade da Andreia. Replicando para todas as outras unidades...");',
+    $replacement,
+    $content
+);
+file_put_contents('app/Console/Commands/SyncAndreiaPermissions.php', $content);

+ 41 - 0
patch_unitservice.php

@@ -0,0 +1,41 @@
+<?php
+$content = file_get_contents('app/Services/UnitService.php');
+$replacement = <<<'REPLACE'
+        // 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'],
+                    'scope_key' => "unit:{$unit->id}",
+                    'slug' => \Illuminate\Support\Str::slug($templateRole['label'] . '-' . $unit->id),
+                    'label' => $templateRole['label'],
+                    'is_system' => false,
+                    'access_scope' => 'unit',
+                ]);
+
+                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'],
+                    ]);
+                }
+            }
+        }
+REPLACE;
+
+// The original content to replace starts from "// Clona os perfis customizados" up to the end of the if statement
+$startString = "// Clona os perfis customizados (Neurotrainer, Financeiro, Gestor) baseados na unidade da Andreia";
+$endString = "        }\n\n        \$franchisee = Franchisee::create(\$franchiseeData);";
+
+$startPos = strpos($content, $startString);
+$endPos = strpos($content, $endString) + strlen("        }");
+
+$contentToReplace = substr($content, $startPos, $endPos - $startPos);
+$content = str_replace($contentToReplace, $replacement, $content);
+
+file_put_contents('app/Services/UnitService.php', $content);