PermissionSeeder.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. }
  12. public function run(): void
  13. {
  14. // Criação de Permissões
  15. /*
  16. view = 1
  17. add = 2
  18. edit = 4
  19. delete = 8
  20. print = 16
  21. export = 32
  22. import = 64
  23. limit = 128
  24. menu = 256
  25. para cada cada bit selecionado se faz a soma dos valores.
  26. */
  27. $permissions = [
  28. [
  29. 'scope' => 'dashboard',
  30. 'description' => 'Dashboard',
  31. 'bits' => 511,
  32. 'children' => []
  33. ],
  34. [
  35. 'scope' => 'config',
  36. 'description' => 'Configurações',
  37. 'bits' => 271,
  38. 'children' => [
  39. [
  40. 'scope' => 'config.user',
  41. 'description' => 'Configurações de Usuários',
  42. 'bits' => 271,
  43. 'children' => []
  44. ],
  45. [
  46. 'scope' => 'config.permission',
  47. 'description' => 'Configurações de Permissões',
  48. 'bits' => 271,
  49. 'children' => []
  50. ],
  51. [
  52. 'scope' => 'config.city',
  53. 'description' => 'Configurações de Cidades',
  54. 'bits' => 271,
  55. 'children' => []
  56. ],
  57. [
  58. 'scope' => 'config.country',
  59. 'description' => 'Configurações de Países',
  60. 'bits' => 271,
  61. 'children' => []
  62. ],
  63. [
  64. 'scope' => 'config.state',
  65. 'description' => 'Configurações de Estados',
  66. 'bits' => 271,
  67. 'children' => []
  68. ],
  69. ],
  70. ],
  71. ];
  72. $this->createPermissionsAndChildren(permissions: $permissions);
  73. }
  74. private function createPermissionsAndChildren(array $permissions): void
  75. {
  76. foreach ($permissions as $permission) {
  77. $this->permissionService->store(permissionDTO: PermissionDTO::fromArray(data: (array) $permission));
  78. if (!empty($permission['children'])) {
  79. $this->createPermissionsAndChildren(permissions: $permission['children']);
  80. }
  81. }
  82. }
  83. }