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 @@
-