register ok, just do email uniq for DB
continuous-integration/drone/push Build is passing Details

pull/12/head
samuel 1 year ago
parent 961d0d5e26
commit b967e9615f

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

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

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

@ -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]]);
}

@ -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);
}
}

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

@ -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 %}
</style>
@ -76,9 +71,9 @@
<label for="username">Nom d'utilisateur :</label>
<input type="text" id="username" name="username" required>
<label for= "password">Mot de passe :</label>
<input type="text" id="password" name="password" required>
<input type="password" id="password" name="password" required>
<label for="confirmpassword">Confirmer le mot de passe :</label>
<input type="text" id="confirmpassword" name="confirmpassword" required>
<input type="password" id="confirmpassword" name="confirmpassword" required>
<label for="email">Email :</label>
<input type="text" id="email" name="email" required>

@ -6,9 +6,12 @@
</head>
<body>
<center><h2>Nouveau Compte</h2></center>
<center><h1>Nouveau Compte</h1></center>
{% for row in results %}
<h2>Votre pseudo : {{ row['username']}}</h2>
<h2>Votre Email : {{ row['email']}}</h2>
{% endfor %}
</body>
</html>
Loading…
Cancel
Save