84 lines
2.6 KiB
PHP
84 lines
2.6 KiB
PHP
<?php
|
|
namespace App\Filament\Auth;
|
|
|
|
use App\Filament\Pages\Fornitore\TicketOperativi;
|
|
use App\Filament\Pages\Gescon\FornitoreScheda;
|
|
use App\Models\Fornitore;
|
|
use App\Models\FornitoreDipendente;
|
|
use App\Models\User;
|
|
use Filament\Auth\Http\Responses\Contracts\LoginResponse;
|
|
use Filament\Auth\Pages\Login as BaseLogin;
|
|
use Filament\Facades\Filament;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class Login extends BaseLogin
|
|
{
|
|
/**
|
|
* Forza la permanenza nel pannello Filament dopo il login.
|
|
* Evita che una precedente visita a /admin (UI classica) imposti url.intended.
|
|
*/
|
|
protected function getRedirectUrl(): string
|
|
{
|
|
$user = Auth::user();
|
|
|
|
if ($user instanceof User && $user->hasRole('fornitore')) {
|
|
return $this->resolveSupplierRedirectUrl($user);
|
|
}
|
|
|
|
return '/admin-filament';
|
|
}
|
|
|
|
private function resolveSupplierRedirectUrl(User $user): string
|
|
{
|
|
$email = mb_strtolower(trim((string) $user->email));
|
|
if ($email !== '') {
|
|
$fornitore = Fornitore::query()
|
|
->whereRaw('LOWER(email) = ?', [$email])
|
|
->orderByDesc('id')
|
|
->first();
|
|
|
|
if ($fornitore instanceof Fornitore) {
|
|
return FornitoreScheda::getUrl(['record' => (int) $fornitore->id], panel: 'admin-filament');
|
|
}
|
|
}
|
|
|
|
$dipendente = FornitoreDipendente::query()
|
|
->where('attivo', true)
|
|
->where(function ($query) use ($user, $email): void {
|
|
$query->where('user_id', (int) $user->id);
|
|
|
|
if ($email !== '') {
|
|
$query->orWhereRaw('LOWER(email) = ?', [$email]);
|
|
}
|
|
})
|
|
->orderByDesc('id')
|
|
->first();
|
|
|
|
if ($dipendente instanceof FornitoreDipendente) {
|
|
return FornitoreScheda::getUrl(['record' => (int) $dipendente->fornitore_id], panel: 'admin-filament');
|
|
}
|
|
|
|
return TicketOperativi::getUrl(panel: 'admin-filament');
|
|
}
|
|
|
|
public function authenticate(): ?LoginResponse
|
|
{
|
|
$response = parent::authenticate();
|
|
|
|
if ($response) {
|
|
Log::info('Filament login forced redirect (backup4)', [
|
|
'panel_id' => Filament::getCurrentPanel()?->getId(),
|
|
'filament_guard_check' => Filament::auth()->check(),
|
|
'filament_user_id' => Filament::auth()->id(),
|
|
]);
|
|
|
|
$this->redirect($this->getRedirectUrl());
|
|
|
|
return null;
|
|
}
|
|
|
|
return $response;
|
|
}
|
|
}
|