| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- <?php
- namespace Database\Seeders;
- use App\Models\Permission;
- use Illuminate\Database\Seeder;
- use App\Services\PermissionService;
- class PermissionSeeder extends Seeder
- {
- public function __construct(
- protected PermissionService $permissionService,
- ) {}
- public function run(): void
- {
- $permissions = [
- [
- "scope" => "dashboard",
- "description" => "Dashboard",
- "bits" => Permission::ALL_PERMS,
- "children" => [],
- ],
- [
- "scope" => "franchisee",
- "description" => "Franchisee",
- "bits" => Permission::MENU | Permission::VIEW,
- "children" => [
- [
- "scope" => "unit",
- "description" => "Unidades",
- "bits" => Permission::ALL_PERMS,
- "children" => [],
- ],
- ],
- ],
- [
- "scope" => "tbr",
- "description" => "TBR",
- "bits" => Permission::ALL_PERMS,
- "children" => [
- [
- "scope" => "royalties-base-bracket",
- "description" => "Faixas de Royalties Base",
- "bits" => Permission::ALL_PERMS,
- "children" => [],
- ],
- [
- "scope" => "fnm-base-bracket",
- "description" => "Faixas de FNM Base",
- "bits" => Permission::ALL_PERMS,
- "children" => [],
- ],
- [
- "scope" => "maintenance-base-bracket",
- "description" => "Faixas de Manutenção Base",
- "bits" => Permission::ALL_PERMS,
- "children" => [],
- ],
- [
- "scope" => "franchisee-tbr",
- "description" => "TBR por Franqueado",
- "bits" => Permission::ALL_PERMS,
- "children" => [],
- ],
- [
- "scope" => "tbr-calculation",
- "description" => "Cálculo de TBR",
- "bits" => Permission::VIEW | Permission::ADD,
- "children" => [],
- ],
- ],
- ],
- [
- "scope" => "config",
- "description" => "Configurações",
- "bits" => Permission::MENU | Permission::VIEW,
- "children" => [
- [
- "scope" => "config.user",
- "description" => "Configurações de Usuários",
- "bits" => Permission::CRUD,
- "children" => [],
- ],
- [
- "scope" => "config.permission",
- "description" => "Configurações de Permissões",
- "bits" => Permission::CRUD,
- "children" => [],
- ],
- [
- "scope" => "config.city",
- "description" => "Configurações de Cidades",
- "bits" => Permission::CRUD,
- "children" => [],
- ],
- [
- "scope" => "config.country",
- "description" => "Configurações de Países",
- "bits" => Permission::CRUD,
- "children" => [],
- ],
- [
- "scope" => "config.state",
- "description" => "Configurações de Estados",
- "bits" => Permission::CRUD,
- "children" => [],
- ],
- ],
- ],
- ];
- $this->createPermissionsAndChildren(permissions: $permissions);
- }
- /**
- * 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);
- }
- if (!empty($children)) {
- $this->createPermissionsAndChildren(
- permissions: $children,
- parent: $permissionNode,
- );
- }
- }
- }
- }
|