You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
117 lines
4.0 KiB
117 lines
4.0 KiB
<?php
|
|
class VisitorModel
|
|
{
|
|
|
|
private EnigmeGateway $enigme_gateway;
|
|
private PartieGateway $partie_gateway;
|
|
private UtilisateurGateway $utilisateur_gateway;
|
|
|
|
function __construct()
|
|
{
|
|
try {
|
|
global $error, $view, $rep;
|
|
$this->enigme_gateway = new EnigmeGateway();
|
|
$this->partie_gateway = new PartieGateway();
|
|
$this->utilisateur_gateway = new UtilisateurGateway();
|
|
} catch (Exception $e) {
|
|
$error = $e->getMessage();
|
|
require($rep . $view['erreur']);
|
|
}
|
|
}
|
|
|
|
public function signUp()
|
|
{
|
|
global $rep, $vues, $sel, $error;
|
|
try {
|
|
$validation = new Validation();
|
|
if (!$validation->ValidateEmail($_REQUEST['email'])) {
|
|
$error = "Email invalides.";
|
|
throw (new Exception("Email non valide"));
|
|
}
|
|
if (!$validation->ValidateUsername($_REQUEST['username'])) {
|
|
$error = "Nom d'utilisateur invalides. Il ne doit pas contenir de caractère spéciaux.";
|
|
throw (new Exception("Pseudo non valide"));
|
|
}
|
|
if (!$validation->ValidatePassword($_REQUEST['password'])) {
|
|
$error = "Mots de passe invalides. Il ne doit pas dépasser 100 caractères.";
|
|
throw (new Exception("Mot de passe non valide"));
|
|
}
|
|
$j = $this->utilisateur_gateway->getUtilisateurByEmail($_REQUEST['email']);
|
|
if ($j->getEmail() != "null") {
|
|
$error = "Email déjà utilisé.";
|
|
throw (new Exception("Email déjà utilisé"));
|
|
}
|
|
$password = password_hash($_REQUEST['password'] . $sel, PASSWORD_DEFAULT);
|
|
$Utilisateur = new Utilisateur($_REQUEST['email'], $_REQUEST['username'], $password, false);
|
|
$this->utilisateur_gateway->insert($Utilisateur);
|
|
$_SESSION['role'] = 'user';
|
|
require($rep . $vues['main']);
|
|
} catch (PDOException $e) {
|
|
$error = "Erreur de connexion à la base de données.";
|
|
require($rep . $vues['erreur']);
|
|
} catch (Exception $e) {
|
|
$error = $e->getMessage();
|
|
require($rep . $vues['erreur']);
|
|
}
|
|
}
|
|
public function login()
|
|
{
|
|
global $rep, $vues, $sel, $error;
|
|
try {
|
|
$Utilisateur = $this->utilisateur_gateway->getUtilisateurByEmail($_REQUEST['email']);
|
|
if ($Utilisateur->getEmail() == "null") {
|
|
$error = "Utilisateur non trouvé.";
|
|
throw new Exception("Utilisateur introuvable");
|
|
}
|
|
$mdp = $this->utilisateur_gateway->getMdpByEmail($_REQUEST['email']);
|
|
if (password_verify($mdp, $_REQUEST['password'] . $sel)) {
|
|
$error = "Mot de passe incorrect.";
|
|
throw new Exception("Mot de passe invalide");
|
|
}
|
|
$estAdmin = $this->utilisateur_gateway->getEstAdminByEmail($_REQUEST['email']);
|
|
if ($estAdmin == true) {
|
|
$_SESSION['role'] = "admin";
|
|
} else {
|
|
$_SESSION['role'] = "user";
|
|
}
|
|
|
|
$_SESSION['connected'] = 'true';
|
|
require($rep . $vues['main']);
|
|
} catch (Exception $e) {
|
|
require($rep . $vues['erreur']);
|
|
}
|
|
}
|
|
|
|
public function goToHome()
|
|
{
|
|
global $rep, $vues, $error;
|
|
try {
|
|
require($rep . $vues['main']);
|
|
} catch (Exception $e) {
|
|
$error = "404";
|
|
require($rep . $vues['erreur']);
|
|
}
|
|
}
|
|
|
|
public function goToLogin()
|
|
{
|
|
global $rep, $vues, $error;
|
|
try {
|
|
require($rep . $vues['login']);
|
|
} catch (Exception $e) {
|
|
$error = "404";
|
|
require($rep . $vues['erreur']);
|
|
}
|
|
}
|
|
|
|
public function goToSignUp()
|
|
{
|
|
global $rep, $vues, $error;
|
|
try {
|
|
require($rep . $vues['signUp']);
|
|
} catch (Exception $e) {
|
|
$error = "404";
|
|
require($rep . $vues['erreur']);
|
|
}
|
|
}
|
|
} |