From 50db0ab1f2298a175ba9c467706c884a1414fb88 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Thu, 5 Jan 2023 20:12:23 +0100 Subject: [PATCH] Add table publisher and add column description for table users --- ...ProfilEntity.java => PublisherEntity.java} | 9 +- .../java/com/covas/Entity/UsersEntity.java | 5 +- src/main/java/com/covas/Json/UserSingle.java | 4 +- .../covas/Resources/ProfileRessources.java | 115 ------------------ .../com/covas/Resources/UsersRessources.java | 2 +- 5 files changed, 10 insertions(+), 125 deletions(-) rename src/main/java/com/covas/Entity/{ProfilEntity.java => PublisherEntity.java} (78%) delete mode 100644 src/main/java/com/covas/Resources/ProfileRessources.java diff --git a/src/main/java/com/covas/Entity/ProfilEntity.java b/src/main/java/com/covas/Entity/PublisherEntity.java similarity index 78% rename from src/main/java/com/covas/Entity/ProfilEntity.java rename to src/main/java/com/covas/Entity/PublisherEntity.java index 5307856..f0fc80a 100644 --- a/src/main/java/com/covas/Entity/ProfilEntity.java +++ b/src/main/java/com/covas/Entity/PublisherEntity.java @@ -1,6 +1,5 @@ package com.covas.Entity; -import java.time.LocalDate; import java.time.LocalDateTime; import java.util.UUID; @@ -8,23 +7,19 @@ import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; -import javax.persistence.NamedQueries; -import javax.persistence.NamedQuery; import javax.persistence.Table; import javax.persistence.OneToOne; import javax.persistence.JoinColumn; import javax.persistence.CascadeType; -import com.covas.Classes.Hash; -import org.hibernate.annotations.ColumnDefault; import org.hibernate.annotations.GenericGenerator; import io.quarkus.hibernate.orm.panache.PanacheEntityBase; @Entity -@Table(name = "profile") -public class ProfilEntity extends PanacheEntityBase { +@Table(name = "publisher") +public class PublisherEntity extends PanacheEntityBase { @Id @Column(name = "id") @GeneratedValue(generator = "UUID") diff --git a/src/main/java/com/covas/Entity/UsersEntity.java b/src/main/java/com/covas/Entity/UsersEntity.java index bb28c2e..92c3af1 100644 --- a/src/main/java/com/covas/Entity/UsersEntity.java +++ b/src/main/java/com/covas/Entity/UsersEntity.java @@ -70,8 +70,10 @@ public class UsersEntity extends PanacheEntityBase { @ColumnDefault("null") public LocalDateTime connected_at; + public String description; + @OneToOne(mappedBy = "users") - public ProfilEntity profile; + public PublisherEntity publisher; public static UsersEntity findByPseudo(String pseudo){ return find("pseudo", pseudo).firstResult(); @@ -94,6 +96,7 @@ public class UsersEntity extends PanacheEntityBase { users.roles = roles; users.created_at = LocalDateTime.now(); users.updated_at = LocalDateTime.now(); + users.description = ""; users.persist(); diff --git a/src/main/java/com/covas/Json/UserSingle.java b/src/main/java/com/covas/Json/UserSingle.java index ef003d0..7563049 100644 --- a/src/main/java/com/covas/Json/UserSingle.java +++ b/src/main/java/com/covas/Json/UserSingle.java @@ -8,11 +8,13 @@ public class UserSingle { public final String name; public final String pseudo; public final String firstname; + public final String description; - public UserSingle(String name, String pseudo, String firstname){ + public UserSingle(String name, String pseudo, String firstname, String description){ this.name = name; this.pseudo = pseudo; this.firstname = firstname; + this.description = description; } diff --git a/src/main/java/com/covas/Resources/ProfileRessources.java b/src/main/java/com/covas/Resources/ProfileRessources.java deleted file mode 100644 index 58ffeef..0000000 --- a/src/main/java/com/covas/Resources/ProfileRessources.java +++ /dev/null @@ -1,115 +0,0 @@ -package com.covas.Resources; - -import javax.annotation.security.RolesAllowed; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; -import javax.ws.rs.GET; -import javax.ws.rs.core.SecurityContext; -import javax.ws.rs.PathParam; -import javax.ws.rs.core.SecurityContext; -import javax.ws.rs.CookieParam; -import javax.ws.rs.core.Context; -import org.eclipse.microprofile.jwt.JsonWebToken; -import java.nio.charset.StandardCharsets; -import java.util.UUID; -import org.eclipse.microprofile.jwt.Claims; - -import com.covas.Json.ProfileSingle; - - -import org.postgresql.shaded.com.ongres.scram.common.bouncycastle.base64.Base64; - -import javax.inject.Inject; -import com.covas.Entity.UsersEntity; -import com.covas.Entity.ProfilEntity; -import javax.ws.rs.Path; - - - -@Produces(MediaType.APPLICATION_JSON) -@Path("profile") -public class ProfileRessources { - - @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; - } - - // GET - - @GET - @RolesAllowed("Admin") - @Path("{id}") - public Response getSingleProfile(@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)) { - UUID uid = UUID.fromString(id); - UsersEntity users = UsersEntity.findById(uid); - ProfilEntity profile = users.profile; - responseHttp = Response.status(Response.Status.NOT_FOUND).build(); - if (profile != null) { - responseHttp = Response.ok(users).build(); - } - - } - return responseHttp; - } - - @GET - @RolesAllowed("User") - public Response getInfoProfile(@Context SecurityContext ctx, @CookieParam("user") String userCookie) { - UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); - UsersEntity user = UsersEntity.findById(kid); - String description = ""; - Response.Status status = getResponseCheck(ctx, userCookie, user); - if (status.equals(Response.Status.OK)) { - if (user == null) { - status = Response.Status.NOT_FOUND; - } - if (!checkUserCookie(userCookie, user)) { - status = Response.Status.FORBIDDEN; - } - if (user != null){ - ProfilEntity profile = user.profile; - if(profile == null){ - status = Response.Status.NOT_FOUND; - } else { - description = profile.description; - } - } - } - Response responseHttp = Response.status(status).build(); - if (status.equals(Response.Status.OK)) { - responseHttp = Response.status(status).entity(new ProfileSingle(description)) - .build(); - } - return responseHttp; - } - - -} \ 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 e2e4199..30c9fbe 100644 --- a/src/main/java/com/covas/Resources/UsersRessources.java +++ b/src/main/java/com/covas/Resources/UsersRessources.java @@ -198,7 +198,7 @@ public class UsersRessources { } Response responseHttp = Response.status(status).build(); if (status.equals(Response.Status.OK)) { - responseHttp = Response.status(status).entity(new UserSingle(user.name, user.pseudo, user.firstName)) + responseHttp = Response.status(status).entity(new UserSingle(user.name, user.pseudo, user.firstName, user.description)) .build(); } return responseHttp;