From d73ba69a0784c13d4348488910caeb8b54dc9a6c Mon Sep 17 00:00:00 2001 From: SIMAILA Djalim Date: Tue, 24 Jan 2023 16:35:20 +0100 Subject: [PATCH] finished userModel refactor --- Controllers/ManageUserController.php | 1 - Controllers/UserController.php | 31 +++++++++++---------- Models/RecipeModel.php | 1 - Models/UserModel.php | 41 ++++++++++++++++++---------- 4 files changed, 42 insertions(+), 32 deletions(-) diff --git a/Controllers/ManageUserController.php b/Controllers/ManageUserController.php index 1f639af..2aa1151 100644 --- a/Controllers/ManageUserController.php +++ b/Controllers/ManageUserController.php @@ -28,7 +28,6 @@ final class ManageUserController $S_query = $A_getParams["query"]; $A_results = UserModel::searchUsers($S_query); - //TODO: User to Array var_dump($A_results); echo "Terme de recherche choisi: $S_query"; diff --git a/Controllers/UserController.php b/Controllers/UserController.php index e9c7c24..e326abd 100644 --- a/Controllers/UserController.php +++ b/Controllers/UserController.php @@ -25,14 +25,13 @@ final class UserController { $S_email = Utils::getOrDie($A_postParams, "email"); $S_password = Utils::getOrDie($A_postParams, "password"); - - + $O_user = UserModel::getByEmail($S_email); if ($O_user == null) { $S_errmsg = "No user with this email"; - }else if (!password_verify($S_password,$O_user->PASS_HASH)) { + }else if (!password_verify($S_password,$O_user->S_PASSWORD_HASH)) { $S_errmsg = "Invalid password"; - }else if ($O_user->DISABLED) { + }else if ($O_user->B_DISABLED) { $S_errmsg = "This account is disabled"; } @@ -42,7 +41,7 @@ final class UserController return header("Location: /user/login"); } - Session::set_login($O_user->ID); + Session::set_login($O_user->I_ID); header("Location: /"); @@ -193,16 +192,18 @@ final class UserController if (count($A_urlParams) !== 1 ) throw new HTTPSpecialCaseException(404); $O_user = UserModel::getByID($A_urlParams[0]); - - if (isset($A_user) && $A_user["PROFILE_PIC"] !== null) { - header("Content-Type: image"); - echo $A_user["PROFILE_PIC"]; - } else { - header("Content-Type: image/svg+xml"); - echo file_get_contents(Constants::rootDir()."/static/img/default_user.svg"); - } - + + if (isset($A_user)) { + $S_pfp = $O_user->getProfilePic(); + if($S_pfp !== null) { + header("Content-Type: image"); + echo $A_user["PROFILE_PIC"]; + return Utils::RETURN_RAW; + } + } + + header("Content-Type: image/svg+xml"); + echo file_get_contents(Constants::rootDir()."/static/img/default_user.svg"); return Utils::RETURN_RAW; } - } diff --git a/Models/RecipeModel.php b/Models/RecipeModel.php index 72f3033..452dec2 100644 --- a/Models/RecipeModel.php +++ b/Models/RecipeModel.php @@ -25,7 +25,6 @@ final class RecipeModel $O_ingredientModel = new IngredientModel(); $A_recipe["INGREDIENTS"] = $O_ingredientModel->searchByRecipe($A_recipe["ID"]); - $A_recipe["AUTHOR_USERNAME"] = UserModel::getByID($A_recipe["AUTHOR_ID"])->S_USERNAME; $O_difficultyModel = new DifficultyModel(); diff --git a/Models/UserModel.php b/Models/UserModel.php index 4b96e81..cbbe1d2 100644 --- a/Models/UserModel.php +++ b/Models/UserModel.php @@ -12,7 +12,7 @@ final class UserModel extends UserSessionModel public $B_DISABLED = 0; public function __construct($S_EMAIL, $S_USERNAME,$S_PASSWORD_HASH,$S_LAST_SEEN,$S_FIRST_SEEN,$B_ADMIN,$B_DISABLED) - { + { $this->S_EMAIL = $S_EMAIL; $this->S_USERNAME = $S_USERNAME; $this->S_PASSWORD_HASH = $S_PASSWORD_HASH; @@ -29,7 +29,7 @@ final class UserModel extends UserSessionModel $stmt->bindParam("password_hash", $this->S_PASSWORD_HASH); $stmt->bindParam("first_seen", $this->S_FIRST_SEEN); $stmt->execute(); - #TODO instantly get the created user's id, for everything else to work + $this->I_ID = Model::get()->lastInsertId(); } public function update(){ $O_model = Model::get(); @@ -74,9 +74,10 @@ final class UserModel extends UserSessionModel $row = $stmt->fetch(); if ($row === false) return null; - return $row; - //TODO create an user object and return it - // return new User() + + $O_user = new UserModel($row["EMAIL"],$row["USERNAME"],$row["PASS_HASH"],$row["LAST_SEEN"],$row["FIRST_SEEN"],$row["ADMIN"],$row["DISABLED"]); + $O_user->I_ID = $I_id; + return $O_user; } public static function isEmailInDatabase($S_email){ @@ -96,9 +97,7 @@ final class UserModel extends UserSessionModel $row = $stmt->fetch(); if ($row === false) return null; - return $row; - #TODO create an user object and return it - //return UserModel::getById() + return UserModel::getById($row["ID"]); } public function updateProfilePic($profile_pic_fp){ $O_model = Model::get(); @@ -115,7 +114,7 @@ final class UserModel extends UserSessionModel $stmt->execute(); $row = $stmt->fetch(); if ($row === false) return null; - return $row; + return $row["PROFILE_PIC"]; } public static function searchUsers($S_query) @@ -136,11 +135,23 @@ final class UserModel extends UserSessionModel } public static function anonymiseByID($I_id){ - User::getByID($I_id)->anonymise(); + $O_model = Model::get(); + + $stmt = $O_model->prepare("UPDATE RECIPE SET AUTHOR_ID = NULL WHERE AUTHOR_ID = :id"); + $stmt->bindParam("id", $I_id); + $stmt->execute(); + + $stmt = $O_model->prepare("UPDATE APPRECIATION SET AUTHOR_ID = NULL WHERE AUTHOR_ID = :id"); + $stmt->bindParam("id", $I_id); + $stmt->execute(); } - public static function deleteByID($I_id){ - //TODO Make static - User::getByID($I_id)->delete(); - -} + public static function deleteByID($I_id) + { + $O_model = Model::get(); + UserModel::anonymiseByID($I_id); + $stmt = $O_model->prepare("DELETE FROM USER WHERE ID=:id"); + $stmt->bindParam("id", $I_id); + $stmt->execute(); + } +} \ No newline at end of file