PermissionSeeder.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace Database\Seeders;
  3. use App\Models\Permission;
  4. use Illuminate\Database\Seeder;
  5. use App\Services\PermissionService;
  6. class PermissionSeeder extends Seeder
  7. {
  8. public function __construct(
  9. protected PermissionService $permissionService,
  10. ) {}
  11. public function run(): void
  12. {
  13. $permissions = [
  14. [
  15. "scope" => "dashboard",
  16. "description" => "Dashboard",
  17. "bits" => Permission::ALL_PERMS,
  18. "children" => [],
  19. ],
  20. [
  21. "scope" => "franchisee",
  22. "description" => "Franchisee",
  23. "bits" => Permission::MENU | Permission::VIEW,
  24. "children" => [
  25. [
  26. "scope" => "unit",
  27. "description" => "Unidades",
  28. "bits" => Permission::ALL_PERMS,
  29. "children" => [],
  30. ],
  31. ],
  32. ],
  33. [
  34. "scope" => "class-package",
  35. "description" => "Pacotes (Franqueadora)",
  36. "bits" => Permission::ALL_PERMS,
  37. "children" => [],
  38. ],
  39. [
  40. "scope" => "class-package-unit",
  41. "description" => "Pacotes (Unidade)",
  42. "bits" => Permission::ALL_PERMS,
  43. "children" => [],
  44. ],
  45. [
  46. "scope" => "tbr",
  47. "description" => "TBR",
  48. "bits" => Permission::ALL_PERMS,
  49. "children" => [
  50. [
  51. "scope" => "royalties-base-bracket",
  52. "description" => "Faixas de Royalties Base",
  53. "bits" => Permission::ALL_PERMS,
  54. "children" => [],
  55. ],
  56. [
  57. "scope" => "fnm-base-bracket",
  58. "description" => "Faixas de FNM Base",
  59. "bits" => Permission::ALL_PERMS,
  60. "children" => [],
  61. ],
  62. [
  63. "scope" => "maintenance-base-bracket",
  64. "description" => "Faixas de Manutenção Base",
  65. "bits" => Permission::ALL_PERMS,
  66. "children" => [],
  67. ],
  68. [
  69. "scope" => "franchisee-tbr",
  70. "description" => "TBR por Franqueado",
  71. "bits" => Permission::ALL_PERMS,
  72. "children" => [],
  73. ],
  74. [
  75. "scope" => "tbr-calculation",
  76. "description" => "Cálculo de TBR",
  77. "bits" => Permission::VIEW | Permission::ADD,
  78. "children" => [],
  79. ],
  80. ],
  81. ],
  82. [
  83. "scope" => "config",
  84. "description" => "Configurações",
  85. "bits" => Permission::MENU | Permission::VIEW,
  86. "children" => [
  87. [
  88. "scope" => "config.user",
  89. "description" => "Configurações de Usuários",
  90. "bits" => Permission::CRUD,
  91. "children" => [],
  92. ],
  93. [
  94. "scope" => "config.permission",
  95. "description" => "Configurações de Permissões",
  96. "bits" => Permission::CRUD,
  97. "children" => [],
  98. ],
  99. [
  100. "scope" => "config.city",
  101. "description" => "Configurações de Cidades",
  102. "bits" => Permission::CRUD,
  103. "children" => [],
  104. ],
  105. [
  106. "scope" => "config.country",
  107. "description" => "Configurações de Países",
  108. "bits" => Permission::CRUD,
  109. "children" => [],
  110. ],
  111. [
  112. "scope" => "config.state",
  113. "description" => "Configurações de Estados",
  114. "bits" => Permission::CRUD,
  115. "children" => [],
  116. ],
  117. ],
  118. ],
  119. ];
  120. $this->createPermissionsAndChildren(permissions: $permissions);
  121. }
  122. /**
  123. * Recursively creates or updates permissions and handles nesting.
  124. *
  125. * @param array $permissions The array of permission data.
  126. * @param Permission|null $parent The parent Permission object (for nested sets).
  127. */
  128. private function createPermissionsAndChildren(
  129. array $permissions,
  130. ?Permission $parent = null,
  131. ): void {
  132. foreach ($permissions as $permissionData) {
  133. $children = $permissionData["children"];
  134. unset($permissionData["children"]);
  135. $permissionNode = Permission::updateOrCreate(
  136. ["scope" => $permissionData["scope"]],
  137. $permissionData,
  138. );
  139. if ($parent) {
  140. $parent->appendNode($permissionNode);
  141. }
  142. if (!empty($children)) {
  143. $this->createPermissionsAndChildren(
  144. permissions: $children,
  145. parent: $permissionNode,
  146. );
  147. }
  148. }
  149. }
  150. }