PermissionSeeder.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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" => "tbr",
  35. "description" => "TBR",
  36. "bits" => Permission::ALL_PERMS,
  37. "children" => [
  38. [
  39. "scope" => "royalties-base-bracket",
  40. "description" => "Faixas de Royalties Base",
  41. "bits" => Permission::ALL_PERMS,
  42. "children" => [],
  43. ],
  44. [
  45. "scope" => "fnm-base-bracket",
  46. "description" => "Faixas de FNM Base",
  47. "bits" => Permission::ALL_PERMS,
  48. "children" => [],
  49. ],
  50. [
  51. "scope" => "maintenance-base-bracket",
  52. "description" => "Faixas de Manutenção Base",
  53. "bits" => Permission::ALL_PERMS,
  54. "children" => [],
  55. ],
  56. [
  57. "scope" => "franchisee-tbr",
  58. "description" => "TBR por Franqueado",
  59. "bits" => Permission::ALL_PERMS,
  60. "children" => [],
  61. ],
  62. [
  63. "scope" => "tbr-calculation",
  64. "description" => "Cálculo de TBR",
  65. "bits" => Permission::VIEW | Permission::ADD,
  66. "children" => [],
  67. ],
  68. ],
  69. ],
  70. [
  71. "scope" => "config",
  72. "description" => "Configurações",
  73. "bits" => Permission::MENU | Permission::VIEW,
  74. "children" => [
  75. [
  76. "scope" => "config.user",
  77. "description" => "Configurações de Usuários",
  78. "bits" => Permission::CRUD,
  79. "children" => [],
  80. ],
  81. [
  82. "scope" => "config.permission",
  83. "description" => "Configurações de Permissões",
  84. "bits" => Permission::CRUD,
  85. "children" => [],
  86. ],
  87. [
  88. "scope" => "config.city",
  89. "description" => "Configurações de Cidades",
  90. "bits" => Permission::CRUD,
  91. "children" => [],
  92. ],
  93. [
  94. "scope" => "config.country",
  95. "description" => "Configurações de Países",
  96. "bits" => Permission::CRUD,
  97. "children" => [],
  98. ],
  99. [
  100. "scope" => "config.state",
  101. "description" => "Configurações de Estados",
  102. "bits" => Permission::CRUD,
  103. "children" => [],
  104. ],
  105. ],
  106. ],
  107. ];
  108. $this->createPermissionsAndChildren(permissions: $permissions);
  109. }
  110. /**
  111. * Recursively creates or updates permissions and handles nesting.
  112. *
  113. * @param array $permissions The array of permission data.
  114. * @param Permission|null $parent The parent Permission object (for nested sets).
  115. */
  116. private function createPermissionsAndChildren(
  117. array $permissions,
  118. ?Permission $parent = null,
  119. ): void {
  120. foreach ($permissions as $permissionData) {
  121. $children = $permissionData["children"];
  122. unset($permissionData["children"]);
  123. $permissionNode = Permission::updateOrCreate(
  124. ["scope" => $permissionData["scope"]],
  125. $permissionData,
  126. );
  127. if ($parent) {
  128. $parent->appendNode($permissionNode);
  129. }
  130. if (!empty($children)) {
  131. $this->createPermissionsAndChildren(
  132. permissions: $children,
  133. parent: $permissionNode,
  134. );
  135. }
  136. }
  137. }
  138. }