|
|
@@ -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.");
|
|
|
+ }
|
|
|
+}
|