218 lines
6.1 KiB
PHP
218 lines
6.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Universal;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Amministratore;
|
|
use App\Models\User;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Schema;
|
|
|
|
class UserSpaceController extends Controller
|
|
{
|
|
/**
|
|
* Gestisce l'accesso universale tramite codice 8 cifre
|
|
*
|
|
* @param string $userCode Codice utente di 8 caratteri
|
|
* @param Request $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function handleUserSpace($userCode, Request $request)
|
|
{
|
|
// Validazione formato codice
|
|
if (!$this->isValidUserCode($userCode)) {
|
|
abort(404, 'Codice utente non valido');
|
|
}
|
|
|
|
// Trova l'utente dal codice
|
|
$user = $this->findUserByCode($userCode);
|
|
|
|
if (!$user) {
|
|
abort(404, 'Utente non trovato');
|
|
}
|
|
|
|
// Verifica se l'utente loggato corrisponde al codice
|
|
if (!Auth::check() || !$this->canAccessUserSpace(Auth::user(), $user)) {
|
|
return redirect()->route('login')->with('error', 'Accesso non autorizzato');
|
|
}
|
|
|
|
// Reindirizza alla dashboard appropriata
|
|
return $this->redirectToDashboard($user, $userCode);
|
|
}
|
|
|
|
/**
|
|
* Dashboard universale per utente specifico
|
|
*
|
|
* @param string $userCode
|
|
* @return \Illuminate\View\View
|
|
*/
|
|
public function dashboard($userCode)
|
|
{
|
|
$user = $this->findUserByCode($userCode);
|
|
|
|
if (!$user || !$this->canAccessUserSpace(Auth::user(), $user)) {
|
|
abort(403, 'Accesso negato');
|
|
}
|
|
|
|
// Determina il tipo di dashboard
|
|
if ($user->hasRole('super-admin')) {
|
|
return $this->superAdminDashboard($user, $userCode);
|
|
} elseif ($user->hasRole(['admin', 'amministratore'])) {
|
|
return $this->adminDashboard($user, $userCode);
|
|
} elseif ($user->hasRole('condomino')) {
|
|
return $this->condominoDashboard($user, $userCode);
|
|
}
|
|
|
|
abort(403, 'Ruolo non riconosciuto');
|
|
}
|
|
|
|
/**
|
|
* Valida il formato del codice utente
|
|
*
|
|
* @param string $code
|
|
* @return bool
|
|
*/
|
|
private function isValidUserCode($code)
|
|
{
|
|
// Formato: 8 caratteri alfanumerici maiuscoli
|
|
return preg_match('/^[A-Z0-9]{8}$/', $code);
|
|
}
|
|
|
|
/**
|
|
* Trova utente dal codice
|
|
*
|
|
* @param string $code
|
|
* @return User|null
|
|
*/
|
|
private function findUserByCode($code)
|
|
{
|
|
// Prima cerca negli amministratori
|
|
$amministratore = Amministratore::where('codice_univoco', $code)->first();
|
|
if ($amministratore && $amministratore->user) {
|
|
return $amministratore->user;
|
|
}
|
|
|
|
// Poi cerca nella tabella users (se ha campo codice)
|
|
if (Schema::hasColumn('users', 'codice')) {
|
|
return User::where('codice', $code)->first();
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
// Altri casi di accesso autorizzato (es. impersonificazione)
|
|
// TODO: Implementare logica impersonificazione
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Reindirizza alla dashboard appropriata
|
|
*
|
|
* @param User $user
|
|
* @param string $userCode
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
private function redirectToDashboard($user, $userCode)
|
|
{
|
|
return redirect()->route('userspace.dashboard', ['userCode' => $userCode]);
|
|
}
|
|
|
|
/**
|
|
* Dashboard Super Admin
|
|
*
|
|
* @param User $user
|
|
* @param string $userCode
|
|
* @return \Illuminate\View\View
|
|
*/
|
|
private function superAdminDashboard($user, $userCode)
|
|
{
|
|
$stats = [
|
|
'amministratori_count' => Amministratore::count(),
|
|
'amministratori_attivi' => Amministratore::whereHas('user', function($q) {
|
|
$q->where('email_verified_at', '!=', null);
|
|
})->count(),
|
|
'database_count' => 0, // TODO: Implementare conteggio database
|
|
'sistema_versione' => config('app.version', '1.0.0'),
|
|
'uptime' => $this->getSystemUptime(),
|
|
];
|
|
|
|
return view('universal.dashboard.superadmin', compact('user', 'userCode', 'stats'));
|
|
}
|
|
|
|
/**
|
|
* Dashboard Amministratore
|
|
*
|
|
* @param User $user
|
|
* @param string $userCode
|
|
* @return \Illuminate\View\View
|
|
*/
|
|
private function adminDashboard($user, $userCode)
|
|
{
|
|
$amministratore = $user->amministratore;
|
|
|
|
$stats = [
|
|
'stabili_count' => $amministratore ? $amministratore->stabili()->count() : 0,
|
|
'unita_count' => 0, // TODO: Conteggio unità immobiliari
|
|
'tickets_aperti' => 0, // TODO: Conteggio ticket aperti
|
|
'scadenze_prossime' => 0, // TODO: Conteggio scadenze
|
|
];
|
|
|
|
return view('universal.dashboard.admin', compact('user', 'userCode', 'amministratore', 'stats'));
|
|
}
|
|
|
|
/**
|
|
* Dashboard Condomino
|
|
*
|
|
* @param User $user
|
|
* @param string $userCode
|
|
* @return \Illuminate\View\View
|
|
*/
|
|
private function condominoDashboard($user, $userCode)
|
|
{
|
|
$stats = [
|
|
'unita_di_proprieta' => 0, // TODO: Implementare
|
|
'tickets_aperti' => 0,
|
|
'documenti_recenti' => 0,
|
|
'avvisi_non_letti' => 0,
|
|
];
|
|
|
|
return view('universal.dashboard.condomino', compact('user', 'userCode', 'stats'));
|
|
}
|
|
|
|
/**
|
|
* Ottiene l'uptime del sistema
|
|
*
|
|
* @return string
|
|
*/
|
|
private function getSystemUptime()
|
|
{
|
|
if (PHP_OS_FAMILY === 'Linux') {
|
|
$uptime = shell_exec('uptime -p');
|
|
return trim($uptime ?: 'N/A');
|
|
}
|
|
|
|
return 'N/A';
|
|
}
|
|
}
|