| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- <?php
- namespace Database\Seeders;
- use App\DTO\PermissionDTO;
- use Illuminate\Database\Seeder;
- use App\Services\PermissionService;
- class PermissionSeeder extends Seeder
- {
- public function __construct(
- protected PermissionService $permissionService,
- ) {}
- public function run(): void
- {
- // Criação de Permissões
- /*
- view = 1
- add = 2
- edit = 4
- delete = 8
- print = 16
- export = 32
- import = 64
- limit = 128
- menu = 256
- para cada cada bit selecionado se faz a soma dos valores.
- */
- $permissions = [
- [
- 'scope' => 'dashboard',
- 'description' => 'Dashboard',
- 'bits' => 511,
- 'children' => []
- ],
- [
- 'scope' => 'config',
- 'description' => 'Configurações',
- 'bits' => 271,
- 'children' => [
- [
- 'scope' => 'config.user',
- 'description' => 'Configurações de Usuários',
- 'bits' => 271,
- 'children' => []
- ],
- [
- 'scope' => 'config.permission',
- 'description' => 'Configurações de Permissões',
- 'bits' => 271,
- 'children' => []
- ],
- [
- 'scope' => 'config.city',
- 'description' => 'Configurações de Cidades',
- 'bits' => 271,
- 'children' => []
- ],
- [
- 'scope' => 'config.country',
- 'description' => 'Configurações de Países',
- 'bits' => 271,
- 'children' => []
- ],
- [
- 'scope' => 'config.state',
- 'description' => 'Configurações de Estados',
- 'bits' => 271,
- 'children' => []
- ],
- ],
- ],
- ];
- $this->createPermissionsAndChildren(permissions: $permissions);
- }
- private function createPermissionsAndChildren(array $permissions, ?int $parent_id = null): void
- {
- foreach ($permissions as $permission) {
- if ($parent_id != null) {
- array_merge($permission, [
- 'parent_id' => $parent_id,
- ]);
- }
- $permissionDb = $this->permissionService->store(permissionDTO: PermissionDTO::fromArray(data: (array) $permission));
- if (!empty($permission['children'])) {
- $this->createPermissionsAndChildren(permissions: $permission['children'], parent_id: $permissionDb->id);
- }
- }
- }
- }
|