PermissionSeeder.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace Database\Seeders;
  3. use App\Models\Permission;
  4. use Illuminate\Database\Seeder;
  5. class PermissionSeeder extends Seeder
  6. {
  7. public function run(): void
  8. {
  9. $permissions = [
  10. [
  11. 'scope' => 'dashboard',
  12. 'description' => 'Dashboard',
  13. 'bits' => Permission::ALL_PERMS,
  14. ],
  15. [
  16. 'scope' => 'franchisor_franchisee',
  17. 'description' => 'Franqueados',
  18. 'bits' => Permission::ALL_PERMS,
  19. ],
  20. [
  21. 'scope' => 'franchisor_packages',
  22. 'description' => 'Pacotes',
  23. 'bits' => Permission::MENU | Permission::VIEW | Permission::ADD | Permission::EDIT,
  24. ],
  25. [
  26. 'scope' => 'franchisor_products',
  27. 'description' => 'Produtos',
  28. 'bits' => Permission::CRUD,
  29. ],
  30. [
  31. 'scope' => 'franchisor_tbr',
  32. 'description' => 'TBR',
  33. 'bits' => Permission::MENU | Permission::VIEW | Permission::ADD | Permission::EDIT,
  34. ],
  35. [
  36. 'scope' => 'franchisor_financial',
  37. 'description' => 'Financeiro',
  38. 'bits' => Permission::MENU | Permission::VIEW | Permission::ADD | Permission::EDIT,
  39. ],
  40. [
  41. 'scope' => 'franchisor_support',
  42. 'description' => 'Suporte',
  43. 'bits' => Permission::CRUD,
  44. ],
  45. [
  46. 'scope' => 'franchisor_activities',
  47. 'description' => 'Atividades',
  48. 'bits' => Permission::CRUD,
  49. ],
  50. [
  51. 'scope' => 'franchisor_registrations',
  52. 'description' => 'Cadastros',
  53. 'bits' => Permission::CRUD,
  54. ],
  55. ...collect([
  56. 'dashboard' => 'Dashboard',
  57. 'students' => 'Alunos',
  58. 'contracts' => 'Contratos',
  59. 'classes' => 'Agenda / Aulas',
  60. 'financial' => 'Financeiro',
  61. 'packages' => 'Pacotes',
  62. 'users' => 'Usuários',
  63. 'unit' => 'Unidade',
  64. 'integrations' => 'APIs',
  65. 'support' => 'Suporte',
  66. 'activities' => 'Atividades',
  67. ])->map(fn (string $description, string $scope) => [
  68. 'scope' => "franchisee_{$scope}",
  69. 'description' => $description,
  70. 'bits' => Permission::CRUD,
  71. ])->values()->all(),
  72. ];
  73. foreach ($permissions as $permission) {
  74. Permission::updateOrCreate(
  75. ['scope' => $permission['scope']],
  76. $permission + ['parent_id' => null],
  77. );
  78. }
  79. $activeScopes = array_column($permissions, 'scope');
  80. Permission::query()
  81. ->whereNotIn('scope', $activeScopes)
  82. ->orderByDesc('_lft')
  83. ->get()
  84. ->each
  85. ->delete();
  86. }
  87. }