up
This commit is contained in:
@@ -40,19 +40,29 @@ class ScanResourcesPermissions extends Command
|
||||
continue;
|
||||
}
|
||||
|
||||
$className = 'App\\Filament\\Resources\\' . str_replace('.php', '', $file->getFilename());
|
||||
$filename = pathinfo($file->getFilename(), PATHINFO_FILENAME);
|
||||
$className = 'App\\Filament\\Resources\\' . $filename;
|
||||
|
||||
// Si la clase está disponible, intentamos obtener su navigationLabel para usar nombres en español
|
||||
// Intentar obtener un label legible: preferimos el método getNavigationLabel(), luego la propiedad estática
|
||||
$label = null;
|
||||
if (class_exists($className)) {
|
||||
$label = $className::$navigationLabel ?? null;
|
||||
} else {
|
||||
$label = null;
|
||||
if (method_exists($className, 'getNavigationLabel')) {
|
||||
$label = $className::getNavigationLabel();
|
||||
} elseif (isset($className::$navigationLabel)) {
|
||||
$label = $className::$navigationLabel;
|
||||
}
|
||||
}
|
||||
|
||||
$base = $label ? Str::lower($label) : Str::lower(Str::replaceLast('Resource', '', $file->getFilename()));
|
||||
// Si no hay label, usar el nombre del archivo sin extensión ni sufijo Resource
|
||||
if ($label) {
|
||||
$base = Str::lower($label);
|
||||
} else {
|
||||
$base = Str::lower(Str::replaceLast('Resource', '', $filename));
|
||||
}
|
||||
|
||||
// Normalizar: eliminar espacios extra
|
||||
// Normalizar: eliminar espacios extra y restos de extensión
|
||||
$base = trim((string) Str::of($base)->lower());
|
||||
$base = preg_replace('/\.php$/', '', $base);
|
||||
|
||||
// Generar formas singular y plural
|
||||
$plural = Str::lower(Str::plural($base));
|
||||
@@ -79,6 +89,9 @@ class ScanResourcesPermissions extends Command
|
||||
$this->info('Permisos generados/asegurados: ' . count($created));
|
||||
$this->line(implode("\n", array_slice($created, 0, 200)));
|
||||
|
||||
// Eliminar permisos mal formados (ej. que contienen '.php') generados por escaneos previos
|
||||
Permission::where('name', 'like', '%.php%')->orWhere('name', 'like', '%.phps%')->delete();
|
||||
|
||||
// Crear rol y asignar todos los permisos
|
||||
$roleName = $this->option('role') ?? 'Administrador';
|
||||
$role = Role::firstOrCreate(['name' => $roleName]);
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
require __DIR__ . '/../vendor/autoload.php';
|
||||
$app = require_once __DIR__ . '/../bootstrap/app.php';
|
||||
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
|
||||
$kernel->bootstrap();
|
||||
|
||||
use Spatie\Permission\Models\Permission;
|
||||
use Spatie\Permission\Models\Role;
|
||||
use App\Models\User;
|
||||
|
||||
echo "Verificando permisos y roles...\n";
|
||||
|
||||
$permissions = Permission::pluck('name')->toArray();
|
||||
$hasVerColores = in_array('ver colores', $permissions);
|
||||
|
||||
echo "Permisos totales: " . count($permissions) . "\n";
|
||||
echo "Existe permiso 'ver colores': " . ($hasVerColores ? 'si' : 'no') . "\n";
|
||||
|
||||
$role = Role::firstOrCreate(['name' => 'Administrador']);
|
||||
$role->syncPermissions($permissions);
|
||||
|
||||
echo "Role 'Administrador' ahora tiene " . $role->permissions->count() . " permisos.\n";
|
||||
|
||||
$user = User::where('email', 'info@creacionesheidivers.com')->first();
|
||||
if (! $user) {
|
||||
echo "Usuario info@creacionesheidivers.com no encontrado en la BD.\n";
|
||||
exit(0);
|
||||
}
|
||||
|
||||
$user->assignRole($role);
|
||||
|
||||
echo "Usuario '" . $user->email . "' roles: " . implode(', ', $user->getRoleNames()->toArray()) . "\n";
|
||||
echo "Usuario tiene permiso 'ver colores': " . ($user->can('ver colores') ? 'si' : 'no') . "\n";
|
||||
|
||||
// Mostrar permisos específicos del role que contengan 'ver'
|
||||
$verPerms = $role->permissions->pluck('name')->filter(function ($p) { return str_starts_with($p, 'ver '); })->toArray();
|
||||
|
||||
echo "Permisos 'ver' asignados al rol Administrador: " . implode(', ', $verPerms) . "\n";
|
||||
|
||||
echo "Hecho.\n";
|
||||
Reference in New Issue
Block a user