Register with email confirmation - to test w/ Theo

This commit is contained in:
Francois G 2023-09-05 11:44:24 +02:00
parent 5161916f54
commit bff438073a
4 changed files with 58 additions and 51 deletions

View file

@ -22,12 +22,12 @@ public class Player implements Comparable<Player> {
private Set<Badge> badges;
public Player(String pseudo, String email, String firstname, String lastname, String description) {
public Player(String pseudo, String email, String firstname, String lastname) {
this.pseudo = pseudo;
this.email = email;
this.firstname = firstname;
this.lastname = lastname;
this.description = description;
this.description = "";
}
public Player(String pseudo, int score, int tries) {
@ -37,6 +37,11 @@ public class Player implements Comparable<Player> {
email = ""; // TO make compareTo and equals works as usual
}
public Player(String email) {
// For player find in Map during register process
this.email = email;
}
public String getPseudo() {
return this.pseudo;
}

View file

@ -129,8 +129,7 @@ public class DatabaseRepository {
private Player makePlayer(ResultSet playerResult, int id) throws SQLException {
Player p = new Player(playerResult.getString("pseudo"), playerResult.getString("email"),
playerResult.getString("firstName"), playerResult.getString("lastName"),
playerResult.getString("description"));
playerResult.getString("firstName"), playerResult.getString("lastName"));
if (hasColumn(playerResult, "avatar")) {
p.setAvatar(playerResult.getBytes("avatar"));
}

View file

@ -26,10 +26,10 @@ public class MailConfirmation implements Response {
private DatabaseRepository databaseRepo;
private Router<PeerAtUser> router;
private String usersFilesPath;
private Map<Integer, Player> playersWaiting;
private Map<Player, Integer> playersWaiting;
public MailConfirmation(DatabaseRepository databaseRepo, Router<PeerAtUser> router, String initUsersFilesPath,
Map<Integer, Player> playersWaiting) {
Map<Player, Integer> playersWaiting) {
this.databaseRepo = databaseRepo;
this.router = router;
usersFilesPath = initUsersFilesPath;
@ -46,20 +46,22 @@ public class MailConfirmation implements Response {
return;
}
JSONObject informations = reader.readJson();
if (informations != null) {
boolean allNecessaryFieldsFilled = informations.containsKey("email") && informations.containsKey("code")
&& informations.containsKey("passwd");
if (!allNecessaryFieldsFilled) {
context.response(400);
return;
}
String email = (String) informations.get("email");
String password = (String) informations.get("passwd");
int code = (int) informations.get("code");
if (checkIfValided(code, email)) {
String pseudo = playersWaiting.get(code).getPseudo();
String firstname = playersWaiting.get(code).getLastname();
String lastname = playersWaiting.get(code).getLastname();
String description = playersWaiting.get(code).getDescription();
String group = null;
String avatar = null;
Player newPlayer = getPlayerFromEmail(email);
if (newPlayer != null && code == playersWaiting.get(newPlayer)) {
String pseudo = newPlayer.getPseudo();
int id;
if ((id = databaseRepo.register(pseudo, email, password, firstname, lastname, description, group,
avatar)) >= 0) {
if ((id = databaseRepo.register(pseudo, email, password, newPlayer.getFirstname(), newPlayer.getLastname(), "", "", "")) >= 0) {
context.response(200, "Access-Control-Expose-Headers: Authorization",
"Authorization: Bearer " + this.router.createAuthUser(new PeerAtUser(id)));
createFolderToSaveSourceCode(pseudo);
@ -73,7 +75,8 @@ public class MailConfirmation implements Response {
return;
}
}
}
context.response(400);
}
private void createFolderToSaveSourceCode(String pseudo) throws IOException {
@ -81,8 +84,13 @@ public class MailConfirmation implements Response {
Files.createDirectories(Paths.get(String.format("%s/%s", usersFilesPath, pseudo)));
}
private boolean checkIfValided(int code, String email) {
return playersWaiting.get(code).getEmail().equals(email);
private Player getPlayerFromEmail(String email) {
Player toMatch = new Player(email);
for (Player p: playersWaiting.keySet()) {
if (p.equals(toMatch)) {
return p;
}
}
return null;
}
}

View file

@ -28,10 +28,10 @@ public class Register implements Response {
private DatabaseRepository databaseRepo;
private Router<PeerAtUser> router;
private String usersFilesPath;
private Map<Integer, Player> playersWaiting;
private Map<Player, Integer> playersWaiting;
public Register(DatabaseRepository databaseRepo, Router<PeerAtUser> router, String initUsersFilesPath,
Map<Integer, Player> playersWaiting) {
Map<Player, Integer> playersWaiting) {
this.databaseRepo = databaseRepo;
this.router = router;
usersFilesPath = initUsersFilesPath;
@ -49,28 +49,22 @@ public class Register implements Response {
}
JSONObject informations = reader.readJson();
if (informations != null) {
boolean allFieldsFilled = informations.containsKey("pseudo") && informations.containsKey("email")
&& informations.containsKey("passwd") && informations.containsKey("firstname")
&& informations.containsKey("lastname") && informations.containsKey("description")
&& informations.containsKey("sgroup") && informations.containsKey("avatar");
if (!allFieldsFilled) {
context.response(403);
boolean allNecessaryFieldsFilled = informations.containsKey("pseudo") && informations.containsKey("email")
&& informations.containsKey("firstname") && informations.containsKey("lastname");
if (!allNecessaryFieldsFilled) {
context.response(400);
return;
}
String pseudo = (String) informations.get("pseudo");
String email = (String) informations.get("email");
String password = (String) informations.get("passwd");
String firstname = (String) informations.get("firstname");
String lastname = (String) informations.get("lastname");
String description = (String) informations.get("description");
String group = (String) informations.get("sgroup");
String avatar = (String) informations.get("avatar");
boolean pseudoAvailable = databaseRepo.checkPseudoAvailability(pseudo);
boolean emailAvailable = databaseRepo.checkEmailAvailability(email);
if (pseudoAvailable && emailAvailable) {
Player player = new Player(pseudo, email, firstname, lastname, description);
playersWaiting.put(codeGenerator(), player);
Player player = new Player(pseudo, email, firstname, lastname);
playersWaiting.put(player, codeGenerator());
context.response(200);
} else {
context.response(400);
@ -86,8 +80,9 @@ public class Register implements Response {
}
private int codeGenerator() {
Random rand = new Random();
return rand.nextInt(1000);
int min = 1000;
int max = 9999;
return new Random().nextInt((max-min)) + min;
}