backup du projet
This commit is contained in:
213
models/user_model.php
Normal file
213
models/user_model.php
Normal file
@@ -0,0 +1,213 @@
|
||||
<?php
|
||||
require_once('mother_model.php');
|
||||
|
||||
|
||||
/**
|
||||
* Traitement des requêtes pour les utilisateurs
|
||||
* @author : meilleurGroup
|
||||
*/
|
||||
|
||||
class UserModel extends Connect{
|
||||
|
||||
public function __construct(){
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fonction de recherche des utilisateurs et leur niveau d'autorisation
|
||||
* @return array
|
||||
*/
|
||||
public function findAllUsers():array{
|
||||
$strRq = "SELECT user_id, user_firstname, user_name, user_image, user_status, authorisation_name, user_pseudo
|
||||
FROM users INNER JOIN authorisation ON authorisation.authorisation_id = users.user_status
|
||||
WHERE user_deleted_at IS NULL";
|
||||
return $this->_db->query($strRq)->fetchAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fonction de vérification des utilisateurs
|
||||
* @param string $strMail
|
||||
* @param string $strPwd
|
||||
* @return array|bool
|
||||
*/
|
||||
public function verifUser(string $strMail, string $strPwd):array|bool{
|
||||
|
||||
$strRq = "SELECT user_id, user_name, user_firstname, user_password, user_image, user_status, authorisation_name, user_pseudo
|
||||
FROM users INNER JOIN authorisation ON authorisation.authorisation_id = users.user_status
|
||||
WHERE user_mail = '".$strMail."'";
|
||||
|
||||
$arrUser = $this->_db->query($strRq)->fetch();
|
||||
if (password_verify($strPwd, $arrUser['user_password'])){
|
||||
unset($arrUser['user_password']);
|
||||
return $arrUser;
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public function insert(object $objUser):bool{
|
||||
|
||||
$strRq = "INSERT INTO users (user_name, user_firstname, user_pseudo, user_mail, user_password, user_phone, user_work, user_location, user_description)
|
||||
VALUES (:name, :firstname, :pseudo,:mail, :pwd, :phone, :work, :location,:description)";
|
||||
|
||||
$rqPrep = $this->_db->prepare($strRq);
|
||||
|
||||
$rqPrep->bindValue(":name", $objUser->getName(), PDO::PARAM_STR);
|
||||
$rqPrep->bindValue(":firstname", $objUser->getFirstname(), PDO::PARAM_STR);
|
||||
$rqPrep->bindValue(":pseudo", $objUser->getPseudo(), PDO::PARAM_STR);
|
||||
$rqPrep->bindValue(":mail", $objUser->getMail(), PDO::PARAM_STR);
|
||||
$rqPrep->bindValue(":pwd", $objUser->getPwdHash(), PDO::PARAM_STR);
|
||||
$rqPrep->bindValue(':phone', $objUser->getPhone() ?? "", PDO::PARAM_STR);
|
||||
$rqPrep->bindValue(':work', $objUser->getWork() ?? "", PDO::PARAM_STR);
|
||||
$rqPrep->bindValue(':location', $objUser->getLocation() ?? "", PDO::PARAM_STR);
|
||||
$rqPrep->bindValue(':description', $objUser->getDescription() ?? "", PDO::PARAM_STR);
|
||||
|
||||
return $rqPrep->execute();
|
||||
}
|
||||
|
||||
public function remember(int $userId, string $token):bool{
|
||||
$strRq = "INSERT INTO tokens (token_user_id, token_hash, token_created_at, token_expire_at) VALUES (:id, :token, NOW(), :exp)";
|
||||
$rqPrep = $this->_db->prepare($strRq);
|
||||
$rqPrep->bindValue(":id", $userId, PDO::PARAM_INT);
|
||||
$rqPrep->bindValue(":token", $token, PDO::PARAM_STR);
|
||||
$rqPrep->bindValue(":exp",
|
||||
//pour faire que le cookies soit valable 15 jours
|
||||
date('Y-m-d H:i:s', time() + (15*24*60*60))
|
||||
, PDO::PARAM_STR);
|
||||
return $rqPrep->execute();
|
||||
|
||||
}
|
||||
public function getTokenUser(string $hash){
|
||||
$strRq = $this->_db->prepare("SELECT token_user_id FROM tokens WHERE token_hash = :hash AND token_expire_at > NOW()");
|
||||
$strRq->execute(['hash' => $hash]);
|
||||
return $strRq->fetch();
|
||||
}
|
||||
public function deleteToken(string $hash){
|
||||
$strRq = $this->_db->prepare("DELETE FROM tokens WHERE token_hash = :hash");
|
||||
return $strRq->execute(['hash' => $hash]);
|
||||
}
|
||||
|
||||
public function update(object $objUser):bool{
|
||||
$strRq = "UPDATE users SET
|
||||
user_name = :name,
|
||||
user_firstname = :firstname,
|
||||
user_pseudo = :pseudo,
|
||||
user_mail = :mail,
|
||||
user_phone = :phone,
|
||||
user_work = :work,
|
||||
user_location = :location,
|
||||
user_description = :description,
|
||||
user_image = :image
|
||||
WHERE user_id = :id";
|
||||
|
||||
|
||||
$rqPrep = $this->_db->prepare($strRq);
|
||||
|
||||
$rqPrep->bindValue(":id", $objUser->getId(), PDO::PARAM_INT);
|
||||
$rqPrep->bindValue(":name", $objUser->getName(), PDO::PARAM_STR);
|
||||
$rqPrep->bindValue(":firstname", $objUser->getFirstname(), PDO::PARAM_STR);
|
||||
$rqPrep->bindValue(":pseudo", $objUser->getPseudo(), PDO::PARAM_STR);
|
||||
$rqPrep->bindValue(":mail", $objUser->getMail(), PDO::PARAM_STR);
|
||||
$rqPrep->bindValue(':phone', $objUser->getPhone() ?? "", PDO::PARAM_STR);
|
||||
$rqPrep->bindValue(':work', $objUser->getWork() ?? "", PDO::PARAM_STR);
|
||||
$rqPrep->bindValue(':location', $objUser->getLocation() ?? "", PDO::PARAM_STR);
|
||||
$rqPrep->bindValue(':description', $objUser->getDescription() ?? "", PDO::PARAM_STR);
|
||||
$rqPrep->bindValue(':image', $objUser->getImage() ?? "", PDO::PARAM_STR);
|
||||
|
||||
return $rqPrep->execute();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Fonction de vérification de mail
|
||||
* @param string $mail
|
||||
* @return bool Est-ce que la requête s'est bien passée
|
||||
*/
|
||||
public function mailExists(string $mail): bool{
|
||||
|
||||
$rq = $this->_db->prepare("SELECT 1 FROM users WHERE user_mail = :mail LIMIT 1");
|
||||
$rq->bindValue(":mail", $mail);
|
||||
$rq->execute();
|
||||
|
||||
return (bool)$rq->fetchColumn();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fonction de changement de status d'un utilisateur
|
||||
* @param object $objUser L'objet utilisateur
|
||||
* @return bool Est-ce que la requête s'est bien passée
|
||||
*/
|
||||
|
||||
public function editStatus(object $objUser):bool{
|
||||
|
||||
$strRq = "UPDATE users
|
||||
SET user_status = :status
|
||||
WHERE user_id = :id";
|
||||
|
||||
$rqPrep = $this->_db->prepare($strRq);
|
||||
$rqPrep->bindValue(":id", $objUser->getId(), PDO::PARAM_INT);
|
||||
$rqPrep->bindValue(":status", $objUser->getStatus(), PDO::PARAM_INT);
|
||||
return $rqPrep->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fonction permettant de supprimer un utilisateur avec une date de suppression
|
||||
* @param int $intId L'identifiant de l'utilisateur
|
||||
* @return bool Est-ce que la requête s'est bien passée
|
||||
*/
|
||||
public function delete_soft(int $intId):bool{
|
||||
|
||||
$strRq = "UPDATE users
|
||||
SET user_deleted_at = NOW()
|
||||
WHERE user_id = :id";
|
||||
|
||||
$rqPrep = $this->_db->prepare($strRq);
|
||||
$rqPrep->bindValue(":id", $intId, PDO::PARAM_INT);
|
||||
return $rqPrep->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* Récupère les informations d'un utilisateur par son ID
|
||||
* @param int $intId L'identifiant de l'utilisateur
|
||||
* @return array Tableau associatif (ou false si pas trouvé)
|
||||
*/
|
||||
public function findUserById(int $intId): array|bool {
|
||||
|
||||
$strRq = "SELECT user_id,user_status ,user_image ,user_name, user_firstname, user_pseudo, user_mail, user_phone, user_work, user_location, user_description, authorisation_name
|
||||
FROM users INNER JOIN authorisation ON authorisation.authorisation_id = users.user_status
|
||||
WHERE user_id = :id";
|
||||
|
||||
$prep = $this->_db->prepare($strRq);
|
||||
$prep->bindValue(':id', $intId, PDO::PARAM_INT);
|
||||
$prep->execute();
|
||||
|
||||
return $prep->fetch();
|
||||
}
|
||||
|
||||
public function findUserByPseudo(string $strPseudo): array|bool {
|
||||
|
||||
$strRq = "SELECT user_id,user_image, user_status ,user_name, user_firstname, user_pseudo, user_mail, user_phone, user_work, user_location, user_description, authorisation_name
|
||||
FROM users INNER JOIN authorisation ON authorisation.authorisation_id = users.user_status
|
||||
WHERE user_pseudo = :pseudo";
|
||||
|
||||
$prep = $this->_db->prepare($strRq);
|
||||
$prep->bindValue(':pseudo', $strPseudo, PDO::PARAM_STR);
|
||||
$prep->execute();
|
||||
|
||||
return $prep->fetch();
|
||||
}
|
||||
|
||||
public function pseudoExists(string $pseudo): bool{
|
||||
|
||||
$rq = $this->_db->prepare("SELECT 1 FROM users WHERE user_pseudo = :pseudo LIMIT 1");
|
||||
$rq->bindValue(":pseudo", $pseudo, PDO::PARAM_STR);
|
||||
$rq->execute();
|
||||
|
||||
return $rq->fetchColumn();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user