PermissionSeeder.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. ],
  53. ];
  54. $this->createPermissionsAndChildren(permissions: $permissions);
  55. }
  56. private function createPermissionsAndChildren(array $permissions): void
  57. {
  58. foreach ($permissions as $permission) {
  59. $this->permissionService->store(permissionDTO: PermissionDTO::fromArray(data: (array) $permission));
  60. if (!empty($permission['children'])) {
  61. $this->createPermissionsAndChildren(permissions: $permission['children']);
  62. }
  63. }
  64. }
  65. }