diff --git a/project/src/composer.json b/project/src/composer.json index d505363..9e1c6da 100755 --- a/project/src/composer.json +++ b/project/src/composer.json @@ -11,8 +11,7 @@ "twig/twig": "^3.7", "twbs/bootstrap": "^5.3", "ext-iconv": "*", - "ext-pdo": "*", - "ext-http": "*" + "ext-pdo": "*" }, "scripts": { "post-update-cmd": [ diff --git a/project/src/controller/AdminController.php b/project/src/controller/AdminController.php index d9e6c07..9cc7486 100755 --- a/project/src/controller/AdminController.php +++ b/project/src/controller/AdminController.php @@ -2,6 +2,11 @@ namespace controller; use Exception; use PDOException; +use model\MdlDifficulte; +use model\MdlScientifique; +use model\MdlSexe; +use model\MdlThematique; +use model\Scientifique; //gerer la connexion des admins class AdminController { @@ -12,47 +17,69 @@ class AdminController { //verifier si l'utilisateur est connecté et admin if(isset($_SESSION["isAdmin"])){ - if($_SESSION["isAdmin"]) { - try { - switch ($action) { - case '': - echo "accueil admin"; - exit; - // echo $twig->render('admin/accueil.html'); - case 'stats': - echo "stats admin"; - exit; - // echo $twig->render('admin/stats.html'); - case 'ajouterScientifiques': - echo "page ajout scientifiques admin"; - exit; - // echo $twig->render('admin/ajouter.html'); - //mauvaise action - default: - $dVueErreur[] = "Erreur d'appel php"; - echo $twig->render('accueil.html', ['dVueErreur' => $dVueErreur]); - break; - } - } catch (PDOException $e) { - $dVueErreur[] = 'Erreur avec la base de données !'; - echo $twig->render('erreur.html', ['dVueErreur' => $dVueErreur]); - } catch (Exception $e2) { - $dVueErreur[] = 'Erreur inattendue !'; - echo $twig->render('erreur.html', ['dVueErreur' => $dVueErreur]); - } - } + if($_SESSION["isAdmin"]==true){ + //donner la page admin a l'admin + try { + switch($action) { + case '': + echo $twig->render('admin/accueil.html'); + break; + case 'stats': + echo $twig->render('admin/stats.html'); + break; + case 'ajouterScientifiques': + $sexe = new MdlSexe(); + $theme = new MdlThematique(); + $diff = new MdlDifficulte(); + if(!empty($_POST)){ + $sci=new MdlScientifique(); + $sci->addScientifique(new Scientifique(0, + $_POST["name"], + $_POST["prenom"], + $_POST["url"], + \DateTime::createFromFormat("Y-m-d",$_POST["date"]), + $_POST["description"], + 0, + $theme->getFromId(intval($_POST["theme"])), + $diff->getFromId(intval($_POST["difficulte"])), + $sexe->getFromId(intval($_POST["sexe"])) + )); + } + echo $twig->render('admin/ajouterScientifiques.html',['sexe' => $sexe->getAll(), 'themes' => $theme->getAll(), 'difficultes' => $diff->getAll()]); + break; + //mauvaise action + default: + $dVueErreur[] = "Erreur d'appel php"; + echo $twig->render('erreur.html', ['dVueErreur' => $dVueErreur]); + break; + } + } catch (\PDOException $e) { + $dVueErreur[] = 'Erreur avec la base de données !'; + echo $twig->render('erreur.html', ['dVueErreur' => $dVueErreur]); + } catch (\Exception $e2) { + $dVueErreur[] = 'Erreur inattendue !'; + echo $twig->render('erreur.html', ['dVueErreur' => $dVueErreur]); + } + } } - //verifier si l'utilisateur est connecté mais pas admin - if(isset($_SESSION["isLogged"])){ - if($_SESSION["isLogged"]) { + else if(isset($_SESSION["isLogged"])){ + //verifier si l'utilisateur est connecté mais pas admin + if($_SESSION["isLogged"]==true) { //dire acces interdit aux non admins - $dVueErreur[] = "Erreur 403 : Acces interdit"; + $dVueErreur[] = 'Erreur 403 : Accès interdit !'; echo $twig->render('erreur.html', ['dVueErreur' => $dVueErreur]); exit(0); } - } - //renvoyer a la page de connexion pour les non connectés - echo $twig->render('login.html'); + } else { + //renvoyer a la page de connexion pour les non connectés + echo ''; + } exit(0); } + } + + + + +?> \ No newline at end of file diff --git a/project/src/controller/FrontController.php b/project/src/controller/FrontController.php index 1b6b1f1..7d01be4 100755 --- a/project/src/controller/FrontController.php +++ b/project/src/controller/FrontController.php @@ -35,6 +35,7 @@ class FrontController $router->map('GET|POST','/pseudo/[a:action]?','PseudoController'); $router->map('GET|POST','/admin/[a:action]','AdminController'); $router->map('GET|POST','/[a:action]?','UserController'); + $router->map('GET|POST','/login','login'); session_start(); @@ -56,16 +57,15 @@ class FrontController case 'AdminController': $action = $match['params']['action']; - if (!MdlAdmin::isAdmin()) { - $action = 'login'; - } - $this->callController('AdminController',$action); + //if (!MdlAdmin::isAdmin()) { + // $action = 'login'; + //} + new AdminController($action); break; case 'PseudoController': $this->callController('PseudoController',$match); break; - //mauvaise action default: $dVueErreur[] = "Erreur d'appel php"; diff --git a/project/src/controller/UserController.php b/project/src/controller/UserController.php index f98983c..f60a20d 100755 --- a/project/src/controller/UserController.php +++ b/project/src/controller/UserController.php @@ -10,6 +10,9 @@ use model\GameGateway; use model\MdlDifficulte; use model\MdlJeu; use model\ValidationException; +use model\MdlUser; +use model\MdlAdmin; +use model\LoginException; class UserController { @@ -62,6 +65,33 @@ class UserController { } } + public function login() { + global $twig; + if($_SERVER['REQUEST_METHOD'] === 'POST'){ + Validation::valUserLogin($_REQUEST['login'], $dVueErreur); + $ug = new MdlUser(); + if($ug->login($_REQUEST['login'], $_REQUEST['password'])) { + $_SESSION['pseudo'] = $_REQUEST['login']; + $_SESSION['isLogged'] = true; + header("Location: ."); + } else { + //voir si c'est un admin + $ug = new MdlAdmin(); + if($ug->login($_REQUEST['login'], $_REQUEST['password'])) { + $_SESSION['pseudo'] = $_REQUEST['login']; + $_SESSION['isAdmin'] = true; + $_SESSION['isLogged'] = true; + header("Location: ."); + } else { + $dVueErreur[] = "Connexion échouée"; + throw new LoginException("Connexion err"); + } + } + } else { + echo $twig->render('login.html'); + } + } + public function createParty(array $params) : void { global $twig; diff --git a/project/src/model/gateways/ScientifiqueGateway.php b/project/src/model/gateways/ScientifiqueGateway.php index 9de8cc1..b48c8d9 100755 --- a/project/src/model/gateways/ScientifiqueGateway.php +++ b/project/src/model/gateways/ScientifiqueGateway.php @@ -25,4 +25,20 @@ class ScientifiqueGateway ); return $this->con->getOneResult(); } + + public function addScientifique(Scientifique $sci): bool{ + return $this->con->executeQuery( + "INSERT INTO Scientifique(nom, prenom, photo, dateNaissance, descriptif, ratioTrouvee, idThematique, idDifficulte, idSexe) VALUES (:nom, :prenom, :photo, :dateNaissance, :descriptif, :ratioTrouvee, :idThematique, :idDifficulte, :idSexe);" + ,[ + ":nom"=>[$sci->getNom(),$this->con::PARAM_STR], + ":prenom"=>[$sci->getPrenom(),$this->con::PARAM_STR], + ":photo"=>[$sci->getPhoto(),$this->con::PARAM_STR], + ":dateNaissance"=>[date("Y-m-d H:i:s", $sci->getDateNaiss()->getTimestamp()),$this->con::PARAM_STR], + ":descriptif"=>[$sci->getDescriptif(),$this->con::PARAM_STR], + ":ratioTrouvee"=>[$sci->getRatioTrouvee(),$this->con::PARAM_STR], + ":idThematique"=>[$sci->getThematique()->getId(),$this->con::PARAM_STR], + ":idDifficulte"=>[$sci->getDifficulte()->getId(),$this->con::PARAM_STR], + ":idSexe"=>[$sci->getSexe()->getId(),$this->con::PARAM_STR] + ]); + } } \ No newline at end of file diff --git a/project/src/model/gateways/SexeGateway.php b/project/src/model/gateways/SexeGateway.php index 49ceedd..6902a5f 100755 --- a/project/src/model/gateways/SexeGateway.php +++ b/project/src/model/gateways/SexeGateway.php @@ -16,4 +16,10 @@ class SexeGateway [':id' => [$id, $this->con::PARAM_INT]]); return $this->con->getOneResult(); } + + public function getAll(): array + { + $this->con->executeQuery("SELECT id, libelle FROM Sexe;"); + return $this->con->getResults(); + } } \ No newline at end of file diff --git a/project/src/model/gateways/ThematiqueGateway.php b/project/src/model/gateways/ThematiqueGateway.php index 5f60426..5220c69 100755 --- a/project/src/model/gateways/ThematiqueGateway.php +++ b/project/src/model/gateways/ThematiqueGateway.php @@ -16,4 +16,9 @@ class ThematiqueGateway [':id' => [$id, $this->con::PARAM_INT]]); return $this->con->getOneResult(); } + public function getAll(): array + { + $this->con->executeQuery("SELECT id, libelle FROM Thematique;"); + return $this->con->getResults(); + } } \ No newline at end of file diff --git a/project/src/model/mdl/MdlScientifique.php b/project/src/model/mdl/MdlScientifique.php index fa12ff6..bcac636 100755 --- a/project/src/model/mdl/MdlScientifique.php +++ b/project/src/model/mdl/MdlScientifique.php @@ -41,4 +41,7 @@ class MdlScientifique extends MdlBase{ $difficulte, $sexe); } + public function addScientifique(Scientifique $s){ + return $this->gw->addScientifique($s); + } } \ No newline at end of file diff --git a/project/src/model/mdl/MdlSexe.php b/project/src/model/mdl/MdlSexe.php index bc9cf40..beaf9d8 100755 --- a/project/src/model/mdl/MdlSexe.php +++ b/project/src/model/mdl/MdlSexe.php @@ -14,4 +14,12 @@ class MdlSexe extends MdlBase{ $row = $this->gw->getFromId($id); return new Sexe($row['id'], $row['libelle']); } + public function getAll(): array { + $ret=array(); + $row = $this->gw->getAll(); + for($i=0; $i< count($row); $i++){ + array_push($ret, new Sexe($row[$i]['id'], $row[$i]['libelle'])); + } + return $ret; + } } \ No newline at end of file diff --git a/project/src/model/mdl/MdlThematique.php b/project/src/model/mdl/MdlThematique.php index d09f2ca..e477104 100755 --- a/project/src/model/mdl/MdlThematique.php +++ b/project/src/model/mdl/MdlThematique.php @@ -14,4 +14,13 @@ class MdlThematique extends MdlBase{ $row = $this->gw->getFromId($id); return new Thematique($row['id'], $row['libelle']); } + + public function getAll(): array { + $ret=array(); + $row = $this->gw->getAll(); + for($i=0; $i< count($row); $i++){ + array_push($ret, new Thematique($row[$i]['id'], $row[$i]['libelle'])); + } + return $ret; + } } \ No newline at end of file diff --git a/project/src/templates/admin/accueil.html b/project/src/templates/admin/accueil.html new file mode 100644 index 0000000..71ebfb5 --- /dev/null +++ b/project/src/templates/admin/accueil.html @@ -0,0 +1,26 @@ + + +
+ + + + +{{error}}