started model refactor

This commit is contained in:
Djalim Simaila 2023-01-23 19:20:52 +01:00 committed by Thomas Rubini
parent 5e47005e97
commit 1919fb359b
No known key found for this signature in database
GPG Key ID: C7D287C8C1CAC373
4 changed files with 118 additions and 99 deletions

View File

@ -27,9 +27,8 @@ final class ManageUserController
{ {
$S_query = $A_getParams["query"]; $S_query = $A_getParams["query"];
$O_userModel = new UserModel(); $A_results = UserModel::searchUsers($S_query);
$A_results = $O_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,13 @@ 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_userModel = new UserModel();
$A_user = $O_userModel->getUserByEmail($S_email); $O_user = UserModel::getByEmail($S_email);
if ($A_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, $A_user["PASS_HASH"])) { }else if (!password_verify($S_password,$O_user->PASS_HASH)) {
$S_errmsg = "Invalid password"; $S_errmsg = "Invalid password";
}else if ($A_user["DISABLED"]) { }else if ($O_user->DISABLED) {
$S_errmsg = "This account is disabled"; $S_errmsg = "This account is disabled";
} }
@ -42,7 +42,7 @@ final class UserController
return header("Location: /user/login"); return header("Location: /user/login");
} }
Session::set_login($A_user["ID"]); Session::set_login($O_user->ID);
header("Location: /"); header("Location: /");
@ -54,13 +54,12 @@ final class UserController
$S_username = Utils::getOrDie($A_postParams, "username"); $S_username = Utils::getOrDie($A_postParams, "username");
$S_password = Utils::getOrDie($A_postParams, "password"); $S_password = Utils::getOrDie($A_postParams, "password");
$O_userModel = new UserModel();
if (!filter_var($S_email, FILTER_VALIDATE_EMAIL)) { if (!filter_var($S_email, FILTER_VALIDATE_EMAIL)) {
$S_errmsg = "invalid email"; $S_errmsg = "invalid email";
} else if( strlen($S_password) < 8 || strlen($S_password) > 150 ) { } else if( strlen($S_password) < 8 || strlen($S_password) > 150 ) {
$S_errmsg = "password must be between 8 and 150 characters"; $S_errmsg = "password must be between 8 and 150 characters";
} else if($O_userModel->isEmailInDatabase($S_email)) { } else if(UserModel::isEmailInDatabase($S_email)) {
$S_errmsg = "An user with this email is already registered"; $S_errmsg = "An user with this email is already registered";
} }
@ -71,9 +70,9 @@ final class UserController
} }
$S_password_hash = password_hash($S_password, PASSWORD_DEFAULT); $S_password_hash = password_hash($S_password, PASSWORD_DEFAULT);
$O_userModel->createUser($S_email, $S_username, $S_password_hash);
$O_user = new UserModel($S_email, $S_username, $S_password_hash, null, date("Y-m-d"), 0, 0);
$O_user->insert();
return header("Location: /"); return header("Location: /");
} }
@ -99,17 +98,17 @@ final class UserController
Session::login_or_die(); Session::login_or_die();
$O_userModel = new UserModel(); $O_user = UserModel::getByID($_SESSION["ID"]);
$A_user = $O_userModel->getUserByID($_SESSION["ID"]);
//TODO Convert User into array
return View::show("user/edit", $A_user); return View::show("user/edit", $O_user);
} }
public function updateAction(Array $A_urlParams = null, Array $A_postParams = null) public function updateAction(Array $A_urlParams = null, Array $A_postParams = null)
{ {
Session::login_or_die(); Session::login_or_die();
$O_userModel = new UserModel(); $O_user = UserModel::getByID($_SESSION["ID"]);
if (isset($_FILES["profilPicture"])) { if (isset($_FILES["profilPicture"])) {
@ -133,12 +132,13 @@ final class UserController
} }
$fp = fopen($_FILES['profilPicture']['tmp_name'], 'rb'); $fp = fopen($_FILES['profilPicture']['tmp_name'], 'rb');
$O_userModel->updateProfilePicByID($_SESSION["ID"], $fp); $O_user->updateProfilePic($fp);
} }
if (isset($_POST["email"])) { if (isset($_POST["email"])) {
$S_email = $_POST["email"]; $S_email = $_POST["email"];
if (!empty($S_email) && filter_var($S_email, FILTER_VALIDATE_EMAIL)) { if (!empty($S_email) && filter_var($S_email, FILTER_VALIDATE_EMAIL)) {
$O_userModel->updateEmailByID($_SESSION["ID"], $_POST["email"]); $O_user->S_EMAIL = $_POST["email"];
$O_user->update();
}else{ }else{
throw new HTTPSpecialCaseException(400, "invalid email"); throw new HTTPSpecialCaseException(400, "invalid email");
} }
@ -146,7 +146,8 @@ final class UserController
if (isset($_POST["username"])) { if (isset($_POST["username"])) {
$S_username = $_POST["username"]; $S_username = $_POST["username"];
if (!empty($S_username)) { if (!empty($S_username)) {
$O_userModel->updateUsernameByID($_SESSION["ID"], $_POST["username"]); $O_user->S_USERNAME = $_POST["username"];
$O_user->update();
}else{ }else{
throw new HTTPSpecialCaseException(400, "invalid username"); throw new HTTPSpecialCaseException(400, "invalid username");
} }
@ -168,8 +169,7 @@ final class UserController
{ {
Session::login_or_die(); Session::login_or_die();
$O_userModel = new UserModel(); UserModel::deleteByID($_SESSION["ID"]);
$O_userModel->deleteByID($_SESSION["ID"]);
Session::destroy_session(); Session::destroy_session();
@ -182,9 +182,7 @@ final class UserController
$I_user_id = Utils::intOrDie($A_urlParams[0]); $I_user_id = Utils::intOrDie($A_urlParams[0]);
UserModel::deleteByID($I_user_id);
$O_userModel = new UserModel();
$O_userModel->deleteByID($I_user_id);
echo "Le compte à été supprimé avec succès"; echo "Le compte à été supprimé avec succès";
@ -194,8 +192,7 @@ final class UserController
{ {
if (count($A_urlParams) !== 1 ) throw new HTTPSpecialCaseException(404); if (count($A_urlParams) !== 1 ) throw new HTTPSpecialCaseException(404);
$O_userModel = new UserModel(); $O_user = UserModel::getByID($A_urlParams[0]);
$A_user = $O_userModel->getUserByID($A_urlParams[0]);
if (isset($A_user) && $A_user["PROFILE_PIC"] !== null) { if (isset($A_user) && $A_user["PROFILE_PIC"] !== null) {
header("Content-Type: image"); header("Content-Type: image");

View File

@ -25,11 +25,11 @@ 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"]);
$O_userModel = new UserModel();
$A_recipe["AUTHOR_USERNAME"] = $O_userModel->getUsernameByID($A_recipe["AUTHOR_ID"]); $A_recipe["AUTHOR_USERNAME"] = UserModel::getByID($A_recipe["AUTHOR_ID"])->S_USERNAME;
$O_userModel = new DifficultyModel(); $O_difficultyModel = new DifficultyModel();
$A_recipe["DIFFICULTY_NAME"] = $O_userModel->getByID($A_recipe["DIFFICULTY_ID"]); $A_recipe["DIFFICULTY_NAME"] = $O_difficultyModel->getByID($A_recipe["DIFFICULTY_ID"]);
return $A_recipe; return $A_recipe;
} }

View File

@ -2,29 +2,71 @@
final class UserModel extends UserSessionModel final class UserModel extends UserSessionModel
{ {
public $I_ID = null;
public $S_EMAIL = null;
public $S_USERNAME= null;
public $S_PASSWORD_HASH = null;
public $S_LAST_SEEN = null;
public $S_FIRST_SEEN = null;
public $B_ADMIN = 0;
public $B_DISABLED = 0;
public function createUser($S_email, $S_username, $S_password_hash){ 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;
$this->S_LAST_SEEN = $S_LAST_SEEN;
$this->S_FIRST_SEEN = $S_FIRST_SEEN;
$this->B_ADMIN = $B_ADMIN;
$this->B_DISABLED = $B_DISABLED;
}
public function insert(){
$O_model = Model::get(); $O_model = Model::get();
$stmt = $O_model->prepare("INSERT INTO USER (EMAIL, USERNAME, PASS_HASH) VALUES(:email, :username, :password_hash)"); $stmt = $O_model->prepare("INSERT INTO USER (EMAIL, USERNAME, PASS_HASH, FIRST_SEEN) VALUES(:email, :username, :password_hash, :first_seen)");
$stmt->bindParam("email", $S_email); $stmt->bindParam("email", $this->S_EMAIL);
$stmt->bindParam("username", $S_username); $stmt->bindParam("username", $this->S_USERNAME);
$stmt->bindParam("password_hash", $S_password_hash); $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
}
public function update(){
$O_model = Model::get();
$stmt = $O_model->prepare("UPDATE USER SET EMAIL=:email, USERNAME=:username, PASSWORD_HASH=:password_hash, FIRST_SEEN:first_seen, LAST_SEEN:last_seen, ADMIN=:admin, DISABLED=:disabled) WHERE ID=:id");
$stmt->bindParam("id", $this->I_ID);
$stmt->bindParam("email", $this->S_EMAIL);
$stmt->bindParam("username", $this->S_USERNAME);
$stmt->bindParam("password_hash", $this->S_PASSWORD_HASH);
$stmt->bindParam("first_seen", $this->S_FIRST_SEEN);
$stmt->bindParam("last_seen", $this->S_LAST_SEEN);
$stmt->bindParam("admin", $this->B_ADMIN);
$stmt->bindParam("disabled", $this->B_DISABLED);
$stmt->execute(); $stmt->execute();
} }
public function isEmailInDatabase($S_email){ public function delete(){
self::anonymise();
$O_model = Model::get(); $O_model = Model::get();
$stmt = $O_model->prepare("SELECT count(*) FROM USER WHERE EMAIL=:email"); $stmt = $O_model->prepare("DELETE FROM USER WHERE ID=:id");
$stmt->bindParam("email", $S_email); $stmt->bindParam("id", $this->I_ID);
$stmt->execute(); $stmt->execute();
$count = $stmt->fetch()[0];
return $count != 0;
} }
public function anonymise(){
$O_model = Model::get();
public function getUserByID($I_id){ $stmt = $O_model->prepare("UPDATE RECIPE SET AUTHOR_ID = NULL WHERE AUTHOR_ID = :id");
$stmt->bindParam("id", $this->I_ID);
$stmt->execute();
$stmt = $O_model->prepare("UPDATE APPRECIATION SET AUTHOR_ID = NULL WHERE AUTHOR_ID = :id");
$stmt->bindParam("id", $this->I_ID);
$stmt->execute();
}
public static function getByID($I_id){
$O_model = Model::get(); $O_model = Model::get();
$stmt = $O_model->prepare("SELECT * FROM USER WHERE ID=:id"); $stmt = $O_model->prepare("SELECT * FROM USER WHERE ID=:id");
$stmt->bindParam("id", $I_id); $stmt->bindParam("id", $I_id);
@ -33,77 +75,50 @@ final class UserModel extends UserSessionModel
$row = $stmt->fetch(); $row = $stmt->fetch();
if ($row === false) return null; if ($row === false) return null;
return $row; return $row;
//TODO create an user object and return it
// return new User()
} }
public function getUserByEmail($S_email){ public static function isEmailInDatabase($S_email){
$O_model = Model::get(); $O_model = Model::get();
$stmt = $O_model->prepare("SELECT * FROM USER WHERE email=:email"); $stmt = $O_model->prepare("SELECT count(*) FROM USER WHERE EMAIL=:email");
$stmt->bindParam("email", $S_email);
$stmt->execute();
$count = $stmt->fetch()[0];
return $count != 0;
}
public static function getByEmail($S_email){
$O_model = Model::get();
$stmt = $O_model->prepare("SELECT ID FROM USER WHERE email=:email");
$stmt->bindParam("email", $S_email); $stmt->bindParam("email", $S_email);
$stmt->execute(); $stmt->execute();
$row = $stmt->fetch(); $row = $stmt->fetch();
if ($row === false) return null; if ($row === false) return null;
return $row; return $row;
#TODO create an user object and return it
//return UserModel::getById()
} }
public function updateProfilePic($profile_pic_fp){
public function getUsernameByID($I_id)
{
$O_model = Model::get();
$stmt = $O_model->prepare("SELECT USERNAME FROM USER WHERE ID=:id");
$stmt->bindParam("id", $I_id);
$stmt->execute();
$row = $stmt->fetch();
if ($row === false) return null;
return $row["USERNAME"];
}
public function updateProfilePicByID($I_id, $profile_pic_fp){
$O_model = Model::get(); $O_model = Model::get();
$stmt = $O_model->prepare("UPDATE USER SET PROFILE_PIC=:profile_pic WHERE ID=:id"); $stmt = $O_model->prepare("UPDATE USER SET PROFILE_PIC=:profile_pic WHERE ID=:id");
$stmt->bindParam("id", $I_id); $stmt->bindParam("id", $this->I_ID);
$stmt->bindParam("profile_pic", $profile_pic_fp, PDO::PARAM_LOB); $stmt->bindParam("profile_pic", $profile_pic_fp, PDO::PARAM_LOB);
$stmt->execute(); $stmt->execute();
} }
public function updateEmailByID($I_id, $S_newEmail){ public function getProfilePic(){
$O_model = Model::get(); $O_model = Model::get();
$stmt = $O_model->prepare("UPDATE USER SET EMAIL=:new_email WHERE ID=:id"); $stmt = $O_model->prepare("SELECT PROFILE_PIC FROM USER WHERE ID=:id");
$stmt->bindParam("id", $I_id); $stmt->bindParam("id", $this->I_ID);
$stmt->bindParam("new_email", $S_newEmail);
$stmt->execute();
}
public function updateUsernameByID($I_id, $S_newUsername){
$O_model = Model::get();
$stmt = $O_model->prepare("UPDATE USER SET USERNAME=:new_username WHERE ID=:id");
$stmt->bindParam("id", $I_id);
$stmt->bindParam("new_username", $S_newUsername);
$stmt->execute(); $stmt->execute();
$row = $stmt->fetch();
if ($row === false) return null;
return $row;
} }
public function anonymiseByID($I_id){ public static function searchUsers($S_query)
$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 function deleteByID($I_id){
self::anonymiseByID($I_id);
$O_model = Model::get();
$stmt = $O_model->prepare("DELETE FROM USER WHERE ID=:id");
$stmt->bindParam("id", $I_id);
$stmt->execute();
}
public function searchUsers($S_query)
{ {
$O_model = Model::get(); $O_model = Model::get();
$stmt = $O_model->prepare(" $stmt = $O_model->prepare("
@ -116,9 +131,17 @@ final class UserModel extends UserSessionModel
$S_full_query = "%".$S_query."%"; $S_full_query = "%".$S_query."%";
$stmt->bindParam("full_query", $S_full_query); $stmt->bindParam("full_query", $S_full_query);
$stmt->execute(); $stmt->execute();
$rows = $stmt->fetchAll(); $rows = $stmt->fetchAll();
return $rows; return $rows;
} }
public static function anonymiseByID($I_id){
User::getByID($I_id)->anonymise();
}
public static function deleteByID($I_id){
//TODO Make static
User::getByID($I_id)->delete();
}
} }