✅ Completato: - Database modernizzato con chiavi id standard Laravel - Relazioni corrette Amministratore→Stabili→Movimenti - UI universale responsive con sidebar permission-based - Codici alfanumerici 8 caratteri implementati - Seeders con dati di test funzionanti - Documentazione tecnica completa (INSTALL_LINUX, TECHNICAL_SPECS, UPDATE_SYSTEM) 🔧 Miglioramenti: - Helper userSetting() funzionante - Sistema multi-database preparato - .gitignore aggiornato per sicurezza - Migration cleanup e ottimizzazione 📚 Documentazione: - Guida installazione Linux completa - Specifiche tecniche dettagliate - Sistema aggiornamenti progettato - Progress log aggiornato
94 lines
2.6 KiB
PHP
94 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
use App\Models\Amministratore;
|
|
use App\Models\User;
|
|
use Closure;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
class UserSpaceAccess
|
|
{
|
|
/**
|
|
* Verifica l'accesso allo spazio utente tramite codice 8 cifre
|
|
*
|
|
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
|
|
*/
|
|
public function handle(Request $request, Closure $next): Response
|
|
{
|
|
$userCode = $request->route('userCode');
|
|
|
|
// Validazione formato codice
|
|
if (!preg_match('/^[A-Z0-9]{8}$/', $userCode)) {
|
|
abort(404, 'Codice utente non valido');
|
|
}
|
|
|
|
// Trova l'utente dal codice
|
|
$targetUser = $this->findUserByCode($userCode);
|
|
|
|
if (!$targetUser) {
|
|
abort(404, 'Utente non trovato');
|
|
}
|
|
|
|
// Verifica autorizzazione
|
|
if (!Auth::check() || !$this->canAccessUserSpace(Auth::user(), $targetUser)) {
|
|
return redirect()->route('login')->with('error', 'Accesso non autorizzato a questo spazio utente');
|
|
}
|
|
|
|
// Aggiungi i dati dell'utente target alla request
|
|
$request->merge([
|
|
'targetUser' => $targetUser,
|
|
'targetUserCode' => $userCode
|
|
]);
|
|
|
|
return $next($request);
|
|
}
|
|
|
|
/**
|
|
* Trova utente dal codice
|
|
*
|
|
* @param string $code
|
|
* @return User|null
|
|
*/
|
|
private function findUserByCode($code)
|
|
{
|
|
// Prima cerca negli amministratori
|
|
$amministratore = Amministratore::where('codice', $code)->first();
|
|
if ($amministratore && $amministratore->user) {
|
|
return $amministratore->user;
|
|
}
|
|
|
|
// Poi cerca nella tabella users (futuro sviluppo)
|
|
// TODO: Aggiungere campo codice alla tabella users per condomini
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Verifica se l'utente può accedere allo spazio
|
|
*
|
|
* @param User $loggedUser
|
|
* @param User $targetUser
|
|
* @return bool
|
|
*/
|
|
private function canAccessUserSpace($loggedUser, $targetUser)
|
|
{
|
|
// L'utente può accedere al proprio spazio
|
|
if ($loggedUser->id === $targetUser->id) {
|
|
return true;
|
|
}
|
|
|
|
// Super-admin può accedere a qualsiasi spazio
|
|
if ($loggedUser->hasRole('super-admin')) {
|
|
return true;
|
|
}
|
|
|
|
// Amministratori possono accedere solo al proprio spazio
|
|
// (in futuro: gestire impersonificazione, deleghe, ecc.)
|
|
|
|
return false;
|
|
}
|
|
}
|