From c569f4f28068de001cde4778bc8c424fb300cdec Mon Sep 17 00:00:00 2001 From: Samuel Date: Fri, 10 Nov 2023 08:17:07 +0100 Subject: [PATCH 01/19] auhtentification bootstrap --- src/Controller/AuthController.php | 18 +++++++ src/Gateway/AuthGateway.php | 8 +++ src/Model/AuthModel.php | 8 +++ src/Views/display_register.html.twig | 81 ++++++++++++++++++++++++++++ 4 files changed, 115 insertions(+) create mode 100644 src/Controller/AuthController.php create mode 100644 src/Gateway/AuthGateway.php create mode 100644 src/Model/AuthModel.php create mode 100644 src/Views/display_register.html.twig diff --git a/src/Controller/AuthController.php b/src/Controller/AuthController.php new file mode 100644 index 0000000..3f952c9 --- /dev/null +++ b/src/Controller/AuthController.php @@ -0,0 +1,18 @@ + + + + + Log in + + + + + +
+

S'enregistrer

+
+
+ + + + + + + + + + +
+
+ +
+
+
+ + + + \ No newline at end of file -- 2.36.3 From 5b390fdab49a4db1e4e2f8ef130df5e3477b3b49 Mon Sep 17 00:00:00 2001 From: Samuel Date: Fri, 10 Nov 2023 08:17:54 +0100 Subject: [PATCH 02/19] authentification bootstrap --- Documentation/data.puml | 20 ++++++++++++++++++++ public/index.php | 3 +++ sql/setup-tables.sql | 2 ++ src/Controller/AuthController.php | 23 +++++++++++++++++------ src/Gateway/AuthGateway.php | 2 +- src/Model/AuthModel.php | 3 +++ src/Views/display_register.html.twig | 5 ++--- 7 files changed, 48 insertions(+), 10 deletions(-) diff --git a/Documentation/data.puml b/Documentation/data.puml index 0ad5135..7e6e777 100755 --- a/Documentation/data.puml +++ b/Documentation/data.puml @@ -68,4 +68,24 @@ class Color { + getValue(): int } +class AuthController{ + + + login (requete) : int + + register (requete) : int +} +AuthController --> "- modelAuth" AuthModel + +class AuthModel{ + + login (mail : string, password : string) + + register (mail : string, password :string) +} +AuthModel --> "- gateway" AuthGateway + +class AuthGateway{ + + + insert(mail : string, password : string) + + isAccountEqual(mail : string, password : string) +} + + @enduml \ No newline at end of file diff --git a/public/index.php b/public/index.php index 4c5290b..74124e7 100644 --- a/public/index.php +++ b/public/index.php @@ -35,10 +35,13 @@ $router = new AltoRouter(); $router->setBasePath($basePath); $sampleFormController = new SampleFormController(new FormResultGateway($con), $twig); +$authController = new \App\Controller\AuthController(new \App\Gateway\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()); $match = $router->match(); diff --git a/sql/setup-tables.sql b/sql/setup-tables.sql index 0c6fbe7..4f6e0d4 100644 --- a/sql/setup-tables.sql +++ b/sql/setup-tables.sql @@ -1,8 +1,10 @@ -- drop tables here DROP TABLE IF EXISTS FormEntries; +DROP TABLE IF EXISTS AccountUser; CREATE TABLE FormEntries(name varchar, description varchar); +CREATE TABLE AccountUser(name varchar, profilPicture varchar, age int); diff --git a/src/Controller/AuthController.php b/src/Controller/AuthController.php index 3f952c9..f584810 100644 --- a/src/Controller/AuthController.php +++ b/src/Controller/AuthController.php @@ -2,16 +2,27 @@ namespace App\Controller; -use App\Gateway\AccountGateway; +use App\Gateway\AuthGateway; +use App\Model\AuthModel; use Twig\Environment; -class AccountController +class AuthController { - private AccountGateway $gateway; - private Environment $twing; + private AuthModel $model; + private Environment $twig; - public function display(){ - require "src/Views/display_login.html.twig"; + /** + * @param AuthModel $model + * @param 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",[]); } diff --git a/src/Gateway/AuthGateway.php b/src/Gateway/AuthGateway.php index 865faa9..a3f1765 100644 --- a/src/Gateway/AuthGateway.php +++ b/src/Gateway/AuthGateway.php @@ -2,7 +2,7 @@ namespace App\Gateway; -class AccountGateway +class AuthGateway { } \ No newline at end of file diff --git a/src/Model/AuthModel.php b/src/Model/AuthModel.php index 3c94e50..035893b 100644 --- a/src/Model/AuthModel.php +++ b/src/Model/AuthModel.php @@ -2,7 +2,10 @@ namespace App\Model; +use App\Controller\AuthController; + class AuthModel { + private AuthController $controller; } \ No newline at end of file diff --git a/src/Views/display_register.html.twig b/src/Views/display_register.html.twig index 08e6e9f..0e643cb 100644 --- a/src/Views/display_register.html.twig +++ b/src/Views/display_register.html.twig @@ -2,7 +2,7 @@ - Log in + S'enregistrer @@ -56,12 +56,11 @@
-

S'enregistrer

+

S'enregistrer

- -- 2.36.3 From 2c784086ac53c21604a116b9ec2100434e44452a Mon Sep 17 00:00:00 2001 From: Samuel Date: Fri, 10 Nov 2023 08:17:07 +0100 Subject: [PATCH 03/19] auhtentification bootstrap --- src/Controller/AuthController.php | 18 +++++++ src/Gateway/AuthGateway.php | 8 +++ src/Model/AuthModel.php | 8 +++ src/Views/display_register.html.twig | 81 ++++++++++++++++++++++++++++ 4 files changed, 115 insertions(+) create mode 100644 src/Controller/AuthController.php create mode 100644 src/Gateway/AuthGateway.php create mode 100644 src/Model/AuthModel.php create mode 100644 src/Views/display_register.html.twig diff --git a/src/Controller/AuthController.php b/src/Controller/AuthController.php new file mode 100644 index 0000000..3f952c9 --- /dev/null +++ b/src/Controller/AuthController.php @@ -0,0 +1,18 @@ + + + + + Log in + + + + + +
+

S'enregistrer

+ +
+ + + + + + + + + + +
+
+ +
+ +
+ + + + \ No newline at end of file -- 2.36.3 From 102c7a21be2c19f9dbeecacfee456ab947145e25 Mon Sep 17 00:00:00 2001 From: Samuel Date: Fri, 10 Nov 2023 08:17:54 +0100 Subject: [PATCH 04/19] authentification bootstrap --- Documentation/data.puml | 20 ++++++++++++++++++++ public/index.php | 3 +++ sql/setup-tables.sql | 2 ++ src/Controller/AuthController.php | 23 +++++++++++++++++------ src/Gateway/AuthGateway.php | 2 +- src/Model/AuthModel.php | 3 +++ src/Views/display_register.html.twig | 5 ++--- 7 files changed, 48 insertions(+), 10 deletions(-) diff --git a/Documentation/data.puml b/Documentation/data.puml index 0ad5135..7e6e777 100755 --- a/Documentation/data.puml +++ b/Documentation/data.puml @@ -68,4 +68,24 @@ class Color { + getValue(): int } +class AuthController{ + + + login (requete) : int + + register (requete) : int +} +AuthController --> "- modelAuth" AuthModel + +class AuthModel{ + + login (mail : string, password : string) + + register (mail : string, password :string) +} +AuthModel --> "- gateway" AuthGateway + +class AuthGateway{ + + + insert(mail : string, password : string) + + isAccountEqual(mail : string, password : string) +} + + @enduml \ No newline at end of file diff --git a/public/index.php b/public/index.php index 4c5290b..74124e7 100644 --- a/public/index.php +++ b/public/index.php @@ -35,10 +35,13 @@ $router = new AltoRouter(); $router->setBasePath($basePath); $sampleFormController = new SampleFormController(new FormResultGateway($con), $twig); +$authController = new \App\Controller\AuthController(new \App\Gateway\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()); $match = $router->match(); diff --git a/sql/setup-tables.sql b/sql/setup-tables.sql index 0c6fbe7..4f6e0d4 100644 --- a/sql/setup-tables.sql +++ b/sql/setup-tables.sql @@ -1,8 +1,10 @@ -- drop tables here DROP TABLE IF EXISTS FormEntries; +DROP TABLE IF EXISTS AccountUser; CREATE TABLE FormEntries(name varchar, description varchar); +CREATE TABLE AccountUser(name varchar, profilPicture varchar, age int); diff --git a/src/Controller/AuthController.php b/src/Controller/AuthController.php index 3f952c9..f584810 100644 --- a/src/Controller/AuthController.php +++ b/src/Controller/AuthController.php @@ -2,16 +2,27 @@ namespace App\Controller; -use App\Gateway\AccountGateway; +use App\Gateway\AuthGateway; +use App\Model\AuthModel; use Twig\Environment; -class AccountController +class AuthController { - private AccountGateway $gateway; - private Environment $twing; + private AuthModel $model; + private Environment $twig; - public function display(){ - require "src/Views/display_login.html.twig"; + /** + * @param AuthModel $model + * @param 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",[]); } diff --git a/src/Gateway/AuthGateway.php b/src/Gateway/AuthGateway.php index 865faa9..a3f1765 100644 --- a/src/Gateway/AuthGateway.php +++ b/src/Gateway/AuthGateway.php @@ -2,7 +2,7 @@ namespace App\Gateway; -class AccountGateway +class AuthGateway { } \ No newline at end of file diff --git a/src/Model/AuthModel.php b/src/Model/AuthModel.php index 3c94e50..035893b 100644 --- a/src/Model/AuthModel.php +++ b/src/Model/AuthModel.php @@ -2,7 +2,10 @@ namespace App\Model; +use App\Controller\AuthController; + class AuthModel { + private AuthController $controller; } \ No newline at end of file diff --git a/src/Views/display_register.html.twig b/src/Views/display_register.html.twig index 08e6e9f..0e643cb 100644 --- a/src/Views/display_register.html.twig +++ b/src/Views/display_register.html.twig @@ -2,7 +2,7 @@ - Log in + S'enregistrer @@ -56,12 +56,11 @@
-

S'enregistrer

+

S'enregistrer

- -- 2.36.3 From eb99f43a8989c0561bb0978a6f0a9385115bc85e Mon Sep 17 00:00:00 2001 From: samuel Date: Fri, 10 Nov 2023 09:05:24 +0100 Subject: [PATCH 05/19] CI ok --- public/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/index.php b/public/index.php index 74124e7..d4c8ae7 100644 --- a/public/index.php +++ b/public/index.php @@ -35,7 +35,7 @@ $router = new AltoRouter(); $router->setBasePath($basePath); $sampleFormController = new SampleFormController(new FormResultGateway($con), $twig); -$authController = new \App\Controller\AuthController(new \App\Gateway\AuthGateway(), $twig); +$authController = new \App\Controller\AuthController(new \App\Model\AuthModel(), $twig); $router->map("GET", "/", fn() => $sampleFormController->displayForm()); $router->map("POST", "/submit", fn() => $sampleFormController->submitForm($_POST)); -- 2.36.3 From 00c3c43d1f9984f057b08c82c004b4104a317613 Mon Sep 17 00:00:00 2001 From: samuel Date: Fri, 10 Nov 2023 15:26:05 +0100 Subject: [PATCH 06/19] 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 -- 2.36.3 From 905735ce95067f0cf6d30228bb0cc3cdafc0a49f Mon Sep 17 00:00:00 2001 From: samuel Date: Mon, 13 Nov 2023 14:16:15 +0100 Subject: [PATCH 07/19] puml update --- Documentation/data.puml | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Documentation/data.puml b/Documentation/data.puml index 7e6e777..48973d4 100755 --- a/Documentation/data.puml +++ b/Documentation/data.puml @@ -69,19 +69,21 @@ class Color { } class AuthController{ + -twig: Environment - + login (requete) : int - + register (requete) : int + + login (request) : int + + register (request) : int } AuthController --> "- modelAuth" AuthModel class AuthModel{ - + login (mail : string, password : string) - + register (mail : string, password :string) + + validationRegister(username : string, email : string, password : string) + + validationLogin (username : string, email : string, password : string) } AuthModel --> "- gateway" AuthGateway class AuthGateway{ + -con : Connection + insert(mail : string, password : string) + isAccountEqual(mail : string, password : string) -- 2.36.3 From e965bb63c730f738c14b56fdf81e153f3ae33fc1 Mon Sep 17 00:00:00 2001 From: samuel Date: Mon, 13 Nov 2023 15:25:34 +0100 Subject: [PATCH 08/19] insert changed for validator --- sql/setup-tables.sql | 2 +- src/Controller/AuthController.php | 2 +- src/Gateway/AuthGateway.php | 15 ++++++--------- src/Model/AuthModel.php | 6 ++++-- 4 files changed, 12 insertions(+), 13 deletions(-) diff --git a/sql/setup-tables.sql b/sql/setup-tables.sql index 4f6e0d4..8a7debc 100644 --- a/sql/setup-tables.sql +++ b/sql/setup-tables.sql @@ -4,7 +4,7 @@ DROP TABLE IF EXISTS FormEntries; DROP TABLE IF EXISTS AccountUser; CREATE TABLE FormEntries(name varchar, description varchar); -CREATE TABLE AccountUser(name varchar, profilPicture varchar, age int); +CREATE TABLE AccountUser(username varchar, password varchar, email varchar); diff --git a/src/Controller/AuthController.php b/src/Controller/AuthController.php index b6285b2..27b5847 100644 --- a/src/Controller/AuthController.php +++ b/src/Controller/AuthController.php @@ -26,7 +26,7 @@ class AuthController { 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']); + $errors = $this->model->validationRegister($request['username'],$request["password"], $request['confirmpassword'],$request['email']); if (empty($errors)) { echo $this->twig->render("display_register_confirm.html.twig", [$request]); diff --git a/src/Gateway/AuthGateway.php b/src/Gateway/AuthGateway.php index 99ad300..1ae3fa3 100644 --- a/src/Gateway/AuthGateway.php +++ b/src/Gateway/AuthGateway.php @@ -3,24 +3,21 @@ namespace App\Gateway; use App\Connexion; +use \PDO; -class AuthGateway -{ +class AuthGateway { private Connexion $con; /** * @param Connexion $con */ - public function __construct(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)"); + public function insertAccount(string $username, string $password, string $email) { + $this->con->exec("INSERT INTO AccountUser VALUES (:username,:password,:email)", [':username' => [$username, PDO::PARAM_STR],':password'=> [$password, PDO::PARAM_STR],':email'=>[$email, PDO::PARAM_STR]]); } -} \ No newline at end of file +} \ No newline at end of file diff --git a/src/Model/AuthModel.php b/src/Model/AuthModel.php index 8ced973..c591e6c 100644 --- a/src/Model/AuthModel.php +++ b/src/Model/AuthModel.php @@ -18,13 +18,15 @@ class AuthModel { } - public function validationRegister(string $password, string $confirmPassword): array { + public function validationRegister(string $username, string $password, string $confirmPassword,string $email): array { $errors = []; if ($password != $confirmPassword) { $errors[] = self::PASSWORD_CONFIRM_NOT_EQUALS; } - // si pas d'erreurs alors on appelle la gateway + else{ + $this->gateway->insertAccount($username,$password,$email); + }// si pas d'erreurs alors on appelle la gateway return $errors; } -- 2.36.3 From b967e9615f63b8b5193e63ebb080cece2b8cb63a Mon Sep 17 00:00:00 2001 From: samuel Date: Tue, 14 Nov 2023 17:30:35 +0100 Subject: [PATCH 09/19] register ok, just do email uniq for DB --- .nfs0000000006fb02b80000003f | 0 public/index.php | 2 +- sql/setup-tables.sql | 9 +-- src/Controller/AuthController.php | 58 +++++++++++--------- src/Gateway/AuthGateway.php | 9 ++- src/Model/AuthModel.php | 15 +++-- src/Validation/Validation.php | 2 +- src/Views/display_register.html.twig | 15 ++--- src/Views/display_register_confirm.html.twig | 7 ++- 9 files changed, 68 insertions(+), 49 deletions(-) create mode 100644 .nfs0000000006fb02b80000003f diff --git a/.nfs0000000006fb02b80000003f b/.nfs0000000006fb02b80000003f new file mode 100644 index 0000000..e69de29 diff --git a/public/index.php b/public/index.php index f764c3f..bce242c 100644 --- a/public/index.php +++ b/public/index.php @@ -68,7 +68,7 @@ if ($response instanceof ViewHttpResponse) { } catch (\Twig\Error\RuntimeError|\Twig\Error\SyntaxError $e) { http_response_code(500); echo "There was an error rendering your view, please refer to an administrator.\nlogs date: " . date("YYYD, d M Y H:i:s"); - throw e; + throw $e; } break; } diff --git a/sql/setup-tables.sql b/sql/setup-tables.sql index 00cfb93..108b62a 100644 --- a/sql/setup-tables.sql +++ b/sql/setup-tables.sql @@ -1,14 +1,15 @@ -- drop tables here DROP TABLE IF EXISTS FormEntries; -<<<<<<< HEAD DROP TABLE IF EXISTS AccountUser; -======= DROP TABLE IF EXISTS TacticInfo; ->>>>>>> 86373fb81bf0c9bca0daf49a26f973e3145d8ff5 CREATE TABLE FormEntries(name varchar, description varchar); -CREATE TABLE AccountUser(username varchar, password varchar, email varchar); +CREATE TABLE AccountUser( + username varchar, + hash varchar, + email varchar unique +); CREATE TABLE TacticInfo( id integer PRIMARY KEY AUTOINCREMENT, diff --git a/src/Controller/AuthController.php b/src/Controller/AuthController.php index efcb61d..864190e 100644 --- a/src/Controller/AuthController.php +++ b/src/Controller/AuthController.php @@ -3,10 +3,15 @@ namespace App\Controller; use App\Gateway\AuthGateway; +use App\Http\HttpRequest; use App\Http\HttpResponse; +use App\Http\ViewHttpResponse; use App\Model\AuthModel; +use App\Validation\FieldValidationFail; +use App\Validation\Validators; use Twig\Environment; + class AuthController { private AuthModel $model; @@ -17,42 +22,45 @@ class AuthController { $this->model = $model; } - public function displayRegister() { - echo $this->twig->render("display_register.html.twig", []); + public function displayRegister(): HttpResponse { + return ViewHttpResponse::twig("display_register.html.twig", []); } public function confirmRegister(array $request): HttpResponse { - - - - if (isset($request['username']) && isset($request['password']) && isset($request['confirmpassword']) && isset($request['email'])) { - $errors = $this->model->validationRegister($request['username'],$request["password"], $request['confirmpassword'],$request['email']); - - if (empty($errors)) { - echo $this->twig->render("display_register_confirm.html.twig", [$request]); - } else { - $bad_fields = []; + $fails = []; + $request = HttpRequest::from($request, $fails, [ + "username" => [Validators::name(), Validators::lenBetween(0, 32)], + "password" => [Validators::lenBetween(0, 256)], + "confirmpassword" => [Validators::lenBetween(0, 256)], + "email" => [Validators::regex("/@/")] + ]); - foreach ($errors as $error_code) { - switch ($error_code) { - case AuthModel::PASSWORD_CONFIRM_NOT_EQUALS: - $bad_fields[] = "password"; - $bad_fields[] = "confirmpassword"; - break; - } + if (!empty($fails)) { + $bad_fields = []; + foreach ($fails as $err){ + if ($err instanceof FieldValidationFail){ + $bad_fields[] = $err->getFieldName(); } - echo $this->twig->render("display_register.html.twig", ['bad_fields' => $bad_fields]); } + return ViewHttpResponse::twig("display_register.html.twig", ['bad_fields' => $bad_fields]); + } - return ; + $fails = $this->model->validationRegister($request['username'], $request["password"], $request['confirmpassword'], $request['email']); + if (empty($fails)) { + $results = $this->model->getUserFields($request['email']); + return ViewHttpResponse::twig("display_register_confirm.html.twig", ['results' => $results]); } - // Invalid request shape - http_response_code(400); - echo "la requêtte est invalide"; + $bad_fields = []; + foreach ($fails as $err){ + if ($err instanceof FieldValidationFail){ + $bad_fields[] = $err->getFieldName(); + } + } + return ViewHttpResponse::twig("display_register.html.twig", ['bad_fields' => $bad_fields]); } - +//GARDER LES EMAIL ET USERNAME ET REGLER SURLIGNAGE DES MDP QUAND CA VA PAS } \ No newline at end of file diff --git a/src/Gateway/AuthGateway.php b/src/Gateway/AuthGateway.php index 1ae3fa3..c0cafe7 100644 --- a/src/Gateway/AuthGateway.php +++ b/src/Gateway/AuthGateway.php @@ -15,8 +15,13 @@ class AuthGateway { $this->con = $con; } - public function insertAccount(string $username, string $password, string $email) { - $this->con->exec("INSERT INTO AccountUser VALUES (:username,:password,:email)", [':username' => [$username, PDO::PARAM_STR],':password'=> [$password, PDO::PARAM_STR],':email'=>[$email, PDO::PARAM_STR]]); + public function insertAccount(string $username, string $hash, string $email) { + + $this->con->exec("INSERT INTO AccountUser VALUES (:username,:hash,:email)", [':username' => [$username, PDO::PARAM_STR],':hash'=> [$hash, PDO::PARAM_STR],':email'=>[$email, PDO::PARAM_STR]]); + } + + public function getUserFields (string $email):array{ + return $this->con->fetch ("SELECT username,email FROM AccountUser WHERE email = :email",[':email'=>[$email, PDO::PARAM_STR]]); } diff --git a/src/Model/AuthModel.php b/src/Model/AuthModel.php index c591e6c..430fadc 100644 --- a/src/Model/AuthModel.php +++ b/src/Model/AuthModel.php @@ -4,10 +4,10 @@ namespace App\Model; use App\Controller\AuthController; use App\Gateway\AuthGateway; +use App\Validation\FieldValidationFail; class AuthModel { - public const PASSWORD_CONFIRM_NOT_EQUALS = 0; private AuthGateway $gateway; /** @@ -21,14 +21,21 @@ class AuthModel { public function validationRegister(string $username, string $password, string $confirmPassword,string $email): array { $errors = []; if ($password != $confirmPassword) { - $errors[] = self::PASSWORD_CONFIRM_NOT_EQUALS; + $errors[] = new FieldValidationFail("confirmpassword","passwords not equals"); } else{ - $this->gateway->insertAccount($username,$password,$email); - }// si pas d'erreurs alors on appelle la gateway + $hash = password_hash($password,PASSWORD_DEFAULT); + $this->gateway->insertAccount($username,$hash,$email); + } return $errors; } + public function getUserFields(string $email):array{ + return $this->gateway->getUserFields($email); + } + + + } \ No newline at end of file diff --git a/src/Validation/Validation.php b/src/Validation/Validation.php index b797edc..4372380 100644 --- a/src/Validation/Validation.php +++ b/src/Validation/Validation.php @@ -20,7 +20,7 @@ class Validation { foreach ($validators as $validator) { $error = $validator->validate($valName, $val); if ($error != null) { - $failures[] = $error; + $failures = array_merge($failures, $error); $had_errors = true; } } diff --git a/src/Views/display_register.html.twig b/src/Views/display_register.html.twig index 7338702..a082638 100644 --- a/src/Views/display_register.html.twig +++ b/src/Views/display_register.html.twig @@ -54,17 +54,12 @@ background-color: #0056b3; } - {% if 'password' in bad_fields %} - .form-group #password { + {% for err in bad_fields %} + .form-group #{{ err }} { border-color: red; } - {% endif %} + {% endfor %} - {% if 'confirmpassword' in bad_fields %} - .form-group #confirmpassword { - border-color: red; - } - {% endif %} @@ -76,9 +71,9 @@ - + - + diff --git a/src/Views/display_register_confirm.html.twig b/src/Views/display_register_confirm.html.twig index 7bb45e0..1b73b31 100644 --- a/src/Views/display_register_confirm.html.twig +++ b/src/Views/display_register_confirm.html.twig @@ -6,9 +6,12 @@ -

Nouveau Compte

- +

Nouveau Compte

+{% for row in results %} +

Votre pseudo : {{ row['username']}}

+

Votre Email : {{ row['email']}}

+{% endfor %} \ No newline at end of file -- 2.36.3 From 7b65a4bd23e67b19ae148ee839b23f02a57ac39e Mon Sep 17 00:00:00 2001 From: samuel Date: Wed, 15 Nov 2023 14:24:53 +0100 Subject: [PATCH 10/19] register finish + login to do --- .nfs0000000006fb02b80000003f | 0 src/Controller/AuthController.php | 10 +++++----- src/Gateway/AuthGateway.php | 16 +++++++++++++--- src/Model/AuthModel.php | 6 +++++- src/Views/display_register_confirm.html.twig | 8 ++++---- 5 files changed, 27 insertions(+), 13 deletions(-) delete mode 100644 .nfs0000000006fb02b80000003f diff --git a/.nfs0000000006fb02b80000003f b/.nfs0000000006fb02b80000003f deleted file mode 100644 index e69de29..0000000 diff --git a/src/Controller/AuthController.php b/src/Controller/AuthController.php index 864190e..48217eb 100644 --- a/src/Controller/AuthController.php +++ b/src/Controller/AuthController.php @@ -39,8 +39,8 @@ class AuthController { if (!empty($fails)) { $bad_fields = []; - foreach ($fails as $err){ - if ($err instanceof FieldValidationFail){ + foreach ($fails as $err) { + if ($err instanceof FieldValidationFail) { $bad_fields[] = $err->getFieldName(); } } @@ -50,12 +50,12 @@ class AuthController { $fails = $this->model->validationRegister($request['username'], $request["password"], $request['confirmpassword'], $request['email']); if (empty($fails)) { $results = $this->model->getUserFields($request['email']); - return ViewHttpResponse::twig("display_register_confirm.html.twig", ['results' => $results]); + return ViewHttpResponse::twig("display_register_confirm.html.twig", ['username' => $results['username'], 'email' => $results['email']]); } $bad_fields = []; - foreach ($fails as $err){ - if ($err instanceof FieldValidationFail){ + foreach ($fails as $err) { + if ($err instanceof FieldValidationFail) { $bad_fields[] = $err->getFieldName(); } } diff --git a/src/Gateway/AuthGateway.php b/src/Gateway/AuthGateway.php index c0cafe7..f128a20 100644 --- a/src/Gateway/AuthGateway.php +++ b/src/Gateway/AuthGateway.php @@ -15,14 +15,24 @@ class AuthGateway { $this->con = $con; } - public function insertAccount(string $username, string $hash, string $email) { + public function mailExist(string $email):bool{ + return $this->getUserFields($email) != null; + } + + + public function insertAccount(string $username, string $hash, string $email) { $this->con->exec("INSERT INTO AccountUser VALUES (:username,:hash,:email)", [':username' => [$username, PDO::PARAM_STR],':hash'=> [$hash, PDO::PARAM_STR],':email'=>[$email, PDO::PARAM_STR]]); } - public function getUserFields (string $email):array{ - return $this->con->fetch ("SELECT username,email FROM AccountUser WHERE email = :email",[':email'=>[$email, PDO::PARAM_STR]]); + + public function getUserFields (string $email): ?array { + $results = $this->con->fetch ("SELECT username,email FROM AccountUser WHERE email = :email",[':email'=>[$email, PDO::PARAM_STR]]); + $firstRow = $results[0] ?? null; + return $firstRow; } + + } \ No newline at end of file diff --git a/src/Model/AuthModel.php b/src/Model/AuthModel.php index 430fadc..8853962 100644 --- a/src/Model/AuthModel.php +++ b/src/Model/AuthModel.php @@ -24,7 +24,11 @@ class AuthModel { $errors[] = new FieldValidationFail("confirmpassword","passwords not equals"); } - else{ + if ($this->gateway->mailExist($email)){ + $errors[] = new FieldValidationFail("email","email already exist"); + } + + if(empty($errors)){ $hash = password_hash($password,PASSWORD_DEFAULT); $this->gateway->insertAccount($username,$hash,$email); } diff --git a/src/Views/display_register_confirm.html.twig b/src/Views/display_register_confirm.html.twig index 1b73b31..115676b 100644 --- a/src/Views/display_register_confirm.html.twig +++ b/src/Views/display_register_confirm.html.twig @@ -8,10 +8,10 @@

Nouveau Compte

-{% for row in results %} -

Votre pseudo : {{ row['username']}}

-

Votre Email : {{ row['email']}}

-{% endfor %} + +

Votre pseudo : {{ username }}

+

Votre Email : {{ email }}

+ \ No newline at end of file -- 2.36.3 From e1cefff40dcf408133b99b4183d77f76ea2b0008 Mon Sep 17 00:00:00 2001 From: samuel Date: Wed, 15 Nov 2023 15:24:09 +0100 Subject: [PATCH 11/19] work login in progress --- public/index.php | 1 + src/Controller/AuthController.php | 5 +- src/Views/display_login.html.twig | 86 +++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 src/Views/display_login.html.twig diff --git a/public/index.php b/public/index.php index bce242c..8f9a00c 100644 --- a/public/index.php +++ b/public/index.php @@ -38,6 +38,7 @@ $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)); +$router->map("GET", "/login", fn() => $authController->displayLogin()); $router->map("GET", "/tactic/new", fn() => $editorController->makeNew()); $router->map("GET", "/tactic/[i:id]/edit", fn(int $id) => $editorController->openEditorFor($id)); diff --git a/src/Controller/AuthController.php b/src/Controller/AuthController.php index 48217eb..9589af1 100644 --- a/src/Controller/AuthController.php +++ b/src/Controller/AuthController.php @@ -61,6 +61,9 @@ class AuthController { } return ViewHttpResponse::twig("display_register.html.twig", ['bad_fields' => $bad_fields]); } -//GARDER LES EMAIL ET USERNAME ET REGLER SURLIGNAGE DES MDP QUAND CA VA PAS + + public function displayLogin():HttpResponse{ + return ViewHttpResponse::twig("display_login.html.twig", []); + } } \ No newline at end of file diff --git a/src/Views/display_login.html.twig b/src/Views/display_login.html.twig new file mode 100644 index 0000000..48d4bfd --- /dev/null +++ b/src/Views/display_login.html.twig @@ -0,0 +1,86 @@ + + + + + Connexion + + + + + +
+

Se connecter

+ +
+ + + + + + +
+
+ +
+ +
+ + + + \ No newline at end of file -- 2.36.3 From 291f8acc2b6f3c8f74433c41ba12ad5f8aaede2a Mon Sep 17 00:00:00 2001 From: samuel Date: Thu, 16 Nov 2023 13:19:56 +0100 Subject: [PATCH 12/19] loginpassed to finish --- src/Controller/AuthController.php | 12 ++++++++++++ src/Views/display_login_passed.html.twig | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 src/Views/display_login_passed.html.twig diff --git a/src/Controller/AuthController.php b/src/Controller/AuthController.php index 9589af1..30363da 100644 --- a/src/Controller/AuthController.php +++ b/src/Controller/AuthController.php @@ -66,4 +66,16 @@ class AuthController { return ViewHttpResponse::twig("display_login.html.twig", []); } + public function displayLoginPassed(array $request):HttpResponse{ + $fails = []; + $request = HttpRequest::from($request, $fails, [ + "password" => [Validators::lenBetween(0, 256)], + "email" => [Validators::regex("/@/")] + ]); + + $results = $this->model->getUserFields($request['email']); + + return ViewHttpResponse::twig("display_login_passed.html.twig",[['username' => $results['username'], 'email' => $results['email']]]) + } + } \ No newline at end of file diff --git a/src/Views/display_login_passed.html.twig b/src/Views/display_login_passed.html.twig new file mode 100644 index 0000000..1795479 --- /dev/null +++ b/src/Views/display_login_passed.html.twig @@ -0,0 +1,19 @@ + + + + + Compte + + + +

Votre Compte

+ + +{% for v in results %} +

username: {{ v.name }}

+

description: {{ v.description }}

+{% endfor %} + + + + \ No newline at end of file -- 2.36.3 From c9a86f02f252baf7b80b2e2604f130921f8663a8 Mon Sep 17 00:00:00 2001 From: samuel Date: Fri, 17 Nov 2023 11:06:14 +0100 Subject: [PATCH 13/19] nothing --- src/Controller/AuthController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Controller/AuthController.php b/src/Controller/AuthController.php index 30363da..d2aba83 100644 --- a/src/Controller/AuthController.php +++ b/src/Controller/AuthController.php @@ -75,7 +75,7 @@ class AuthController { $results = $this->model->getUserFields($request['email']); - return ViewHttpResponse::twig("display_login_passed.html.twig",[['username' => $results['username'], 'email' => $results['email']]]) + return ViewHttpResponse::twig("display_login_passed.html.twig",[['username' => $results['username'], 'email' => $results['email']]]); } } \ No newline at end of file -- 2.36.3 From a4a6b5d6f289ad7607d595d5cf558c21aebfa6d9 Mon Sep 17 00:00:00 2001 From: samuel Date: Fri, 17 Nov 2023 14:53:57 +0100 Subject: [PATCH 14/19] authentication completed, view more pretty in progress --- public/index.php | 1 + src/Controller/AuthController.php | 16 ++++++++++++--- src/Gateway/AuthGateway.php | 5 +++++ src/Model/AuthModel.php | 20 +++++++++++++++++++ ...ml.twig => display_auth_confirm.html.twig} | 0 src/Views/display_login_passed.html.twig | 19 ------------------ 6 files changed, 39 insertions(+), 22 deletions(-) rename src/Views/{display_register_confirm.html.twig => display_auth_confirm.html.twig} (100%) delete mode 100644 src/Views/display_login_passed.html.twig diff --git a/public/index.php b/public/index.php index 8f9a00c..6eca6c3 100644 --- a/public/index.php +++ b/public/index.php @@ -39,6 +39,7 @@ $router->map("POST", "/submit-twig", fn() => $sampleFormController->submitFormTw $router->map("GET", "/register", fn() => $authController->displayRegister()); $router->map("POST", "/register", fn() => $authController->confirmRegister($_POST)); $router->map("GET", "/login", fn() => $authController->displayLogin()); +$router->map("POST", "/login", fn() => $authController->displayLoginPassed($_POST)); $router->map("GET", "/tactic/new", fn() => $editorController->makeNew()); $router->map("GET", "/tactic/[i:id]/edit", fn(int $id) => $editorController->openEditorFor($id)); diff --git a/src/Controller/AuthController.php b/src/Controller/AuthController.php index d2aba83..999a89b 100644 --- a/src/Controller/AuthController.php +++ b/src/Controller/AuthController.php @@ -50,7 +50,7 @@ class AuthController { $fails = $this->model->validationRegister($request['username'], $request["password"], $request['confirmpassword'], $request['email']); if (empty($fails)) { $results = $this->model->getUserFields($request['email']); - return ViewHttpResponse::twig("display_register_confirm.html.twig", ['username' => $results['username'], 'email' => $results['email']]); + return ViewHttpResponse::twig("display_auth_confirm.html.twig", ['username' => $results['username'], 'email' => $results['email']]); } $bad_fields = []; @@ -73,9 +73,19 @@ class AuthController { "email" => [Validators::regex("/@/")] ]); - $results = $this->model->getUserFields($request['email']); - return ViewHttpResponse::twig("display_login_passed.html.twig",[['username' => $results['username'], 'email' => $results['email']]]); + $fails = $this->model->validationLogin($request['email'],$request['password']); + $bad_fields = []; + foreach ($fails as $err){ + if ($err instanceof FieldValidationFail){ + $bad_fields [] = $err->getFieldName(); + } + } + if (empty($fails)){ + $results = $this->model->getUserFields($request['email']); + return ViewHttpResponse::twig("display_auth_confirm.html.twig",['username' => $results['username'], 'email' => $results['email']]); + } + return ViewHttpResponse::twig("display_login.html.twig",['bad_fields' => $bad_fields]); } } \ No newline at end of file diff --git a/src/Gateway/AuthGateway.php b/src/Gateway/AuthGateway.php index f128a20..c04e5e3 100644 --- a/src/Gateway/AuthGateway.php +++ b/src/Gateway/AuthGateway.php @@ -25,6 +25,11 @@ class AuthGateway { $this->con->exec("INSERT INTO AccountUser VALUES (:username,:hash,:email)", [':username' => [$username, PDO::PARAM_STR],':hash'=> [$hash, PDO::PARAM_STR],':email'=>[$email, PDO::PARAM_STR]]); } + public function getUserHash(string $email):string{ + $results = $this->con->fetch ("SELECT hash FROM AccountUser WHERE email = :email",[':email'=>[$email, PDO::PARAM_STR]]); + return $results[0]['hash']; + } + public function getUserFields (string $email): ?array { $results = $this->con->fetch ("SELECT username,email FROM AccountUser WHERE email = :email",[':email'=>[$email, PDO::PARAM_STR]]); diff --git a/src/Model/AuthModel.php b/src/Model/AuthModel.php index 8853962..febe186 100644 --- a/src/Model/AuthModel.php +++ b/src/Model/AuthModel.php @@ -20,6 +20,7 @@ class AuthModel { public function validationRegister(string $username, string $password, string $confirmPassword,string $email): array { $errors = []; + if ($password != $confirmPassword) { $errors[] = new FieldValidationFail("confirmpassword","passwords not equals"); } @@ -41,5 +42,24 @@ class AuthModel { } + public function validationLogin(string $email,string $password): array{ + $errors = []; + + if (!$this->gateway->mailExist($email)) { + $errors[] = new FieldValidationFail("email", "email doesnt exists"); + return $errors; + } + $hash = $this->gateway->getUserHash($email); + + if (!password_verify($password, $hash)) { + $errors[] = new FieldValidationFail("password", "invalid password"); + } + + return $errors; + } + + + + } \ No newline at end of file diff --git a/src/Views/display_register_confirm.html.twig b/src/Views/display_auth_confirm.html.twig similarity index 100% rename from src/Views/display_register_confirm.html.twig rename to src/Views/display_auth_confirm.html.twig diff --git a/src/Views/display_login_passed.html.twig b/src/Views/display_login_passed.html.twig deleted file mode 100644 index 1795479..0000000 --- a/src/Views/display_login_passed.html.twig +++ /dev/null @@ -1,19 +0,0 @@ - - - - - Compte - - - -

Votre Compte

- - -{% for v in results %} -

username: {{ v.name }}

-

description: {{ v.description }}

-{% endfor %} - - - - \ No newline at end of file -- 2.36.3 From eb37915b00013c1c120b01dd007c590f2a04d8c3 Mon Sep 17 00:00:00 2001 From: samuel Date: Fri, 17 Nov 2023 15:24:55 +0100 Subject: [PATCH 15/19] correction for pr --- src/Controller/AuthController.php | 40 +++++++++------------- src/Views/display_auth_confirm.html.twig | 43 ++++++++++++++++++++---- 2 files changed, 53 insertions(+), 30 deletions(-) diff --git a/src/Controller/AuthController.php b/src/Controller/AuthController.php index 999a89b..1628d9c 100644 --- a/src/Controller/AuthController.php +++ b/src/Controller/AuthController.php @@ -26,25 +26,28 @@ class AuthController { return ViewHttpResponse::twig("display_register.html.twig", []); } + private function displayRegisterBadFields(array $fails): HttpResponse{ + $bad_fields = []; + foreach ($fails as $err) { + if ($err instanceof FieldValidationFail) { + $bad_fields[] = $err->getFieldName(); + } + } + return ViewHttpResponse::twig("display_register.html.twig", ['bad_fields' => $bad_fields]); + } public function confirmRegister(array $request): HttpResponse { $fails = []; $request = HttpRequest::from($request, $fails, [ "username" => [Validators::name(), Validators::lenBetween(0, 32)], - "password" => [Validators::lenBetween(0, 256)], - "confirmpassword" => [Validators::lenBetween(0, 256)], - "email" => [Validators::regex("/@/")] + "password" => [Validators::lenBetween(6, 256)], + "confirmpassword" => [Validators::lenBetween(6, 256)], + "email" => [Validators::regex("/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/g"),Validators::lenBetween(5, 256)] ]); if (!empty($fails)) { - $bad_fields = []; - foreach ($fails as $err) { - if ($err instanceof FieldValidationFail) { - $bad_fields[] = $err->getFieldName(); - } - } - return ViewHttpResponse::twig("display_register.html.twig", ['bad_fields' => $bad_fields]); + return $this->displayRegisterBadFields($fails); } $fails = $this->model->validationRegister($request['username'], $request["password"], $request['confirmpassword'], $request['email']); @@ -53,13 +56,7 @@ class AuthController { return ViewHttpResponse::twig("display_auth_confirm.html.twig", ['username' => $results['username'], 'email' => $results['email']]); } - $bad_fields = []; - foreach ($fails as $err) { - if ($err instanceof FieldValidationFail) { - $bad_fields[] = $err->getFieldName(); - } - } - return ViewHttpResponse::twig("display_register.html.twig", ['bad_fields' => $bad_fields]); + return $this->displayRegisterBadFields($fails); } public function displayLogin():HttpResponse{ @@ -75,17 +72,12 @@ class AuthController { $fails = $this->model->validationLogin($request['email'],$request['password']); - $bad_fields = []; - foreach ($fails as $err){ - if ($err instanceof FieldValidationFail){ - $bad_fields [] = $err->getFieldName(); - } - } + if (empty($fails)){ $results = $this->model->getUserFields($request['email']); return ViewHttpResponse::twig("display_auth_confirm.html.twig",['username' => $results['username'], 'email' => $results['email']]); } - return ViewHttpResponse::twig("display_login.html.twig",['bad_fields' => $bad_fields]); + return $this->displayRegisterBadFields($fails); } } \ No newline at end of file diff --git a/src/Views/display_auth_confirm.html.twig b/src/Views/display_auth_confirm.html.twig index 115676b..89823f4 100644 --- a/src/Views/display_auth_confirm.html.twig +++ b/src/Views/display_auth_confirm.html.twig @@ -1,17 +1,48 @@ + - Compte - - + + Profil Utilisateur + + + + \ No newline at end of file -- 2.36.3 From ff77f2fb89e7adf471a6f025a4d5e3906a72f235 Mon Sep 17 00:00:00 2001 From: Samuel Date: Sun, 19 Nov 2023 00:46:27 +0100 Subject: [PATCH 16/19] apply suggestions --- src/Controller/AuthController.php | 22 +++++++++---------- src/Model/AuthModel.php | 6 ++--- src/Views/display_auth_confirm.html.twig | 2 -- ...isplay_error_validation_register.html.twig | 1 - src/Views/display_login.html.twig | 1 - src/Views/display_register.html.twig | 1 - src/Views/display_results.html.twig | 1 - src/Views/sample_form.html.twig | 1 - 8 files changed, 14 insertions(+), 21 deletions(-) diff --git a/src/Controller/AuthController.php b/src/Controller/AuthController.php index 1628d9c..acc3aab 100644 --- a/src/Controller/AuthController.php +++ b/src/Controller/AuthController.php @@ -26,37 +26,37 @@ class AuthController { return ViewHttpResponse::twig("display_register.html.twig", []); } - private function displayRegisterBadFields(array $fails): HttpResponse{ + private function displayBadFields(string $viewName, array $fails): HttpResponse{ $bad_fields = []; foreach ($fails as $err) { if ($err instanceof FieldValidationFail) { $bad_fields[] = $err->getFieldName(); } } - return ViewHttpResponse::twig("display_register.html.twig", ['bad_fields' => $bad_fields]); + return ViewHttpResponse::twig($viewName, ['bad_fields' => $bad_fields]); } public function confirmRegister(array $request): HttpResponse { $fails = []; $request = HttpRequest::from($request, $fails, [ - "username" => [Validators::name(), Validators::lenBetween(0, 32)], + "username" => [Validators::name(), Validators::lenBetween(2, 32)], "password" => [Validators::lenBetween(6, 256)], "confirmpassword" => [Validators::lenBetween(6, 256)], - "email" => [Validators::regex("/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/g"),Validators::lenBetween(5, 256)] + "email" => [Validators::regex("/^\\S+@\\S+\\.\\S+$/"),Validators::lenBetween(5, 256)] ]); if (!empty($fails)) { - return $this->displayRegisterBadFields($fails); + return $this->displayBadFields("display_register.html.twig",$fails); } - $fails = $this->model->validationRegister($request['username'], $request["password"], $request['confirmpassword'], $request['email']); + $fails = $this->model->register($request['username'], $request["password"], $request['confirmpassword'], $request['email']); if (empty($fails)) { $results = $this->model->getUserFields($request['email']); return ViewHttpResponse::twig("display_auth_confirm.html.twig", ['username' => $results['username'], 'email' => $results['email']]); } - return $this->displayRegisterBadFields($fails); + return $this->displayBadFields("display_register.html.twig",$fails); } public function displayLogin():HttpResponse{ @@ -66,18 +66,18 @@ class AuthController { public function displayLoginPassed(array $request):HttpResponse{ $fails = []; $request = HttpRequest::from($request, $fails, [ - "password" => [Validators::lenBetween(0, 256)], - "email" => [Validators::regex("/@/")] + "password" => [Validators::lenBetween(6, 256)], + "email" => [Validators::regex("/^\\S+@\\S+\\.\\S+$/"),Validators::lenBetween(5, 256)] ]); - $fails = $this->model->validationLogin($request['email'],$request['password']); + $fails = $this->model->login($request['email'],$request['password']); if (empty($fails)){ $results = $this->model->getUserFields($request['email']); return ViewHttpResponse::twig("display_auth_confirm.html.twig",['username' => $results['username'], 'email' => $results['email']]); } - return $this->displayRegisterBadFields($fails); + return $this->displayBadFields("display_login.html.twig",$fails); } } \ No newline at end of file diff --git a/src/Model/AuthModel.php b/src/Model/AuthModel.php index febe186..fd00bb8 100644 --- a/src/Model/AuthModel.php +++ b/src/Model/AuthModel.php @@ -18,11 +18,11 @@ class AuthModel { } - public function validationRegister(string $username, string $password, string $confirmPassword,string $email): array { + public function register(string $username, string $password, string $confirmPassword,string $email): array { $errors = []; if ($password != $confirmPassword) { - $errors[] = new FieldValidationFail("confirmpassword","passwords not equals"); + $errors[] = new FieldValidationFail("confirmpassword","password and password confirmation are not equals"); } if ($this->gateway->mailExist($email)){ @@ -42,7 +42,7 @@ class AuthModel { } - public function validationLogin(string $email,string $password): array{ + public function login(string $email,string $password): array{ $errors = []; if (!$this->gateway->mailExist($email)) { diff --git a/src/Views/display_auth_confirm.html.twig b/src/Views/display_auth_confirm.html.twig index 89823f4..60c63b2 100644 --- a/src/Views/display_auth_confirm.html.twig +++ b/src/Views/display_auth_confirm.html.twig @@ -1,5 +1,3 @@ - - diff --git a/src/Views/display_error_validation_register.html.twig b/src/Views/display_error_validation_register.html.twig index 3903a16..95befc0 100644 --- a/src/Views/display_error_validation_register.html.twig +++ b/src/Views/display_error_validation_register.html.twig @@ -1,4 +1,3 @@ - diff --git a/src/Views/display_login.html.twig b/src/Views/display_login.html.twig index 48d4bfd..33b2385 100644 --- a/src/Views/display_login.html.twig +++ b/src/Views/display_login.html.twig @@ -1,4 +1,3 @@ - diff --git a/src/Views/display_register.html.twig b/src/Views/display_register.html.twig index a082638..40199a0 100644 --- a/src/Views/display_register.html.twig +++ b/src/Views/display_register.html.twig @@ -1,4 +1,3 @@ - diff --git a/src/Views/display_results.html.twig b/src/Views/display_results.html.twig index 60c3692..a33546b 100644 --- a/src/Views/display_results.html.twig +++ b/src/Views/display_results.html.twig @@ -1,4 +1,3 @@ - diff --git a/src/Views/sample_form.html.twig b/src/Views/sample_form.html.twig index bcb958e..6f4a9b5 100644 --- a/src/Views/sample_form.html.twig +++ b/src/Views/sample_form.html.twig @@ -1,4 +1,3 @@ - -- 2.36.3 From 7685ff845455d84a92f92d02227ba3132a481a97 Mon Sep 17 00:00:00 2001 From: samuel Date: Mon, 20 Nov 2023 09:31:04 +0100 Subject: [PATCH 17/19] apply lasts suggestions --- public/index.php | 2 +- src/Controller/AuthController.php | 16 ++++++++-------- .../display_error_validation_register.html.twig | 13 ------------- 3 files changed, 9 insertions(+), 22 deletions(-) delete mode 100644 src/Views/display_error_validation_register.html.twig diff --git a/public/index.php b/public/index.php index 6eca6c3..35ac549 100644 --- a/public/index.php +++ b/public/index.php @@ -39,7 +39,7 @@ $router->map("POST", "/submit-twig", fn() => $sampleFormController->submitFormTw $router->map("GET", "/register", fn() => $authController->displayRegister()); $router->map("POST", "/register", fn() => $authController->confirmRegister($_POST)); $router->map("GET", "/login", fn() => $authController->displayLogin()); -$router->map("POST", "/login", fn() => $authController->displayLoginPassed($_POST)); +$router->map("POST", "/login", fn() => $authController->confirmLogin($_POST)); $router->map("GET", "/tactic/new", fn() => $editorController->makeNew()); $router->map("GET", "/tactic/[i:id]/edit", fn(int $id) => $editorController->openEditorFor($id)); diff --git a/src/Controller/AuthController.php b/src/Controller/AuthController.php index acc3aab..b4ef8cb 100644 --- a/src/Controller/AuthController.php +++ b/src/Controller/AuthController.php @@ -35,8 +35,9 @@ class AuthController { } return ViewHttpResponse::twig($viewName, ['bad_fields' => $bad_fields]); } - public function confirmRegister(array $request): HttpResponse { + + public function confirmRegister(array $request): HttpResponse { $fails = []; $request = HttpRequest::from($request, $fails, [ "username" => [Validators::name(), Validators::lenBetween(2, 32)], @@ -44,35 +45,34 @@ class AuthController { "confirmpassword" => [Validators::lenBetween(6, 256)], "email" => [Validators::regex("/^\\S+@\\S+\\.\\S+$/"),Validators::lenBetween(5, 256)] ]); - - if (!empty($fails)) { return $this->displayBadFields("display_register.html.twig",$fails); } - $fails = $this->model->register($request['username'], $request["password"], $request['confirmpassword'], $request['email']); if (empty($fails)) { $results = $this->model->getUserFields($request['email']); return ViewHttpResponse::twig("display_auth_confirm.html.twig", ['username' => $results['username'], 'email' => $results['email']]); } - return $this->displayBadFields("display_register.html.twig",$fails); } + public function displayLogin():HttpResponse{ return ViewHttpResponse::twig("display_login.html.twig", []); } - public function displayLoginPassed(array $request):HttpResponse{ + + public function confirmLogin(array $request):HttpResponse{ $fails = []; $request = HttpRequest::from($request, $fails, [ "password" => [Validators::lenBetween(6, 256)], "email" => [Validators::regex("/^\\S+@\\S+\\.\\S+$/"),Validators::lenBetween(5, 256)] ]); - + if (!empty($fails)) { + return $this->displayBadFields("display_login.html.twig",$fails); + } $fails = $this->model->login($request['email'],$request['password']); - if (empty($fails)){ $results = $this->model->getUserFields($request['email']); return ViewHttpResponse::twig("display_auth_confirm.html.twig",['username' => $results['username'], 'email' => $results['email']]); diff --git a/src/Views/display_error_validation_register.html.twig b/src/Views/display_error_validation_register.html.twig deleted file mode 100644 index 95befc0..0000000 --- a/src/Views/display_error_validation_register.html.twig +++ /dev/null @@ -1,13 +0,0 @@ - - - - Compte - - - -

ERROR REGISTER

- - - - - \ No newline at end of file -- 2.36.3 From df929e7458a92b925d8e4b4db4f14576dfe147d2 Mon Sep 17 00:00:00 2001 From: samuel Date: Tue, 21 Nov 2023 10:50:20 +0100 Subject: [PATCH 18/19] conception --- Documentation/models.puml | 24 ++++++++++++++---------- src/Gateway/AuthGateway.php | 2 +- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/Documentation/models.puml b/Documentation/models.puml index 48973d4..1a28172 100755 --- a/Documentation/models.puml +++ b/Documentation/models.puml @@ -69,25 +69,29 @@ class Color { } class AuthController{ - -twig: Environment - + login (request) : int - + register (request) : int + + displayRegister() : HttpResponse + + displayBadFields(viewName : string, fails : array) : HttpResponse + + confirmRegister(request : array) : HttpResponse + + displayLogin() : HttpResponse + + confirmLogin() : HttpResponse } -AuthController --> "- modelAuth" AuthModel +AuthController --> "- model" AuthModel class AuthModel{ - + validationRegister(username : string, email : string, password : string) - + validationLogin (username : string, email : string, password : string) + + + register(username : string, password : string, confirmPassword : string, email : string): array + + getUserFields(email : string):array + + login(email : string, password : string) } AuthModel --> "- gateway" AuthGateway class AuthGateway{ -con : Connection - + insert(mail : string, password : string) - + isAccountEqual(mail : string, password : string) + + mailExist(email : string) : bool + + insertAccount(username : string, hash : string, email : string) + + getUserHash(email : string):string + + getUserFields (email : string): array } - - @enduml \ No newline at end of file diff --git a/src/Gateway/AuthGateway.php b/src/Gateway/AuthGateway.php index c04e5e3..e6c7199 100644 --- a/src/Gateway/AuthGateway.php +++ b/src/Gateway/AuthGateway.php @@ -21,7 +21,7 @@ class AuthGateway { } - public function insertAccount(string $username, string $hash, string $email) { + public function insertAccount(string $username, string $hash, string $email):void{ $this->con->exec("INSERT INTO AccountUser VALUES (:username,:hash,:email)", [':username' => [$username, PDO::PARAM_STR],':hash'=> [$hash, PDO::PARAM_STR],':email'=>[$email, PDO::PARAM_STR]]); } -- 2.36.3 From c1fec9243b0057d90547e0874116230a809fe14f Mon Sep 17 00:00:00 2001 From: samuel Date: Tue, 21 Nov 2023 11:12:11 +0100 Subject: [PATCH 19/19] format and fix phpstan --- front/views/Visualizer.tsx | 7 +++-- src/Controller/AuthController.php | 43 +++++++++++++++++++------------ src/Gateway/AuthGateway.php | 22 +++++++++------- src/Model/AuthModel.php | 39 +++++++++++++++++++--------- 4 files changed, 70 insertions(+), 41 deletions(-) diff --git a/front/views/Visualizer.tsx b/front/views/Visualizer.tsx index ddf7fe2..541da09 100644 --- a/front/views/Visualizer.tsx +++ b/front/views/Visualizer.tsx @@ -2,9 +2,8 @@ import React, { CSSProperties, useState } from "react" import "../style/visualizer.css" import Court from "../assets/basketball_court.svg" - -export default function Visualizer({id, name}: { id: number; name: string }) { - const [style, setStyle] = useState({}); +export default function Visualizer({ id, name }: { id: number; name: string }) { + const [style, setStyle] = useState({}) return (
@@ -20,5 +19,5 @@ export default function Visualizer({id, name}: { id: number; name: string }) { />
- ); + ) } diff --git a/src/Controller/AuthController.php b/src/Controller/AuthController.php index b4ef8cb..e42c27d 100644 --- a/src/Controller/AuthController.php +++ b/src/Controller/AuthController.php @@ -8,10 +8,10 @@ use App\Http\HttpResponse; use App\Http\ViewHttpResponse; use App\Model\AuthModel; use App\Validation\FieldValidationFail; +use App\Validation\ValidationFail; use App\Validation\Validators; use Twig\Environment; - class AuthController { private AuthModel $model; @@ -26,7 +26,12 @@ class AuthController { return ViewHttpResponse::twig("display_register.html.twig", []); } - private function displayBadFields(string $viewName, array $fails): HttpResponse{ + /** + * @param string $viewName + * @param ValidationFail[] $fails + * @return HttpResponse + */ + private function displayBadFields(string $viewName, array $fails): HttpResponse { $bad_fields = []; foreach ($fails as $err) { if ($err instanceof FieldValidationFail) { @@ -36,48 +41,54 @@ class AuthController { return ViewHttpResponse::twig($viewName, ['bad_fields' => $bad_fields]); } - + /** + * @param mixed[] $request + * @return HttpResponse + */ public function confirmRegister(array $request): HttpResponse { $fails = []; $request = HttpRequest::from($request, $fails, [ "username" => [Validators::name(), Validators::lenBetween(2, 32)], "password" => [Validators::lenBetween(6, 256)], "confirmpassword" => [Validators::lenBetween(6, 256)], - "email" => [Validators::regex("/^\\S+@\\S+\\.\\S+$/"),Validators::lenBetween(5, 256)] + "email" => [Validators::regex("/^\\S+@\\S+\\.\\S+$/"),Validators::lenBetween(5, 256)], ]); if (!empty($fails)) { - return $this->displayBadFields("display_register.html.twig",$fails); + return $this->displayBadFields("display_register.html.twig", $fails); } $fails = $this->model->register($request['username'], $request["password"], $request['confirmpassword'], $request['email']); if (empty($fails)) { $results = $this->model->getUserFields($request['email']); return ViewHttpResponse::twig("display_auth_confirm.html.twig", ['username' => $results['username'], 'email' => $results['email']]); } - return $this->displayBadFields("display_register.html.twig",$fails); + return $this->displayBadFields("display_register.html.twig", $fails); } - public function displayLogin():HttpResponse{ + public function displayLogin(): HttpResponse { return ViewHttpResponse::twig("display_login.html.twig", []); } - - public function confirmLogin(array $request):HttpResponse{ + /** + * @param mixed[] $request + * @return HttpResponse + */ + public function confirmLogin(array $request): HttpResponse { $fails = []; $request = HttpRequest::from($request, $fails, [ "password" => [Validators::lenBetween(6, 256)], - "email" => [Validators::regex("/^\\S+@\\S+\\.\\S+$/"),Validators::lenBetween(5, 256)] + "email" => [Validators::regex("/^\\S+@\\S+\\.\\S+$/"),Validators::lenBetween(5, 256)], ]); if (!empty($fails)) { - return $this->displayBadFields("display_login.html.twig",$fails); + return $this->displayBadFields("display_login.html.twig", $fails); } - $fails = $this->model->login($request['email'],$request['password']); - if (empty($fails)){ + $fails = $this->model->login($request['email'], $request['password']); + if (empty($fails)) { $results = $this->model->getUserFields($request['email']); - return ViewHttpResponse::twig("display_auth_confirm.html.twig",['username' => $results['username'], 'email' => $results['email']]); + return ViewHttpResponse::twig("display_auth_confirm.html.twig", ['username' => $results['username'], 'email' => $results['email']]); } - return $this->displayBadFields("display_login.html.twig",$fails); + return $this->displayBadFields("display_login.html.twig", $fails); } -} \ No newline at end of file +} diff --git a/src/Gateway/AuthGateway.php b/src/Gateway/AuthGateway.php index e6c7199..5acc01c 100644 --- a/src/Gateway/AuthGateway.php +++ b/src/Gateway/AuthGateway.php @@ -3,7 +3,7 @@ namespace App\Gateway; use App\Connexion; -use \PDO; +use PDO; class AuthGateway { private Connexion $con; @@ -16,23 +16,27 @@ class AuthGateway { } - public function mailExist(string $email):bool{ + public function mailExist(string $email): bool { return $this->getUserFields($email) != null; } - public function insertAccount(string $username, string $hash, string $email):void{ - $this->con->exec("INSERT INTO AccountUser VALUES (:username,:hash,:email)", [':username' => [$username, PDO::PARAM_STR],':hash'=> [$hash, PDO::PARAM_STR],':email'=>[$email, PDO::PARAM_STR]]); + public function insertAccount(string $username, string $hash, string $email): void { + $this->con->exec("INSERT INTO AccountUser VALUES (:username,:hash,:email)", [':username' => [$username, PDO::PARAM_STR],':hash' => [$hash, PDO::PARAM_STR],':email' => [$email, PDO::PARAM_STR]]); } - public function getUserHash(string $email):string{ - $results = $this->con->fetch ("SELECT hash FROM AccountUser WHERE email = :email",[':email'=>[$email, PDO::PARAM_STR]]); + public function getUserHash(string $email): string { + $results = $this->con->fetch("SELECT hash FROM AccountUser WHERE email = :email", [':email' => [$email, PDO::PARAM_STR]]); return $results[0]['hash']; } - public function getUserFields (string $email): ?array { - $results = $this->con->fetch ("SELECT username,email FROM AccountUser WHERE email = :email",[':email'=>[$email, PDO::PARAM_STR]]); + /** + * @param string $email + * @return array|null + */ + public function getUserFields(string $email): ?array { + $results = $this->con->fetch("SELECT username,email FROM AccountUser WHERE email = :email", [':email' => [$email, PDO::PARAM_STR]]); $firstRow = $results[0] ?? null; return $firstRow; } @@ -40,4 +44,4 @@ class AuthGateway { -} \ No newline at end of file +} diff --git a/src/Model/AuthModel.php b/src/Model/AuthModel.php index fd00bb8..45b63e4 100644 --- a/src/Model/AuthModel.php +++ b/src/Model/AuthModel.php @@ -5,10 +5,9 @@ namespace App\Model; use App\Controller\AuthController; use App\Gateway\AuthGateway; use App\Validation\FieldValidationFail; +use App\Validation\ValidationFail; class AuthModel { - - private AuthGateway $gateway; /** * @param AuthGateway $gateway @@ -18,31 +17,47 @@ class AuthModel { } - public function register(string $username, string $password, string $confirmPassword,string $email): array { + /** + * @param string $username + * @param string $password + * @param string $confirmPassword + * @param string $email + * @return ValidationFail[] + */ + public function register(string $username, string $password, string $confirmPassword, string $email): array { $errors = []; if ($password != $confirmPassword) { - $errors[] = new FieldValidationFail("confirmpassword","password and password confirmation are not equals"); + $errors[] = new FieldValidationFail("confirmpassword", "password and password confirmation are not equals"); } - if ($this->gateway->mailExist($email)){ - $errors[] = new FieldValidationFail("email","email already exist"); + if ($this->gateway->mailExist($email)) { + $errors[] = new FieldValidationFail("email", "email already exist"); } - if(empty($errors)){ - $hash = password_hash($password,PASSWORD_DEFAULT); - $this->gateway->insertAccount($username,$hash,$email); + if(empty($errors)) { + $hash = password_hash($password, PASSWORD_DEFAULT); + $this->gateway->insertAccount($username, $hash, $email); } return $errors; } - public function getUserFields(string $email):array{ + /** + * @param string $email + * @return array|null + */ + public function getUserFields(string $email): ?array { return $this->gateway->getUserFields($email); } - public function login(string $email,string $password): array{ + /** + * @param string $email + * @param string $password + * @return ValidationFail[] $errors + */ + public function login(string $email, string $password): array { $errors = []; if (!$this->gateway->mailExist($email)) { @@ -62,4 +77,4 @@ class AuthModel { -} \ No newline at end of file +} -- 2.36.3