Mise en place et vérification de la connection et de l'inscription

ServeurDeTest
Noé GARNIER 2 years ago
parent 3f9f52f9b0
commit ee688b5366

@ -0,0 +1,15 @@
<?php
//préfixe
$rep='../';
//BD
$dsn = 'mysql:host=londres.uca.local; dbname=dbnogarnier1';
$user = 'nogarnier1';
$password = 'achanger';
//View
$vues['erreurSignUp']='View/Error/ErreurSignUp.php';
$vues['erreur']='View/Error/Erreur.php';
$vues['erreurBd']='View/Error/ErreurBd.php';
$vues['erreurLoginEmail']='View/Error/ErreurLoginEmail.php';
$vues['erreurLoginMdp']='View/Error/ErreurLoginMdp.php';

@ -1,6 +1,9 @@
<?php <?php
require 'JoueurGateway.php'; require 'JoueurGateway.php';
require '../Config/Validation.php';
require './JoueurNotFoundException.php';
require './InvalidMdpException.php';
class Controller class Controller
{ {
@ -13,33 +16,59 @@ class Controller
$this->con=$con; $this->con=$con;
session_start(); session_start();
try{ try{
global $rep, $vues;
$action=$_REQUEST['action']; $action=$_REQUEST['action'];
switch($action) { switch($action) {
case NULL: case NULL:
//require ('../View/src/pages/Main.html'); //require ('../View/src/pages/Main.html');
header('Location: http://londres.uca.local/~nogarnier1/Scripted/WEB/View/src/pages/Main.html'); header('Location: http://londres.uca.local/~nogarnier1/Scripted/WEB/View/src/pages/Main.html');
break; break;
case "signUp":
$this->signUp();
break;
case "login": case "login":
$this->login(); $this->login();
break; break;
} }
} catch (PDOException $e) } catch (PDOException $e)
{ {
//si erreur BD, pas le cas ici require ($rep.$vues['erreurBd']);
$dataVueEreur[] = "Erreur inattendue!!! ";
//require(__DIR__.'/../vues/erreur.php'); // ajout du code de la vue ici
}
catch (Exception $e2)
{
$dataVueEreur[] = "Erreur inattendue!!! ";
//require ($rep.$vues['erreur']);
} }
} }
private function login() { private function signUp() {
$gateway=new JoueurGateway($this->con); global $rep, $vues;
$joueur=new Joueur($_REQUEST['email'], $_REQUEST['username'], $_REQUEST['password']); try {
$gateway = new JoueurGateway($this->con);
$validation = new Validation();
if (! $validation->ValidateEmail($_REQUEST['email'])) {
throw (new Exception("Email non valide"));
}
$joueur = new Joueur($_REQUEST['email'], $_REQUEST['username'], $_REQUEST['password']);
$gateway->insert($joueur); $gateway->insert($joueur);
$gateway->showAll(); //$gateway->showAll();
header('Location: http://londres.uca.local/~nogarnier1/Scripted/WEB/View/src/pages/Main.html');
}catch (Exception $e){
require($rep.$vues['erreurSignUp']);
}
}
private function login(){
global $rep, $vues;
try {
$gateway = new JoueurGateway($this->con);
$joueur = $gateway->getJoueurByEmail($_REQUEST['email']);
if ($joueur->getEmail() == null){
throw new JoueurNotFoundException("Joueur introuvable");
}
$mdp = $gateway->getMdpByEmail($_REQUEST['email']);
if ($mdp != $_REQUEST['password']){
throw new InvalidMdpException("Mot de passe invalide");
}
header('Location: http://londres.uca.local/~nogarnier1/Scripted/WEB/View/src/pages/Main.html');
}catch (JoueurNotFoundException $e){
require($rep.$vues['erreurLoginEmail']);
}catch (InvalidMdpException $m) {
require($rep . $vues['erreurLoginMdp']);
}
} }
} }

@ -0,0 +1,10 @@
<?php
class InvalidMdpException extends Exception {
public function errorMessage() {
//error message
$errorMsg = 'Error on line '.$this->getLine().' in '.$this->getFile()
.': <b>'.$this->getMessage().'</b> Mdp invalide';
return $errorMsg;
}
}

@ -22,12 +22,12 @@ class JoueurGateway
$this->con = $con; $this->con = $con;
} }
public function insert(string $email,string $pseudo,string $mdp) : void{ public function insert(Joueur $joueur) : void{
$query = "INSERT INTO Joueur VALUE (:email,:pseudo,:mdp)"; $query = "INSERT INTO Joueur VALUE (:email,:pseudo,:mdp)";
$this->con->executeQuery($query, array( $this->con->executeQuery($query, array(
':email' => array($email,PDO::PARAM_STR), ':email' => array($joueur->getEmail(),PDO::PARAM_STR),
':pseudo' => array($pseudo,PDO::PARAM_STR), ':pseudo' => array($joueur->getPseudo(),PDO::PARAM_STR),
':mdp' => array($mdp,PDO::PARAM_STR))); ':mdp' => array($joueur->getMdp(),PDO::PARAM_STR)));
} }
public function delete(string $email) : void{ public function delete(string $email) : void{
@ -37,6 +37,38 @@ class JoueurGateway
)); ));
} }
public function getJoueurByEmail(string $email) : Joueur{
$query = "SELECT * FROM Joueur WHERE email=:email";
$this->con->executeQuery($query, array(
':email' => array($email,PDO::PARAM_STR)
));
$results=$this->con->getResults();
foreach ($results as $row) {
$email=$row['email'];
$pseudo=$row['pseudo'];
$mdp=$row['mdp'];
}
if ($results == null){
throw new JoueurNotFoundException("Joueur Introuvable");
}
return new Joueur($email, $pseudo, $mdp);
}
public function getMdpByEmail(string $email) : string{
$query = "SELECT mdp FROM Joueur WHERE email=:email";
$this->con->executeQuery($query, array(
':email' => array($email,PDO::PARAM_STR)
));
$results=$this->con->getResults();
foreach ($results as $row) {
$mdp=$row['mdp'];
}
if ($results == null){
throw new InvalidMdpException("Mots de passe Incorrect");
}
return $mdp;
}
public function showAll() : void{ public function showAll() : void{
$query = "SELECT * FROM Joueur"; $query = "SELECT * FROM Joueur";
$this->con->executeQuery($query); $this->con->executeQuery($query);

@ -0,0 +1,10 @@
<?php
class JoueurNotFoundException extends Exception {
public function errorMessage() {
//error message
$errorMsg = 'Error on line '.$this->getLine().' in '.$this->getFile()
.': <b>'.$this->getMessage().'</b> Joueur not found';
return $errorMsg;
}
}

@ -1,12 +1,6 @@
<html> <?php
<body> require_once 'Controller.php';
<?php require_once '../Config/Config.php';
require 'Controller.php';
$dsn = 'mysql:host=londres.uca.local; dbname=dbnogarnier1'; $con = new Connection($dsn, $user, $password);
$user = 'nogarnier1'; $control = new Controller($con);
$password = 'achanger';
$con = new Connection($dsn, $user, $password);
$control = new Controller($con)
?>
</body>
</html>

@ -0,0 +1,11 @@
<html>
<body>
<h1>ERREUR</h1>
<p>Erreur inatendu</p>
<?php
?>
</body>
</html>
<?php

@ -0,0 +1,9 @@
<html>
<body>
<h1>ERREUR</h1>
<p>Erreur avec la base de donnée</p>
<?php
?>
</body>
</html>

@ -0,0 +1,10 @@
<html>
<body>
<h1>ERREUR</h1>
<p>Joueur introuvable</p>
<?php
?>
</body>
</html>

@ -0,0 +1,10 @@
<html>
<body>
<h1>ERREUR</h1>
<p>Mot de passe invalide</p>
<?php
?>
</body>
</html>

@ -0,0 +1,10 @@
<html>
<body>
<h1>ERREUR</h1>
<p>E-mail invalide</p>
<?php
?>
</body>
</html>

@ -15,10 +15,10 @@
<div class="top"> <div class="top">
<h2>Sign up</h2> <h2>Sign up</h2>
</div> </div>
<form action="" method="POST"> <form action="../../../../Controller/Traitement.php?action=login" method="POST">
<div class="user-box"> <div class="user-box">
<input type="text" id="username" name="username" required=""> <input type="text" id="email" name="email" required="">
<label>Username</label> <label>Email</label>
</div> </div>
<div class="user-box"> <div class="user-box">
<input type="password" id="password" name="password" required=""> <input type="password" id="password" name="password" required="">

@ -15,7 +15,7 @@
<div class="top"> <div class="top">
<h2>Sign up</h2> <h2>Sign up</h2>
</div> </div>
<form action="../../../../Controller/Traitement.php?action=login" method="POST"> <form action="../../../../Controller/Traitement.php?action=signUp" method="POST">
<div class="user-box"> <div class="user-box">
<input type="email" id="email" name="email" required=""> <input type="email" id="email" name="email" required="">
<label>Email</label> <label>Email</label>
@ -30,19 +30,12 @@
</div> </div>
<div> <div>
<button class="left" type="submit"> <button class="left" type="submit">
<span></span>
<span></span>
<span></span>
<span></span>
Submit
</button>
<a href="SignUp.php" class="right">
<span></span> <span></span>
<span></span> <span></span>
<span></span> <span></span>
<span></span> <span></span>
Sign up Sign up
</a> </button>
</div> </div>
</form> </form>
</div> </div>

Loading…
Cancel
Save