PermissionSeeder.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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()
  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. $this->command->info('Permissions seeded successfully.');
  43. }
  44. private function createPermissionsAndChildren(array $permissions)
  45. {
  46. foreach ($permissions as $permission) {
  47. $this->permissionService->store(PermissionDto::fromArray($permission));
  48. if (isset($permission['children'])) {
  49. $this->createPermissionsAndChildren($permission['children']);
  50. }
  51. }
  52. }
  53. }