diff --git a/project/src/controller/UserController.php b/project/src/controller/UserController.php
index 2cd9e34..f1884af 100755
--- a/project/src/controller/UserController.php
+++ b/project/src/controller/UserController.php
@@ -9,6 +9,7 @@ use model\Connection;
use model\GameGateway;
use model\MdlDifficulte;
use model\MdlJeu;
+use model\PseudoDejaPrisException;
use model\ValidationException;
use model\MdlUser;
use model\MdlAdmin;
@@ -95,6 +96,35 @@ class UserController {
}
}
+ public function register() {
+ global $twig, $dVueErreur;
+ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
+ if($_REQUEST['password']!=$_REQUEST['cpassword']){
+ $dVueErreur[]="Mots de passe différents.";
+ echo $twig->render('erreur.html',["dVueErreur" => $dVueErreur]);
+ return;
+ }
+ $ug = new MdlUser();
+ try{
+ if($ug->register($_REQUEST['login'], $_REQUEST['password'])){
+ header('Location: login');
+ } else {
+ $dVueErreur[]="Erreur de création de compte. Le compte doit déjà exister.";
+ echo $twig->render('erreur.html',["dVueErreur" => $dVueErreur]);
+ }
+ } catch (PseudoDejaPrisException $ex){
+ $dVueErreur[]="Erreur de création de compte. Le compte existe déjà.";
+ echo $twig->render('erreur.html',["dVueErreur" => $dVueErreur]);
+ }catch(Exception $ex){
+ $dVueErreur[]="Erreur de création de compte.";
+ echo $twig->render('erreur.html',["dVueErreur" => $dVueErreur]);
+ }
+
+ } else {
+ echo $twig->render('register.html');
+ }
+ }
+
public function logout(){
$_SESSION=[];
header("Location: .");
diff --git a/project/src/model/gateways/JoueurGateway.php b/project/src/model/gateways/JoueurGateway.php
index b01d83b..c8c25c4 100755
--- a/project/src/model/gateways/JoueurGateway.php
+++ b/project/src/model/gateways/JoueurGateway.php
@@ -2,7 +2,7 @@
namespace model;
-abstract class JoueurGateway
+class JoueurGateway
{
protected Connection $con;
@@ -21,7 +21,7 @@ abstract class JoueurGateway
* @param string $pseudo
* @return array|bool
*/
- protected function getFromPseudo(string $pseudo) { // <- PHP 7.4
+ public function getFromPseudo(string $pseudo) { // <- PHP 7.4
$this->con->executeQuery(
"SELECT id, pseudo FROM Joueur WHERE pseudo = :pseudo;",
[":pseudo" => [$pseudo, $this->con::PARAM_STR]]
@@ -29,7 +29,7 @@ abstract class JoueurGateway
return $this->con->getOneResult();
}
- protected function insertJoueur(string $pseudo): int{
+ public function insertJoueur(string $pseudo): int{
if($this->getFromPseudo($pseudo)){
throw new PseudoDejaPrisException();
}else{
diff --git a/project/src/model/gateways/UtilisateurConnecteGateway.php b/project/src/model/gateways/UtilisateurConnecteGateway.php
index 066483e..b70a1d8 100644
--- a/project/src/model/gateways/UtilisateurConnecteGateway.php
+++ b/project/src/model/gateways/UtilisateurConnecteGateway.php
@@ -22,4 +22,14 @@ class UtilisateurConnecteGateway extends JoueurGateway {
}
return false;
}
+ public function register(string $email, string $password, int $idjoueur): bool
+ {
+ $sql = "INSERT INTO Utilisateur(email, password,idjoueur) VALUES (:email,:password,:idjoueur);";
+ return $this->con->executeQuery($sql, [
+ ':email' => array($email, \PDO::PARAM_STR),
+ ':password' => array(password_hash($password, PASSWORD_BCRYPT), \PDO::PARAM_STR),
+ ':idjoueur' => array($idjoueur, \PDO::PARAM_INT)
+ ]
+ );
+ }
}
\ No newline at end of file
diff --git a/project/src/model/mdl/MdlUser.php b/project/src/model/mdl/MdlUser.php
index 1638684..68a23c1 100755
--- a/project/src/model/mdl/MdlUser.php
+++ b/project/src/model/mdl/MdlUser.php
@@ -12,6 +12,11 @@ class MdlUser extends MdlBase{
public function login(string $username, string $password): bool{
return $this->gw->login($username, $password);
}
+ public function register(string $username, string $password): bool{
+ $temp = new JoueurGateway($this->con);
+ $temp->insertJoueur($username);
+ return $this->gw->register($username, $password, $temp->getFromPseudo($username)["id"]);
+ }
/*
public function setPseudo(int $id, string $pseudo): User{
$this->gw->setPseudo($id, $pseudo);
diff --git a/project/src/templates/login.html b/project/src/templates/login.html
index c52d4f0..29f1fc7 100755
--- a/project/src/templates/login.html
+++ b/project/src/templates/login.html
@@ -13,12 +13,11 @@
-
-
-
+
+ Creer un compte
{% for error in dErreur %}
diff --git a/project/src/templates/register.html b/project/src/templates/register.html new file mode 100755 index 0000000..384ab00 --- /dev/null +++ b/project/src/templates/register.html @@ -0,0 +1,45 @@ + + +
+ +