finished userModel refactor

This commit is contained in:
Djalim Simaila 2023-01-24 16:35:20 +01:00 committed by Thomas Rubini
parent 2826a2c791
commit d73ba69a07
No known key found for this signature in database
GPG Key ID: C7D287C8C1CAC373
4 changed files with 42 additions and 32 deletions

View File

@ -28,7 +28,6 @@ final class ManageUserController
$S_query = $A_getParams["query"]; $S_query = $A_getParams["query"];
$A_results = UserModel::searchUsers($S_query); $A_results = UserModel::searchUsers($S_query);
//TODO: User to Array
var_dump($A_results); var_dump($A_results);
echo "Terme de recherche choisi: $S_query"; echo "Terme de recherche choisi: $S_query";

View File

@ -26,13 +26,12 @@ final class UserController
$S_email = Utils::getOrDie($A_postParams, "email"); $S_email = Utils::getOrDie($A_postParams, "email");
$S_password = Utils::getOrDie($A_postParams, "password"); $S_password = Utils::getOrDie($A_postParams, "password");
$O_user = UserModel::getByEmail($S_email); $O_user = UserModel::getByEmail($S_email);
if ($O_user == null) { if ($O_user == null) {
$S_errmsg = "No user with this email"; $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"; $S_errmsg = "Invalid password";
}else if ($O_user->DISABLED) { }else if ($O_user->B_DISABLED) {
$S_errmsg = "This account is disabled"; $S_errmsg = "This account is disabled";
} }
@ -42,7 +41,7 @@ final class UserController
return header("Location: /user/login"); return header("Location: /user/login");
} }
Session::set_login($O_user->ID); Session::set_login($O_user->I_ID);
header("Location: /"); header("Location: /");
@ -194,15 +193,17 @@ final class UserController
$O_user = UserModel::getByID($A_urlParams[0]); $O_user = UserModel::getByID($A_urlParams[0]);
if (isset($A_user) && $A_user["PROFILE_PIC"] !== null) { if (isset($A_user)) {
$S_pfp = $O_user->getProfilePic();
if($S_pfp !== null) {
header("Content-Type: image"); header("Content-Type: image");
echo $A_user["PROFILE_PIC"]; echo $A_user["PROFILE_PIC"];
} else {
header("Content-Type: image/svg+xml");
echo file_get_contents(Constants::rootDir()."/static/img/default_user.svg");
}
return Utils::RETURN_RAW; 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;
}
} }

View File

@ -25,7 +25,6 @@ final class RecipeModel
$O_ingredientModel = new IngredientModel(); $O_ingredientModel = new IngredientModel();
$A_recipe["INGREDIENTS"] = $O_ingredientModel->searchByRecipe($A_recipe["ID"]); $A_recipe["INGREDIENTS"] = $O_ingredientModel->searchByRecipe($A_recipe["ID"]);
$A_recipe["AUTHOR_USERNAME"] = UserModel::getByID($A_recipe["AUTHOR_ID"])->S_USERNAME; $A_recipe["AUTHOR_USERNAME"] = UserModel::getByID($A_recipe["AUTHOR_ID"])->S_USERNAME;
$O_difficultyModel = new DifficultyModel(); $O_difficultyModel = new DifficultyModel();

View File

@ -29,7 +29,7 @@ final class UserModel extends UserSessionModel
$stmt->bindParam("password_hash", $this->S_PASSWORD_HASH); $stmt->bindParam("password_hash", $this->S_PASSWORD_HASH);
$stmt->bindParam("first_seen", $this->S_FIRST_SEEN); $stmt->bindParam("first_seen", $this->S_FIRST_SEEN);
$stmt->execute(); $stmt->execute();
#TODO instantly get the created user's id, for everything else to work $this->I_ID = Model::get()->lastInsertId();
} }
public function update(){ public function update(){
$O_model = Model::get(); $O_model = Model::get();
@ -74,9 +74,10 @@ final class UserModel extends UserSessionModel
$row = $stmt->fetch(); $row = $stmt->fetch();
if ($row === false) return null; if ($row === false) return null;
return $row;
//TODO create an user object and return it $O_user = new UserModel($row["EMAIL"],$row["USERNAME"],$row["PASS_HASH"],$row["LAST_SEEN"],$row["FIRST_SEEN"],$row["ADMIN"],$row["DISABLED"]);
// return new User() $O_user->I_ID = $I_id;
return $O_user;
} }
public static function isEmailInDatabase($S_email){ public static function isEmailInDatabase($S_email){
@ -96,9 +97,7 @@ final class UserModel extends UserSessionModel
$row = $stmt->fetch(); $row = $stmt->fetch();
if ($row === false) return null; if ($row === false) return null;
return $row; return UserModel::getById($row["ID"]);
#TODO create an user object and return it
//return UserModel::getById()
} }
public function updateProfilePic($profile_pic_fp){ public function updateProfilePic($profile_pic_fp){
$O_model = Model::get(); $O_model = Model::get();
@ -115,7 +114,7 @@ final class UserModel extends UserSessionModel
$stmt->execute(); $stmt->execute();
$row = $stmt->fetch(); $row = $stmt->fetch();
if ($row === false) return null; if ($row === false) return null;
return $row; return $row["PROFILE_PIC"];
} }
public static function searchUsers($S_query) public static function searchUsers($S_query)
@ -136,11 +135,23 @@ final class UserModel extends UserSessionModel
} }
public static function anonymiseByID($I_id){ 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){ public static function deleteByID($I_id)
//TODO Make static {
User::getByID($I_id)->delete(); $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();
}
} }