From be11abbd9f86c060fc3d8b866eb5deb3bdfff042 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Fri, 5 Aug 2022 21:03:28 +0200 Subject: [PATCH] add function to disable and delete --- src/main/java/com/covas/Json/UserSingle.java | 2 - .../com/covas/Resources/TokenRessource.java | 1 - .../com/covas/Resources/UsersRessources.java | 83 ++++++++++++++++++- 3 files changed, 80 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/covas/Json/UserSingle.java b/src/main/java/com/covas/Json/UserSingle.java index 22ea520..ef003d0 100644 --- a/src/main/java/com/covas/Json/UserSingle.java +++ b/src/main/java/com/covas/Json/UserSingle.java @@ -1,7 +1,5 @@ package com.covas.Json; -import java.time.LocalDate; - import io.quarkus.runtime.annotations.RegisterForReflection; @RegisterForReflection diff --git a/src/main/java/com/covas/Resources/TokenRessource.java b/src/main/java/com/covas/Resources/TokenRessource.java index 20404e8..b77b216 100644 --- a/src/main/java/com/covas/Resources/TokenRessource.java +++ b/src/main/java/com/covas/Resources/TokenRessource.java @@ -17,7 +17,6 @@ import javax.ws.rs.core.Response; import com.covas.Classes.Hash; import com.covas.Entity.UsersEntity; -import com.fasterxml.jackson.datatype.jsr310.deser.DurationDeserializer; import io.smallrye.jwt.auth.principal.JWTParser; import io.smallrye.jwt.auth.principal.ParseException; diff --git a/src/main/java/com/covas/Resources/UsersRessources.java b/src/main/java/com/covas/Resources/UsersRessources.java index cefe303..47c2f9b 100644 --- a/src/main/java/com/covas/Resources/UsersRessources.java +++ b/src/main/java/com/covas/Resources/UsersRessources.java @@ -224,14 +224,14 @@ public class UsersRessources { @DELETE @RolesAllowed("User") @Transactional - public Response changeStatus(@Context SecurityContext ctx, @CookieParam("user") String userCookie) { + public Response changeStatusToDelete(@Context SecurityContext ctx, @CookieParam("user") String userCookie) { 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)) { - user.status = 0; + user.status = -1; user.updated_at = LocalDateTime.now(); user.deleted_at = LocalDateTime.now(); user.persist(); @@ -246,7 +246,57 @@ public class UsersRessources { @Path("{id}") @RolesAllowed("Admin") @Transactional - public Response changeStatusSingleUser(@Context SecurityContext ctx, @CookieParam("user") String userCookie, + public Response changeStatusSingleUserToDelete(@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)) { + UsersEntity singleUser = UsersEntity.find("id", UUID.fromString(id)).firstResult(); + if (singleUser == null) { + status = Response.Status.NOT_FOUND; + } else { + singleUser.status = -1; + singleUser.updated_at = LocalDateTime.now(); + singleUser.deleted_at = LocalDateTime.now(); + singleUser.persist(); + if (!singleUser.isPersistent()) { + status = Response.Status.NOT_MODIFIED; + } + } + } + return Response.status(status).build(); + } + + + + @DELETE + @RolesAllowed("User") + @Transactional + @Path("disable") + public Response changeStatusToDisable(@Context SecurityContext ctx, @CookieParam("user") String userCookie) { + 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)) { + user.status = -1; + user.updated_at = LocalDateTime.now(); + user.deleted_at = LocalDateTime.now(); + user.persist(); + if (!user.isPersistent()) { + status = Response.Status.NOT_MODIFIED; + } + } + return Response.status(status).build(); + } + + @DELETE + @Path("/disable/{id}") + @RolesAllowed("Admin") + @Transactional + public Response changeStatusSingleUserToDisable(@Context SecurityContext ctx, @CookieParam("user") String userCookie, @PathParam("id") String id) { UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); UsersEntity user = UsersEntity.findById(kid); @@ -307,6 +357,33 @@ public class UsersRessources { return Response.status(status).build(); } + + + @PATCH + @RolesAllowed("Admin") + @Consumes(MediaType.APPLICATION_JSON) + @Transactional + @Path("enable/{id}") + public Response enableUserAdmin(@Context SecurityContext ctx, @CookieParam("user") String userCookie, @QueryParam("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)) { + + UsersEntity usersOrig = UsersEntity.findById(UUID.fromString(id)); + if (usersOrig == null) { + status = Response.Status.NOT_FOUND; + } else { + usersOrig.status = 1; + usersOrig.persist(); + if (!usersOrig.isPersistent()) { + status = Response.Status.NOT_MODIFIED; + } + } + } + return Response.status(status).build(); + } + @PATCH @RolesAllowed("User") @Consumes(MediaType.APPLICATION_JSON)