commit
1b8f1d7fef
4 changed files with 113 additions and 3 deletions
|
|
@ -25,6 +25,27 @@
|
|||
public function home(){
|
||||
|
||||
|
||||
if (!isset($_SESSION['user']) && isset($_COOKIE['remember_me'])) {
|
||||
|
||||
$token_du_cookie = $_COOKIE['remember_me'];
|
||||
$hash_a_verifier = hash('sha256', $token_du_cookie);
|
||||
|
||||
$objUserModel = new UserModel;
|
||||
// 2. On cherche le jeton dans TA table "tokens" (avec token_user_id)
|
||||
$row = $objUserModel->getTokenUser($hash_a_verifier);
|
||||
|
||||
if ($row) {
|
||||
// 3. Jeton trouvé ! On récupère les infos de l'utilisateur
|
||||
$user = $objUserModel->findUserById($row['token_user_id']);
|
||||
|
||||
if ($user) {
|
||||
// 4. On recrée la session comme lors d'un login normal
|
||||
$_SESSION['user'] = $user;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$intCategory = 0;
|
||||
if (!empty($_GET['filter_cat'])) {
|
||||
$intCategory = (int) $_GET['filter_cat'];
|
||||
|
|
|
|||
|
|
@ -8,14 +8,80 @@
|
|||
|
||||
class UserCtrl extends MotherCtrl {
|
||||
|
||||
/**
|
||||
public function login(){
|
||||
|
||||
$strMail = $_POST['user_mail']??"";
|
||||
$strPwd = $_POST['user_password']??"";
|
||||
|
||||
// Tester le formulaire
|
||||
$arrError = [];
|
||||
if (count($_POST) > 0) {
|
||||
// Vérifier le formulaire
|
||||
if ($strMail == ""){
|
||||
$arrError['mail'] = "Le mail est obligatoire";
|
||||
}
|
||||
if ($strPwd == ""){
|
||||
$arrError['pwd'] = "Le mot de passe est obligatoire";
|
||||
}
|
||||
|
||||
// Si le formulaire est rempli correctement
|
||||
if (count($arrError) == 0){
|
||||
// Vérifier l'utilisateur en BDD
|
||||
$objUserModel = new UserModel;
|
||||
$arrResult = $objUserModel->verifUser($strMail, $strPwd);
|
||||
//var_dump($arrResult);
|
||||
if ($arrResult === false){ // Si la base de données ne renvoie rien
|
||||
$arrError[] = "Mail ou mot de passe invalide";
|
||||
}else{
|
||||
// Ajoute l'utilisateur en session
|
||||
$_SESSION['user'] = $arrResult;
|
||||
$_SESSION['success'] = "Bienvenue, vous êtes bien connecté";
|
||||
if (isset($_POST['remember_me'])) {
|
||||
|
||||
$token = bin2hex(random_bytes(32));
|
||||
$token_hash = hash('sha256', $token);
|
||||
$objUserModel->remember($_SESSION['user']['user_id'],$token_hash);
|
||||
|
||||
setcookie('remember_me', $token, time() + (15*24*60*60), "/", "", true, true);
|
||||
|
||||
}
|
||||
header("Location:index.php");
|
||||
exit;
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->_arrData['arrError'] = $arrError;
|
||||
$this->_display("login");
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function logout(){
|
||||
|
||||
if (isset($_COOKIE['remember_me'])) {
|
||||
$hash = hash('sha256', $_COOKIE['remember_me']);
|
||||
|
||||
$objUserModel = new UserModel;
|
||||
$objUserModel->deleteToken($hash);
|
||||
|
||||
setcookie('remember_me', '', time() - 3600, "/");
|
||||
}
|
||||
unset($_SESSION['user']);
|
||||
|
||||
$_SESSION['success'] = "Vous êtes bien déconnecté";
|
||||
|
||||
header("Location:index.php");
|
||||
exit;
|
||||
|
||||
}
|
||||
/**
|
||||
* Fonction d'inscription d'un utilisateur
|
||||
* Effectue les validations du formulaire,
|
||||
* vérifie l'unicité du mail et du pseudo,
|
||||
* puis insère l'utilisateur en base de données
|
||||
* @return void
|
||||
*/
|
||||
public function signup(){
|
||||
public function signin(){
|
||||
|
||||
// Entité pour réafficher les valeurs dans le formulaire
|
||||
$objUser = new User();
|
||||
|
|
|
|||
|
|
@ -70,6 +70,28 @@
|
|||
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,
|
||||
|
|
|
|||
|
|
@ -85,3 +85,4 @@
|
|||
</div>
|
||||
</nav>
|
||||
{include file="views/_partial/messages.tpl"}
|
||||
{$smarty.cookies|vardump}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue