PermissionSeeder.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. // Criação de Permissões
  14. /*
  15. view = 1
  16. add = 2
  17. edit = 4
  18. delete = 8
  19. print = 16
  20. export = 32
  21. import = 64
  22. limit = 128
  23. menu = 256
  24. para cada cada bit selecionado se faz a soma dos valores.
  25. */
  26. $permissions = [
  27. [
  28. "scope" => "dashboard",
  29. "description" => "Dashboard",
  30. "bits" => 511,
  31. "children" => [],
  32. ],
  33. [
  34. "scope" => "config",
  35. "description" => "Configurações",
  36. "bits" => 271,
  37. "children" => [
  38. [
  39. "scope" => "config.user",
  40. "description" => "Configurações de Usuários",
  41. "bits" => 271,
  42. "children" => [],
  43. ],
  44. [
  45. "scope" => "config.permission",
  46. "description" => "Configurações de Permissões",
  47. "bits" => 271,
  48. "children" => [],
  49. ],
  50. [
  51. "scope" => "config.address",
  52. "description" => "Configurações de Endereços",
  53. "bits" => 271,
  54. "children" => [],
  55. ],
  56. [
  57. "scope" => "config.city",
  58. "description" => "Configurações de Cidades",
  59. "bits" => 271,
  60. "children" => [],
  61. ],
  62. [
  63. "scope" => "config.client",
  64. "description" => "Configurações de Clientes",
  65. "bits" => 271,
  66. "children" => [],
  67. ],
  68. [
  69. "scope" => "config.country",
  70. "description" => "Configurações de Países",
  71. "bits" => 271,
  72. "children" => [],
  73. ],
  74. [
  75. "scope" => "config.state",
  76. "description" => "Configurações de Estados",
  77. "bits" => 271,
  78. "children" => [],
  79. ],
  80. [
  81. "scope" => "config.provider",
  82. "description" => "Configurações de Prestadores",
  83. "bits" => 271,
  84. "children" => [],
  85. ],
  86. [
  87. "scope" => "config.improvement_type",
  88. "description" => "Configurações de Tipos de Melhoria",
  89. "bits" => 271,
  90. "children" => [],
  91. ],
  92. [
  93. "scope" => "config.media",
  94. "description" => "Configurações de Mídia",
  95. "bits" => 271,
  96. "children" => [],
  97. ],
  98. [
  99. "scope" => "config.service_type",
  100. "description" => "Configurações de Tipos de Serviço",
  101. "bits" => 271,
  102. "children" => [],
  103. ],
  104. [
  105. "scope" => "config.speciality",
  106. "description" => "Configurações de Especialidades",
  107. "bits" => 271,
  108. "children" => [],
  109. ],
  110. ],
  111. ],
  112. ];
  113. $this->createPermissionsAndChildren(permissions: $permissions);
  114. }
  115. /**
  116. * Recursively creates or updates permissions and handles nesting.
  117. *
  118. * @param array $permissions The array of permission data.
  119. * @param Permission|null $parent The parent Permission object (for nested sets).
  120. */
  121. private function createPermissionsAndChildren(
  122. array $permissions,
  123. ?Permission $parent = null,
  124. ): void {
  125. foreach ($permissions as $permissionData) {
  126. $children = $permissionData["children"];
  127. unset($permissionData["children"]);
  128. $permissionNode = Permission::updateOrCreate(
  129. ["scope" => $permissionData["scope"]],
  130. $permissionData,
  131. );
  132. if ($parent) {
  133. $parent->appendNode($permissionNode);
  134. }
  135. if (!empty($children)) {
  136. $this->createPermissionsAndChildren(
  137. permissions: $children,
  138. parent: $permissionNode,
  139. );
  140. }
  141. }
  142. }
  143. }