|
|
@@ -2,7 +2,7 @@
|
|
|
|
|
|
namespace Database\Seeders;
|
|
|
|
|
|
-use App\DTO\PermissionDTO;
|
|
|
+use App\Models\Permission;
|
|
|
use Illuminate\Database\Seeder;
|
|
|
use App\Services\PermissionService;
|
|
|
|
|
|
@@ -30,45 +30,45 @@ public function run(): void
|
|
|
*/
|
|
|
$permissions = [
|
|
|
[
|
|
|
- 'scope' => 'dashboard',
|
|
|
- 'description' => 'Dashboard',
|
|
|
- 'bits' => 511,
|
|
|
- 'children' => []
|
|
|
+ "scope" => "dashboard",
|
|
|
+ "description" => "Dashboard",
|
|
|
+ "bits" => 511,
|
|
|
+ "children" => [],
|
|
|
],
|
|
|
[
|
|
|
- 'scope' => 'config',
|
|
|
- 'description' => 'Configurações',
|
|
|
- 'bits' => 271,
|
|
|
- 'children' => [
|
|
|
+ "scope" => "config",
|
|
|
+ "description" => "Configurações",
|
|
|
+ "bits" => 271,
|
|
|
+ "children" => [
|
|
|
[
|
|
|
- 'scope' => 'config.user',
|
|
|
- 'description' => 'Configurações de Usuários',
|
|
|
- '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.permission",
|
|
|
+ "description" => "Configurações de Permissões",
|
|
|
+ "bits" => 271,
|
|
|
+ "children" => [],
|
|
|
],
|
|
|
[
|
|
|
- 'scope' => 'config.city',
|
|
|
- 'description' => 'Configurações de Cidades',
|
|
|
- '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.country",
|
|
|
+ "description" => "Configurações de Países",
|
|
|
+ "bits" => 271,
|
|
|
+ "children" => [],
|
|
|
],
|
|
|
[
|
|
|
- 'scope' => 'config.state',
|
|
|
- 'description' => 'Configurações de Estados',
|
|
|
- 'bits' => 271,
|
|
|
- 'children' => []
|
|
|
+ "scope" => "config.state",
|
|
|
+ "description" => "Configurações de Estados",
|
|
|
+ "bits" => 271,
|
|
|
+ "children" => [],
|
|
|
],
|
|
|
],
|
|
|
],
|
|
|
@@ -77,17 +77,34 @@ public function run(): void
|
|
|
$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,
|
|
|
- ]);
|
|
|
+ /**
|
|
|
+ * Recursively creates or updates permissions and handles nesting.
|
|
|
+ *
|
|
|
+ * @param array $permissions The array of permission data.
|
|
|
+ * @param Permission|null $parent The parent Permission object (for nested sets).
|
|
|
+ */
|
|
|
+ private function createPermissionsAndChildren(
|
|
|
+ array $permissions,
|
|
|
+ ?Permission $parent = null,
|
|
|
+ ): void {
|
|
|
+ foreach ($permissions as $permissionData) {
|
|
|
+ $children = $permissionData["children"];
|
|
|
+ unset($permissionData["children"]);
|
|
|
+
|
|
|
+ $permissionNode = Permission::updateOrCreate(
|
|
|
+ ["scope" => $permissionData["scope"]],
|
|
|
+ $permissionData,
|
|
|
+ );
|
|
|
+
|
|
|
+ if ($parent) {
|
|
|
+ $parent->appendNode($permissionNode);
|
|
|
}
|
|
|
- $permissionDb = $this->permissionService->store(permissionDTO: PermissionDTO::fromArray(data: (array) $permission));
|
|
|
- if (!empty($permission['children'])) {
|
|
|
- $this->createPermissionsAndChildren(permissions: $permission['children'], parent_id: $permissionDb->id);
|
|
|
+
|
|
|
+ if (!empty($children)) {
|
|
|
+ $this->createPermissionsAndChildren(
|
|
|
+ permissions: $children,
|
|
|
+ parent: $permissionNode,
|
|
|
+ );
|
|
|
}
|
|
|
}
|
|
|
}
|