From a312ae4289a6c521ce179f151cc02d8794b69fcc Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sun, 15 May 2022 15:11:09 +0200 Subject: [PATCH 01/17] 100% work for getSingleUser INfo --- src/main/java/com/covas/Json/UserSingle.java | 21 ++++++++++++ .../com/covas/Resources/TokenRessource.java | 3 -- .../com/covas/Resources/UsersRessources.java | 34 ++++++++++++++++--- 3 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/covas/Json/UserSingle.java diff --git a/src/main/java/com/covas/Json/UserSingle.java b/src/main/java/com/covas/Json/UserSingle.java new file mode 100644 index 0000000..22ea520 --- /dev/null +++ b/src/main/java/com/covas/Json/UserSingle.java @@ -0,0 +1,21 @@ +package com.covas.Json; + +import java.time.LocalDate; + +import io.quarkus.runtime.annotations.RegisterForReflection; + +@RegisterForReflection +public class UserSingle { + + public final String name; + public final String pseudo; + public final String firstname; + + public UserSingle(String name, String pseudo, String firstname){ + this.name = name; + this.pseudo = pseudo; + this.firstname = firstname; + + } + +} diff --git a/src/main/java/com/covas/Resources/TokenRessource.java b/src/main/java/com/covas/Resources/TokenRessource.java index 84daf33..1411acb 100644 --- a/src/main/java/com/covas/Resources/TokenRessource.java +++ b/src/main/java/com/covas/Resources/TokenRessource.java @@ -2,9 +2,6 @@ package com.covas.Resources; import java.nio.charset.StandardCharsets; import java.time.Duration; -import java.util.Arrays; -import java.util.HashSet; - import javax.inject.Inject; import javax.ws.rs.CookieParam; diff --git a/src/main/java/com/covas/Resources/UsersRessources.java b/src/main/java/com/covas/Resources/UsersRessources.java index 1f193fe..dcf3221 100644 --- a/src/main/java/com/covas/Resources/UsersRessources.java +++ b/src/main/java/com/covas/Resources/UsersRessources.java @@ -1,24 +1,38 @@ package com.covas.Resources; +import java.nio.charset.StandardCharsets; import java.util.UUID; import javax.annotation.security.RolesAllowed; +import javax.inject.Inject; +import javax.ws.rs.core.SecurityContext; +import javax.ws.rs.CookieParam; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; +import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import com.covas.Entity.UsersEntity; +import com.covas.Json.UserSingle; +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; + +import io.quarkus.hibernate.orm.panache.PanacheQuery; @Produces(MediaType.APPLICATION_JSON) @Path("users") public class UsersRessources { private static final Logger LOGGER = Logger.getLogger(UsersRessources.class); + @Inject + JsonWebToken jwt; + @GET @RolesAllowed("Admin") public Response getUsers(){ @@ -40,9 +54,19 @@ public class UsersRessources { @GET @RolesAllowed("User") @Path("info") - public Response getInfoUser(){ - return Response.ok().build(); - } - - + public Response getInfoUser(@Context SecurityContext ctx, @CookieParam("user") String userCookie){ + if(!ctx.getUserPrincipal().getName().equals(jwt.getName())){ + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); + } + UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); + UsersEntity user = UsersEntity.findById(kid); + if (user == null){ + return Response.status(Response.Status.NOT_FOUND).build(); + } + String name = new String(Base64.decode(userCookie), StandardCharsets.UTF_8); + if(!name.equals(user.pseudo)){ + return Response.status(Response.Status.FORBIDDEN).build(); + } + return Response.ok(new UserSingle(user.name, user.pseudo, user.firstName)).build(); + } } From d800602681df6eeccec9256a0bb561480a7c6993 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sun, 15 May 2022 15:35:12 +0200 Subject: [PATCH 02/17] add exception --- .../com/covas/Resources/TokenRessource.java | 4 ++-- .../com/covas/Resources/UsersRessources.java | 22 +++++++++++++++---- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/covas/Resources/TokenRessource.java b/src/main/java/com/covas/Resources/TokenRessource.java index 1411acb..9daf5e3 100644 --- a/src/main/java/com/covas/Resources/TokenRessource.java +++ b/src/main/java/com/covas/Resources/TokenRessource.java @@ -56,8 +56,8 @@ public class TokenRessource { return Response.status(Response.Status.NOT_FOUND).build(); } if (jwtCookie == null) { - if(!password.equals(users.password)){ - return Response.status(Response.Status.FORBIDDEN).build(); + if((!password.equals(users.password)) && (!users.status)) { + return Response.status(Response.Status.FORBIDDEN).build(); } // Create a JWT token signed using the 'HS256' algorithm String newJwtCookie = Jwt.issuer("https://example.com/issuer").upn(name).groups(users.roles).claim(Claims.kid, users.id.toString()).expiresIn(Duration.ofMinutes(1)).sign(); diff --git a/src/main/java/com/covas/Resources/UsersRessources.java b/src/main/java/com/covas/Resources/UsersRessources.java index dcf3221..be8fea2 100644 --- a/src/main/java/com/covas/Resources/UsersRessources.java +++ b/src/main/java/com/covas/Resources/UsersRessources.java @@ -42,7 +42,15 @@ public class UsersRessources { @GET @RolesAllowed("Admin") @Path("{id}") - public Response getSingleUser(@PathParam("id") String id){ + public Response getSingleUser(@PathParam("id") String id, @CookieParam("user") String userCookie, @Context SecurityContext ctx){ + if(!ctx.getUserPrincipal().getName().equals(jwt.getName())){ + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); + } + UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); + UsersEntity user = UsersEntity.findById(kid); + if(!checkUserCookie(userCookie, user)){ + return Response.status(Response.Status.FORBIDDEN).build(); + } UUID uid = UUID.fromString(id); UsersEntity users = UsersEntity.findById(uid); if(users == null){ @@ -63,10 +71,16 @@ public class UsersRessources { if (user == null){ return Response.status(Response.Status.NOT_FOUND).build(); } - String name = new String(Base64.decode(userCookie), StandardCharsets.UTF_8); - if(!name.equals(user.pseudo)){ + if(!checkUserCookie(userCookie, user)){ return Response.status(Response.Status.FORBIDDEN).build(); } return Response.ok(new UserSingle(user.name, user.pseudo, user.firstName)).build(); - } + } + private Boolean checkUserCookie(String userCookie, UsersEntity users){ + String name = new String(Base64.decode(userCookie), StandardCharsets.UTF_8); + if(!name.equals(users.pseudo) && (!users.status)){ + return false; + } + return true; + } } From 1914022fda60e80c15b8542be1e1dc66148b5e14 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sun, 15 May 2022 15:39:13 +0200 Subject: [PATCH 03/17] remove import unused --- src/main/java/com/covas/Resources/UsersRessources.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/java/com/covas/Resources/UsersRessources.java b/src/main/java/com/covas/Resources/UsersRessources.java index be8fea2..fa122dd 100644 --- a/src/main/java/com/covas/Resources/UsersRessources.java +++ b/src/main/java/com/covas/Resources/UsersRessources.java @@ -23,9 +23,6 @@ import org.eclipse.microprofile.jwt.JsonWebToken; import org.jboss.logging.Logger; import org.postgresql.shaded.com.ongres.scram.common.bouncycastle.base64.Base64; -import io.quarkus.hibernate.orm.panache.PanacheQuery; - - @Produces(MediaType.APPLICATION_JSON) @Path("users") public class UsersRessources { From 7aeb473d02f85312c4f1623304498d6660e58d97 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sun, 15 May 2022 17:08:00 +0200 Subject: [PATCH 04/17] Organisation code avec commentaire --- .../com/covas/Resources/UsersRessources.java | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/covas/Resources/UsersRessources.java b/src/main/java/com/covas/Resources/UsersRessources.java index fa122dd..86cac89 100644 --- a/src/main/java/com/covas/Resources/UsersRessources.java +++ b/src/main/java/com/covas/Resources/UsersRessources.java @@ -29,7 +29,19 @@ public class UsersRessources { private static final Logger LOGGER = Logger.getLogger(UsersRessources.class); @Inject JsonWebToken jwt; - + + ///Function + private Boolean checkUserCookie(String userCookie, UsersEntity users){ + String name = new String(Base64.decode(userCookie), StandardCharsets.UTF_8); + if(!name.equals(users.pseudo) && (!users.status)){ + return false; + } + return true; + } + + /// Appel HTTP + + ///GET @GET @RolesAllowed("Admin") public Response getUsers(){ @@ -73,11 +85,6 @@ public class UsersRessources { } return Response.ok(new UserSingle(user.name, user.pseudo, user.firstName)).build(); } - private Boolean checkUserCookie(String userCookie, UsersEntity users){ - String name = new String(Base64.decode(userCookie), StandardCharsets.UTF_8); - if(!name.equals(users.pseudo) && (!users.status)){ - return false; - } - return true; - } + + } From caf7f514cf942e2e7773da1de08cd3074c236a39 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sun, 15 May 2022 17:21:27 +0200 Subject: [PATCH 05/17] add swagger --- pom.xml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pom.xml b/pom.xml index b188d1d..853ed70 100644 --- a/pom.xml +++ b/pom.xml @@ -60,6 +60,10 @@ io.quarkus quarkus-security-jpa + + io.quarkus + quarkus-smallrye-openapi + io.quarkus quarkus-junit5 From 85eb18f518b93b50dbee3499e2982dc6791cc19a Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sun, 15 May 2022 18:03:47 +0200 Subject: [PATCH 06/17] Add column created_at and updated_at + PUT add --- src/main/java/com/covas/Entity/UsersEntity.java | 11 ++++++++--- .../java/com/covas/Resources/UsersRessources.java | 11 +++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/covas/Entity/UsersEntity.java b/src/main/java/com/covas/Entity/UsersEntity.java index 0d17bc8..15c114b 100644 --- a/src/main/java/com/covas/Entity/UsersEntity.java +++ b/src/main/java/com/covas/Entity/UsersEntity.java @@ -3,11 +3,9 @@ package com.covas.Entity; import java.time.LocalDate; import java.util.UUID; -import javax.annotation.Generated; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @@ -42,8 +40,13 @@ public class UsersEntity extends PanacheEntityBase { public Boolean status; @Column(nullable = false) public String password; + @Column(nullable = false) public String roles; - + @Column(nullable = false) + public LocalDate created_at; + @Column(nullable = false) + public LocalDate updated_at; + public static UsersEntity findByPseudo(String pseudo){ return find("pseudo", pseudo).firstResult(); } @@ -59,6 +62,8 @@ public class UsersEntity extends PanacheEntityBase { users.status = false; users.password = Hash.encryptSHA512(password); users.roles = roles; + users.created_at = LocalDate.now(); + users.updated_at = LocalDate.now(); users.persist(); } } \ No newline at end of file diff --git a/src/main/java/com/covas/Resources/UsersRessources.java b/src/main/java/com/covas/Resources/UsersRessources.java index 86cac89..64d978e 100644 --- a/src/main/java/com/covas/Resources/UsersRessources.java +++ b/src/main/java/com/covas/Resources/UsersRessources.java @@ -5,9 +5,11 @@ import java.util.UUID; import javax.annotation.security.RolesAllowed; import javax.inject.Inject; +import javax.transaction.Transactional; import javax.ws.rs.core.SecurityContext; import javax.ws.rs.CookieParam; import javax.ws.rs.GET; +import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; @@ -86,5 +88,14 @@ public class UsersRessources { return Response.ok(new UserSingle(user.name, user.pseudo, user.firstName)).build(); } + //PUT + + @PUT + @RolesAllowed("Admin") + @Transactional + public Response createUser(@Context SecurityContext ctx, @CookieParam("user") String userCookie){ + return Response.status(Response.Status.CREATED).build(); + } + } From dc355a7a296f5ca0d6a489639135b79ec731c463 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sun, 15 May 2022 18:10:51 +0200 Subject: [PATCH 07/17] change datetime to datetimelocal --- src/main/java/com/covas/Entity/UsersEntity.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/covas/Entity/UsersEntity.java b/src/main/java/com/covas/Entity/UsersEntity.java index 15c114b..8d08a75 100644 --- a/src/main/java/com/covas/Entity/UsersEntity.java +++ b/src/main/java/com/covas/Entity/UsersEntity.java @@ -1,6 +1,7 @@ package com.covas.Entity; import java.time.LocalDate; +import java.time.LocalDateTime; import java.util.UUID; import javax.persistence.Column; @@ -43,9 +44,9 @@ public class UsersEntity extends PanacheEntityBase { @Column(nullable = false) public String roles; @Column(nullable = false) - public LocalDate created_at; + public LocalDateTime created_at; @Column(nullable = false) - public LocalDate updated_at; + public LocalDateTime updated_at; public static UsersEntity findByPseudo(String pseudo){ return find("pseudo", pseudo).firstResult(); @@ -62,8 +63,8 @@ public class UsersEntity extends PanacheEntityBase { users.status = false; users.password = Hash.encryptSHA512(password); users.roles = roles; - users.created_at = LocalDate.now(); - users.updated_at = LocalDate.now(); + users.created_at = LocalDateTime.now(); + users.updated_at = LocalDateTime.now(); users.persist(); } } \ No newline at end of file From d9cfa03756d943db67054e2dc0e7dfc0d654725b Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sun, 15 May 2022 19:41:28 +0200 Subject: [PATCH 08/17] put http 75% wip --- .../com/covas/Resources/UsersRessources.java | 37 ++++++++++++++++++- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/covas/Resources/UsersRessources.java b/src/main/java/com/covas/Resources/UsersRessources.java index 64d978e..51ce3a8 100644 --- a/src/main/java/com/covas/Resources/UsersRessources.java +++ b/src/main/java/com/covas/Resources/UsersRessources.java @@ -1,12 +1,15 @@ package com.covas.Resources; import java.nio.charset.StandardCharsets; +import java.time.LocalDate; +import java.time.LocalDateTime; import java.util.UUID; 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.GET; import javax.ws.rs.PUT; @@ -17,6 +20,7 @@ import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; +import com.covas.Classes.Hash; import com.covas.Entity.UsersEntity; import com.covas.Json.UserSingle; @@ -92,9 +96,38 @@ public class UsersRessources { @PUT @RolesAllowed("Admin") + @Consumes(MediaType.APPLICATION_JSON) @Transactional - public Response createUser(@Context SecurityContext ctx, @CookieParam("user") String userCookie){ - return Response.status(Response.Status.CREATED).build(); + public Response createUser(@Context SecurityContext ctx, @CookieParam("user") String userCookie, UsersEntity users){ + if(!ctx.getUserPrincipal().getName().equals(jwt.getName())){ + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); + } + UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); + UsersEntity user = UsersEntity.findById(kid); + if(!checkUserCookie(userCookie, user)){ + return Response.status(Response.Status.FORBIDDEN).build(); + } + + UsersEntity usersOrig = UsersEntity.findByPseudo(users.pseudo); + if(usersOrig != null){ + return Response.status(Response.Status.CONFLICT).build(); + } + UsersEntity usersNew = new UsersEntity(); + usersNew.id = UUID.randomUUID(); + usersNew.name = users.name; + usersNew.pseudo = users.pseudo; + usersNew.firstName = users.firstName; + usersNew.birth = LocalDate.of(users.birth.getYear(), users.birth.getMonth(), users.birth.getDayOfMonth()); + usersNew.created_at = LocalDateTime.now(); + usersNew.updated_at = LocalDateTime.now(); + usersNew.password = Hash.encryptSHA512(Base64.toBase64String(users.password.getBytes(StandardCharsets.UTF_8))); + usersNew.roles = users.roles; + usersNew.status = false; + usersNew.persist(); + if(usersNew.isPersistent()){ + return Response.status(Response.Status.CREATED).build(); + } + return Response.status(Response.Status.NO_CONTENT).build(); } From 320eebefa5c8b8402925a31f5c18637fc50e003b Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sun, 15 May 2022 21:38:52 +0200 Subject: [PATCH 09/17] put work 100% --- src/main/java/com/covas/Resources/UsersRessources.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/covas/Resources/UsersRessources.java b/src/main/java/com/covas/Resources/UsersRessources.java index 51ce3a8..a927286 100644 --- a/src/main/java/com/covas/Resources/UsersRessources.java +++ b/src/main/java/com/covas/Resources/UsersRessources.java @@ -38,6 +38,9 @@ public class UsersRessources { ///Function private Boolean checkUserCookie(String userCookie, UsersEntity users){ + if(userCookie == null){ + return false; + } String name = new String(Base64.decode(userCookie), StandardCharsets.UTF_8); if(!name.equals(users.pseudo) && (!users.status)){ return false; @@ -113,10 +116,10 @@ public class UsersRessources { return Response.status(Response.Status.CONFLICT).build(); } UsersEntity usersNew = new UsersEntity(); - usersNew.id = UUID.randomUUID(); usersNew.name = users.name; usersNew.pseudo = users.pseudo; usersNew.firstName = users.firstName; + usersNew.email = users.email; usersNew.birth = LocalDate.of(users.birth.getYear(), users.birth.getMonth(), users.birth.getDayOfMonth()); usersNew.created_at = LocalDateTime.now(); usersNew.updated_at = LocalDateTime.now(); @@ -129,6 +132,4 @@ public class UsersRessources { } return Response.status(Response.Status.NO_CONTENT).build(); } - - } From 8e22daaff4e19bfdb811e85304b72517e978ae00 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Mon, 16 May 2022 22:55:52 +0200 Subject: [PATCH 10/17] changement error function put --- src/main/java/com/covas/Resources/UsersRessources.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/covas/Resources/UsersRessources.java b/src/main/java/com/covas/Resources/UsersRessources.java index a927286..71acd51 100644 --- a/src/main/java/com/covas/Resources/UsersRessources.java +++ b/src/main/java/com/covas/Resources/UsersRessources.java @@ -96,7 +96,6 @@ public class UsersRessources { } //PUT - @PUT @RolesAllowed("Admin") @Consumes(MediaType.APPLICATION_JSON) @@ -113,7 +112,7 @@ public class UsersRessources { UsersEntity usersOrig = UsersEntity.findByPseudo(users.pseudo); if(usersOrig != null){ - return Response.status(Response.Status.CONFLICT).build(); + return Response.status(Response.Status.UNAUTHORIZED).build(); } UsersEntity usersNew = new UsersEntity(); usersNew.name = users.name; @@ -132,4 +131,6 @@ public class UsersRessources { } return Response.status(Response.Status.NO_CONTENT).build(); } + + // Delete } From c461bae0d0bfbce00201570019f9f16183799aba Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Mon, 16 May 2022 23:19:27 +0200 Subject: [PATCH 11/17] add function delete --- .../com/covas/Resources/UsersRessources.java | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/main/java/com/covas/Resources/UsersRessources.java b/src/main/java/com/covas/Resources/UsersRessources.java index 71acd51..057ddb1 100644 --- a/src/main/java/com/covas/Resources/UsersRessources.java +++ b/src/main/java/com/covas/Resources/UsersRessources.java @@ -11,6 +11,7 @@ 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.PUT; import javax.ws.rs.Path; @@ -133,4 +134,50 @@ public class UsersRessources { } // Delete + @DELETE + @RolesAllowed("User") + @Transactional + public Response changeStatus(@Context SecurityContext ctx, @CookieParam("user") String userCookie){ + if(!ctx.getUserPrincipal().getName().equals(jwt.getName())){ + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); + } + UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); + UsersEntity user = UsersEntity.findById(kid); + if(!checkUserCookie(userCookie, user)){ + return Response.status(Response.Status.FORBIDDEN).build(); + } + + user.status = false; + user.persist(); + if(user.isPersistent()){ + return Response.ok().build(); + } + return Response.status(Response.Status.NOT_MODIFIED).build(); + } + + @DELETE + @Path("{id}") + @RolesAllowed("Admin") + @Transactional + public Response changeStatusSingleUser(@Context SecurityContext ctx, @CookieParam("user") String userCookie, @PathParam("id") String id){ + if(!ctx.getUserPrincipal().getName().equals(jwt.getName())){ + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); + } + UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); + UsersEntity user = UsersEntity.findById(kid); + if(!checkUserCookie(userCookie, user)){ + return Response.status(Response.Status.FORBIDDEN).build(); + } + + UsersEntity singleUser = UsersEntity.find("id", UUID.fromString(id)).firstResult(); + if(singleUser == null){ + return Response.status(Response.Status.NOT_FOUND).build(); + } + singleUser.status = false; + singleUser.persist(); + if(singleUser.isPersistent()){ + return Response.ok().build(); + } + return Response.status(Response.Status.NOT_MODIFIED).build(); + } } From 1ea2a40a0a8da93b43325d740a5d33abbe68ad65 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Wed, 18 May 2022 23:03:13 +0200 Subject: [PATCH 12/17] add private method + refactoring code --- .../com/covas/Resources/UsersRessources.java | 195 ++++++++++-------- 1 file changed, 110 insertions(+), 85 deletions(-) diff --git a/src/main/java/com/covas/Resources/UsersRessources.java b/src/main/java/com/covas/Resources/UsersRessources.java index 057ddb1..bc2a396 100644 --- a/src/main/java/com/covas/Resources/UsersRessources.java +++ b/src/main/java/com/covas/Resources/UsersRessources.java @@ -9,6 +9,7 @@ import javax.annotation.security.RolesAllowed; import javax.inject.Inject; import javax.transaction.Transactional; import javax.ws.rs.core.SecurityContext; +import javax.ws.rs.core.Response.Status; import javax.ws.rs.Consumes; import javax.ws.rs.CookieParam; import javax.ws.rs.DELETE; @@ -36,148 +37,172 @@ public class UsersRessources { private static final Logger LOGGER = Logger.getLogger(UsersRessources.class); @Inject JsonWebToken jwt; - - ///Function - private Boolean checkUserCookie(String userCookie, UsersEntity users){ - if(userCookie == null){ + + /// Function + private Boolean checkUserCookie(String userCookie, UsersEntity users) { + if (userCookie == null) { return false; } String name = new String(Base64.decode(userCookie), StandardCharsets.UTF_8); - if(!name.equals(users.pseudo) && (!users.status)){ + if (!name.equals(users.pseudo) && (!users.status)) { 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 @GET @RolesAllowed("Admin") - public Response getUsers(){ - return Response.ok(UsersEntity.listAll()).build(); + public Response getUsers(@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.getFamily().equals(Response.Status.Family.SUCCESSFUL)){ + responseHttp = Response.ok(UsersEntity.listAll()).build(); + } + return responseHttp; } @GET @RolesAllowed("Admin") @Path("{id}") - public Response getSingleUser(@PathParam("id") String id, @CookieParam("user") String userCookie, @Context SecurityContext ctx){ - if(!ctx.getUserPrincipal().getName().equals(jwt.getName())){ - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); - } + public Response getSingleUser(@PathParam("id") String id, @CookieParam("user") String userCookie, + @Context SecurityContext ctx) { UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); UsersEntity user = UsersEntity.findById(kid); - if(!checkUserCookie(userCookie, user)){ - return Response.status(Response.Status.FORBIDDEN).build(); + Response.Status status = getResponseCheck(ctx, userCookie, user); + Response responseHttp = Response.status(status).build(); + if (status.getFamily().equals(Response.Status.Family.SUCCESSFUL)) { + UUID uid = UUID.fromString(id); + UsersEntity users = UsersEntity.findById(uid); + responseHttp = Response.status(Response.Status.NOT_FOUND).build(); + if (users != null) { + responseHttp = Response.ok(users).build(); + } + } - UUID uid = UUID.fromString(id); - UsersEntity users = UsersEntity.findById(uid); - if(users == null){ - return Response.status(Response.Status.NOT_FOUND).build(); - } - return Response.ok(users).build(); + return responseHttp; } @GET @RolesAllowed("User") @Path("info") - public Response getInfoUser(@Context SecurityContext ctx, @CookieParam("user") String userCookie){ - if(!ctx.getUserPrincipal().getName().equals(jwt.getName())){ - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); - } + public Response getInfoUser(@Context SecurityContext ctx, @CookieParam("user") String userCookie) { UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); UsersEntity user = UsersEntity.findById(kid); - if (user == null){ - return Response.status(Response.Status.NOT_FOUND).build(); + Response.Status status = getResponseCheck(ctx, userCookie, user); + if (status.getFamily().equals(Response.Status.Family.SUCCESSFUL)) { + if (user == null) { + status = Response.Status.NOT_FOUND; + } + if (!checkUserCookie(userCookie, user)) { + status = Response.Status.FORBIDDEN; + } } - if(!checkUserCookie(userCookie, user)){ - return Response.status(Response.Status.FORBIDDEN).build(); + Response responseHttp = Response.status(status).build(); + if (status.getFamily().equals(Status.Family.SUCCESSFUL)) { + responseHttp = Response.status(status).entity(new UserSingle(user.name, user.pseudo, user.firstName)) + .build(); } - return Response.ok(new UserSingle(user.name, user.pseudo, user.firstName)).build(); + return responseHttp; } - //PUT + // PUT @PUT @RolesAllowed("Admin") @Consumes(MediaType.APPLICATION_JSON) @Transactional - public Response createUser(@Context SecurityContext ctx, @CookieParam("user") String userCookie, UsersEntity users){ - if(!ctx.getUserPrincipal().getName().equals(jwt.getName())){ - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); - } + public Response createUser(@Context SecurityContext ctx, @CookieParam("user") String userCookie, + UsersEntity users) { UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); UsersEntity user = UsersEntity.findById(kid); - if(!checkUserCookie(userCookie, user)){ - return Response.status(Response.Status.FORBIDDEN).build(); - } + Response.Status status = getResponseCheck(ctx, userCookie, user); - UsersEntity usersOrig = UsersEntity.findByPseudo(users.pseudo); - if(usersOrig != null){ - return Response.status(Response.Status.UNAUTHORIZED).build(); + if (status.getFamily().equals(Response.Status.Family.SUCCESSFUL)) { + + UsersEntity usersOrig = UsersEntity.findByPseudo(users.pseudo); + if (usersOrig != null) { + status = Response.Status.UNAUTHORIZED; + } else { + UsersEntity usersNew = new UsersEntity(); + usersNew.name = users.name; + usersNew.pseudo = users.pseudo; + usersNew.firstName = users.firstName; + usersNew.email = users.email; + usersNew.birth = LocalDate.of(users.birth.getYear(), users.birth.getMonth(), + users.birth.getDayOfMonth()); + usersNew.created_at = LocalDateTime.now(); + usersNew.updated_at = LocalDateTime.now(); + usersNew.password = Hash + .encryptSHA512(Base64.toBase64String(users.password.getBytes(StandardCharsets.UTF_8))); + usersNew.roles = users.roles; + usersNew.status = false; + usersNew.persist(); + if (usersNew.isPersistent()) { + status = Response.Status.CREATED; + } else { + status = Response.Status.NO_CONTENT; + } + } } - UsersEntity usersNew = new UsersEntity(); - usersNew.name = users.name; - usersNew.pseudo = users.pseudo; - usersNew.firstName = users.firstName; - usersNew.email = users.email; - usersNew.birth = LocalDate.of(users.birth.getYear(), users.birth.getMonth(), users.birth.getDayOfMonth()); - usersNew.created_at = LocalDateTime.now(); - usersNew.updated_at = LocalDateTime.now(); - usersNew.password = Hash.encryptSHA512(Base64.toBase64String(users.password.getBytes(StandardCharsets.UTF_8))); - usersNew.roles = users.roles; - usersNew.status = false; - usersNew.persist(); - if(usersNew.isPersistent()){ - return Response.status(Response.Status.CREATED).build(); - } - return Response.status(Response.Status.NO_CONTENT).build(); + return Response.status(status).build(); } // Delete @DELETE @RolesAllowed("User") @Transactional - public Response changeStatus(@Context SecurityContext ctx, @CookieParam("user") String userCookie){ - if(!ctx.getUserPrincipal().getName().equals(jwt.getName())){ - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); - } + public Response changeStatus(@Context SecurityContext ctx, @CookieParam("user") String userCookie) { UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); UsersEntity user = UsersEntity.findById(kid); - if(!checkUserCookie(userCookie, user)){ - return Response.status(Response.Status.FORBIDDEN).build(); - } - user.status = false; - user.persist(); - if(user.isPersistent()){ - return Response.ok().build(); + Response.Status status = getResponseCheck(ctx, userCookie, user); + + if (status.getFamily().equals(Response.Status.Family.SUCCESSFUL)) { + user.status = false; + user.persist(); + if (!user.isPersistent()) { + status = Response.Status.NOT_MODIFIED; + } } - return Response.status(Response.Status.NOT_MODIFIED).build(); + return Response.status(status).build(); } @DELETE @Path("{id}") @RolesAllowed("Admin") @Transactional - public Response changeStatusSingleUser(@Context SecurityContext ctx, @CookieParam("user") String userCookie, @PathParam("id") String id){ - if(!ctx.getUserPrincipal().getName().equals(jwt.getName())){ - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); - } + public Response changeStatusSingleUser(@Context SecurityContext ctx, @CookieParam("user") String userCookie, + @PathParam("id") String id) { UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); UsersEntity user = UsersEntity.findById(kid); - if(!checkUserCookie(userCookie, user)){ - return Response.status(Response.Status.FORBIDDEN).build(); + Response.Status status = getResponseCheck(ctx, userCookie, user); + if (status.getFamily().equals(Response.Status.Family.SUCCESSFUL)) { + UsersEntity singleUser = UsersEntity.find("id", UUID.fromString(id)).firstResult(); + if (singleUser == null) { + status = Response.Status.NOT_FOUND; + } + if (status.getFamily().equals(Response.Status.Family.SUCCESSFUL)) { + singleUser.status = false; + singleUser.persist(); + if (!singleUser.isPersistent()) { + status = Response.Status.NOT_MODIFIED; + } + } } - - UsersEntity singleUser = UsersEntity.find("id", UUID.fromString(id)).firstResult(); - if(singleUser == null){ - return Response.status(Response.Status.NOT_FOUND).build(); - } - singleUser.status = false; - singleUser.persist(); - if(singleUser.isPersistent()){ - return Response.ok().build(); - } - return Response.status(Response.Status.NOT_MODIFIED).build(); + return Response.status(status).build(); } } From 5cb64e205f0e0d5270f967c7726dc411d98063d7 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Thu, 19 May 2022 22:52:01 +0200 Subject: [PATCH 13/17] remove getfamily --- .../com/covas/Resources/UsersRessources.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/covas/Resources/UsersRessources.java b/src/main/java/com/covas/Resources/UsersRessources.java index bc2a396..2507fc9 100644 --- a/src/main/java/com/covas/Resources/UsersRessources.java +++ b/src/main/java/com/covas/Resources/UsersRessources.java @@ -70,7 +70,7 @@ public class UsersRessources { UsersEntity user = UsersEntity.findById(kid); Response.Status status = getResponseCheck(ctx, userCookie, user); Response responseHttp = Response.status(status).build(); - if(status.getFamily().equals(Response.Status.Family.SUCCESSFUL)){ + if(status.equals(Response.Status.OK)){ responseHttp = Response.ok(UsersEntity.listAll()).build(); } return responseHttp; @@ -85,7 +85,7 @@ public class UsersRessources { UsersEntity user = UsersEntity.findById(kid); Response.Status status = getResponseCheck(ctx, userCookie, user); Response responseHttp = Response.status(status).build(); - if (status.getFamily().equals(Response.Status.Family.SUCCESSFUL)) { + if (status.equals(Response.Status.OK)) { UUID uid = UUID.fromString(id); UsersEntity users = UsersEntity.findById(uid); responseHttp = Response.status(Response.Status.NOT_FOUND).build(); @@ -104,7 +104,7 @@ public class UsersRessources { UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); UsersEntity user = UsersEntity.findById(kid); Response.Status status = getResponseCheck(ctx, userCookie, user); - if (status.getFamily().equals(Response.Status.Family.SUCCESSFUL)) { + if (status.equals(Response.Status.OK)) { if (user == null) { status = Response.Status.NOT_FOUND; } @@ -113,7 +113,7 @@ public class UsersRessources { } } Response responseHttp = Response.status(status).build(); - if (status.getFamily().equals(Status.Family.SUCCESSFUL)) { + if (status.equals(Response.Status.OK)) { responseHttp = Response.status(status).entity(new UserSingle(user.name, user.pseudo, user.firstName)) .build(); } @@ -131,7 +131,7 @@ public class UsersRessources { UsersEntity user = UsersEntity.findById(kid); Response.Status status = getResponseCheck(ctx, userCookie, user); - if (status.getFamily().equals(Response.Status.Family.SUCCESSFUL)) { + if (status.equals(Response.Status.OK)) { UsersEntity usersOrig = UsersEntity.findByPseudo(users.pseudo); if (usersOrig != null) { @@ -171,7 +171,7 @@ public class UsersRessources { Response.Status status = getResponseCheck(ctx, userCookie, user); - if (status.getFamily().equals(Response.Status.Family.SUCCESSFUL)) { + if (status.equals(Response.Status.OK)) { user.status = false; user.persist(); if (!user.isPersistent()) { @@ -190,12 +190,12 @@ public class UsersRessources { UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); UsersEntity user = UsersEntity.findById(kid); Response.Status status = getResponseCheck(ctx, userCookie, user); - if (status.getFamily().equals(Response.Status.Family.SUCCESSFUL)) { + if (status.equals(Response.Status.OK)) { UsersEntity singleUser = UsersEntity.find("id", UUID.fromString(id)).firstResult(); if (singleUser == null) { status = Response.Status.NOT_FOUND; } - if (status.getFamily().equals(Response.Status.Family.SUCCESSFUL)) { + else { singleUser.status = false; singleUser.persist(); if (!singleUser.isPersistent()) { From 3e45ad224d66c477a3a86e7f80ac1f49df4a064e Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Mon, 23 May 2022 23:33:08 +0200 Subject: [PATCH 14/17] Add patch request --- .../com/covas/Resources/UsersRessources.java | 72 +++++++++++++++++-- 1 file changed, 67 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/covas/Resources/UsersRessources.java b/src/main/java/com/covas/Resources/UsersRessources.java index 2507fc9..68479f1 100644 --- a/src/main/java/com/covas/Resources/UsersRessources.java +++ b/src/main/java/com/covas/Resources/UsersRessources.java @@ -9,11 +9,11 @@ import javax.annotation.security.RolesAllowed; import javax.inject.Inject; import javax.transaction.Transactional; import javax.ws.rs.core.SecurityContext; -import javax.ws.rs.core.Response.Status; 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; @@ -40,7 +40,7 @@ public class UsersRessources { /// Function private Boolean checkUserCookie(String userCookie, UsersEntity users) { - if (userCookie == null) { + if ((userCookie == null) || (users == null)) { return false; } String name = new String(Base64.decode(userCookie), StandardCharsets.UTF_8); @@ -70,7 +70,7 @@ public class UsersRessources { UsersEntity user = UsersEntity.findById(kid); Response.Status status = getResponseCheck(ctx, userCookie, user); Response responseHttp = Response.status(status).build(); - if(status.equals(Response.Status.OK)){ + if (status.equals(Response.Status.OK)) { responseHttp = Response.ok(UsersEntity.listAll()).build(); } return responseHttp; @@ -194,8 +194,7 @@ public class UsersRessources { UsersEntity singleUser = UsersEntity.find("id", UUID.fromString(id)).firstResult(); if (singleUser == null) { status = Response.Status.NOT_FOUND; - } - else { + } else { singleUser.status = false; singleUser.persist(); if (!singleUser.isPersistent()) { @@ -205,4 +204,67 @@ public class UsersRessources { } return Response.status(status).build(); } + + // PATCH + @PATCH + @RolesAllowed("Admin") + @Consumes(MediaType.APPLICATION_JSON) + @Transactional + public Response updateUserAdmin(@Context SecurityContext ctx, @CookieParam("user") String userCookie, + UsersEntity users) { + 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.findByPseudo(users.pseudo); + if (usersOrig == null) { + status = Response.Status.NOT_FOUND; + } else { + usersOrig.name = users.name; + usersOrig.firstName = users.firstName; + usersOrig.email = users.email; + usersOrig.birth = LocalDate.of(users.birth.getYear(), users.birth.getMonth(), + users.birth.getDayOfMonth()); + usersOrig.updated_at = LocalDateTime.now(); + usersOrig.password = Hash + .encryptSHA512(Base64.toBase64String(users.password.getBytes(StandardCharsets.UTF_8))); + usersOrig.roles = users.roles; + usersOrig.persist(); + if (!usersOrig.isPersistent()) { + status = Response.Status.NOT_MODIFIED; + } + } + } + return Response.status(status).build(); + } + + @PATCH + @RolesAllowed("User") + @Consumes(MediaType.APPLICATION_JSON) + @Transactional + public Response updateSingleUser(@Context SecurityContext ctx, @CookieParam("user") String userCookie, + UsersEntity users) { + 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.name = users.name; + user.firstName = users.firstName; + user.email = users.email; + user.birth = LocalDate.of(users.birth.getYear(), users.birth.getMonth(), + users.birth.getDayOfMonth()); + user.updated_at = LocalDateTime.now(); + user.password = Hash.encryptSHA512(Base64.toBase64String(users.password.getBytes(StandardCharsets.UTF_8))); + user.persist(); + if (!user.isPersistent()) { + status = Response.Status.NOT_MODIFIED; + } + } + + return Response.status(status).build(); + } } From e3dbd0b5d18b94c60224952dbb0e1a2fa5217eb9 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Thu, 26 May 2022 22:52:32 +0200 Subject: [PATCH 15/17] add column active mail --- src/main/java/com/covas/Entity/UsersEntity.java | 2 ++ src/main/java/com/covas/Resources/UsersRessources.java | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/covas/Entity/UsersEntity.java b/src/main/java/com/covas/Entity/UsersEntity.java index 8d08a75..ce8979f 100644 --- a/src/main/java/com/covas/Entity/UsersEntity.java +++ b/src/main/java/com/covas/Entity/UsersEntity.java @@ -39,6 +39,8 @@ public class UsersEntity extends PanacheEntityBase { public LocalDate birth; @ColumnDefault("false") public Boolean status; + @ColumnDefault("false") + public Boolean active_mail; @Column(nullable = false) public String password; @Column(nullable = false) diff --git a/src/main/java/com/covas/Resources/UsersRessources.java b/src/main/java/com/covas/Resources/UsersRessources.java index 68479f1..a857ef2 100644 --- a/src/main/java/com/covas/Resources/UsersRessources.java +++ b/src/main/java/com/covas/Resources/UsersRessources.java @@ -150,6 +150,7 @@ public class UsersRessources { .encryptSHA512(Base64.toBase64String(users.password.getBytes(StandardCharsets.UTF_8))); usersNew.roles = users.roles; usersNew.status = false; + usersNew.active_mail = true; usersNew.persist(); if (usersNew.isPersistent()) { status = Response.Status.CREATED; @@ -264,7 +265,6 @@ public class UsersRessources { status = Response.Status.NOT_MODIFIED; } } - return Response.status(status).build(); } } From b48f2bc73a18175d7e6748d3ab2febd87986d188 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Fri, 27 May 2022 23:14:05 +0200 Subject: [PATCH 16/17] add post function --- .../com/covas/Resources/UsersRessources.java | 38 ++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/covas/Resources/UsersRessources.java b/src/main/java/com/covas/Resources/UsersRessources.java index a857ef2..525749d 100644 --- a/src/main/java/com/covas/Resources/UsersRessources.java +++ b/src/main/java/com/covas/Resources/UsersRessources.java @@ -14,6 +14,7 @@ import javax.ws.rs.CookieParam; import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.PATCH; +import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; @@ -120,12 +121,47 @@ public class UsersRessources { return responseHttp; } + // POST + @POST + @Consumes(MediaType.APPLICATION_JSON) + @Transactional + public Response createUser(UsersEntity users) { + Response.Status status = Response.Status.OK; + UsersEntity usersOrig = UsersEntity.findByPseudo(users.pseudo); + if (usersOrig != null) { + status = Response.Status.UNAUTHORIZED; + } else { + UsersEntity usersNew = new UsersEntity(); + usersNew.name = users.name; + usersNew.pseudo = users.pseudo; + usersNew.firstName = users.firstName; + usersNew.email = users.email; + usersNew.birth = LocalDate.of(users.birth.getYear(), users.birth.getMonth(), + users.birth.getDayOfMonth()); + usersNew.created_at = LocalDateTime.now(); + usersNew.updated_at = LocalDateTime.now(); + usersNew.password = Hash + .encryptSHA512(Base64.toBase64String(users.password.getBytes(StandardCharsets.UTF_8))); + usersNew.roles = users.roles; + usersNew.status = false; + usersNew.active_mail = false; + usersNew.persist(); + if (usersNew.isPersistent()) { + status = Response.Status.CREATED; + } else { + status = Response.Status.NO_CONTENT; + } + } + return Response.status(status).build(); + } + + // PUT @PUT @RolesAllowed("Admin") @Consumes(MediaType.APPLICATION_JSON) @Transactional - public Response createUser(@Context SecurityContext ctx, @CookieParam("user") String userCookie, + public Response addUser(@Context SecurityContext ctx, @CookieParam("user") String userCookie, UsersEntity users) { UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); UsersEntity user = UsersEntity.findById(kid); From 9ef8bb92ffdc778617ec02b7af6624fd7ffa16df Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sat, 28 May 2022 09:58:59 +0200 Subject: [PATCH 17/17] add coluln deleted_at and update uri for patch --- src/main/java/com/covas/Entity/UsersEntity.java | 2 ++ .../java/com/covas/Resources/UsersRessources.java | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/covas/Entity/UsersEntity.java b/src/main/java/com/covas/Entity/UsersEntity.java index ce8979f..1f1d2e9 100644 --- a/src/main/java/com/covas/Entity/UsersEntity.java +++ b/src/main/java/com/covas/Entity/UsersEntity.java @@ -49,6 +49,8 @@ public class UsersEntity extends PanacheEntityBase { public LocalDateTime created_at; @Column(nullable = false) public LocalDateTime updated_at; + @ColumnDefault("null") + public LocalDateTime deleted_at; public static UsersEntity findByPseudo(String pseudo){ return find("pseudo", pseudo).firstResult(); diff --git a/src/main/java/com/covas/Resources/UsersRessources.java b/src/main/java/com/covas/Resources/UsersRessources.java index 525749d..9392a36 100644 --- a/src/main/java/com/covas/Resources/UsersRessources.java +++ b/src/main/java/com/covas/Resources/UsersRessources.java @@ -158,10 +158,11 @@ public class UsersRessources { // PUT @PUT + @Path("{pseudo}") @RolesAllowed("Admin") @Consumes(MediaType.APPLICATION_JSON) @Transactional - public Response addUser(@Context SecurityContext ctx, @CookieParam("user") String userCookie, + public Response addUser(@Context SecurityContext ctx, @CookieParam("user") String userCookie, @PathParam("pseudo") String pseudo, UsersEntity users) { UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); UsersEntity user = UsersEntity.findById(kid); @@ -169,13 +170,13 @@ public class UsersRessources { if (status.equals(Response.Status.OK)) { - UsersEntity usersOrig = UsersEntity.findByPseudo(users.pseudo); + UsersEntity usersOrig = UsersEntity.findByPseudo(pseudo); if (usersOrig != null) { status = Response.Status.UNAUTHORIZED; } else { UsersEntity usersNew = new UsersEntity(); usersNew.name = users.name; - usersNew.pseudo = users.pseudo; + usersNew.pseudo = pseudo; usersNew.firstName = users.firstName; usersNew.email = users.email; usersNew.birth = LocalDate.of(users.birth.getYear(), users.birth.getMonth(), @@ -210,6 +211,8 @@ public class UsersRessources { if (status.equals(Response.Status.OK)) { user.status = false; + user.updated_at = LocalDateTime.now(); + user.deleted_at = LocalDateTime.now(); user.persist(); if (!user.isPersistent()) { status = Response.Status.NOT_MODIFIED; @@ -233,6 +236,8 @@ public class UsersRessources { status = Response.Status.NOT_FOUND; } else { singleUser.status = false; + singleUser.updated_at = LocalDateTime.now(); + singleUser.deleted_at = LocalDateTime.now(); singleUser.persist(); if (!singleUser.isPersistent()) { status = Response.Status.NOT_MODIFIED; @@ -268,6 +273,10 @@ public class UsersRessources { usersOrig.password = Hash .encryptSHA512(Base64.toBase64String(users.password.getBytes(StandardCharsets.UTF_8))); usersOrig.roles = users.roles; + if(users.status){ + usersOrig.deleted_at = null; + usersOrig.status = true; + } usersOrig.persist(); if (!usersOrig.isPersistent()) { status = Response.Status.NOT_MODIFIED;