From 3b5666cb44646c2800799e184d2e6ebabce361ca Mon Sep 17 00:00:00 2001 From: Noe GARNIER Date: Mon, 28 Nov 2022 10:34:43 +0100 Subject: [PATCH] =?UTF-8?q?Testd=20de=20d=C3=A9ploiment?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WEB/Config/Config.php | 2 +- WEB/Config/Connection.php | 39 +++--- WEB/Controller/UserController.php | 194 +++++++++++++++--------------- 3 files changed, 119 insertions(+), 116 deletions(-) diff --git a/WEB/Config/Config.php b/WEB/Config/Config.php index e0c6b63b..78c5f776 100644 --- a/WEB/Config/Config.php +++ b/WEB/Config/Config.php @@ -14,7 +14,7 @@ $rep = __DIR__ . '/../'; // $password = 'p'; // SQULITE3 -$dsn = './Model/scripted.db'; +$dsn = './Model/scripted.db'; //Sel de hashage $sel = "JeSuisUnSeldeHashageEtJeSuisUniqueEtTresSecuriseEtJeSuisTresLong"; diff --git a/WEB/Config/Connection.php b/WEB/Config/Connection.php index 0ecf2b5e..45677c0a 100644 --- a/WEB/Config/Connection.php +++ b/WEB/Config/Connection.php @@ -19,23 +19,26 @@ class Connection extends SQLite3 { - private $stmt; - private $result; - function __construct($dsn) - { - $this->open($dsn); - } + private $stmt; + private $result; + function __construct($dsn) + { + $this->enableExceptions(true); + parent::__construct($dsn, SQLITE3_OPEN_READWRITE); + } - public function executeQuery(string $query, array $parameters = []) :bool { - $this->stmt = $this->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(); - } -} \ No newline at end of file + public function getResults(): array + { + return $this->result->fetchArray(); + } +} diff --git a/WEB/Controller/UserController.php b/WEB/Controller/UserController.php index 5cdad8f1..18fd13c6 100644 --- a/WEB/Controller/UserController.php +++ b/WEB/Controller/UserController.php @@ -2,40 +2,40 @@ class UserController { - //private Connection $con; + private Connection $con; function __construct() { try{ echo "UserController"; global $dsn, $rep, $vues, $error; - //$this->con=new Connection ($dsn); + $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 "goToPresentation": - // $this->goToPresentation(); - // break; - // case "goToHome": - // $this->goToHome(); - // break; - // case "goToLogin": - // $this->goToLogin(); - // break; - // case "goToSignUp": - // $this->goToSignUp(); - // break; - // case "goToEnigme": - // $this->goToEnigme(); - // break; + case "signUp": + $this->signUp(); + break; + case "login": + $this->login(); + break; + case "goToPresentation": + $this->goToPresentation(); + break; + case "goToHome": + $this->goToHome(); + break; + case "goToLogin": + $this->goToLogin(); + break; + case "goToSignUp": + $this->goToSignUp(); + break; + case "goToEnigme": + $this->goToEnigme(); + break; } } catch (PDOException $e) { @@ -48,82 +48,82 @@ 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; - // try { - // require ($rep.$vues['presentation']); - // }catch (Exception $e){ - // $error = "Erreur Inconnue"; - // require($rep.$vues['erreur']); - // } - // } + private function goToPresentation() { + global $rep, $vues, $error; + try { + require ($rep.$vues['presentation']); + }catch (Exception $e){ + $error = "Erreur Inconnue"; + require($rep.$vues['erreur']); + } + } private function goToHome() { global $rep, $vues, $error;