From 35d9d1964636e60bb05215e153bb1e8e032e7148 Mon Sep 17 00:00:00 2001 From: johan Date: Tue, 7 Mar 2023 20:40:01 +0100 Subject: [PATCH] Ajout de la page de login --- Source/Config/Autoload.php | 1 + Source/Config/Clean.php | 2 +- Source/Config/Validate.php | 2 +- Source/Config/config.php | 11 ++++ Source/Controller/ControllerCandidate.php | 8 ++- Source/Controller/FrontController.php | 75 ++++++++++++++++------- Source/Model/ModelAdmin.php | 9 +++ Source/Views/HTML/form.php | 2 +- Source/index.php | 4 +- 9 files changed, 87 insertions(+), 27 deletions(-) diff --git a/Source/Config/Autoload.php b/Source/Config/Autoload.php index 72c9ecb..f819cdb 100644 --- a/Source/Config/Autoload.php +++ b/Source/Config/Autoload.php @@ -1,5 +1,6 @@ getForm(); + //$html = (new ModelCandidate())->getForm(); require_once($rep.$views['form']); } + public function goToAdminLogin(): void + { + global $rep, $views; + require_once($rep.$views['adminLogin']); + } + /** * Permet de finaliser la saisie du formulaire et de le soumettre. * diff --git a/Source/Controller/FrontController.php b/Source/Controller/FrontController.php index 767e061..86448a0 100644 --- a/Source/Controller/FrontController.php +++ b/Source/Controller/FrontController.php @@ -4,37 +4,68 @@ namespace Controller; use Exception; use PDOException; -use Config\DataManagement; +use Config\Validate; +use Config\Clean; +use Config\AltoRouter; /** * Permet de gérer l'appel des controllers en fonction de l'action et du rôle de l'utilisateur */ -class FrontController -{ - /** - * Définit le comportement de la classe à sa création, on appelle le bon controller en fonction de l'action - * et du rôle de la personne qui souhaite réaliser cette action (utilisateur, administrateur...). - */ - public function __construct() - { - $listControllers = array("\\Controller\\ControllerCandidate", "\\Controller\\ControllerAdmin"); - - global $rep, $views; - $dVueError = array(); +class FrontController { + private $router; + private $rights; + + public function __construct() { + $this->router = new AltoRouter(); + $this->router->setBasePath($_SERVER['BASE_URI']); + $this->mapRoutes(); + $this->rights = array ( + 'Candidate' => array('ControllerCandidate'), + 'Admin' => array('ControllerCandidate','ControllerAdmin') + ); + } - try { - $action = $_REQUEST['action'] ? $action = $_REQUEST['action'] : (new ControllerCandidate())->goToForm(); - foreach ($listControllers as $controller) { - if (method_exists($controller, $action)) { - (new $controller)->$action(); // Si oui, on appelle cette fonction + public function run() { + global $error,$rep,$views; + $exists=false; + $match = $this->router->match(); + if ($match) { + $target = $match['target']; + $params = $match['params']; + if(!isset($_SESSION['role'])) { + $_SESSION['role'] = 'Candidate'; + } + $role = Clean::simpleString($_SESSION['role']); + foreach($this->rights[$role] as $controllerName) { + if(strcmp($controllerName,$target[0])===0) { + $controllerClass = '\Controller\\' . $target[0]; + $controller = new $controllerClass(); + $controller->{$target[1]}($params); + $exists=true; } } - } catch (PDOException|Exception $e) { - $dVueError[] = "Erreur innatendue !"; // Ecriture du message d'erreur - echo "ERREUUUUUR"; + if(!$exists) { + $error = $error['403']; + require_once($rep . $views['error']); + } + } else { + // no route was matched + $error = $error['404']; + require_once($rep . $views['error']); } + } - exit(0); + private function mapRoutes() { + global $controller; + $this->router->map('GET', '/', array($controller['Candidate'], 'goToForm'), 'goToForm'); + $this->router->map('POST', '/submitForm', array($controller['Candidate'], 'submitForm'), 'submitForm'); + $this->router->map('POST', '/addQuestion', array($controller['Admin'], 'addQuestion'), 'addQuestion'); + $this->router->map('POST', '/addResponse', array($controller['Admin'], 'addResponse'), 'addResponse'); + $this->router->map('POST','/continueResponse',array($controller['Admin'],'continueResponse'),'continueResponse'); + $this->router->map('POST','/createForm',array($controller['Admin'],'createForm'),'createForm'); + $this->router->map('POST','/addKeyword',array($controller['Admin'],'addKeyword'),'addKeyword'); + $this->router->map('GET','/goToAdmin',array($controller['Admin'],'goToAdmin'),'goToAdmin'); + $this->router->map('GET','/goToAdminLogin',array($controller['Candidate'],'goToAdminLogin'),'goToLogin'); } } diff --git a/Source/Model/ModelAdmin.php b/Source/Model/ModelAdmin.php index 22653ca..315f077 100644 --- a/Source/Model/ModelAdmin.php +++ b/Source/Model/ModelAdmin.php @@ -13,6 +13,15 @@ use BusinessClass\Form; */ class ModelAdmin { + public function goToAdmin(): void + { + global $rep, $views; + try{ + require_once($rep . $views['admin']); + } catch (PDOException $e) { + $error = $e->getMessage(); + require_once($rep . $views['form']); + } /** * Permet de créer et d'ajouter une question et de retourner son ID afin de la reconnaitre facilement dans * la suite du code. diff --git a/Source/Views/HTML/form.php b/Source/Views/HTML/form.php index 759300a..ee9aaa7 100644 --- a/Source/Views/HTML/form.php +++ b/Source/Views/HTML/form.php @@ -19,7 +19,7 @@ logo UCA - + diff --git a/Source/index.php b/Source/index.php index 85aec21..9a80a32 100644 --- a/Source/index.php +++ b/Source/index.php @@ -1,8 +1,9 @@ run();