From dcd00285f2e4aae2e08193d20e2c7b2c2931f1e7 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Mon, 16 Jan 2023 21:52:04 +0100 Subject: [PATCH 1/3] add entity blacklist --- .../com/covas/Entity/BlacklistEntity.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/main/java/com/covas/Entity/BlacklistEntity.java diff --git a/src/main/java/com/covas/Entity/BlacklistEntity.java b/src/main/java/com/covas/Entity/BlacklistEntity.java new file mode 100644 index 0000000..3dff23a --- /dev/null +++ b/src/main/java/com/covas/Entity/BlacklistEntity.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="blacklist") +public class BlacklistEntity extends PanacheMongoEntity { + + public String owner; + + public Collection blocked; + public Short status; + + public LocalDateTime created_at; + public LocalDateTime updated_at; + public LocalDateTime deleted_at; +} From efab19818d9087acbb06eea2ca4fa09ed448c756 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Mon, 16 Jan 2023 22:23:56 +0100 Subject: [PATCH 2/3] blacklist ressources --- .../covas/Resources/BlacklistRessources.java | 238 ++++++++++++++++++ 1 file changed, 238 insertions(+) create mode 100644 src/main/java/com/covas/Resources/BlacklistRessources.java diff --git a/src/main/java/com/covas/Resources/BlacklistRessources.java b/src/main/java/com/covas/Resources/BlacklistRessources.java new file mode 100644 index 0000000..d250db5 --- /dev/null +++ b/src/main/java/com/covas/Resources/BlacklistRessources.java @@ -0,0 +1,238 @@ +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.BlacklistEntity; +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("blacklist") +public class BlacklistRessources { + 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 getFriends(@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 listBlacklist = BlacklistEntity.findAll().page(Page.of(page, nbPages)).list(); + responseHttp = Response.ok(listBlacklist).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(BlacklistEntity.count()).build(); + } + return responseHttp; + } + + @GET + @RolesAllowed("Admin") + @Path("{id}") + 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); + Response.Status status = getResponseCheck(ctx, userCookie, user); + Response responseHttp = Response.status(status).build(); + if (status.equals(Response.Status.OK)) { + BlacklistEntity blacklist = BlacklistEntity.findById(new ObjectId(id)); + responseHttp = Response.status(Response.Status.NOT_FOUND).build(); + if (blacklist != null) { + responseHttp = Response.ok(blacklist).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)) { + BlacklistEntity blacklist = BlacklistEntity.find("owner", id).firstResult(); + responseHttp = Response.status(Response.Status.NOT_FOUND).build(); + if (blacklist != null) { + responseHttp = Response.ok(blacklist).build(); + } + + } + return responseHttp; + } + + + // PUT + @PUT + @Consumes(MediaType.APPLICATION_JSON) + @RolesAllowed({"Admin", "User"}) + @Path("{id}") + @Transactional + public Response createFriend(@Context SecurityContext ctx, @CookieParam("user") String userCookie, @PathParam("id") String id, BlacklistEntity blacklist) { + + UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); + UsersEntity user = UsersEntity.findById(kid); + Response.Status status = getResponseCheck(ctx, userCookie, user); + BlacklistEntity newBlacklist = new BlacklistEntity(); + if (status.equals(Response.Status.OK)) { + newBlacklist.owner = blacklist.owner; + newBlacklist.blocked = blacklist.blocked; + newBlacklist.created_at = LocalDateTime.now(); + newBlacklist.updated_at = LocalDateTime.now(); + newBlacklist.persist(); + status = Response.Status.CREATED; + + } + return Response.status(status).entity(newBlacklist).build(); + } + + + // DELETE + @DELETE + @Path("{id}") + @RolesAllowed({"Admin", "User"}) + @Transactional + 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); + Response.Status status = getResponseCheck(ctx, userCookie, user); + if (status.equals(Response.Status.OK)) { + BlacklistEntity singleBlacklist = BlacklistEntity.find("id", new ObjectId(id)).firstResult(); + if (singleBlacklist == null) { + status = Response.Status.NOT_FOUND; + } else { + singleBlacklist.status = -1; + singleBlacklist.updated_at = LocalDateTime.now(); + singleBlacklist.deleted_at = LocalDateTime.now(); + singleBlacklist.persist(); + } + } + return Response.status(status).build(); + } + + + + // PATCH + @PATCH + @RolesAllowed({"Admin", "User"}) + @Consumes(MediaType.APPLICATION_JSON) + @Transactional + @Path("{id}") + public Response updateFriend(@Context SecurityContext ctx, @CookieParam("user") String userCookie, + BlacklistEntity blacklist, @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)) { + + BlacklistEntity blacklistOrig = BlacklistEntity.findById(new ObjectId(id)); + if (blacklistOrig == null) { + status = Response.Status.NOT_FOUND; + } else { + blacklistOrig.owner = blacklist.owner; + blacklistOrig.blocked = blacklist.blocked; + + + blacklistOrig.updated_at = LocalDateTime.now(); + + if(blacklist.status == 1){ + blacklistOrig.deleted_at = null; + blacklistOrig.status = 1; + } + blacklistOrig.persist(); + + } + } + return Response.status(status).build(); + } +} \ No newline at end of file From b072ad7b70a672779235218c8f803359a3cccec1 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Mon, 16 Jan 2023 22:25:12 +0100 Subject: [PATCH 3/3] change name --- .../com/covas/Resources/BlacklistRessources.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/covas/Resources/BlacklistRessources.java b/src/main/java/com/covas/Resources/BlacklistRessources.java index d250db5..12ff241 100644 --- a/src/main/java/com/covas/Resources/BlacklistRessources.java +++ b/src/main/java/com/covas/Resources/BlacklistRessources.java @@ -74,7 +74,7 @@ public class BlacklistRessources { /// GET @GET @RolesAllowed("Admin") - public Response getFriends(@CookieParam("user") String userCookie, @Context SecurityContext ctx, + public Response getBlacklist(@CookieParam("user") String userCookie, @Context SecurityContext ctx, @QueryParam("page") Integer page, @QueryParam("nbPages") Integer nbPages) { if(nbPages == null){ nbPages = 20; @@ -111,7 +111,7 @@ public class BlacklistRessources { @GET @RolesAllowed("Admin") @Path("{id}") - public Response getSingleFriend(@PathParam("id") String id, @CookieParam("user") String userCookie, + public Response getSingleBlacklist(@PathParam("id") String id, @CookieParam("user") String userCookie, @Context SecurityContext ctx) { UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); UsersEntity user = UsersEntity.findById(kid); @@ -132,7 +132,7 @@ public class BlacklistRessources { @GET @RolesAllowed("Admin") @Path("/user/{id}") - public Response getSingleFriendbyOwner(@PathParam("id") String id, @CookieParam("user") String userCookie, + public Response getSingleBlacklistbyOwner(@PathParam("id") String id, @CookieParam("user") String userCookie, @Context SecurityContext ctx) { UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); UsersEntity user = UsersEntity.findById(kid); @@ -156,7 +156,7 @@ public class BlacklistRessources { @RolesAllowed({"Admin", "User"}) @Path("{id}") @Transactional - public Response createFriend(@Context SecurityContext ctx, @CookieParam("user") String userCookie, @PathParam("id") String id, BlacklistEntity blacklist) { + public Response createBlacklist(@Context SecurityContext ctx, @CookieParam("user") String userCookie, @PathParam("id") String id, BlacklistEntity blacklist) { UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); UsersEntity user = UsersEntity.findById(kid); @@ -180,7 +180,7 @@ public class BlacklistRessources { @Path("{id}") @RolesAllowed({"Admin", "User"}) @Transactional - public Response changeStatusSingleFriendToDelete(@Context SecurityContext ctx, @CookieParam("user") String userCookie, + public Response changeStatusSingleBlacklistToDelete(@Context SecurityContext ctx, @CookieParam("user") String userCookie, @PathParam("id") String id) { UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); UsersEntity user = UsersEntity.findById(kid); @@ -207,7 +207,7 @@ public class BlacklistRessources { @Consumes(MediaType.APPLICATION_JSON) @Transactional @Path("{id}") - public Response updateFriend(@Context SecurityContext ctx, @CookieParam("user") String userCookie, + public Response updateBlacklist(@Context SecurityContext ctx, @CookieParam("user") String userCookie, BlacklistEntity blacklist, @PathParam("id") String id) { UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); UsersEntity user = UsersEntity.findById(kid);