112 lines
2.1 KiB
PHP
Executable File
112 lines
2.1 KiB
PHP
Executable File
<?php
|
|
|
|
session_start();
|
|
|
|
require_once __DIR__ . "/config/database.php";
|
|
|
|
if ($_SERVER["REQUEST_METHOD"] !== "POST"){
|
|
|
|
header("Location: index.php");
|
|
exit;
|
|
|
|
}
|
|
|
|
$email = trim($_POST["email"] ?? "");
|
|
$password = $_POST["password"] ?? "";
|
|
|
|
// Cerco l'utente
|
|
$stmt = $pdo->prepare("
|
|
SELECT
|
|
utente.idUtente,
|
|
utente.nome,
|
|
utente.cognome,
|
|
utente.email,
|
|
utente.passwordHash,
|
|
utente.attivo,
|
|
|
|
CASE
|
|
WHEN proprietario.idUtente IS NOT NULL
|
|
THEN 'proprietario'
|
|
|
|
WHEN personale.idUtente IS NOT NULL
|
|
THEN 'personale'
|
|
|
|
WHEN cliente.idUtente IS NOT NULL
|
|
THEN 'cliente'
|
|
|
|
ELSE NULL
|
|
END as ruolo
|
|
|
|
FROM utente
|
|
|
|
LEFT JOIN proprietario ON proprietario.idUtente = utente.idUtente
|
|
LEFT JOIN personale ON personale.idUtente = utente.idUtente
|
|
LEFT JOIN cliente ON cliente.idUtente = utente.idUtente
|
|
|
|
WHERE utente.email = :email
|
|
|
|
");
|
|
|
|
$stmt->execute([
|
|
"email" => $email
|
|
]);
|
|
|
|
$utente = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
// Utente inesistente
|
|
if (!$utente) {
|
|
|
|
header("Location: index.php?errore=login");
|
|
exit;
|
|
|
|
}
|
|
|
|
// Account disabilitato
|
|
if (!$utente["attivo"]) {
|
|
|
|
header("Location: index.php?errore=login");
|
|
exit;
|
|
|
|
}
|
|
|
|
// Password sbagliata
|
|
if (!password_verify($password, $utente["passwordHash"])) {
|
|
|
|
header("Location: index.php?errore=login");
|
|
exit;
|
|
|
|
}
|
|
|
|
// Utente senza ruolo
|
|
if ($utente["ruolo"] === null) {
|
|
|
|
die ("Errore: utente senza ruolo");
|
|
|
|
}
|
|
|
|
// Login riuscito
|
|
session_regenerate_id(true);
|
|
|
|
// Mettiamo i dati nella sessione
|
|
$_SESSION["idUtente"] = $utente["idUtente"];
|
|
|
|
$_SESSION["nome"] = $utente["nome"];
|
|
|
|
$_SESSION["cognome"] = $utente["cognome"];
|
|
|
|
$_SESSION["email"] = $utente["email"];
|
|
|
|
$_SESSION["ruolo"] = $utente["ruolo"];
|
|
|
|
|
|
// Reindirizzamento alla dashboard specifica del ruolo.
|
|
$dashboard = [
|
|
"cliente" => "cliente/dashboard.php",
|
|
"personale" => "personale/dashboard.php",
|
|
"proprietario" => "proprietario/dashboard.php",
|
|
];
|
|
|
|
header("Location: " . $dashboard[$utente["ruolo"]]);
|
|
|
|
exit;
|