PermissionSeeder.php 6.8 KB

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