From 4b8477b1762b10287b6f49cff9643975e9f37b49 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sun, 15 Jan 2023 21:25:32 +0100 Subject: [PATCH 1/5] remove class useless --- src/main/java/com/covas/Entity/GroupEntity.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/com/covas/Entity/GroupEntity.java b/src/main/java/com/covas/Entity/GroupEntity.java index 6fc08d3..f2a7e5e 100644 --- a/src/main/java/com/covas/Entity/GroupEntity.java +++ b/src/main/java/com/covas/Entity/GroupEntity.java @@ -3,8 +3,6 @@ package com.covas.Entity; import java.time.LocalDateTime; import java.util.Collection; -import com.covas.Json.Organisateurs; - import io.quarkus.mongodb.panache.PanacheMongoEntity; import io.quarkus.mongodb.panache.common.MongoEntity; From b6fc5783c952f9d3755d9e0f63102bb145212815 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sun, 15 Jan 2023 21:32:31 +0100 Subject: [PATCH 2/5] add friend entity --- .../java/com/covas/Entity/FriendEntity.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/main/java/com/covas/Entity/FriendEntity.java diff --git a/src/main/java/com/covas/Entity/FriendEntity.java b/src/main/java/com/covas/Entity/FriendEntity.java new file mode 100644 index 0000000..43eebc2 --- /dev/null +++ b/src/main/java/com/covas/Entity/FriendEntity.java @@ -0,0 +1,20 @@ +package com.covas.Entity; + +import java.time.LocalDateTime; +import java.util.Collection; + +import io.quarkus.mongodb.panache.PanacheMongoEntity; +import io.quarkus.mongodb.panache.common.MongoEntity; + +@MongoEntity(collection="friend") +public class FriendEntity extends PanacheMongoEntity { + + public String owner; + + public Collection friends; + + + public LocalDateTime created_at; + public LocalDateTime updated_at; + public LocalDateTime deleted_at; +} From c767a448e8e69bd4023460eb6a3d2b7c0695044f Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sun, 15 Jan 2023 21:44:16 +0100 Subject: [PATCH 3/5] add friend for resources --- .../com/covas/Resources/FriendRessources.java | 281 ++++++++++++++++++ 1 file changed, 281 insertions(+) create mode 100644 src/main/java/com/covas/Resources/FriendRessources.java diff --git a/src/main/java/com/covas/Resources/FriendRessources.java b/src/main/java/com/covas/Resources/FriendRessources.java new file mode 100644 index 0000000..51186a6 --- /dev/null +++ b/src/main/java/com/covas/Resources/FriendRessources.java @@ -0,0 +1,281 @@ +package com.covas.Resources; + +import java.nio.charset.StandardCharsets; +import java.time.LocalDateTime; +import java.util.List; +import java.util.UUID; +import java.util.ArrayList; +import java.util.Collection; + + +import javax.annotation.security.RolesAllowed; +import javax.inject.Inject; +import javax.transaction.Transactional; +import javax.ws.rs.core.SecurityContext; +import javax.ws.rs.Consumes; +import javax.ws.rs.CookieParam; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.PATCH; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import com.covas.Entity.FriendEntity; +import com.covas.Entity.GroupEntity; +import com.covas.Entity.UsersEntity; + +import io.quarkus.panache.common.Page; + +import org.bson.types.ObjectId; +import org.eclipse.microprofile.jwt.Claims; +import org.eclipse.microprofile.jwt.JsonWebToken; +import org.jboss.logging.Logger; +import org.postgresql.shaded.com.ongres.scram.common.bouncycastle.base64.Base64; + +@Produces(MediaType.APPLICATION_JSON) +@Path("friend") +public class FriendRessources { + private static final Logger LOGGER = Logger.getLogger(UsersRessources.class); + @Inject + JsonWebToken jwt; + + /// Function + private Boolean checkUserCookie(String userCookie, UsersEntity users) { + if ((userCookie == null) || (users == null)) { + return false; + } + String name = new String(Base64.decode(userCookie), StandardCharsets.UTF_8); + if (!name.equals(users.pseudo) && (users.status != 1)) { + return false; + } + return true; + } + + private Response.Status getResponseCheck(SecurityContext ctx, String userCookie, UsersEntity users) { + if (!ctx.getUserPrincipal().getName().equals(jwt.getName())) { + return Response.Status.INTERNAL_SERVER_ERROR; + } + if (!checkUserCookie(userCookie, users)) { + return Response.Status.FORBIDDEN; + } + return Response.Status.OK; + } + + /// Appel HTTP + + + /// GET + @GET + @RolesAllowed("Admin") + public Response getGroups(@CookieParam("user") String userCookie, @Context SecurityContext ctx, + @QueryParam("page") Integer page, @QueryParam("nbPages") Integer nbPages) { + if(nbPages == null){ + nbPages = 20; + } + if(page == null){ + page = 0; + } + UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); + UsersEntity user = UsersEntity.findById(kid); + Response.Status statusHttp = getResponseCheck(ctx, userCookie, user); + Response responseHttp = Response.status(statusHttp).build(); + if (statusHttp.equals(Response.Status.OK)) { + List listFriends = FriendEntity.findAll().page(Page.of(page, nbPages)).list(); + responseHttp = Response.ok(listFriends).build(); + + } + return responseHttp; + } + + @GET + @RolesAllowed("Admin") + @Path("count") + public Response getCount(@CookieParam("user") String userCookie, @Context SecurityContext ctx){ + UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); + UsersEntity user = UsersEntity.findById(kid); + Response.Status status = getResponseCheck(ctx, userCookie, user); + Response responseHttp = Response.status(status).build(); + if (status.equals(Response.Status.OK)){ + responseHttp = Response.ok(FriendEntity.count()).build(); + } + return responseHttp; + } + + @GET + @RolesAllowed("Admin") + @Path("{id}") + public Response getSingleGroup(@PathParam("id") String id, @CookieParam("user") String userCookie, + @Context SecurityContext ctx) { + UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); + UsersEntity user = UsersEntity.findById(kid); + Response.Status status = getResponseCheck(ctx, userCookie, user); + Response responseHttp = Response.status(status).build(); + if (status.equals(Response.Status.OK)) { + FriendEntity friend = FriendEntity.findById(new ObjectId(id)); + responseHttp = Response.status(Response.Status.NOT_FOUND).build(); + if (friend != null) { + responseHttp = Response.ok(friend).build(); + } + + } + return responseHttp; + } + + + @GET + @RolesAllowed("Admin") + @Path("/user/{id}") + public Response getSingleFriendbyOwner(@PathParam("id") String id, @CookieParam("user") String userCookie, + @Context SecurityContext ctx) { + UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); + UsersEntity user = UsersEntity.findById(kid); + Response.Status status = getResponseCheck(ctx, userCookie, user); + Response responseHttp = Response.status(status).build(); + if (status.equals(Response.Status.OK)) { + FriendEntity friend = FriendEntity.find("owner", id).firstResult(); + responseHttp = Response.status(Response.Status.NOT_FOUND).build(); + if (friend != null) { + responseHttp = Response.ok(friend).build(); + } + + } + return responseHttp; + } + + + // PUT + @PUT + @Consumes(MediaType.APPLICATION_JSON) + @RolesAllowed({"Admin", "User"}) + @Path("{id}") + @Transactional + public Response createGroup(@Context SecurityContext ctx, @CookieParam("user") String userCookie, @PathParam("id") String id, GroupEntity group) { + + UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); + UsersEntity user = UsersEntity.findById(kid); + Response.Status status = getResponseCheck(ctx, userCookie, user); + GroupEntity newGroup = new GroupEntity(); + if (status.equals(Response.Status.OK)) { + newGroup.name = group.name; + newGroup.admins = group.admins; + newGroup.created_at = LocalDateTime.now(); + newGroup.updated_at = LocalDateTime.now(); + newGroup.persist(); + status = Response.Status.CREATED; + + } + return Response.status(status).entity(newGroup).build(); + } + + + // DELETE + @DELETE + @Path("{id}") + @RolesAllowed({"Admin", "User"}) + @Transactional + public Response changeStatusSingleGroupToDelete(@Context SecurityContext ctx, @CookieParam("user") String userCookie, + @PathParam("id") String id) { + UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); + UsersEntity user = UsersEntity.findById(kid); + Response.Status status = getResponseCheck(ctx, userCookie, user); + if (status.equals(Response.Status.OK)) { + GroupEntity singleGroup = GroupEntity.find("id", new ObjectId(id)).firstResult(); + if (singleGroup == null) { + status = Response.Status.NOT_FOUND; + } else { + singleGroup.status = -1; + singleGroup.updated_at = LocalDateTime.now(); + singleGroup.deleted_at = LocalDateTime.now(); + singleGroup.persist(); + } + } + return Response.status(status).build(); + } + + @DELETE + @Path("/disable/{id}") + @RolesAllowed({"Admin", "User"}) + @Transactional + public Response changeStatusSingleGroupToDisable(@Context SecurityContext ctx, @CookieParam("user") String userCookie, + @PathParam("id") String id) { + UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); + UsersEntity user = UsersEntity.findById(kid); + Response.Status status = getResponseCheck(ctx, userCookie, user); + if (status.equals(Response.Status.OK)) { + GroupEntity singleGroup = GroupEntity.find("id", new ObjectId(id)).firstResult(); + if (singleGroup == null) { + status = Response.Status.NOT_FOUND; + } else { + singleGroup.status = 0; + singleGroup.updated_at = LocalDateTime.now(); + singleGroup.deleted_at = LocalDateTime.now(); + singleGroup.persist(); + } + } + return Response.status(status).build(); + } + + // PATCH + @PATCH + @RolesAllowed({"Admin", "User"}) + @Consumes(MediaType.APPLICATION_JSON) + @Transactional + @Path("{id}") + public Response updateGroup(@Context SecurityContext ctx, @CookieParam("user") String userCookie, + GroupEntity group, @PathParam("id") String id) { + UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); + UsersEntity user = UsersEntity.findById(kid); + Response.Status status = getResponseCheck(ctx, userCookie, user); + + if (status.equals(Response.Status.OK)) { + + GroupEntity groupOrig = GroupEntity.findById(new ObjectId(id)); + if (groupOrig == null) { + status = Response.Status.NOT_FOUND; + } else { + groupOrig.name = group.name; + + + groupOrig.updated_at = LocalDateTime.now(); + + if(group.status == 1){ + groupOrig.deleted_at = null; + groupOrig.status = 1; + } + groupOrig.persist(); + + } + } + return Response.status(status).build(); + } + + + + @PATCH + @RolesAllowed({"Admin", "User"}) + @Consumes(MediaType.APPLICATION_JSON) + @Transactional + @Path("enable/{id}") + public Response enableGroup(@Context SecurityContext ctx, @CookieParam("user") String userCookie, @PathParam("id") String id) { + UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); + UsersEntity user = UsersEntity.findById(kid); + Response.Status status = getResponseCheck(ctx, userCookie, user); + if (status.equals(Response.Status.OK)) { + + GroupEntity groupOrig = GroupEntity.findById(new ObjectId(id)); + if (groupOrig == null) { + status = Response.Status.NOT_FOUND; + } else { + groupOrig.status = 1; + groupOrig.persist(); + } + } + return Response.status(status).build(); + } +} \ No newline at end of file From 981ed99434e0b933ae1c5af4eef7c96f8108f83a Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sun, 15 Jan 2023 21:53:42 +0100 Subject: [PATCH 4/5] add friend resources --- .../java/com/covas/Entity/FriendEntity.java | 2 +- .../com/covas/Resources/FriendRessources.java | 94 ++++++------------- 2 files changed, 28 insertions(+), 68 deletions(-) diff --git a/src/main/java/com/covas/Entity/FriendEntity.java b/src/main/java/com/covas/Entity/FriendEntity.java index 43eebc2..f62840c 100644 --- a/src/main/java/com/covas/Entity/FriendEntity.java +++ b/src/main/java/com/covas/Entity/FriendEntity.java @@ -12,7 +12,7 @@ public class FriendEntity extends PanacheMongoEntity { public String owner; public Collection friends; - + public Short status; public LocalDateTime created_at; public LocalDateTime updated_at; diff --git a/src/main/java/com/covas/Resources/FriendRessources.java b/src/main/java/com/covas/Resources/FriendRessources.java index 51186a6..941250f 100644 --- a/src/main/java/com/covas/Resources/FriendRessources.java +++ b/src/main/java/com/covas/Resources/FriendRessources.java @@ -155,22 +155,22 @@ public class FriendRessources { @RolesAllowed({"Admin", "User"}) @Path("{id}") @Transactional - public Response createGroup(@Context SecurityContext ctx, @CookieParam("user") String userCookie, @PathParam("id") String id, GroupEntity group) { + public Response createGroup(@Context SecurityContext ctx, @CookieParam("user") String userCookie, @PathParam("id") String id, FriendEntity friend) { UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); UsersEntity user = UsersEntity.findById(kid); Response.Status status = getResponseCheck(ctx, userCookie, user); - GroupEntity newGroup = new GroupEntity(); + FriendEntity newFriend = new FriendEntity(); if (status.equals(Response.Status.OK)) { - newGroup.name = group.name; - newGroup.admins = group.admins; - newGroup.created_at = LocalDateTime.now(); - newGroup.updated_at = LocalDateTime.now(); - newGroup.persist(); + newFriend.owner = friend.owner; + newFriend.friends = friend.friends; + newFriend.created_at = LocalDateTime.now(); + newFriend.updated_at = LocalDateTime.now(); + newFriend.persist(); status = Response.Status.CREATED; } - return Response.status(status).entity(newGroup).build(); + return Response.status(status).entity(newFriend).build(); } @@ -185,41 +185,20 @@ public class FriendRessources { UsersEntity user = UsersEntity.findById(kid); Response.Status status = getResponseCheck(ctx, userCookie, user); if (status.equals(Response.Status.OK)) { - GroupEntity singleGroup = GroupEntity.find("id", new ObjectId(id)).firstResult(); - if (singleGroup == null) { + FriendEntity singleFriend = FriendEntity.find("id", new ObjectId(id)).firstResult(); + if (singleFriend == null) { status = Response.Status.NOT_FOUND; } else { - singleGroup.status = -1; - singleGroup.updated_at = LocalDateTime.now(); - singleGroup.deleted_at = LocalDateTime.now(); - singleGroup.persist(); + singleFriend.status = -1; + singleFriend.updated_at = LocalDateTime.now(); + singleFriend.deleted_at = LocalDateTime.now(); + singleFriend.persist(); } } return Response.status(status).build(); } - @DELETE - @Path("/disable/{id}") - @RolesAllowed({"Admin", "User"}) - @Transactional - public Response changeStatusSingleGroupToDisable(@Context SecurityContext ctx, @CookieParam("user") String userCookie, - @PathParam("id") String id) { - UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); - UsersEntity user = UsersEntity.findById(kid); - Response.Status status = getResponseCheck(ctx, userCookie, user); - if (status.equals(Response.Status.OK)) { - GroupEntity singleGroup = GroupEntity.find("id", new ObjectId(id)).firstResult(); - if (singleGroup == null) { - status = Response.Status.NOT_FOUND; - } else { - singleGroup.status = 0; - singleGroup.updated_at = LocalDateTime.now(); - singleGroup.deleted_at = LocalDateTime.now(); - singleGroup.persist(); - } - } - return Response.status(status).build(); - } + // PATCH @PATCH @@ -228,27 +207,28 @@ public class FriendRessources { @Transactional @Path("{id}") public Response updateGroup(@Context SecurityContext ctx, @CookieParam("user") String userCookie, - GroupEntity group, @PathParam("id") String id) { + FriendEntity friend, @PathParam("id") String id) { UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); UsersEntity user = UsersEntity.findById(kid); Response.Status status = getResponseCheck(ctx, userCookie, user); if (status.equals(Response.Status.OK)) { - GroupEntity groupOrig = GroupEntity.findById(new ObjectId(id)); - if (groupOrig == null) { + FriendEntity friendOrig = FriendEntity.findById(new ObjectId(id)); + if (friendOrig == null) { status = Response.Status.NOT_FOUND; } else { - groupOrig.name = group.name; + friendOrig.owner = friend.owner; + friendOrig.friends = friend.friends; + + friendOrig.updated_at = LocalDateTime.now(); - groupOrig.updated_at = LocalDateTime.now(); - - if(group.status == 1){ - groupOrig.deleted_at = null; - groupOrig.status = 1; + if(friend.status == 1){ + friendOrig.deleted_at = null; + friendOrig.status = 1; } - groupOrig.persist(); + friendOrig.persist(); } } @@ -257,25 +237,5 @@ public class FriendRessources { - @PATCH - @RolesAllowed({"Admin", "User"}) - @Consumes(MediaType.APPLICATION_JSON) - @Transactional - @Path("enable/{id}") - public Response enableGroup(@Context SecurityContext ctx, @CookieParam("user") String userCookie, @PathParam("id") String id) { - UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); - UsersEntity user = UsersEntity.findById(kid); - Response.Status status = getResponseCheck(ctx, userCookie, user); - if (status.equals(Response.Status.OK)) { - - GroupEntity groupOrig = GroupEntity.findById(new ObjectId(id)); - if (groupOrig == null) { - status = Response.Status.NOT_FOUND; - } else { - groupOrig.status = 1; - groupOrig.persist(); - } - } - return Response.status(status).build(); - } + } \ No newline at end of file From f8752395d460959e2a230db09c9a0dd83a1990ba Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Mon, 16 Jan 2023 21:29:22 +0100 Subject: [PATCH 5/5] change name --- .../java/com/covas/Resources/FriendRessources.java | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/covas/Resources/FriendRessources.java b/src/main/java/com/covas/Resources/FriendRessources.java index 941250f..a511cfd 100644 --- a/src/main/java/com/covas/Resources/FriendRessources.java +++ b/src/main/java/com/covas/Resources/FriendRessources.java @@ -73,7 +73,7 @@ public class FriendRessources { /// GET @GET @RolesAllowed("Admin") - public Response getGroups(@CookieParam("user") String userCookie, @Context SecurityContext ctx, + public Response getFriends(@CookieParam("user") String userCookie, @Context SecurityContext ctx, @QueryParam("page") Integer page, @QueryParam("nbPages") Integer nbPages) { if(nbPages == null){ nbPages = 20; @@ -110,7 +110,7 @@ public class FriendRessources { @GET @RolesAllowed("Admin") @Path("{id}") - public Response getSingleGroup(@PathParam("id") String id, @CookieParam("user") String userCookie, + public Response getSingleFriend(@PathParam("id") String id, @CookieParam("user") String userCookie, @Context SecurityContext ctx) { UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); UsersEntity user = UsersEntity.findById(kid); @@ -155,7 +155,7 @@ public class FriendRessources { @RolesAllowed({"Admin", "User"}) @Path("{id}") @Transactional - public Response createGroup(@Context SecurityContext ctx, @CookieParam("user") String userCookie, @PathParam("id") String id, FriendEntity friend) { + public Response createFriend(@Context SecurityContext ctx, @CookieParam("user") String userCookie, @PathParam("id") String id, FriendEntity friend) { UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); UsersEntity user = UsersEntity.findById(kid); @@ -179,7 +179,7 @@ public class FriendRessources { @Path("{id}") @RolesAllowed({"Admin", "User"}) @Transactional - public Response changeStatusSingleGroupToDelete(@Context SecurityContext ctx, @CookieParam("user") String userCookie, + public Response changeStatusSingleFriendToDelete(@Context SecurityContext ctx, @CookieParam("user") String userCookie, @PathParam("id") String id) { UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); UsersEntity user = UsersEntity.findById(kid); @@ -206,7 +206,7 @@ public class FriendRessources { @Consumes(MediaType.APPLICATION_JSON) @Transactional @Path("{id}") - public Response updateGroup(@Context SecurityContext ctx, @CookieParam("user") String userCookie, + public Response updateFriend(@Context SecurityContext ctx, @CookieParam("user") String userCookie, FriendEntity friend, @PathParam("id") String id) { UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); UsersEntity user = UsersEntity.findById(kid); @@ -234,8 +234,4 @@ public class FriendRessources { } return Response.status(status).build(); } - - - - } \ No newline at end of file