SyncAndreiaPermissions.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Models\User;
  5. use App\Models\Unit;
  6. use App\Models\UserType;
  7. use App\Models\UserTypePermission;
  8. class SyncAndreiaPermissions extends Command
  9. {
  10. protected $signature = 'app:sync-andreia-permissions';
  11. protected $description = 'Padroniza os perfis customizados (Neurotrainer, Financeiro, Gestor) para todos os franqueados usando a unidade da Andreia como template.';
  12. public function handle()
  13. {
  14. $andreia = User::find(11);
  15. if (!$andreia) {
  16. $this->error("Usuária Andreia não encontrada no banco.");
  17. return;
  18. }
  19. $andreiaUnitId = $andreia->units()->value('units.id');
  20. if (!$andreiaUnitId) {
  21. $this->error("A usuária {$andreia->name} não está vinculada a nenhuma unidade.");
  22. return;
  23. }
  24. // Pega todos os perfis customizados criados pela Andreia
  25. $templateRoles = UserType::where('unit_id', $andreiaUnitId)
  26. ->where('is_system', false)
  27. ->with('permissions')
  28. ->get();
  29. if ($templateRoles->isEmpty()) {
  30. $this->error("A unidade da Andreia não possui perfis customizados para replicar.");
  31. return;
  32. }
  33. // NOVIDADE: Exporta a configuração para um arquivo fixo no código!
  34. $configArray = [];
  35. foreach ($templateRoles as $templateRole) {
  36. $perms = [];
  37. foreach ($templateRole->permissions as $p) {
  38. $perms[] = [
  39. 'permission_id' => $p->permission_id,
  40. 'bits' => $p->bits,
  41. ];
  42. }
  43. $configArray[] = [
  44. 'label' => $templateRole->label,
  45. 'system_key' => $templateRole->system_key,
  46. 'slug_prefix' => \Illuminate\Support\Str::slug($templateRole->label),
  47. 'permissions' => $perms,
  48. ];
  49. }
  50. $configContent = "<?php\n\nreturn " . var_export($configArray, true) . ";\n";
  51. file_put_contents(config_path('default_franchisee_roles.php'), $configContent);
  52. $this->info("Arquivo de configuração config/default_franchisee_roles.php gerado com sucesso! Isso garante o funcionamento em Produção.");
  53. $this->info("Encontrados {$templateRoles->count()} perfis na unidade da Andreia. Replicando para todas as outras unidades...");
  54. $units = Unit::where('id', '!=', $andreiaUnitId)->get();
  55. $countUnits = 0;
  56. foreach ($units as $unit) {
  57. foreach ($templateRoles as $templateRole) {
  58. // Checa se a unidade já tem um perfil com esse nome
  59. $existingRole = UserType::where('unit_id', $unit->id)
  60. ->where('label', $templateRole->label)
  61. ->first();
  62. if ($existingRole) {
  63. // Atualiza as permissões do perfil existente
  64. UserTypePermission::where('user_type_id', $existingRole->id)->delete();
  65. foreach ($templateRole->permissions as $p) {
  66. UserTypePermission::create([
  67. 'user_type' => $existingRole->slug,
  68. 'user_type_id' => $existingRole->id,
  69. 'permission_id' => $p->permission_id,
  70. 'bits' => $p->bits,
  71. ]);
  72. }
  73. } else {
  74. // Cria o novo perfil
  75. $newRole = UserType::create([
  76. 'unit_id' => $unit->id,
  77. 'system_key' => $templateRole->system_key,
  78. 'scope_key' => "unit:{$unit->id}",
  79. 'slug' => \Illuminate\Support\Str::slug($templateRole->label . '-' . $unit->id),
  80. 'label' => $templateRole->label,
  81. 'is_system' => false,
  82. 'access_scope' => 'unit',
  83. ]);
  84. foreach ($templateRole->permissions as $p) {
  85. UserTypePermission::create([
  86. 'user_type' => $newRole->slug,
  87. 'user_type_id' => $newRole->id,
  88. 'permission_id' => $p->permission_id,
  89. 'bits' => $p->bits,
  90. ]);
  91. }
  92. }
  93. }
  94. $countUnits++;
  95. }
  96. $this->info("Concluído! Perfis replicados com sucesso para {$countUnits} unidades.");
  97. }
  98. }