180 lines
6.9 KiB
PHP
Executable File
180 lines
6.9 KiB
PHP
Executable File
<?php
|
|
namespace App\Providers;
|
|
|
|
use Illuminate\Contracts\Session\Session;
|
|
use Illuminate\Foundation\Http\Events\RequestHandled;
|
|
use Illuminate\Support\Facades\Event;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Illuminate\Support\Facades\URL;
|
|
use Illuminate\Support\Facades\View;
|
|
use Illuminate\Support\ServiceProvider;
|
|
|
|
// Spatie Permission (permessi/ruoli)
|
|
use Spatie\Permission\Models\Permission;
|
|
use Spatie\Permission\Models\Role;
|
|
|
|
class AppServiceProvider extends ServiceProvider
|
|
{
|
|
/**
|
|
* Register any application services.
|
|
*/
|
|
public function register(): void
|
|
{
|
|
$this->app->singleton('db.schema', function ($app) {
|
|
$connection = $app['db']->connection();
|
|
|
|
if (class_exists(\Esign\DatabaseTrigger\Facades\Schema::class) && $connection->getDriverName() === 'mysql') {
|
|
return \Esign\DatabaseTrigger\Facades\Schema::getSchemaBuilder($connection);
|
|
}
|
|
|
|
return $connection->getSchemaBuilder();
|
|
});
|
|
|
|
if (interface_exists(\Laravel\Fortify\Contracts\VerifyEmailViewResponse::class)) {
|
|
$this->app->singleton(
|
|
\Laravel\Fortify\Contracts\VerifyEmailViewResponse::class,
|
|
\App\Http\Responses\FortifyVerifyEmailViewResponse::class
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Bootstrap any application services.
|
|
*/
|
|
public function boot(): void
|
|
{
|
|
// For reverse-proxy HTTPS deployments (iPhone/Safari auth cookie compatibility).
|
|
if ((bool) config('app.force_https', false)) {
|
|
URL::forceScheme('https');
|
|
URL::forceRootUrl((string) config('app.url'));
|
|
}
|
|
|
|
$this->sanitizeFilamentNotificationSessionPayload();
|
|
$this->registerLivewireDebugLogger();
|
|
|
|
// View Composer per la sidebar
|
|
View::composer([
|
|
'components.menu.sidebar',
|
|
'components.menu.sidebar-netgescon',
|
|
'superadmin.dashboard',
|
|
'admin.dashboard',
|
|
], \App\Http\View\Composers\SidebarComposer::class);
|
|
|
|
// Helper per le impostazioni utente
|
|
if (! function_exists('userSetting')) {
|
|
function userSetting($key, $default = null)
|
|
{
|
|
return \App\Models\UserSetting::get($key, $default);
|
|
}
|
|
}
|
|
|
|
// Registra comandi Artisan applicativi quando in console
|
|
if ($this->app->runningInConsole()) {
|
|
$this->commands([
|
|
\App\Console\Commands\CodeQualityScan::class,
|
|
]);
|
|
}
|
|
|
|
// Bootstrap permessi base (idempotente): crea permessi e assegna a super-admin
|
|
try {
|
|
if (Schema::hasTable('permissions') && Schema::hasTable('roles') && Schema::hasTable('role_has_permissions')) {
|
|
// Crea permessi se mancanti
|
|
$pViewImport = Permission::findOrCreate('gescon-import.view');
|
|
$pExecImport = Permission::findOrCreate('gescon-import.execute');
|
|
$pViewContab = Permission::findOrCreate('contabilita-gescon.view');
|
|
// Permesso specifico per accedere/modificare la Configurazione di Gescon Import
|
|
$pConfigImport = Permission::findOrCreate('gescon-import.config');
|
|
// Permessi gestione utenti (pannello SuperAdmin)
|
|
$pViewUsers = Permission::findOrCreate('view-users');
|
|
$pCreateUsers = Permission::findOrCreate('create-users');
|
|
$pManageUsers = Permission::findOrCreate('manage-users');
|
|
$pImpersonateUsers = Permission::findOrCreate('impersonate-users');
|
|
|
|
// Assicura ruolo super-admin esiste e possiede tutti i permessi sensibili
|
|
$super = Role::findOrCreate('super-admin');
|
|
$super->givePermissionTo([$pViewImport, $pExecImport, $pConfigImport, $pViewContab, $pViewUsers, $pCreateUsers, $pManageUsers, $pImpersonateUsers]);
|
|
|
|
// Nota: ruolo 'amministratore' NON riceve questi permessi di default
|
|
Role::findOrCreate('amministratore');
|
|
Role::findOrCreate('collaboratore');
|
|
}
|
|
} catch (\Throwable $e) {
|
|
// Silenzia in bootstrap (es. durante installazione senza DB)
|
|
}
|
|
}
|
|
|
|
private function sanitizeFilamentNotificationSessionPayload(): void
|
|
{
|
|
if ($this->app->runningInConsole() || ! $this->app->bound('session.store')) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
/** @var Session $session */
|
|
$session = $this->app->make('session.store');
|
|
$raw = $session->get('filament.notifications');
|
|
|
|
if ($raw === null) {
|
|
return;
|
|
}
|
|
|
|
if (! is_array($raw)) {
|
|
$session->forget('filament.notifications');
|
|
|
|
return;
|
|
}
|
|
|
|
$normalized = array_values(array_filter($raw, static fn(mixed $notification): bool => is_array($notification)));
|
|
|
|
if ($normalized !== $raw) {
|
|
$session->put('filament.notifications', $normalized);
|
|
$path = storage_path('app/support/filament-notification-sanitize.json');
|
|
$dir = dirname($path);
|
|
|
|
if (! is_dir($dir)) {
|
|
@mkdir($dir, 0775, true);
|
|
}
|
|
|
|
@file_put_contents($path, json_encode([
|
|
'timestamp' => now()->toIso8601String(),
|
|
'removed_items' => max(0, count($raw) - count($normalized)),
|
|
'remaining_items' => count($normalized),
|
|
'reason' => 'Filament notifications payload contained non-array items.',
|
|
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
|
|
}
|
|
} catch (\Throwable $e) {
|
|
// Evita che una sessione corrotta blocchi il bootstrap dell'applicazione.
|
|
}
|
|
}
|
|
|
|
private function registerLivewireDebugLogger(): void
|
|
{
|
|
if ($this->app->runningInConsole()) {
|
|
return;
|
|
}
|
|
|
|
Event::listen(RequestHandled::class, function (RequestHandled $event): void {
|
|
$request = $event->request;
|
|
|
|
if ($request->path() !== 'livewire/update') {
|
|
return;
|
|
}
|
|
|
|
$content = (string) $request->getContent();
|
|
|
|
$path = storage_path('logs/livewire-login-debug.ndjson');
|
|
|
|
@file_put_contents($path, json_encode([
|
|
'timestamp' => now()->toIso8601String(),
|
|
'method' => $request->method(),
|
|
'path' => $request->path(),
|
|
'ip' => $request->ip(),
|
|
'request' => json_decode($content, true),
|
|
'raw_request' => $content,
|
|
'response_status' => $event->response->getStatusCode(),
|
|
'response_body' => method_exists($event->response, 'getContent') ? $event->response->getContent() : null,
|
|
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL, FILE_APPEND);
|
|
});
|
|
}
|
|
}
|