From 4b3b307d6d6489ee3e3dae1b5dd2b6f7aff18d1c Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sun, 15 Jan 2023 20:26:10 +0100 Subject: [PATCH 1/3] entity and resources for group --- .../java/com/covas/Entity/GroupEntity.java | 25 ++ .../com/covas/Resources/EventRessources.java | 3 - .../com/covas/Resources/GroupRessources.java | 273 ++++++++++++++++++ 3 files changed, 298 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/covas/Entity/GroupEntity.java create mode 100644 src/main/java/com/covas/Resources/GroupRessources.java diff --git a/src/main/java/com/covas/Entity/GroupEntity.java b/src/main/java/com/covas/Entity/GroupEntity.java new file mode 100644 index 0000000..6fc08d3 --- /dev/null +++ b/src/main/java/com/covas/Entity/GroupEntity.java @@ -0,0 +1,25 @@ +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; + +@MongoEntity(collection="group") +public class GroupEntity extends PanacheMongoEntity { + + public String name; + + public Short status; + + public Collection admins; + + public Collection membres; + + public LocalDateTime created_at; + public LocalDateTime updated_at; + public LocalDateTime deleted_at; +} diff --git a/src/main/java/com/covas/Resources/EventRessources.java b/src/main/java/com/covas/Resources/EventRessources.java index b21c8ac..975d8cf 100644 --- a/src/main/java/com/covas/Resources/EventRessources.java +++ b/src/main/java/com/covas/Resources/EventRessources.java @@ -26,11 +26,8 @@ import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -import com.covas.Entity.CommentEntity; import com.covas.Entity.EventEntity; -import com.covas.Entity.PublisherEntity; import com.covas.Entity.UsersEntity; -import com.covas.Json.Organisateurs; import io.quarkus.panache.common.Page; diff --git a/src/main/java/com/covas/Resources/GroupRessources.java b/src/main/java/com/covas/Resources/GroupRessources.java new file mode 100644 index 0000000..35f8fc1 --- /dev/null +++ b/src/main/java/com/covas/Resources/GroupRessources.java @@ -0,0 +1,273 @@ +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.EventEntity; +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("group") +public class GroupRessources { + 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 getEvents(@CookieParam("user") String userCookie, @Context SecurityContext ctx, + @QueryParam("page") Integer page, @QueryParam("nbPages") Integer nbPages, + @QueryParam("status") Short status, + @QueryParam("search") String search, + @QueryParam("uuid") String uuid) { + 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 listEvents = EventEntity.findAll().page(Page.of(page, nbPages)).list(); + + + responseHttp = Response.ok(listEvents).build(); + if(uuid != null){ + EventEntity eventSingle= EventEntity.findById(new ObjectId(uuid)); + responseHttp = Response.ok(eventSingle).build(); + } + + if(search != null){ + List eventsList = EventEntity.find("name", search).page(Page.of(page, nbPages)).list(); + responseHttp = Response.ok(eventsList).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(EventEntity.count()).build(); + } + return responseHttp; + } + + @GET + @RolesAllowed("Admin") + @Path("{id}") + public Response getSingleEvent(@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)) { + EventEntity event = EventEntity.findById(new ObjectId(id)); + responseHttp = Response.status(Response.Status.NOT_FOUND).build(); + if (event != null) { + responseHttp = Response.ok(event).build(); + } + + } + return responseHttp; + } + + + // PUT + @PUT + @Consumes(MediaType.APPLICATION_JSON) + @RolesAllowed({"Admin", "User"}) + @Path("{id}") + @Transactional + public Response createEvent(@Context SecurityContext ctx, @CookieParam("user") String userCookie, @PathParam("id") String id, EventEntity event) { + + UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); + UsersEntity user = UsersEntity.findById(kid); + Response.Status status = getResponseCheck(ctx, userCookie, user); + EventEntity newEvent = new EventEntity(); + if (status.equals(Response.Status.OK)) { + newEvent.name = event.name; + newEvent.address = event.address; + newEvent.organisateurs = event.organisateurs; + newEvent.created_at = LocalDateTime.now(); + newEvent.updated_at = LocalDateTime.now(); + newEvent.persist(); + status = Response.Status.CREATED; + + } + return Response.status(status).entity(newEvent).build(); + } + + + // DELETE + @DELETE + @Path("{id}") + @RolesAllowed({"Admin", "User"}) + @Transactional + public Response changeStatusSingleEventToDelete(@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)) { + EventEntity singleEvent = EventEntity.find("id", new ObjectId(id)).firstResult(); + if (singleEvent == null) { + status = Response.Status.NOT_FOUND; + } else { + singleEvent.status = -1; + singleEvent.updated_at = LocalDateTime.now(); + singleEvent.deleted_at = LocalDateTime.now(); + singleEvent.persist(); + } + } + return Response.status(status).build(); + } + + @DELETE + @Path("/disable/{id}") + @RolesAllowed({"Admin", "User"}) + @Transactional + public Response changeStatusSingleEventToDisable(@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)) { + EventEntity singleEvent = EventEntity.find("id", new ObjectId(id)).firstResult(); + if (singleEvent == null) { + status = Response.Status.NOT_FOUND; + } else { + singleEvent.status = 0; + singleEvent.updated_at = LocalDateTime.now(); + singleEvent.deleted_at = LocalDateTime.now(); + singleEvent.persist(); + } + } + return Response.status(status).build(); + } + + // PATCH + @PATCH + @RolesAllowed({"Admin", "User"}) + @Consumes(MediaType.APPLICATION_JSON) + @Transactional + @Path("{id}") + public Response updateEvent(@Context SecurityContext ctx, @CookieParam("user") String userCookie, + EventEntity event, @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)) { + + EventEntity eventOrig = EventEntity.findById(new ObjectId(id)); + if (eventOrig == null) { + status = Response.Status.NOT_FOUND; + } else { + eventOrig.name = event.name; + + + eventOrig.updated_at = LocalDateTime.now(); + + if(event.status == 1){ + eventOrig.deleted_at = null; + eventOrig.status = 1; + } + eventOrig.persist(); + + } + } + return Response.status(status).build(); + } + + + + @PATCH + @RolesAllowed({"Admin", "User"}) + @Consumes(MediaType.APPLICATION_JSON) + @Transactional + @Path("enable/{id}") + public Response enableEvent(@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)) { + + EventEntity eventOrig = EventEntity.findById(new ObjectId(id)); + if (eventOrig == null) { + status = Response.Status.NOT_FOUND; + } else { + eventOrig.status = 1; + eventOrig.persist(); + } + } + return Response.status(status).build(); + } +} From b597bdb2552c0fa22626e4475b7ad3e0a6c2b96a Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sun, 15 Jan 2023 20:42:23 +0100 Subject: [PATCH 2/3] complete group resources --- .../com/covas/Resources/GroupRessources.java | 89 +++++++++---------- 1 file changed, 44 insertions(+), 45 deletions(-) diff --git a/src/main/java/com/covas/Resources/GroupRessources.java b/src/main/java/com/covas/Resources/GroupRessources.java index 35f8fc1..7b8d118 100644 --- a/src/main/java/com/covas/Resources/GroupRessources.java +++ b/src/main/java/com/covas/Resources/GroupRessources.java @@ -26,7 +26,7 @@ import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -import com.covas.Entity.EventEntity; +import com.covas.Entity.GroupEntity; import com.covas.Entity.UsersEntity; import io.quarkus.panache.common.Page; @@ -88,18 +88,18 @@ public class GroupRessources { Response.Status statusHttp = getResponseCheck(ctx, userCookie, user); Response responseHttp = Response.status(statusHttp).build(); if (statusHttp.equals(Response.Status.OK)) { - List listEvents = EventEntity.findAll().page(Page.of(page, nbPages)).list(); + List listGroups = GroupEntity.findAll().page(Page.of(page, nbPages)).list(); - responseHttp = Response.ok(listEvents).build(); + responseHttp = Response.ok(listGroups).build(); if(uuid != null){ - EventEntity eventSingle= EventEntity.findById(new ObjectId(uuid)); - responseHttp = Response.ok(eventSingle).build(); + GroupEntity groupSingle= GroupEntity.findById(new ObjectId(uuid)); + responseHttp = Response.ok(groupSingle).build(); } if(search != null){ - List eventsList = EventEntity.find("name", search).page(Page.of(page, nbPages)).list(); - responseHttp = Response.ok(eventsList).build(); + List groupsList = GroupEntity.find("name", search).page(Page.of(page, nbPages)).list(); + responseHttp = Response.ok(groupsList).build(); } } return responseHttp; @@ -114,7 +114,7 @@ public class GroupRessources { Response.Status status = getResponseCheck(ctx, userCookie, user); Response responseHttp = Response.status(status).build(); if (status.equals(Response.Status.OK)){ - responseHttp = Response.ok(EventEntity.count()).build(); + responseHttp = Response.ok(GroupEntity.count()).build(); } return responseHttp; } @@ -129,10 +129,10 @@ public class GroupRessources { Response.Status status = getResponseCheck(ctx, userCookie, user); Response responseHttp = Response.status(status).build(); if (status.equals(Response.Status.OK)) { - EventEntity event = EventEntity.findById(new ObjectId(id)); + GroupEntity group = GroupEntity.findById(new ObjectId(id)); responseHttp = Response.status(Response.Status.NOT_FOUND).build(); - if (event != null) { - responseHttp = Response.ok(event).build(); + if (group != null) { + responseHttp = Response.ok(group).build(); } } @@ -146,23 +146,22 @@ public class GroupRessources { @RolesAllowed({"Admin", "User"}) @Path("{id}") @Transactional - public Response createEvent(@Context SecurityContext ctx, @CookieParam("user") String userCookie, @PathParam("id") String id, EventEntity event) { + public Response createEvent(@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); - EventEntity newEvent = new EventEntity(); + GroupEntity newGroup = new GroupEntity(); if (status.equals(Response.Status.OK)) { - newEvent.name = event.name; - newEvent.address = event.address; - newEvent.organisateurs = event.organisateurs; - newEvent.created_at = LocalDateTime.now(); - newEvent.updated_at = LocalDateTime.now(); - newEvent.persist(); + 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(newEvent).build(); + return Response.status(status).entity(newGroup).build(); } @@ -177,14 +176,14 @@ public class GroupRessources { UsersEntity user = UsersEntity.findById(kid); Response.Status status = getResponseCheck(ctx, userCookie, user); if (status.equals(Response.Status.OK)) { - EventEntity singleEvent = EventEntity.find("id", new ObjectId(id)).firstResult(); - if (singleEvent == null) { + GroupEntity singleGroup = GroupEntity.find("id", new ObjectId(id)).firstResult(); + if (singleGroup == null) { status = Response.Status.NOT_FOUND; } else { - singleEvent.status = -1; - singleEvent.updated_at = LocalDateTime.now(); - singleEvent.deleted_at = LocalDateTime.now(); - singleEvent.persist(); + singleGroup.status = -1; + singleGroup.updated_at = LocalDateTime.now(); + singleGroup.deleted_at = LocalDateTime.now(); + singleGroup.persist(); } } return Response.status(status).build(); @@ -200,14 +199,14 @@ public class GroupRessources { UsersEntity user = UsersEntity.findById(kid); Response.Status status = getResponseCheck(ctx, userCookie, user); if (status.equals(Response.Status.OK)) { - EventEntity singleEvent = EventEntity.find("id", new ObjectId(id)).firstResult(); - if (singleEvent == null) { + GroupEntity singleGroup = GroupEntity.find("id", new ObjectId(id)).firstResult(); + if (singleGroup == null) { status = Response.Status.NOT_FOUND; } else { - singleEvent.status = 0; - singleEvent.updated_at = LocalDateTime.now(); - singleEvent.deleted_at = LocalDateTime.now(); - singleEvent.persist(); + singleGroup.status = 0; + singleGroup.updated_at = LocalDateTime.now(); + singleGroup.deleted_at = LocalDateTime.now(); + singleGroup.persist(); } } return Response.status(status).build(); @@ -220,27 +219,27 @@ public class GroupRessources { @Transactional @Path("{id}") public Response updateEvent(@Context SecurityContext ctx, @CookieParam("user") String userCookie, - EventEntity event, @PathParam("id") String id) { + 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)) { - EventEntity eventOrig = EventEntity.findById(new ObjectId(id)); - if (eventOrig == null) { + GroupEntity groupOrig = GroupEntity.findById(new ObjectId(id)); + if (groupOrig == null) { status = Response.Status.NOT_FOUND; } else { - eventOrig.name = event.name; + groupOrig.name = group.name; - eventOrig.updated_at = LocalDateTime.now(); + groupOrig.updated_at = LocalDateTime.now(); - if(event.status == 1){ - eventOrig.deleted_at = null; - eventOrig.status = 1; + if(group.status == 1){ + groupOrig.deleted_at = null; + groupOrig.status = 1; } - eventOrig.persist(); + groupOrig.persist(); } } @@ -260,12 +259,12 @@ public class GroupRessources { Response.Status status = getResponseCheck(ctx, userCookie, user); if (status.equals(Response.Status.OK)) { - EventEntity eventOrig = EventEntity.findById(new ObjectId(id)); - if (eventOrig == null) { + GroupEntity groupOrig = GroupEntity.findById(new ObjectId(id)); + if (groupOrig == null) { status = Response.Status.NOT_FOUND; } else { - eventOrig.status = 1; - eventOrig.persist(); + groupOrig.status = 1; + groupOrig.persist(); } } return Response.status(status).build(); From 1e990820d13f77a30d912d848adfd4dc391a242f Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sun, 15 Jan 2023 20:44:21 +0100 Subject: [PATCH 3/3] change name for group resources --- .../com/covas/Resources/GroupRessources.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/covas/Resources/GroupRessources.java b/src/main/java/com/covas/Resources/GroupRessources.java index 7b8d118..9b61b86 100644 --- a/src/main/java/com/covas/Resources/GroupRessources.java +++ b/src/main/java/com/covas/Resources/GroupRessources.java @@ -72,7 +72,7 @@ public class GroupRessources { /// GET @GET @RolesAllowed("Admin") - public Response getEvents(@CookieParam("user") String userCookie, @Context SecurityContext ctx, + public Response getGroups(@CookieParam("user") String userCookie, @Context SecurityContext ctx, @QueryParam("page") Integer page, @QueryParam("nbPages") Integer nbPages, @QueryParam("status") Short status, @QueryParam("search") String search, @@ -122,7 +122,7 @@ public class GroupRessources { @GET @RolesAllowed("Admin") @Path("{id}") - public Response getSingleEvent(@PathParam("id") String id, @CookieParam("user") String userCookie, + 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); @@ -146,7 +146,7 @@ public class GroupRessources { @RolesAllowed({"Admin", "User"}) @Path("{id}") @Transactional - public Response createEvent(@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, GroupEntity group) { UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); UsersEntity user = UsersEntity.findById(kid); @@ -170,7 +170,7 @@ public class GroupRessources { @Path("{id}") @RolesAllowed({"Admin", "User"}) @Transactional - public Response changeStatusSingleEventToDelete(@Context SecurityContext ctx, @CookieParam("user") String userCookie, + 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); @@ -193,7 +193,7 @@ public class GroupRessources { @Path("/disable/{id}") @RolesAllowed({"Admin", "User"}) @Transactional - public Response changeStatusSingleEventToDisable(@Context SecurityContext ctx, @CookieParam("user") String userCookie, + 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); @@ -218,7 +218,7 @@ public class GroupRessources { @Consumes(MediaType.APPLICATION_JSON) @Transactional @Path("{id}") - public Response updateEvent(@Context SecurityContext ctx, @CookieParam("user") String userCookie, + 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); @@ -253,7 +253,7 @@ public class GroupRessources { @Consumes(MediaType.APPLICATION_JSON) @Transactional @Path("enable/{id}") - public Response enableEvent(@Context SecurityContext ctx, @CookieParam("user") String userCookie, @PathParam("id") String 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); @@ -269,4 +269,4 @@ public class GroupRessources { } return Response.status(status).build(); } -} +} \ No newline at end of file