From 082b1ae99ac54016373608f22fd9b102fbfd546e Mon Sep 17 00:00:00 2001 From: Yasder5 <102179445+Yasder5@users.noreply.github.com> Date: Wed, 14 Jan 2026 23:05:10 +0100 Subject: [PATCH] on s'arrete pour ce soir il est 00h05 quand meme --- app/controllers/user_controller.php | 140 ++++++++++++++++++++++++++++ app/entities/user_entity.php | 51 ++++++++++ app/models/user_model.php | 81 ++++++++++++++++ app/views/login.php | 23 +++++ app/views/partials/header.php | 12 +-- app/views/partials/project.php | 2 +- public/index.php | 5 +- 7 files changed, 306 insertions(+), 8 deletions(-) create mode 100644 app/controllers/user_controller.php create mode 100644 app/entities/user_entity.php create mode 100644 app/models/user_model.php create mode 100644 app/views/login.php diff --git a/app/controllers/user_controller.php b/app/controllers/user_controller.php new file mode 100644 index 0000000..454bddb --- /dev/null +++ b/app/controllers/user_controller.php @@ -0,0 +1,140 @@ + 0) { + // Vérifier le formulaire + if ($strMail == ""){ + $arrError['mail'] = "Le mail est obligatoire"; + } + if ($strPwd == ""){ + $arrError['pwd'] = "Le mot de passe est obligatoire"; + } + + // Si le formulaire est rempli correctement + if (count($arrError) == 0){ + // Vérifier l'utilisateur en BDD + $objUserModel = new UserModel; + $arrResult = $objUserModel->verifUser($strMail, $strPwd); + //var_dump($arrResult); + if ($arrResult === false){ // Si la base de données ne renvoie rien + $arrError[] = "Mail ou mot de passe invalide"; + }else{ + // Ajoute l'utilisateur en session + /*$_SESSION['firstname'] = $arrResult['user_firstname']; + $_SESSION['name'] = $arrResult['user_name']; + $_SESSION['id'] = $arrResult['user_id'];*/ + // j'enlève le mot de passe avant la session + //unset($arrResult['user_pwd']); + $_SESSION['user'] = $arrResult; + $_SESSION['success'] = "Bienvenue, vous êtes bien connecté"; + + header("Location:index.php"); + exit; + //var_dump($_SESSION); + //var_dump("Connecté"); + } + } + } + include("../app/views/login.php"); + include("../app/views/partials/footer.php"); + + } + + public function create(){ + // Variables d'affichage + $strH2 = "Créer un compte"; + $strP = "Inscrivez-vous"; + // Variables technique + $strPage = "create_account"; + + // inclusion du header + include("views/_partial/header.php"); + + // Traitement du formulaire + //var_dump($_POST); + $strName = $_POST['name']??""; + $strFirstname = $_POST['firstname']??""; + $strMail = $_POST['mail']??""; + $strPwd = $_POST['pwd']??""; + $strPwdConfirm = $_POST['pwd_confirm']??""; + $objUser = new User; + $objUser->hydrate($_POST); + + /* + $objUser->setName($strName); + $objUser->setFirstname($strFirstname); + $objUser->setMail($strMail); + $objUser->setPwd($strPwd); + */ + // Tester le formulaire + $arrError = []; + if (count($_POST) > 0) { + if ($objUser->getName() == ""){ + $arrError['name'] = "Le nom est obligatoire"; + } + if ($objUser->getFirstname() == ""){ + $arrError['firstname'] = "Le prénom est obligatoire"; + } + if ($objUser->getMail() == ""){ + $arrError['mail'] = "Le mail est obligatoire"; + }else if (!filter_var($objUser->getMail(), FILTER_VALIDATE_EMAIL)){ + $arrError['mail'] = "Le format du mail n'est pas correct"; + } + $strRegex = "/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{16,}$/"; + if ($objUser->getPwd() == ""){ + $arrError['pwd'] = "Le mot de passe est obligatoire"; + }else if (!preg_match($strRegex, $objUser->getPwd())){ + $arrError['pwd'] = "Le mot de passe ne correspond pas aux règles"; + }else if($objUser->getPwd() != $strPwdConfirm){ + $arrError['pwd_confirm'] = "Le mot de passe et sa confirmation ne sont pas identiques"; + } + // Ajouter la vérification du mot de passe par regex + + // Si le formulaire est rempli correctement + if (count($arrError) == 0){ + // => Ajout dans la base de données + $objUserModel = new UserModel; + //$intNbInsert = $objUserModel->insert($strName, $strFirstname, $strMail, $strPwd); + $boolInsert = $objUserModel->insert($objUser); + if ($boolInsert === true){ + $_SESSION['success'] = "Le compte a bien été créé"; + //header("Location:index.php"); + //exit; + }else{ + $arrError[] = "Erreur lors de l'ajout"; + } + //var_dump("tout est ok"); + } + } + include("views/create_account.php"); + include("views/_partial/footer.php"); + } + + + public function logout(){ + session_start(); + /*session_destroy(); + session_start();*/ + + // on supprime l'utilisateur en session + unset($_SESSION['user']); + + $_SESSION['success'] = "Vous êtes bien déconnecté"; + + header("Location:index.php"); + exit; + + } + } \ No newline at end of file diff --git a/app/entities/user_entity.php b/app/entities/user_entity.php new file mode 100644 index 0000000..6fa9d08 --- /dev/null +++ b/app/entities/user_entity.php @@ -0,0 +1,51 @@ +_prefixe = 'user_'; + } + + // Méthodes - getters et setters + public function getName():string{ + return $this->_name; + } + public function setName(string $strNewName){ + $this->_name = $this->nettoyer($strNewName); + } + public function getFirstname():string{ + return $this->_firstname; + } + public function setFirstname(string $strFirstname){ + $this->_firstname = $this->nettoyer($strFirstname); + } + public function getMail():string{ + return $this->_mail; + } + public function setMail(string $strMail){ + $this->_mail = strtolower($this->nettoyer($strMail)); + } + public function getPwd():string{ + return $this->_pwd; + } + public function getPwdHash():string{ + return password_hash($this->_pwd, PASSWORD_DEFAULT); + } + public function setPwd(string $strPwd){ + $this->_pwd = $strPwd; + } + + + + + } \ No newline at end of file diff --git a/app/models/user_model.php b/app/models/user_model.php new file mode 100644 index 0000000..808bd93 --- /dev/null +++ b/app/models/user_model.php @@ -0,0 +1,81 @@ +_db->query($strRq)->fetchAll(); + } + + /** + * @param string $strMail + * @param string $strPwd + * @return array|bool + */ + public function verifUser(string $strMail, string $strPwd):array|bool{ + // 2. Construire la requête + $strRq = "SELECT user_id, user_name, user_firstname, user_pwd + FROM users + WHERE user_mail = '".$strMail."'"; + // Récupère mon utilisateur + // Executer la requête et récupérer les résultats + $arrUser = $this->_db->query($strRq)->fetch(); + // Vérification du mot de passe haché + if (password_verify($strPwd, $arrUser['user_pwd'])){ + // Renvoi l'utilisateur + unset($arrUser['user_pwd']); // on enlève le pwd + return $arrUser; + }else{ + return false; + } + } + + //public function insert(string $strName, string $strFirstname, string $strMail, string $strPwd):int{ + /** + * Fonction d'insertion d'un utilisateur en BDD + * @param object $objUser L'objet utilisateur + * @return bool Est-ce que la requête s'est bien passée (true/false) + */ + public function insert(object $objUser):bool{ + + // 2. Construire la requête + /*$strRq = "INSERT INTO users (user_name, user_firstname, user_mail, user_pwd) + VALUES ('".$objUser->getName()."', + '".$objUser->getFirstname()."', + '".$objUser->getMail()."', + '".$objUser->getPwdHash()."')";*/ + $strRq = "INSERT INTO users (user_name, user_firstname, user_mail, user_pwd) + VALUES (:name, :firstname, :mail, :pwd)"; + // Préparer la requête + $rqPrep = $this->_db->prepare($strRq); + // Donne les informations + $rqPrep->bindValue(":name", $objUser->getName(), PDO::PARAM_STR); + $rqPrep->bindValue(":firstname", $objUser->getFirstname(), PDO::PARAM_STR); + $rqPrep->bindValue(":mail", $objUser->getMail(), PDO::PARAM_STR); + $rqPrep->bindValue(":pwd", $objUser->getPwdHash(), PDO::PARAM_STR); + + // 3. Executer la requête + //var_dump($strRq);die; + //return $db->exec($strRq); + return $rqPrep->execute(); + } + } \ No newline at end of file diff --git a/app/views/login.php b/app/views/login.php new file mode 100644 index 0000000..0021e56 --- /dev/null +++ b/app/views/login.php @@ -0,0 +1,23 @@ +
+ 0) {?> +
+ +

+ +
+ +
+

+ + +

+

+ + +

+

+ +

+
+
\ No newline at end of file diff --git a/app/views/partials/header.php b/app/views/partials/header.php index cd61b10..c4136df 100644 --- a/app/views/partials/header.php +++ b/app/views/partials/header.php @@ -30,16 +30,16 @@ diff --git a/app/views/partials/project.php b/app/views/partials/project.php index ba549fb..1c72d94 100644 --- a/app/views/partials/project.php +++ b/app/views/partials/project.php @@ -7,7 +7,7 @@ - getCreatorname(); ?>

getDescription(); ?>

- + Lire la suite diff --git a/public/index.php b/public/index.php index 206b70a..b5dd23f 100644 --- a/public/index.php +++ b/public/index.php @@ -1,10 +1,13 @@