up
This commit is contained in:
@@ -0,0 +1,113 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use Illuminate\Filesystem\Filesystem;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
use Spatie\Permission\Models\Permission;
|
||||||
|
use Spatie\Permission\Models\Role;
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Support\Facades\Hash;
|
||||||
|
|
||||||
|
class ScanResourcesPermissions extends Command
|
||||||
|
{
|
||||||
|
protected $signature = 'permissions:scan
|
||||||
|
{--create-user= : Email del usuario a crear}
|
||||||
|
{--password= : Contraseña para el usuario (si se usa --create-user)}
|
||||||
|
{--role=Administrador : Nombre del rol a crear}';
|
||||||
|
|
||||||
|
protected $description = 'Escanea app/Filament/Resources para generar permisos CRUD, crear rol y asignar permisos. Opcionalmente crea un usuario y le asigna el rol.';
|
||||||
|
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
$this->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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -14,7 +14,7 @@ use Filament\Forms\Components\DatePicker;
|
|||||||
class OjalResource extends Resource
|
class OjalResource extends Resource
|
||||||
{
|
{
|
||||||
protected static ?string $model = Ojal::class;
|
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';
|
protected static ?string $navigationGroup = 'Producción';
|
||||||
|
|
||||||
public static function form(Forms\Form $form): Forms\Form
|
public static function form(Forms\Form $form): Forms\Form
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ use Filament\Forms\Components\DatePicker;
|
|||||||
class PrensillaResource extends Resource
|
class PrensillaResource extends Resource
|
||||||
{
|
{
|
||||||
protected static ?string $model = Prensilla::class;
|
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';
|
protected static ?string $navigationGroup = 'Producción';
|
||||||
|
|
||||||
public static function form(Forms\Form $form): Forms\Form
|
public static function form(Forms\Form $form): Forms\Form
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Database\Seeders;
|
||||||
|
|
||||||
|
use Illuminate\Database\Seeder;
|
||||||
|
use Illuminate\Support\Facades\Artisan;
|
||||||
|
|
||||||
|
class AdminSeeder extends Seeder
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the database seeds.
|
||||||
|
*/
|
||||||
|
public function run(): void
|
||||||
|
{
|
||||||
|
// Esto ejecutará el comando que escanea recursos, crea permisos, crea/actualiza el rol y crea el usuario
|
||||||
|
Artisan::call('permissions:scan', [
|
||||||
|
'--create-user' => 'info@creacionesheidivers.com',
|
||||||
|
'--password' => 'Creaciones2026*+',
|
||||||
|
'--role' => 'Administrador',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->command->info('AdminSeeder: comando permissions:scan ejecutado.');
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user