PermissionSeeder.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace Database\Seeders;
  3. use App\DataTransferObjects\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. visualizar = 1
  17. adicionar = 2
  18. editar = 4
  19. deletar = 8
  20. imprimir = 16
  21. exportar = 32
  22. importar = 64
  23. limitar = 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' => 'usuarios',
  36. 'description' => 'Usuários',
  37. 'bits' => 271,
  38. 'children' => []
  39. ],
  40. ];
  41. $this->createPermissionsAndChildren($permissions);
  42. }
  43. private function createPermissionsAndChildren(array $permissions)
  44. {
  45. foreach ($permissions as $permission) {
  46. $this->permissionService->store(PermissionDto::fromArray($permission));
  47. if (isset($permission['children'])) {
  48. $this->createPermissionsAndChildren($permission['children']);
  49. }
  50. }
  51. }
  52. }