Merge remote-tracking branch 'origin/SecureGroupsRoutes'

This commit is contained in:
jeffcheasey88 2023-04-21 10:00:11 +02:00
commit 0150fc3a23
4 changed files with 74 additions and 68 deletions

View file

@ -21,7 +21,7 @@ public class Group implements Comparable<Group> {
this.linkToPuzzle = ((Number) json.get("puzzle")).intValue();
}
public Group(String name, int initChap, int initPuzz) {
public Group(String name, Integer initChap, Integer initPuzz) {
this.name = name;
this.linkToChapter = initChap;
this.linkToPuzzle = initPuzz;
@ -84,9 +84,9 @@ public class Group implements Comparable<Group> {
groupJSON.put("name", name);
if (rank != null)
groupJSON.put("rank", rank);
else if (linkToChapter > 0)
else if (linkToChapter != null)
groupJSON.put("chapter", linkToChapter);
else if (linkToPuzzle > 0)
else if (linkToPuzzle != null)
groupJSON.put("puzzle", linkToPuzzle);
if (players != null) {
JSONArray groupsPlayerJSON = new JSONArray();

View file

@ -209,14 +209,16 @@ public class DatabaseRepository {
stmt.setInt(1, user);
stmt.setInt(2, puzzle);
ResultSet result = stmt.executeQuery();
if(result.next()) return result.getInt("score");
if (result.next())
return result.getInt("score");
stmt = DatabaseQuery.SCORE.prepare(this.con);
stmt.setInt(1, user);
stmt.setInt(2, puzzle);
result = stmt.executeQuery();
if(result.next()) return result.getInt("score");
if (result.next())
return result.getInt("score");
} catch (Exception e) {
e.printStackTrace();
}
@ -229,7 +231,8 @@ public class DatabaseRepository {
stmt.setInt(1, user);
stmt.setInt(2, puzzle);
ResultSet result = stmt.executeQuery();
if (result.next()) return makeCompletion(user, puzzle, result);
if (result.next())
return makeCompletion(user, puzzle, result);
} catch (SQLException e) {
e.printStackTrace();
}
@ -575,22 +578,20 @@ public class DatabaseRepository {
statement.executeUpdate();
}
public boolean insertGroup(Group group, User creator){
try {
public boolean insertGroup(Group group, User creator) throws SQLException {
Integer groupId = getGroupId(group);
if (groupId == null)
ensureConnection();
PreparedStatement statement = DatabaseQuery.INSERT_GROUP.prepare(this.con);
statement.setString(1, group.getName());
statement.setObject(2, group.getLinkToChapter());
statement.setObject(3, group.getLinkToPuzzle());
if(statement.executeUpdate() >= 0) return insertUserInGroup(group, creator);
} catch (Exception e){
e.printStackTrace();
}
if (statement.executeUpdate() >= 0)
return insertUserInGroup(group, creator);
return false;
}
public Group getPlayerGroup(int user, Integer chapter, Integer puzzle){
try {
public Group getPlayerGroup(int user, Integer chapter, Integer puzzle) throws SQLException {
ensureConnection();
PreparedStatement stmt = DatabaseQuery.GET_GROUP_FOR_PLAYER.prepare(this.con);
stmt.setInt(1, user);
@ -598,12 +599,12 @@ public class DatabaseRepository {
stmt.setObject(3, puzzle);
ResultSet result = stmt.executeQuery();
if(result.next()) return makeGroup(result);
}catch(Exception e){}
if (result.next())
return makeGroup(result);
return null;
}
private int getGroupId(Group group) throws Exception{
public Integer getGroupId(Group group) throws SQLException {
ensureConnection();
PreparedStatement stmt = DatabaseQuery.GET_GROUP_ID_BY_DATA.prepare(this.con);
stmt.setString(1, group.getName());
@ -611,35 +612,35 @@ public class DatabaseRepository {
stmt.setObject(3, group.getLinkToPuzzle());
ResultSet result = stmt.executeQuery();
if(result.next()) return result.getInt("id_group");
throw new NullPointerException();
if (result.next())
return result.getInt("id_group");
throw null;
}
public boolean insertUserInGroup(Group group, User user){
try {
int id = getGroupId(group);
public boolean insertUserInGroup(Group group, User user) throws SQLException {
Integer id = getGroupId(group);
Group alreadyInGroup = getPlayerGroup(user.getId(), group.getLinkToChapter(), group.getLinkToPuzzle());
if (id != null && alreadyInGroup == null) {
PreparedStatement stmt = DatabaseQuery.INSERT_PLAYER_IN_GROUP.prepare(this.con);
stmt.setInt(1, user.getId());
stmt.setInt(2, id);
return stmt.executeUpdate() >= 0;
}catch(Exception e){
e.printStackTrace();
}
return false;
}
public boolean leaveGroup(Group group, User user){
try {
int id = getGroupId(group);
public boolean leaveGroup(Group group, User user) throws SQLException {
Integer id = getGroupId(group);
if (id != null) {
PreparedStatement stmt = DatabaseQuery.LEAVE_GROUP.prepare(this.con);
stmt.setInt(1, user.getId());
stmt.setInt(2, id);
return stmt.executeUpdate() >= 0;
}catch(Exception e){}
}
return false;
}

View file

@ -28,26 +28,31 @@ public class GroupCreate implements Response {
this.locker = locker;
}
@RouteDoc(path = "/groupCreate", responseCode = 200, responseDescription = "Le groupe à été crée")
@RouteDoc(path = "/groupCreate", responseCode = 200, responseDescription = "Le groupe a été créé")
@RouteDoc(responseCode = 403, responseDescription = "L'utilisateur est déjà dans le groupe / ne peux pas le rejoindre")
@Route(path = "^\\/groupCreate$", type = POST, needLogin = true)
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception{
Group group = new Group((JSONObject) HttpUtil.readJson(reader));
Group newGroup = new Group((JSONObject) HttpUtil.readJson(reader));
Group userGroup = this.repo.getPlayerGroup(user.getId(), group.getLinkToChapter(), group.getLinkToPuzzle());
if(group.equals(userGroup)){
if (this.repo.getPlayerGroup(user.getId(), newGroup.getLinkToChapter(), newGroup.getLinkToPuzzle()) == null) {
try {
this.repo.getGroupId(newGroup);
HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *");
return;
}
if (this.repo.insertGroup(group, user)) {
} catch (NullPointerException e) {
// if group not exist create it
if (this.repo.insertGroup(newGroup, user)) {
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *");
locker.setValue(group);
locker.setValue(newGroup);
} else {
HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *");
}
}
}
else {
HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *");
}
}
}

View file

@ -24,7 +24,7 @@ public class GroupJoin implements Response{
this.repo = repo;
}
@RouteDoc(path = "/groupJoin", responseCode = 200, responseDescription = "L'utilisateur à rejoind le groupe")
@RouteDoc(path = "/groupJoin", responseCode = 200, responseDescription = "L'utilisateur a rejoint le groupe")
@RouteDoc(responseCode = 403, responseDescription = "L'utilisateur est déjà dedans / ne peux pas le rejoindre")
@Route(path = "^\\/groupJoin$", type = POST, needLogin = true)