PermissionSeeder.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. 'permissions' => 'Permissões',
  64. 'unit' => 'Unidade',
  65. 'integrations' => 'APIs',
  66. 'support' => 'Suporte',
  67. 'activities' => 'Atividades',
  68. 'orders' => 'Pedidos',
  69. ])->map(fn (string $description, string $scope) => [
  70. 'scope' => "franchisee_{$scope}",
  71. 'description' => $description,
  72. 'bits' => Permission::CRUD,
  73. ])->values()->all(),
  74. ];
  75. foreach ($permissions as $permission) {
  76. Permission::updateOrCreate(
  77. ['scope' => $permission['scope']],
  78. $permission + ['parent_id' => null],
  79. );
  80. }
  81. $activeScopes = array_column($permissions, 'scope');
  82. Permission::query()
  83. ->whereNotIn('scope', $activeScopes)
  84. ->orderByDesc('_lft')
  85. ->get()
  86. ->each
  87. ->delete();
  88. }
  89. }