From 6ac80edf0825f459ac3b49ac95c3999e8b436899 Mon Sep 17 00:00:00 2001 From: Noe GARNIER Date: Fri, 25 Nov 2022 14:26:52 +0100 Subject: [PATCH] Mise en place du front controller+ changement BD+ correction de bugs --- WEB/Controller/FrontController.php | 11 ++++ .../{Controller.php => UserController.php} | 50 +++++++++++-------- ...ueurGateway.php => UtilisateurGateway.php} | 43 ++++++++++------ WEB/Model/Enigme.php | 1 + WEB/Model/{Joueur.php => Utilisateur.php} | 21 +++++++- WEB/Model/bd.sql | 5 +- WEB/index.php | 6 +-- 7 files changed, 96 insertions(+), 41 deletions(-) create mode 100644 WEB/Controller/FrontController.php rename WEB/Controller/{Controller.php => UserController.php} (75%) rename WEB/Controller/{JoueurGateway.php => UtilisateurGateway.php} (54%) rename WEB/Model/{Joueur.php => Utilisateur.php} (73%) diff --git a/WEB/Controller/FrontController.php b/WEB/Controller/FrontController.php new file mode 100644 index 00000000..75da9c81 --- /dev/null +++ b/WEB/Controller/FrontController.php @@ -0,0 +1,11 @@ +con=$con; - session_start(); + function __construct() { + global $dsn, $user, $password; + $this->con=new Connection($dsn, $user, $password);; try{ global $rep, $vues, $error; $action=$_REQUEST['action']; @@ -49,7 +46,7 @@ class Controller private function signUp() { global $rep, $vues, $sel, $error; try { - $gateway = new JoueurGateway($this->con); + $gateway = new UtilisateurGateway($this->con); $validation = new Validation(); if (! $validation->ValidateEmail($_REQUEST['email'])) { $error = "Email invalides."; @@ -63,41 +60,54 @@ class Controller $error = "Mots de passe invalides. Il ne doit pas dépasser 100 caractères."; throw(new Exception("Mot de passe non valide")); } - $j = $gateway->getJoueurByEmail($_REQUEST['email']); - if ($j != null) { + $j = $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'], PASSWORD_DEFAULT); - $joueur = new Joueur($_REQUEST['email'], $_REQUEST['username'], $password); - $gateway->insert($joueur); + $utilisateur = new Utilisateur($_REQUEST['email'], $_REQUEST['username'], $password, false); + $gateway->insert($utilisateur); $_SESSION['connected'] = 'true'; + $_SESSION['role'] = 'utilisateur'; require ($rep.$vues['main']); - }catch (Exception $e){ + }catch (PDOException $e) + { + $error = "Erreur de connexion à la base de données."; + require ($rep.$vues['erreur']); + } + catch (Exception $e){ require($rep.$vues['erreur']); } } private function login(){ global $rep, $vues, $sel, $error; try { - $gateway = new JoueurGateway($this->con); - $joueur = $gateway->getJoueurByEmail($_REQUEST['email']); - if ($joueur->getEmail() == null){ - $error = "Joueur non trouvé."; - throw new Exception("Joueur introuvable"); + $gateway = new UtilisateurGateway($this->con); + $utilisateur = $gateway->getUtilisateurByEmail($_REQUEST['email']); + if ($utilisateur->getEmail() == null){ + $error = "Utilisateur non trouvé."; + throw new Exception("Utilisateur introuvable"); } $mdp = $gateway->getMdpByEmail($_REQUEST['email']); if (password_verify($mdp, $_REQUEST['password'])){ $error = "Mot de passe incorrect."; throw new Exception("Mot de passe invalide"); } - $_SESSION['connected'] = 'true'; + $estAdmin =$gateway->getEstAdminByEmail($_REQUEST['email']); + if ($estAdmin == true) { + $_SESSION['role'] = "admin"; + } + else{ + $_SESSION['role'] = "utilisateur"; + } + + $_SESSION['connected'] = 'true'; require ($rep.$vues['main']); }catch (Exception $e){ require($rep.$vues['erreur']); } } - // require error page with given message private function goToPresentation() { global $rep, $vues, $error; diff --git a/WEB/Controller/JoueurGateway.php b/WEB/Controller/UtilisateurGateway.php similarity index 54% rename from WEB/Controller/JoueurGateway.php rename to WEB/Controller/UtilisateurGateway.php index 0f867088..9a359c91 100644 --- a/WEB/Controller/JoueurGateway.php +++ b/WEB/Controller/UtilisateurGateway.php @@ -1,6 +1,6 @@ con = $con; } - public function insert(Joueur $joueur) : void{ - $query = "INSERT INTO Joueur VALUE (:email,:pseudo,:mdp)"; + public function insert(Utilisateur $utilisateur) : void{ + $query = "INSERT INTO Utilisateur VALUE (:email,:pseudo,:mdp,:estAdmin)"; $this->con->executeQuery($query, array( - ':email' => array($joueur->getEmail(),PDO::PARAM_STR), - ':pseudo' => array($joueur->getPseudo(),PDO::PARAM_STR), - ':mdp' => array($joueur->getMdp(),PDO::PARAM_STR))); + ':email' => array($utilisateur->getEmail(),PDO::PARAM_STR), + ':pseudo' => array($utilisateur->getPseudo(),PDO::PARAM_STR), + ':mdp' => array($utilisateur->getMdp(),PDO::PARAM_STR), + ':estAdmin' => array($utilisateur->getEstAdmin(),PDO::PARAM_BOOL))); } public function delete(string $email) : void{ - $query = "DELETE FROM Joueur WHERE email=:email"; + $query = "DELETE FROM utilisateur WHERE email=:email"; $this->con->executeQuery($query, array( ':email' => array($email,PDO::PARAM_STR) )); } - public function getJoueurByEmail(string $email) : Joueur{ + public function getUtilisateurByEmail(string $email) : Utilisateur{ global $error; - $query = "SELECT * FROM Joueur WHERE email=:email"; + $query = "SELECT * FROM Utilisateur WHERE email=:email"; $this->con->executeQuery($query, array( ':email' => array($email,PDO::PARAM_STR) )); @@ -45,17 +46,17 @@ class JoueurGateway $email=$row['email']; $pseudo=$row['pseudo']; $mdp=$row['mdp']; + $estAdmin=$row['estAdmin']; } if ($results == null){ - $error = "Joueur non trouvé."; - throw new Exception("Joueur Introuvable"); + return new Utilisateur("null", "null", "null", false); } - return new Joueur($email, $pseudo, $mdp); + return new Utilisateur($email, $pseudo, $mdp, $estAdmin); } public function getMdpByEmail(string $email) : string{ global $error; - $query = "SELECT mdp FROM Joueur WHERE email=:email"; + $query = "SELECT mdp FROM Utilisateur WHERE email=:email"; $this->con->executeQuery($query, array( ':email' => array($email,PDO::PARAM_STR) )); @@ -70,14 +71,28 @@ class JoueurGateway return $mdp; } + public function getEstAdminByEmail(string $email) : bool{ + global $error; + $query = "SELECT estAdmin FROM Utilisateur WHERE email=:email"; + $this->con->executeQuery($query, array( + ':email' => array($email,PDO::PARAM_STR) + )); + $results=$this->con->getResults(); + foreach ($results as $row) { + $estAdmin=$row['estAdmin']; + } + return $estAdmin; + } + public function showAll() : void{ - $query = "SELECT * FROM Joueur"; + $query = "SELECT * FROM Utilisateur"; $this->con->executeQuery($query); $results=$this->con->getResults(); foreach ($results as $row) { echo $row['email'] . '
'; echo $row['pseudo'] . '
'; echo $row['mdp'] . '
'; + echo $row['estAdmin'] . '
'; } } diff --git a/WEB/Model/Enigme.php b/WEB/Model/Enigme.php index 5bd69e72..72409db5 100644 --- a/WEB/Model/Enigme.php +++ b/WEB/Model/Enigme.php @@ -20,6 +20,7 @@ class Enigme * @param string $solution * @param string $test * @param int $tempsDeResolution + * @param int $points */ public function __construct() { diff --git a/WEB/Model/Joueur.php b/WEB/Model/Utilisateur.php similarity index 73% rename from WEB/Model/Joueur.php rename to WEB/Model/Utilisateur.php index 16635cb1..50be44ed 100644 --- a/WEB/Model/Joueur.php +++ b/WEB/Model/Utilisateur.php @@ -1,21 +1,24 @@ email = $email; $this->pseudo = $pseudo; $this->mdp = $mdp; + $this->estAdmin = $estAdmin; } /** @@ -66,4 +69,18 @@ class Joueur $this->mdp = $mdp; } + /** + * @return bool + */ + public function getEstAdmin(): bool + { + return $this->estAdmin; + } + /** + * @param bool $estAdmin + */ + public function setEstAdmin(bool $estAdmin): void + { + $this->estAdmin = $estAdmin; + } } \ No newline at end of file diff --git a/WEB/Model/bd.sql b/WEB/Model/bd.sql index f2ee3908..004c967b 100644 --- a/WEB/Model/bd.sql +++ b/WEB/Model/bd.sql @@ -13,10 +13,11 @@ DROP TABLE Partie; DROP TABLE Admin; DROP TABLE Joueur; -CREATE TABLE Joueur( +CREATE TABLE Utilisateur( email varchar(50) PRIMARY KEY, pseudo varchar(50), -mdp varchar(50) +mdp varchar(500), +estAdmin boolean ); CREATE TABLE Admin( diff --git a/WEB/index.php b/WEB/index.php index ff27a6bc..1b8647e6 100644 --- a/WEB/index.php +++ b/WEB/index.php @@ -2,10 +2,10 @@ require_once('./Config/Config.php'); require_once('./Config/Autoload.php'); Autoload::charger(); -$con = new Connection($dsn, $user, $password); -$control = new Controller($con); -session_regenerate_id(true); +$control = new FrontController(); + +//session_regenerate_id(true); // session_unset(); // session_destroy(); // $_SESSION = null;