PermissionSeeder.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace Database\Seeders;
  3. use App\DTO\PermissionDTO;
  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.city',
  52. 'description' => 'Configurações de Cidades',
  53. 'bits' => 271,
  54. 'children' => []
  55. ],
  56. [
  57. 'scope' => 'config.country',
  58. 'description' => 'Configurações de Países',
  59. 'bits' => 271,
  60. 'children' => []
  61. ],
  62. [
  63. 'scope' => 'config.state',
  64. 'description' => 'Configurações de Estados',
  65. 'bits' => 271,
  66. 'children' => []
  67. ],
  68. ],
  69. ],
  70. ];
  71. $this->createPermissionsAndChildren(permissions: $permissions);
  72. }
  73. private function createPermissionsAndChildren(array $permissions, ?int $parent_id = null): void
  74. {
  75. foreach ($permissions as $permission) {
  76. if ($parent_id != null) {
  77. array_merge($permission, [
  78. 'parent_id' => $parent_id,
  79. ]);
  80. }
  81. $permissionDb = $this->permissionService->store(permissionDTO: PermissionDTO::fromArray(data: (array) $permission));
  82. if (!empty($permission['children'])) {
  83. $this->createPermissionsAndChildren(permissions: $permission['children'], parent_id: $permissionDb->id);
  84. }
  85. }
  86. }
  87. }