Explorar o código

feat(permissions): seed template user types across all units

alvesantos hai 1 semana
pai
achega
b12a2604a0
Modificáronse 2 ficheiros con 123 adicións e 1 borrados
  1. 92 0
      app/Console/Commands/SyncAndreiaPermissions.php
  2. 31 1
      app/Services/UnitService.php

+ 92 - 0
app/Console/Commands/SyncAndreiaPermissions.php

@@ -0,0 +1,92 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+use App\Models\User;
+use App\Models\Unit;
+use App\Models\UserType;
+use App\Models\UserTypePermission;
+
+class SyncAndreiaPermissions extends Command
+{
+    protected $signature = 'app:sync-andreia-permissions';
+    protected $description = 'Padroniza os perfis customizados (Neurotrainer, Financeiro, Gestor) para todos os franqueados usando a unidade da Andreia como template.';
+
+    public function handle()
+    {
+        $andreia = User::find(11);
+
+        if (!$andreia) {
+            $this->error("Usuária Andreia não encontrada no banco.");
+            return;
+        }
+
+        $andreiaUnitId = $andreia->units()->value('units.id');
+        if (!$andreiaUnitId) {
+            $this->error("A usuária {$andreia->name} não está vinculada a nenhuma unidade.");
+            return;
+        }
+
+        // Pega todos os perfis customizados criados pela Andreia
+        $templateRoles = UserType::where('unit_id', $andreiaUnitId)
+            ->where('is_system', false)
+            ->with('permissions')
+            ->get();
+
+        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...");
+
+        $units = Unit::where('id', '!=', $andreiaUnitId)->get();
+        $countUnits = 0;
+
+        foreach ($units as $unit) {
+            foreach ($templateRoles as $templateRole) {
+                // Checa se a unidade já tem um perfil com esse nome
+                $existingRole = UserType::where('unit_id', $unit->id)
+                    ->where('label', $templateRole->label)
+                    ->first();
+
+                if ($existingRole) {
+                    // Atualiza as permissões do perfil existente
+                    UserTypePermission::where('user_type_id', $existingRole->id)->delete();
+                    foreach ($templateRole->permissions as $p) {
+                        UserTypePermission::create([
+                            'user_type' => $existingRole->slug,
+                            'user_type_id' => $existingRole->id,
+                            'permission_id' => $p->permission_id,
+                            'bits' => $p->bits,
+                        ]);
+                    }
+                } else {
+                    // Cria o novo perfil
+                    $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) {
+                        UserTypePermission::create([
+                            'user_type' => $newRole->slug,
+                            'user_type_id' => $newRole->id,
+                            'permission_id' => $p->permission_id,
+                            'bits' => $p->bits,
+                        ]);
+                    }
+                }
+            }
+            $countUnits++;
+        }
+
+        $this->info("Concluído! Perfis replicados com sucesso para {$countUnits} unidades.");
+    }
+}

+ 31 - 1
app/Services/UnitService.php

@@ -51,7 +51,7 @@ public function create(array $data): Unit
         $data = $this->handleAvatar($data);
         $unit = Unit::create($data);
 
-        UserType::create([
+        $userType = UserType::create([
             'unit_id' => $unit->id,
             'system_key' => 'ADMIN_FRANCHISEE',
             'scope_key' => "unit:{$unit->id}",
@@ -61,6 +61,36 @@ 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();
+
+            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,
+                    ]);
+                }
+            }
+        }
+
         $franchisee = Franchisee::create($franchiseeData);
 
         FranchiseeUnit::create([