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"];
$A_results = UserModel::searchUsers($S_query);
//TODO: User to Array
var_dump($A_results);
echo "Terme de recherche choisi: $S_query";

View File

@ -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;
}
}

View File

@ -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();

View File

@ -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();
}
}