From 00c3c43d1f9984f057b08c82c004b4104a317613 Mon Sep 17 00:00:00 2001 From: samuel Date: Fri, 10 Nov 2023 15:26:05 +0100 Subject: [PATCH] connection form ok + start of validation --- public/index.php | 4 +- src/Controller/AuthController.php | 40 ++++++++++++++++--- src/Gateway/AuthGateway.php | 18 +++++++++ src/Model/AuthModel.php | 27 +++++++++++-- ...isplay_error_validation_register.html.twig | 14 +++++++ src/Views/display_register.html.twig | 16 +++++++- src/Views/display_register_confirm.html.twig | 14 +++++++ src/Views/display_results.html.twig | 1 + 8 files changed, 123 insertions(+), 11 deletions(-) create mode 100644 src/Views/display_error_validation_register.html.twig create mode 100644 src/Views/display_register_confirm.html.twig diff --git a/public/index.php b/public/index.php index d4c8ae7..0bbe61b 100644 --- a/public/index.php +++ b/public/index.php @@ -35,13 +35,15 @@ $router = new AltoRouter(); $router->setBasePath($basePath); $sampleFormController = new SampleFormController(new FormResultGateway($con), $twig); -$authController = new \App\Controller\AuthController(new \App\Model\AuthModel(), $twig); +$authGateway = new \App\Gateway\AuthGateway($con); +$authController = new \App\Controller\AuthController(new \App\Model\AuthModel($authGateway), $twig); $router->map("GET", "/", fn() => $sampleFormController->displayForm()); $router->map("POST", "/submit", fn() => $sampleFormController->submitForm($_POST)); $router->map("GET", "/twig", fn() => $sampleFormController->displayFormTwig()); $router->map("POST", "/submit-twig", fn() => $sampleFormController->submitFormTwig($_POST)); $router->map("GET", "/register", fn() => $authController->displayRegister()); +$router->map("POST", "/register", fn() => $authController->confirmRegister($_POST)); $match = $router->match(); diff --git a/src/Controller/AuthController.php b/src/Controller/AuthController.php index f584810..b6285b2 100644 --- a/src/Controller/AuthController.php +++ b/src/Controller/AuthController.php @@ -6,8 +6,7 @@ use App\Gateway\AuthGateway; use App\Model\AuthModel; use Twig\Environment; -class AuthController -{ +class AuthController { private AuthModel $model; private Environment $twig; @@ -15,14 +14,43 @@ class AuthController * @param AuthModel $model * @param Environment $twig */ - public function __construct(AuthModel $model, Environment $twig) - { + public function __construct(AuthModel $model, Environment $twig) { $this->model = $model; $this->twig = $twig; } - public function displayRegister(){ - echo $this->twig->render("display_register.html.twig",[]); + public function displayRegister() { + echo $this->twig->render("display_register.html.twig", []); + } + + public function confirmRegister(array $request) { + + if (isset($request['username']) && isset($request['password']) && isset($request['confirmpassword']) && isset($request['email'])) { + $errors = $this->model->validationRegister($request["password"], $request['confirmpassword']); + + if (empty($errors)) { + echo $this->twig->render("display_register_confirm.html.twig", [$request]); + } else { + $bad_fields = []; + + foreach ($errors as $error_code) { + switch ($error_code) { + case AuthModel::PASSWORD_CONFIRM_NOT_EQUALS: + $bad_fields[] = "password"; + $bad_fields[] = "confirmpassword"; + break; + + } + } + echo $this->twig->render("display_register.html.twig", ['bad_fields' => $bad_fields]); + } + + return; + } + // Invalid request shape + + http_response_code(400); + echo "la requĂȘtte est invalide"; } diff --git a/src/Gateway/AuthGateway.php b/src/Gateway/AuthGateway.php index a3f1765..99ad300 100644 --- a/src/Gateway/AuthGateway.php +++ b/src/Gateway/AuthGateway.php @@ -2,7 +2,25 @@ namespace App\Gateway; +use App\Connexion; + class AuthGateway { + private Connexion $con; + + /** + * @param Connexion $con + */ + public function __construct(Connexion $con) + { + $this->con = $con; + } + + public function insertAccount (string $username, string $password, string $email){ + + + $this->con->exec("INSERT INTO AccountUser VALUES ($username,$password,$email)"); + } + } \ No newline at end of file diff --git a/src/Model/AuthModel.php b/src/Model/AuthModel.php index 035893b..8ced973 100644 --- a/src/Model/AuthModel.php +++ b/src/Model/AuthModel.php @@ -3,9 +3,30 @@ namespace App\Model; use App\Controller\AuthController; +use App\Gateway\AuthGateway; -class AuthModel -{ - private AuthController $controller; +class AuthModel { + + public const PASSWORD_CONFIRM_NOT_EQUALS = 0; + + private AuthGateway $gateway; + /** + * @param AuthGateway $gateway + */ + public function __construct(AuthGateway $gateway) { + $this->gateway = $gateway; + } + + + public function validationRegister(string $password, string $confirmPassword): array { + $errors = []; + if ($password != $confirmPassword) { + $errors[] = self::PASSWORD_CONFIRM_NOT_EQUALS; + } + + // si pas d'erreurs alors on appelle la gateway + + return $errors; + } } \ No newline at end of file diff --git a/src/Views/display_error_validation_register.html.twig b/src/Views/display_error_validation_register.html.twig new file mode 100644 index 0000000..3903a16 --- /dev/null +++ b/src/Views/display_error_validation_register.html.twig @@ -0,0 +1,14 @@ + + + + + Compte + + + +

ERROR REGISTER

+ + + + + \ No newline at end of file diff --git a/src/Views/display_register.html.twig b/src/Views/display_register.html.twig index 0e643cb..7338702 100644 --- a/src/Views/display_register.html.twig +++ b/src/Views/display_register.html.twig @@ -53,11 +53,25 @@ input[type="submit"]:hover { background-color: #0056b3; } + + {% if 'password' in bad_fields %} + .form-group #password { + border-color: red; + } + {% endif %} + + {% if 'confirmpassword' in bad_fields %} + .form-group #confirmpassword { + border-color: red; + } + {% endif %} + +

S'enregistrer

-
+
diff --git a/src/Views/display_register_confirm.html.twig b/src/Views/display_register_confirm.html.twig new file mode 100644 index 0000000..7bb45e0 --- /dev/null +++ b/src/Views/display_register_confirm.html.twig @@ -0,0 +1,14 @@ + + + + + Compte + + + +

Nouveau Compte

+ + + + + \ No newline at end of file diff --git a/src/Views/display_results.html.twig b/src/Views/display_results.html.twig index 6d2aef0..60c3692 100644 --- a/src/Views/display_results.html.twig +++ b/src/Views/display_results.html.twig @@ -14,5 +14,6 @@

description: {{ v.description }}

{% endfor %} + \ No newline at end of file