183 lines
6.0 KiB
PHP
183 lines
6.0 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Illuminate\Console\Command;
|
|
use Spatie\Permission\Models\Permission;
|
|
use Spatie\Permission\Models\Role;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class UpdateStampeRatePermissions extends Command
|
|
{
|
|
protected $signature = 'stampe-rate:update-permissions';
|
|
protected $description = 'Aggiorna e crea tutti i permessi necessari per il modulo StampeRate';
|
|
|
|
public function handle()
|
|
{
|
|
$this->info('🔧 Aggiornamento permessi modulo StampeRate...');
|
|
|
|
// Definisci tutti i permessi necessari
|
|
$permissions = [
|
|
// Permessi base StampeRate
|
|
[
|
|
'name' => 'stampe.rate.view',
|
|
'description' => 'Visualizza stampe rate',
|
|
'guard_name' => 'web'
|
|
],
|
|
[
|
|
'name' => 'stampe.rate.create',
|
|
'description' => 'Crea nuove stampe rate',
|
|
'guard_name' => 'web'
|
|
],
|
|
[
|
|
'name' => 'stampe.rate.update',
|
|
'description' => 'Modifica stampe rate esistenti',
|
|
'guard_name' => 'web'
|
|
],
|
|
[
|
|
'name' => 'stampe.rate.delete',
|
|
'description' => 'Elimina stampe rate',
|
|
'guard_name' => 'web'
|
|
],
|
|
[
|
|
'name' => 'stampe.rate.send',
|
|
'description' => 'Invia stampe rate via email',
|
|
'guard_name' => 'web'
|
|
],
|
|
|
|
// Permessi Template
|
|
[
|
|
'name' => 'stampe_rate_templates_view',
|
|
'description' => 'Visualizza template stampe rate',
|
|
'guard_name' => 'web'
|
|
],
|
|
[
|
|
'name' => 'stampe_rate_templates_create',
|
|
'description' => 'Crea nuovi template',
|
|
'guard_name' => 'web'
|
|
],
|
|
[
|
|
'name' => 'stampe_rate_templates_edit',
|
|
'description' => 'Modifica template esistenti',
|
|
'guard_name' => 'web'
|
|
],
|
|
[
|
|
'name' => 'stampe_rate_templates_delete',
|
|
'description' => 'Elimina template',
|
|
'guard_name' => 'web'
|
|
],
|
|
[
|
|
'name' => 'stampe_rate_templates_import',
|
|
'description' => 'Importa template esterni',
|
|
'guard_name' => 'web'
|
|
],
|
|
|
|
// Permessi Cronologia
|
|
[
|
|
'name' => 'stampe_rate_history_view',
|
|
'description' => 'Visualizza cronologia invii',
|
|
'guard_name' => 'web'
|
|
],
|
|
[
|
|
'name' => 'stampe_rate_reports_view',
|
|
'description' => 'Visualizza report e statistiche',
|
|
'guard_name' => 'web'
|
|
],
|
|
[
|
|
'name' => 'stampe_rate_export',
|
|
'description' => 'Esporta dati stampe rate',
|
|
'guard_name' => 'web'
|
|
],
|
|
[
|
|
'name' => 'stampe_rate_admin',
|
|
'description' => 'Amministrazione completa modulo',
|
|
'guard_name' => 'web'
|
|
],
|
|
];
|
|
|
|
$createdCount = 0;
|
|
$updatedCount = 0;
|
|
|
|
// Crea o aggiorna i permessi
|
|
foreach ($permissions as $permData) {
|
|
$permission = Permission::firstOrCreate(
|
|
['name' => $permData['name'], 'guard_name' => $permData['guard_name']],
|
|
$permData
|
|
);
|
|
|
|
if ($permission->wasRecentlyCreated) {
|
|
$createdCount++;
|
|
$this->info("✅ Creato: {$permData['name']}");
|
|
} else {
|
|
$permission->update(['description' => $permData['description']]);
|
|
$updatedCount++;
|
|
$this->info("🔄 Aggiornato: {$permData['name']}");
|
|
}
|
|
}
|
|
|
|
// Assegna i permessi ai ruoli amministrativi
|
|
$this->assignPermissionsToRoles($permissions);
|
|
|
|
// Aggiorna la configurazione del modulo nel database
|
|
$this->updateModulePermissions($permissions);
|
|
|
|
$this->info("🎉 Completato!");
|
|
$this->info("📊 Permessi creati: {$createdCount}");
|
|
$this->info("📊 Permessi aggiornati: {$updatedCount}");
|
|
|
|
return Command::SUCCESS;
|
|
}
|
|
|
|
private function assignPermissionsToRoles($permissions)
|
|
{
|
|
$adminRoles = ['admin', 'amministratore', 'super-admin'];
|
|
$assignedCount = 0;
|
|
|
|
$this->info('🔐 Assegnazione permessi ai ruoli...');
|
|
|
|
foreach ($adminRoles as $roleName) {
|
|
$role = Role::where('name', $roleName)->first();
|
|
|
|
if (!$role) {
|
|
$this->warn("⚠️ Ruolo '{$roleName}' non trovato, creazione...");
|
|
$role = Role::create(['name' => $roleName, 'guard_name' => 'web']);
|
|
$this->info("✅ Ruolo '{$roleName}' creato");
|
|
}
|
|
|
|
foreach ($permissions as $permData) {
|
|
$permission = Permission::where('name', $permData['name'])->first();
|
|
|
|
if ($permission && !$role->hasPermissionTo($permission)) {
|
|
$role->givePermissionTo($permission);
|
|
$assignedCount++;
|
|
}
|
|
}
|
|
|
|
$this->info("✅ Permessi assegnati al ruolo: {$roleName}");
|
|
}
|
|
|
|
$this->info("📊 Assegnazioni totali: {$assignedCount}");
|
|
}
|
|
|
|
private function updateModulePermissions($permissions)
|
|
{
|
|
$this->info('💾 Aggiornamento configurazione modulo...');
|
|
|
|
$permissionsData = array_map(function ($perm) {
|
|
return [
|
|
'name' => $perm['name'],
|
|
'description' => $perm['description'],
|
|
];
|
|
}, $permissions);
|
|
|
|
DB::table('modules')
|
|
->where('name', 'StampeRate')
|
|
->update([
|
|
'permissions' => json_encode($permissionsData),
|
|
'updated_at' => now()
|
|
]);
|
|
|
|
$this->info('✅ Configurazione modulo aggiornata');
|
|
}
|
|
}
|