From 1907173db527bf383a8d6965e7e069c68c91d0d8 Mon Sep 17 00:00:00 2001 From: TheNagaki Date: Tue, 14 Feb 2023 23:34:13 +0100 Subject: [PATCH] Updated /register route with the new field in the database, updated DatabaseRepo for the same reason --- .../peeratcode/repository/DatabaseRepo.java | 8 ++++---- src/be/jeffcheasey88/peeratcode/routes/Register.java | 10 +++++++++- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepo.java b/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepo.java index cf2a2a0..c0b01a9 100644 --- a/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepo.java +++ b/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepo.java @@ -19,7 +19,7 @@ public class DatabaseRepo { private static final String ALL_CHAPTERS_QUERY = "SELECT * FROM chapter"; private static final String CHECK_PSEUDO_AVAILABLE_QUERY = "SELECT * FROM user WHERE pseudo = ?"; private static final String CHECK_EMAIL_AVAILABLE_QUERY = "SELECT * FROM user WHERE email = ?"; - private static final String REGISTER_QUERY = "INSERT INTO user (pseudo, email, passwd, firstname, lastname, description, `group`, avatar) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"; + private static final String REGISTER_QUERY = "INSERT INTO user (pseudo, email, passwd, firstname, lastname, description, sgroup, avatar) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"; private static final String PASSWORD_FOR_EMAIL_QUERY = "SELECT passwd FROM user WHERE pseudo = ?"; private final Connection con; @@ -152,11 +152,11 @@ public class DatabaseRepo { * @param firstname The firstname of the user * @param lastname The lastname of the user * @param description The description of the user - * @param group The group of the user + * @param sgroup The group of the user * @param avatar The avatar of the user * @return True if the user was registered, false if an error occurred */ - public boolean register(String pseudo, String email, String password, String firstname, String lastname, String description, String group, String avatar) { + public boolean register(String pseudo, String email, String password, String firstname, String lastname, String description, String sgroup, String avatar) { Hash hash = Password.hash(password).withArgon2(); try { PreparedStatement statement = con.prepareStatement(REGISTER_QUERY); @@ -166,7 +166,7 @@ public class DatabaseRepo { statement.setString(4, firstname); statement.setString(5, lastname); statement.setString(6, description); - statement.setString(7, group); + statement.setString(7, sgroup); statement.setString(8, avatar); return statement.executeUpdate() == 1; } catch (SQLException e) { diff --git a/src/be/jeffcheasey88/peeratcode/routes/Register.java b/src/be/jeffcheasey88/peeratcode/routes/Register.java index 1341a2c..a6a575e 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/Register.java +++ b/src/be/jeffcheasey88/peeratcode/routes/Register.java @@ -23,13 +23,21 @@ public class Register implements Response { HttpUtil.skipHeaders(reader); JSONObject informations = (JSONObject) HttpUtil.readJson(reader); 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) { + HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *"); + 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("group"); + String group = (String) informations.get("sgroup"); String avatar = (String) informations.get("avatar"); boolean pseudoAvailable = databaseRepo.checkPseudoAvailability(pseudo);