PermissionSeeder.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace Database\Seeders;
  3. use App\Models\Permission;
  4. use App\Services\PermissionService;
  5. use Illuminate\Database\Seeder;
  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" => "student-contract",
  47. "description" => "Contratos de Alunos",
  48. "bits" => Permission::ALL_PERMS,
  49. "children" => [],
  50. ],
  51. [
  52. "scope" => "tbr",
  53. "description" => "TBR",
  54. "bits" => Permission::ALL_PERMS,
  55. "children" => [
  56. [
  57. "scope" => "royalties-base-bracket",
  58. "description" => "Faixas de Royalties Base",
  59. "bits" => Permission::ALL_PERMS,
  60. "children" => [],
  61. ],
  62. [
  63. "scope" => "fnm-base-bracket",
  64. "description" => "Faixas de FNM Base",
  65. "bits" => Permission::ALL_PERMS,
  66. "children" => [],
  67. ],
  68. [
  69. "scope" => "maintenance-base-bracket",
  70. "description" => "Faixas de Manutenção Base",
  71. "bits" => Permission::ALL_PERMS,
  72. "children" => [],
  73. ],
  74. [
  75. "scope" => "franchisee-tbr",
  76. "description" => "TBR por Franqueado",
  77. "bits" => Permission::ALL_PERMS,
  78. "children" => [],
  79. ],
  80. [
  81. "scope" => "tbr-calculation",
  82. "description" => "Cálculo de TBR",
  83. "bits" => Permission::VIEW | Permission::ADD,
  84. "children" => [],
  85. ],
  86. [
  87. "scope" => "inhabitant-classification",
  88. "description" => "Classificações de Habitantes",
  89. "bits" => Permission::ALL_PERMS,
  90. "children" => [],
  91. ],
  92. [
  93. "scope" => "unit-inhabitant-classification",
  94. "description" => "Classificações de Habitantes por Unidade",
  95. "bits" => Permission::ALL_PERMS,
  96. "children" => [],
  97. ],
  98. ],
  99. ],
  100. [
  101. "scope" => "config",
  102. "description" => "Configurações",
  103. "bits" => Permission::MENU | Permission::VIEW,
  104. "children" => [
  105. [
  106. "scope" => "config.user",
  107. "description" => "Configurações de Usuários",
  108. "bits" => Permission::CRUD,
  109. "children" => [],
  110. ],
  111. [
  112. "scope" => "config.permission",
  113. "description" => "Configurações de Permissões",
  114. "bits" => Permission::CRUD,
  115. "children" => [],
  116. ],
  117. [
  118. "scope" => "config.city",
  119. "description" => "Configurações de Cidades",
  120. "bits" => Permission::CRUD,
  121. "children" => [],
  122. ],
  123. [
  124. "scope" => "config.country",
  125. "description" => "Configurações de Países",
  126. "bits" => Permission::CRUD,
  127. "children" => [],
  128. ],
  129. [
  130. "scope" => "config.state",
  131. "description" => "Configurações de Estados",
  132. "bits" => Permission::CRUD,
  133. "children" => [],
  134. ],
  135. ],
  136. ],
  137. ];
  138. $this->createPermissionsAndChildren(permissions: $permissions);
  139. }
  140. /**
  141. * Recursively creates or updates permissions and handles nesting.
  142. *
  143. * @param array $permissions The array of permission data.
  144. * @param Permission|null $parent The parent Permission object (for nested sets).
  145. */
  146. private function createPermissionsAndChildren(
  147. array $permissions,
  148. ?Permission $parent = null,
  149. ): void {
  150. foreach ($permissions as $permissionData) {
  151. $children = $permissionData["children"];
  152. unset($permissionData["children"]);
  153. $permissionNode = Permission::updateOrCreate(
  154. ["scope" => $permissionData["scope"]],
  155. $permissionData,
  156. );
  157. if ($parent) {
  158. $parent->appendNode($permissionNode);
  159. }
  160. if (!empty($children)) {
  161. $this->createPermissionsAndChildren(
  162. permissions: $children,
  163. parent: $permissionNode,
  164. );
  165. }
  166. }
  167. }
  168. }