| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- <?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;
- }
- // 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();
- $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.");
- }
- }
|