add function to disable and delete

This commit is contained in:
Valentin CZERYBA 2022-08-05 21:03:28 +02:00
parent c20ca0723c
commit be11abbd9f
3 changed files with 80 additions and 6 deletions

View File

@ -1,7 +1,5 @@
package com.covas.Json;
import java.time.LocalDate;
import io.quarkus.runtime.annotations.RegisterForReflection;
@RegisterForReflection

View File

@ -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;

View File

@ -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)