From 43821baf34a4398d72cc1a4f09389e6f4551470f Mon Sep 17 00:00:00 2001 From: Noe GARNIER Date: Mon, 28 Nov 2022 11:08:05 +0100 Subject: [PATCH] Test --- WEB/Config/Connection.php | 54 +++++----- WEB/Controller/UserController.php | 144 +++++++++++++------------- WEB/Controller/UtilisateurGateway.php | 142 ++++++++++++------------- WEB/index.php | 2 +- 4 files changed, 171 insertions(+), 171 deletions(-) diff --git a/WEB/Config/Connection.php b/WEB/Config/Connection.php index 9223e8ea..987a2594 100644 --- a/WEB/Config/Connection.php +++ b/WEB/Config/Connection.php @@ -17,33 +17,33 @@ // } // } -// class Connection extends SQLite3 -// { -// private $stmt; -// private $result; -// function __construct($dsn) -// { -// $this->open($dsn); -// $this->enableExceptions(true); -// } +class Connection extends SQLite3 +{ + private $stmt; + private $result; + function __construct($dsn) + { + $this->open($dsn); + $this->enableExceptions(true); + } -// public function executeQuery(string $query, array $parameters = []) :bool { -// $this->stmt = parent::prepare($query); -// foreach ($parameters as $name => $value) { -// $this->stmt->bindValue($name, $value[0], $value[1]); -// } -// $this->result=$this->stmt->execute(); -// return $this->result; -// } + public function executeQuery(string $query, array $parameters = []) :bool { + $this->stmt = parent::prepare($query); + foreach ($parameters as $name => $value) { + $this->stmt->bindValue($name, $value[0], $value[1]); + } + $this->result=$this->stmt->execute(); + return $this->result; + } -// public function getResults(): array { -// return $this->result->fetchArray(); -// } -// } + public function getResults(): array { + return $this->result->fetchArray(); + } +} -class Connection extends SQLite3{ - function __construct() - { - $this->open("./Model/scripted.db"); - } -} \ No newline at end of file +// class Connection extends SQLite3{ +// function __construct() +// { +// $this->open("./Model/scripted.db"); +// } +// } \ No newline at end of file diff --git a/WEB/Controller/UserController.php b/WEB/Controller/UserController.php index e472a4e0..18fd13c6 100644 --- a/WEB/Controller/UserController.php +++ b/WEB/Controller/UserController.php @@ -8,19 +8,19 @@ class UserController try{ echo "UserController"; global $dsn, $rep, $vues, $error; - $this->con=new Connection (); + $this->con=new Connection ($dsn); echo "USconection"; $action=$_REQUEST['action']; switch($action) { case NULL: $this->goToHome(); break; - // case "signUp": - // $this->signUp(); - // break; - // case "login": - // $this->login(); - // break; + case "signUp": + $this->signUp(); + break; + case "login": + $this->login(); + break; case "goToPresentation": $this->goToPresentation(); break; @@ -48,72 +48,72 @@ class UserController } } - // private function signUp() { - // global $rep, $vues, $sel, $error; - // try { - // $gateway = new UtilisateurGateway($this->con); - // $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 = $gateway->getUtilisateurByEmail($_REQUEST['email']); - // if ($j != null) { - // $error = "Email déjà utilisé."; - // throw (new Exception("Email déjà utilisé")); - // } - // $password = password_hash($_REQUEST['password'], PASSWORD_DEFAULT); - // $estAdmin = $gateway->getEstAdminByEmail($_REQUEST['email']); - // $Utilisateur = new Utilisateur($_REQUEST['email'], $_REQUEST['username'], $password, $estAdmin); - // $gateway->insert($Utilisateur); - // $_SESSION['connected'] = 'true'; - // $_SESSION['role'] = 'utilisateur'; - // require ($rep.$vues['main']); - // }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 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"); - // } - // $estAdmin =$gateway->getEstAdminByEmail($_REQUEST['email']); - // if ($estAdmin == true) { - // $_SESSION['role'] = "admin"; - // } - // else{ - // $_SESSION['role'] = "utilisateur"; - // } + private function signUp() { + global $rep, $vues, $sel, $error; + try { + $gateway = new UtilisateurGateway($this->con); + $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 = $gateway->getUtilisateurByEmail($_REQUEST['email']); + if ($j != null) { + $error = "Email déjà utilisé."; + throw (new Exception("Email déjà utilisé")); + } + $password = password_hash($_REQUEST['password'], PASSWORD_DEFAULT); + $estAdmin = $gateway->getEstAdminByEmail($_REQUEST['email']); + $Utilisateur = new Utilisateur($_REQUEST['email'], $_REQUEST['username'], $password, $estAdmin); + $gateway->insert($Utilisateur); + $_SESSION['connected'] = 'true'; + $_SESSION['role'] = 'utilisateur'; + require ($rep.$vues['main']); + }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 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"); + } + $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']); - // } - // } + $_SESSION['connected'] = 'true'; + require ($rep.$vues['main']); + }catch (Exception $e){ + require($rep.$vues['erreur']); + } + } private function goToPresentation() { global $rep, $vues, $error; diff --git a/WEB/Controller/UtilisateurGateway.php b/WEB/Controller/UtilisateurGateway.php index dcd105d1..a7265fab 100644 --- a/WEB/Controller/UtilisateurGateway.php +++ b/WEB/Controller/UtilisateurGateway.php @@ -19,81 +19,81 @@ class UtilisateurGateway $this->con = $con; } - // public function insert(Utilisateur $utilisateur) : void{ - // $query = "INSERT INTO Utilisateur VALUE (:email,:pseudo,:mdp,:estAdmin)"; - // $this->con->executeQuery($query, array( - // ':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 insert(Utilisateur $utilisateur) : void{ + $query = "INSERT INTO Utilisateur VALUE (:email,:pseudo,:mdp,:estAdmin)"; + $this->con->executeQuery($query, array( + ':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 utilisateur WHERE email=:email"; - // $this->con->executeQuery($query, array( - // ':email' => array($email ,PDO::PARAM_STR) - // )); - // } + public function delete(string $email) : void{ + $query = "DELETE FROM utilisateur WHERE email=:email"; + $this->con->executeQuery($query, array( + ':email' => array($email ,PDO::PARAM_STR) + )); + } - // public function getUtilisateurByEmail(string $email) : Utilisateur{ - // global $error; - // $query = "SELECT * FROM Utilisateur WHERE email=:email"; - // $this->con->executeQuery($query, array( - // ':email' => array($email,PDO::PARAM_STR) - // )); - // $results=$this->con->getResults(); - // foreach ($results as $row) { - // $email=$row['email']; - // $pseudo=$row['pseudo']; - // $mdp=$row['mdp']; - // $estAdmin=$row['estAdmin']; - // } - // if ($results == null){ - // return new Utilisateur("null", "null", "null", false); - // } - // return new Utilisateur($email, $pseudo, $mdp, $estAdmin); - // } + public function getUtilisateurByEmail(string $email) : Utilisateur{ + global $error; + $query = "SELECT * FROM Utilisateur WHERE email=:email"; + $this->con->executeQuery($query, array( + ':email' => array($email,PDO::PARAM_STR) + )); + $results=$this->con->getResults(); + foreach ($results as $row) { + $email=$row['email']; + $pseudo=$row['pseudo']; + $mdp=$row['mdp']; + $estAdmin=$row['estAdmin']; + } + if ($results == null){ + return new Utilisateur("null", "null", "null", false); + } + return new Utilisateur($email, $pseudo, $mdp, $estAdmin); + } - // public function getMdpByEmail(string $email) : string{ - // global $error; - // $query = "SELECT mdp FROM Utilisateur WHERE email=:email"; - // $this->con->executeQuery($query, array( - // ':email' => array($email,PDO::PARAM_STR) - // )); - // $results=$this->con->getResults(); - // foreach ($results as $row) { - // $mdp=$row['mdp']; - // } - // if ($results == null){ - // $error = "Mot de passe non trouvé."; - // throw new Exception("Mots de passe Incorrect"); - // } - // return $mdp; - // } + public function getMdpByEmail(string $email) : string{ + global $error; + $query = "SELECT mdp FROM Utilisateur WHERE email=:email"; + $this->con->executeQuery($query, array( + ':email' => array($email,PDO::PARAM_STR) + )); + $results=$this->con->getResults(); + foreach ($results as $row) { + $mdp=$row['mdp']; + } + if ($results == null){ + $error = "Mot de passe non trouvé."; + throw new Exception("Mots de passe Incorrect"); + } + 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 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 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'] . '
'; - // } + public function showAll() : void{ + $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'] . '
'; + } - // } + } } \ No newline at end of file diff --git a/WEB/index.php b/WEB/index.php index 02769bd9..7a98214c 100644 --- a/WEB/index.php +++ b/WEB/index.php @@ -18,7 +18,7 @@ Autoload::charger(); // } echo "test"; -$db = new Connection(); +$db = new Connection($dsn); echo "test2"; // $control = new FrontController();