From f62d05cb2878d84ca210412c095239832e2d7227 Mon Sep 17 00:00:00 2001 From: Lizandro Guarnizo <77708265+lizandrogd@users.noreply.github.com> Date: Thu, 29 Jan 2026 15:58:22 -0500 Subject: [PATCH] up --- .../Commands/ScanResourcesPermissions.php | 113 ++++++++++++++++++ app/Filament/Resources/OjalResource.php | 2 +- app/Filament/Resources/PrensillaResource.php | 2 +- database/seeders/AdminSeeder.php | 24 ++++ 4 files changed, 139 insertions(+), 2 deletions(-) create mode 100644 app/Console/Commands/ScanResourcesPermissions.php create mode 100644 database/seeders/AdminSeeder.php diff --git a/app/Console/Commands/ScanResourcesPermissions.php b/app/Console/Commands/ScanResourcesPermissions.php new file mode 100644 index 0000000..7f82497 --- /dev/null +++ b/app/Console/Commands/ScanResourcesPermissions.php @@ -0,0 +1,113 @@ +info('Escaneando recursos Filament...'); + + $fs = new Filesystem(); + $resourcesPath = app_path('Filament/Resources'); + + if (! $fs->isDirectory($resourcesPath)) { + $this->error("No se encontró la carpeta {$resourcesPath}. Ningún permiso fue creado."); + return 1; + } + + $files = $fs->allFiles($resourcesPath); + + $created = []; + + foreach ($files as $file) { + if (! Str::endsWith($file->getFilename(), 'Resource.php')) { + continue; + } + + $className = 'App\\Filament\\Resources\\' . str_replace('.php', '', $file->getFilename()); + + // Si la clase está disponible, intentamos obtener su navigationLabel para usar nombres en español + if (class_exists($className)) { + $label = $className::$navigationLabel ?? null; + } else { + $label = null; + } + + $base = $label ? Str::lower($label) : Str::lower(Str::replaceLast('Resource', '', $file->getFilename())); + + // Normalizar: eliminar espacios extra + $base = trim((string) Str::of($base)->lower()); + + // Generar formas singular y plural + $plural = Str::lower(Str::plural($base)); + $singular = Str::lower(Str::singular($base)); + + // Generar permisos comunes + $perms = [ + "ver {$plural}", + "crear {$singular}", + "editar {$singular}", + "eliminar {$singular}", + ]; + + foreach ($perms as $permName) { + $permission = Permission::firstOrCreate(['name' => $permName]); + $created[] = $permission->name; + } + } + + // Asegurar permisos para roles y permisos (usados en Resource::canViewAny en algunos casos) + Permission::firstOrCreate(['name' => 'ver roles']); + Permission::firstOrCreate(['name' => 'ver permisos']); + + $this->info('Permisos generados/asegurados: ' . count($created)); + $this->line(implode("\n", array_slice($created, 0, 200))); + + // Crear rol y asignar todos los permisos + $roleName = $this->option('role') ?? 'Administrador'; + $role = Role::firstOrCreate(['name' => $roleName]); + $all = Permission::all(); + $role->syncPermissions($all); + + $this->info("Rol '{$roleName}' creado/actualizado y se le asignaron " . $all->count() . " permisos."); + + // Si se solicita, crear usuario y asignar rol + $createUserEmail = $this->option('create-user'); + $password = $this->option('password'); + + if ($createUserEmail) { + if (! $password) { + $this->error('Si usas --create-user debes pasar también --password.'); + return 1; + } + + $user = User::updateOrCreate( + ['email' => $createUserEmail], + ['name' => $roleName, 'password' => Hash::make($password)] + ); + + $user->assignRole($role); + $this->info("Usuario '{$createUserEmail}' creado/actualizado y se le asignó el rol '{$roleName}'."); + } + + $this->info('Finalizado.'); + + return 0; + } +} diff --git a/app/Filament/Resources/OjalResource.php b/app/Filament/Resources/OjalResource.php index 7821a55..90a3c5b 100644 --- a/app/Filament/Resources/OjalResource.php +++ b/app/Filament/Resources/OjalResource.php @@ -14,7 +14,7 @@ use Filament\Forms\Components\DatePicker; class OjalResource extends Resource { protected static ?string $model = Ojal::class; - protected static ?string $navigationIcon = 'heroicon-o-collection'; + protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack'; protected static ?string $navigationGroup = 'Producción'; public static function form(Forms\Form $form): Forms\Form diff --git a/app/Filament/Resources/PrensillaResource.php b/app/Filament/Resources/PrensillaResource.php index 1cbdce3..9df5f77 100644 --- a/app/Filament/Resources/PrensillaResource.php +++ b/app/Filament/Resources/PrensillaResource.php @@ -14,7 +14,7 @@ use Filament\Forms\Components\DatePicker; class PrensillaResource extends Resource { protected static ?string $model = Prensilla::class; - protected static ?string $navigationIcon = 'heroicon-o-collection'; + protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack'; protected static ?string $navigationGroup = 'Producción'; public static function form(Forms\Form $form): Forms\Form diff --git a/database/seeders/AdminSeeder.php b/database/seeders/AdminSeeder.php new file mode 100644 index 0000000..38e6fce --- /dev/null +++ b/database/seeders/AdminSeeder.php @@ -0,0 +1,24 @@ + 'info@creacionesheidivers.com', + '--password' => 'Creaciones2026*+', + '--role' => 'Administrador', + ]); + + $this->command->info('AdminSeeder: comando permissions:scan ejecutado.'); + } +}