From 2d6e393e3f8c6f1e7d3c4b366379e9371e2b8e16 Mon Sep 17 00:00:00 2001 From: "renaud.beuret" Date: Tue, 21 Nov 2023 20:56:03 +0100 Subject: [PATCH] FIX : Connection admin --- project/src/controller/AdminController.php | 24 +++++------------- project/src/model/gateways/AdminGateway.php | 3 +-- .../gateways/UtilisateurConnecteGateway.php | 25 +++++++++++++++++++ project/src/model/mdl/MdlAdmin.php | 11 +++++--- project/src/model/mdl/MdlUser.php | 2 +- 5 files changed, 41 insertions(+), 24 deletions(-) create mode 100644 project/src/model/gateways/UtilisateurConnecteGateway.php diff --git a/project/src/controller/AdminController.php b/project/src/controller/AdminController.php index fd26338..0f1e7c1 100755 --- a/project/src/controller/AdminController.php +++ b/project/src/controller/AdminController.php @@ -11,24 +11,12 @@ use model\Scientifique; //gerer la connexion des admins class AdminController { - //public function __construct(array $params) - //{ -// - // //verifier si l'utilisateur est connecté et admin - // if (isset($_SESSION["isAdmin"])) { - // if ($_SESSION["isAdmin"] == true) { - // } else if (isset($_SESSION["isLogged"])) { - // //verifier si l'utilisateur est connecté mais pas admin - // if ($_SESSION["isLogged"] == true) { - // exit(0); - // } - // } else { - // //renvoyer a la page de connexion pour les non connectés - // echo ''; - // } - // } - //} + public function defaultAction(array $params) { + global $twig; + + echo $twig->render('admin/accueil.html'); + } public function notLogged(array $params) { global $twig; //dire acces interdit aux non admins @@ -78,7 +66,7 @@ class AdminController { echo $twig->render('admin/ajouterScientifiques.html',['sexe' => $sexe->getAll(), 'themes' => $theme->getAll(), 'difficultes' => $diff->getAll(), 'scientifique' => $scient]); } - public function listeScientifique() { + public function listeScientifiques(array $params) { global $twig; $ms = new MdlScientifique(); diff --git a/project/src/model/gateways/AdminGateway.php b/project/src/model/gateways/AdminGateway.php index f2837bf..e1b2a97 100755 --- a/project/src/model/gateways/AdminGateway.php +++ b/project/src/model/gateways/AdminGateway.php @@ -7,8 +7,7 @@ use PDOStatement; class AdminGateway { - private PDO $con; - private PDOStatement $stmt; + private Connection $con; public function __construct(Connection $con) { $this->con=$con; diff --git a/project/src/model/gateways/UtilisateurConnecteGateway.php b/project/src/model/gateways/UtilisateurConnecteGateway.php new file mode 100644 index 0000000..066483e --- /dev/null +++ b/project/src/model/gateways/UtilisateurConnecteGateway.php @@ -0,0 +1,25 @@ +con = $con; + } + + public function login(string $email, string $password): bool + { + $sql = "SELECT * FROM Utilisateur WHERE email=:email"; + $this->con->executeQuery($sql, array( + ':email' => array($email, \PDO::PARAM_STR) + )); + + $result = $this->con->getOneResult(); + + if (!empty($result)) { + return password_verify($password,$result['password']); + } + return false; + } +} \ No newline at end of file diff --git a/project/src/model/mdl/MdlAdmin.php b/project/src/model/mdl/MdlAdmin.php index e189c50..ea3a10f 100755 --- a/project/src/model/mdl/MdlAdmin.php +++ b/project/src/model/mdl/MdlAdmin.php @@ -10,15 +10,20 @@ class MdlAdmin extends MdlBase{ $this->gw = new AdminGateway($this->con); } public function login(string $username, string $password): bool{ - return $this->gw->login($username, $password); + if ($this->gw->login($username, $password)) { + $_SESSION['pseudo'] = $username; + $_SESSION['admin'] = true; + return true; + } + return false; } public static function isAdmin(): bool { if(!isset($_SESSION['admin']) || !$_SESSION['admin'] - || !isset($_SESSION['email']) - || $_SESSION['email'] == null) { + || !isset($_SESSION['pseudo']) + || $_SESSION['pseudo'] == null) { return false; } diff --git a/project/src/model/mdl/MdlUser.php b/project/src/model/mdl/MdlUser.php index e4b4bc1..1638684 100755 --- a/project/src/model/mdl/MdlUser.php +++ b/project/src/model/mdl/MdlUser.php @@ -7,7 +7,7 @@ class MdlUser extends MdlBase{ public function __construct(){ parent::__construct(); - $this->gw = new JoueurGateway($this->con); + $this->gw = new UtilisateurConnecteGateway($this->con); } public function login(string $username, string $password): bool{ return $this->gw->login($username, $password);