Register with email confirmation - to test w/ Theo
This commit is contained in:
parent
5161916f54
commit
bff438073a
4 changed files with 58 additions and 51 deletions
|
@ -22,12 +22,12 @@ public class Player implements Comparable<Player> {
|
||||||
|
|
||||||
private Set<Badge> badges;
|
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.pseudo = pseudo;
|
||||||
this.email = email;
|
this.email = email;
|
||||||
this.firstname = firstname;
|
this.firstname = firstname;
|
||||||
this.lastname = lastname;
|
this.lastname = lastname;
|
||||||
this.description = description;
|
this.description = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
public Player(String pseudo, int score, int tries) {
|
public Player(String pseudo, int score, int tries) {
|
||||||
|
@ -36,6 +36,11 @@ public class Player implements Comparable<Player> {
|
||||||
this.completions.add(new Completion(tries, score));
|
this.completions.add(new Completion(tries, score));
|
||||||
email = ""; // TO make compareTo and equals works as usual
|
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() {
|
public String getPseudo() {
|
||||||
return this.pseudo;
|
return this.pseudo;
|
||||||
|
|
|
@ -129,8 +129,7 @@ public class DatabaseRepository {
|
||||||
|
|
||||||
private Player makePlayer(ResultSet playerResult, int id) throws SQLException {
|
private Player makePlayer(ResultSet playerResult, int id) throws SQLException {
|
||||||
Player p = new Player(playerResult.getString("pseudo"), playerResult.getString("email"),
|
Player p = new Player(playerResult.getString("pseudo"), playerResult.getString("email"),
|
||||||
playerResult.getString("firstName"), playerResult.getString("lastName"),
|
playerResult.getString("firstName"), playerResult.getString("lastName"));
|
||||||
playerResult.getString("description"));
|
|
||||||
if (hasColumn(playerResult, "avatar")) {
|
if (hasColumn(playerResult, "avatar")) {
|
||||||
p.setAvatar(playerResult.getBytes("avatar"));
|
p.setAvatar(playerResult.getBytes("avatar"));
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,10 +26,10 @@ public class MailConfirmation implements Response {
|
||||||
private DatabaseRepository databaseRepo;
|
private DatabaseRepository databaseRepo;
|
||||||
private Router<PeerAtUser> router;
|
private Router<PeerAtUser> router;
|
||||||
private String usersFilesPath;
|
private String usersFilesPath;
|
||||||
private Map<Integer, Player> playersWaiting;
|
private Map<Player, Integer> playersWaiting;
|
||||||
|
|
||||||
public MailConfirmation(DatabaseRepository databaseRepo, Router<PeerAtUser> router, String initUsersFilesPath,
|
public MailConfirmation(DatabaseRepository databaseRepo, Router<PeerAtUser> router, String initUsersFilesPath,
|
||||||
Map<Integer, Player> playersWaiting) {
|
Map<Player, Integer> playersWaiting) {
|
||||||
this.databaseRepo = databaseRepo;
|
this.databaseRepo = databaseRepo;
|
||||||
this.router = router;
|
this.router = router;
|
||||||
usersFilesPath = initUsersFilesPath;
|
usersFilesPath = initUsersFilesPath;
|
||||||
|
@ -46,43 +46,51 @@ public class MailConfirmation implements Response {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
JSONObject informations = reader.readJson();
|
JSONObject informations = reader.readJson();
|
||||||
String email = (String) informations.get("email");
|
if (informations != null) {
|
||||||
String password = (String) informations.get("passwd");
|
boolean allNecessaryFieldsFilled = informations.containsKey("email") && informations.containsKey("code")
|
||||||
int code = (int) informations.get("code");
|
&& informations.containsKey("passwd");
|
||||||
|
if (!allNecessaryFieldsFilled) {
|
||||||
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;
|
|
||||||
int id;
|
|
||||||
if ((id = databaseRepo.register(pseudo, email, password, firstname, lastname, description, group,
|
|
||||||
avatar)) >= 0) {
|
|
||||||
context.response(200, "Access-Control-Expose-Headers: Authorization",
|
|
||||||
"Authorization: Bearer " + this.router.createAuthUser(new PeerAtUser(id)));
|
|
||||||
createFolderToSaveSourceCode(pseudo);
|
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
context.response(400);
|
context.response(400);
|
||||||
JSONObject error = new JSONObject();
|
|
||||||
error.put("username_valid", pseudo);
|
|
||||||
error.put("email_valid", email);
|
|
||||||
writer.write(error.toJSONString());
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
String email = (String) informations.get("email");
|
||||||
|
String password = (String) informations.get("passwd");
|
||||||
|
int code = (int) informations.get("code");
|
||||||
|
|
||||||
|
Player newPlayer = getPlayerFromEmail(email);
|
||||||
|
if (newPlayer != null && code == playersWaiting.get(newPlayer)) {
|
||||||
|
String pseudo = newPlayer.getPseudo();
|
||||||
|
int id;
|
||||||
|
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);
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
context.response(400);
|
||||||
|
JSONObject error = new JSONObject();
|
||||||
|
error.put("username_valid", pseudo);
|
||||||
|
error.put("email_valid", email);
|
||||||
|
writer.write(error.toJSONString());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
context.response(400);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createFolderToSaveSourceCode(String pseudo) throws IOException {
|
private void createFolderToSaveSourceCode(String pseudo) throws IOException {
|
||||||
|
|
||||||
Files.createDirectories(Paths.get(String.format("%s/%s", usersFilesPath, pseudo)));
|
Files.createDirectories(Paths.get(String.format("%s/%s", usersFilesPath, pseudo)));
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean checkIfValided(int code, String email) {
|
private Player getPlayerFromEmail(String email) {
|
||||||
return playersWaiting.get(code).getEmail().equals(email);
|
Player toMatch = new Player(email);
|
||||||
|
for (Player p: playersWaiting.keySet()) {
|
||||||
|
if (p.equals(toMatch)) {
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,10 +28,10 @@ public class Register implements Response {
|
||||||
private DatabaseRepository databaseRepo;
|
private DatabaseRepository databaseRepo;
|
||||||
private Router<PeerAtUser> router;
|
private Router<PeerAtUser> router;
|
||||||
private String usersFilesPath;
|
private String usersFilesPath;
|
||||||
private Map<Integer, Player> playersWaiting;
|
private Map<Player, Integer> playersWaiting;
|
||||||
|
|
||||||
public Register(DatabaseRepository databaseRepo, Router<PeerAtUser> router, String initUsersFilesPath,
|
public Register(DatabaseRepository databaseRepo, Router<PeerAtUser> router, String initUsersFilesPath,
|
||||||
Map<Integer, Player> playersWaiting) {
|
Map<Player, Integer> playersWaiting) {
|
||||||
this.databaseRepo = databaseRepo;
|
this.databaseRepo = databaseRepo;
|
||||||
this.router = router;
|
this.router = router;
|
||||||
usersFilesPath = initUsersFilesPath;
|
usersFilesPath = initUsersFilesPath;
|
||||||
|
@ -49,28 +49,22 @@ public class Register implements Response {
|
||||||
}
|
}
|
||||||
JSONObject informations = reader.readJson();
|
JSONObject informations = reader.readJson();
|
||||||
if (informations != null) {
|
if (informations != null) {
|
||||||
boolean allFieldsFilled = informations.containsKey("pseudo") && informations.containsKey("email")
|
boolean allNecessaryFieldsFilled = informations.containsKey("pseudo") && informations.containsKey("email")
|
||||||
&& informations.containsKey("passwd") && informations.containsKey("firstname")
|
&& informations.containsKey("firstname") && informations.containsKey("lastname");
|
||||||
&& informations.containsKey("lastname") && informations.containsKey("description")
|
if (!allNecessaryFieldsFilled) {
|
||||||
&& informations.containsKey("sgroup") && informations.containsKey("avatar");
|
context.response(400);
|
||||||
if (!allFieldsFilled) {
|
|
||||||
context.response(403);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String pseudo = (String) informations.get("pseudo");
|
String pseudo = (String) informations.get("pseudo");
|
||||||
String email = (String) informations.get("email");
|
String email = (String) informations.get("email");
|
||||||
String password = (String) informations.get("passwd");
|
|
||||||
String firstname = (String) informations.get("firstname");
|
String firstname = (String) informations.get("firstname");
|
||||||
String lastname = (String) informations.get("lastname");
|
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 pseudoAvailable = databaseRepo.checkPseudoAvailability(pseudo);
|
||||||
boolean emailAvailable = databaseRepo.checkEmailAvailability(email);
|
boolean emailAvailable = databaseRepo.checkEmailAvailability(email);
|
||||||
if (pseudoAvailable && emailAvailable) {
|
if (pseudoAvailable && emailAvailable) {
|
||||||
Player player = new Player(pseudo, email, firstname, lastname, description);
|
Player player = new Player(pseudo, email, firstname, lastname);
|
||||||
playersWaiting.put(codeGenerator(), player);
|
playersWaiting.put(player, codeGenerator());
|
||||||
context.response(200);
|
context.response(200);
|
||||||
} else {
|
} else {
|
||||||
context.response(400);
|
context.response(400);
|
||||||
|
@ -86,8 +80,9 @@ public class Register implements Response {
|
||||||
}
|
}
|
||||||
|
|
||||||
private int codeGenerator() {
|
private int codeGenerator() {
|
||||||
Random rand = new Random();
|
int min = 1000;
|
||||||
return rand.nextInt(1000);
|
int max = 9999;
|
||||||
|
return new Random().nextInt((max-min)) + min;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue