From b0044d6200ac36e9fd7b6f9399022ae2942910d1 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Fri, 30 Dec 2022 17:32:03 +0100 Subject: [PATCH 01/18] Ajout table profil --- .../java/com/covas/Entity/ProfilEntity.java | 40 +++++++++++++++++++ .../java/com/covas/Entity/UsersEntity.java | 4 ++ src/main/resources/application.properties | 4 +- 3 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/covas/Entity/ProfilEntity.java diff --git a/src/main/java/com/covas/Entity/ProfilEntity.java b/src/main/java/com/covas/Entity/ProfilEntity.java new file mode 100644 index 0000000..72643db --- /dev/null +++ b/src/main/java/com/covas/Entity/ProfilEntity.java @@ -0,0 +1,40 @@ +package com.covas.Entity; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.util.UUID; + +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 { + @Id + @Column(name = "id") + @GeneratedValue(generator = "UUID") + @GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator") + public UUID id; + + @Column(columnDefinition="TEXT") + public String description; + + @OneToOne(cascade = CascadeType.ALL) + @JoinColumn(name = "users_id", referencedColumnName = "id") + public UsersEntity users; +} \ No newline at end of file diff --git a/src/main/java/com/covas/Entity/UsersEntity.java b/src/main/java/com/covas/Entity/UsersEntity.java index 08843f7..a6ec00b 100644 --- a/src/main/java/com/covas/Entity/UsersEntity.java +++ b/src/main/java/com/covas/Entity/UsersEntity.java @@ -11,6 +11,7 @@ import javax.persistence.Id; import javax.persistence.NamedQueries; import javax.persistence.NamedQuery; import javax.persistence.Table; +import javax.persistence.OneToOne; import com.covas.Classes.Hash; @@ -69,6 +70,9 @@ public class UsersEntity extends PanacheEntityBase { @ColumnDefault("null") public LocalDateTime connected_at; + @OneToOne(mappedBy = "users") + public ProfilEntity profile; + public static UsersEntity findByPseudo(String pseudo){ return find("pseudo", pseudo).firstResult(); } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 360aaec..34a392f 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -10,8 +10,8 @@ quarkus.datasource.username = ${POSTGRES_USER:default} quarkus.datasource.password = ${POSTGRES_PASSWORD:default} quarkus.datasource.jdbc.url = jdbc:postgresql://${POSTGRES_URL:localhost}:${POSTGRES_PORT:5432}/${POSTGRES_DB:default} # drop and create the database at startup (use `update` to only update the schema) -#quarkus.hibernate-orm.database.generation = drop-and-create -quarkus.hibernate-orm.database.generation = update +quarkus.hibernate-orm.database.generation = drop-and-create +#quarkus.hibernate-orm.database.generation = update covas.schema.create = true quarkus.mailer.auth-methods=DIGEST-MD5 CRAM-SHA256 CRAM-SHA1 CRAM-MD5 PLAIN LOGIN From 535a89f321b6347c5aacb2b2cf931c02d93c8181 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Mon, 2 Jan 2023 22:34:08 +0100 Subject: [PATCH 02/18] fix error --- src/main/java/com/covas/Entity/ProfilEntity.java | 3 +++ src/main/java/com/covas/Entity/UsersEntity.java | 2 ++ 2 files changed, 5 insertions(+) diff --git a/src/main/java/com/covas/Entity/ProfilEntity.java b/src/main/java/com/covas/Entity/ProfilEntity.java index 72643db..5307856 100644 --- a/src/main/java/com/covas/Entity/ProfilEntity.java +++ b/src/main/java/com/covas/Entity/ProfilEntity.java @@ -34,6 +34,9 @@ public class ProfilEntity extends PanacheEntityBase { @Column(columnDefinition="TEXT") public String description; + @Column(nullable = false) + public LocalDateTime updated_at; + @OneToOne(cascade = CascadeType.ALL) @JoinColumn(name = "users_id", referencedColumnName = "id") public UsersEntity users; diff --git a/src/main/java/com/covas/Entity/UsersEntity.java b/src/main/java/com/covas/Entity/UsersEntity.java index a6ec00b..bb28c2e 100644 --- a/src/main/java/com/covas/Entity/UsersEntity.java +++ b/src/main/java/com/covas/Entity/UsersEntity.java @@ -94,6 +94,8 @@ public class UsersEntity extends PanacheEntityBase { users.roles = roles; users.created_at = LocalDateTime.now(); users.updated_at = LocalDateTime.now(); + users.persist(); + } } \ No newline at end of file From 36cf9f5631356608801ee71aaf37185a48496a3b Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Mon, 2 Jan 2023 23:08:16 +0100 Subject: [PATCH 03/18] add function base for profile resources --- .../covas/Resources/ProfileRessources.java | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/main/java/com/covas/Resources/ProfileRessources.java diff --git a/src/main/java/com/covas/Resources/ProfileRessources.java b/src/main/java/com/covas/Resources/ProfileRessources.java new file mode 100644 index 0000000..7a5c970 --- /dev/null +++ b/src/main/java/com/covas/Resources/ProfileRessources.java @@ -0,0 +1,49 @@ +package com.covas.Resources; + + +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.SecurityContext; +import org.eclipse.microprofile.jwt.JsonWebToken; +import java.nio.charset.StandardCharsets; + +import org.postgresql.shaded.com.ongres.scram.common.bouncycastle.base64.Base64; + +import javax.inject.Inject; +import com.covas.Entity.UsersEntity; +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; + } + + +} \ No newline at end of file From df15b0d19e2630930fad5b47e62ff15c2b42fa58 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Tue, 3 Jan 2023 17:26:44 +0100 Subject: [PATCH 04/18] add GET for profile --- .../java/com/covas/Json/ProfileSingle.java | 15 ++++ .../covas/Resources/ProfileRessources.java | 68 ++++++++++++++++++- 2 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/covas/Json/ProfileSingle.java diff --git a/src/main/java/com/covas/Json/ProfileSingle.java b/src/main/java/com/covas/Json/ProfileSingle.java new file mode 100644 index 0000000..789c4ad --- /dev/null +++ b/src/main/java/com/covas/Json/ProfileSingle.java @@ -0,0 +1,15 @@ +package com.covas.Json; + +import io.quarkus.runtime.annotations.RegisterForReflection; + +@RegisterForReflection +public class ProfileSingle { + + public final String description; + + public ProfileSingle(String description){ + this.description = description; + + } + +} diff --git a/src/main/java/com/covas/Resources/ProfileRessources.java b/src/main/java/com/covas/Resources/ProfileRessources.java index 7a5c970..58ffeef 100644 --- a/src/main/java/com/covas/Resources/ProfileRessources.java +++ b/src/main/java/com/covas/Resources/ProfileRessources.java @@ -1,17 +1,28 @@ 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; @@ -45,5 +56,60 @@ public class ProfileRessources { 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 From 50db0ab1f2298a175ba9c467706c884a1414fb88 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Thu, 5 Jan 2023 20:12:23 +0100 Subject: [PATCH 05/18] 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; From d42dc014b652481cfbb242e8f1d2dd91b99bf8db Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Thu, 5 Jan 2023 20:51:27 +0100 Subject: [PATCH 06/18] add onetomany --- src/main/java/com/covas/Entity/PublisherEntity.java | 7 ++++--- src/main/java/com/covas/Entity/UsersEntity.java | 10 ++++++---- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/covas/Entity/PublisherEntity.java b/src/main/java/com/covas/Entity/PublisherEntity.java index f0fc80a..a79c483 100644 --- a/src/main/java/com/covas/Entity/PublisherEntity.java +++ b/src/main/java/com/covas/Entity/PublisherEntity.java @@ -1,5 +1,6 @@ package com.covas.Entity; +import java.io.Serializable; import java.time.LocalDateTime; import java.util.UUID; @@ -8,8 +9,8 @@ import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; -import javax.persistence.OneToOne; import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; import javax.persistence.CascadeType; @@ -19,7 +20,7 @@ import io.quarkus.hibernate.orm.panache.PanacheEntityBase; @Entity @Table(name = "publisher") -public class PublisherEntity extends PanacheEntityBase { +public class PublisherEntity extends PanacheEntityBase implements Serializable { @Id @Column(name = "id") @GeneratedValue(generator = "UUID") @@ -32,7 +33,7 @@ public class PublisherEntity extends PanacheEntityBase { @Column(nullable = false) public LocalDateTime updated_at; - @OneToOne(cascade = CascadeType.ALL) + @ManyToOne(cascade = CascadeType.ALL) @JoinColumn(name = "users_id", referencedColumnName = "id") public UsersEntity users; } \ No newline at end of file diff --git a/src/main/java/com/covas/Entity/UsersEntity.java b/src/main/java/com/covas/Entity/UsersEntity.java index 92c3af1..e8a5a14 100644 --- a/src/main/java/com/covas/Entity/UsersEntity.java +++ b/src/main/java/com/covas/Entity/UsersEntity.java @@ -1,7 +1,9 @@ package com.covas.Entity; +import java.io.Serializable; import java.time.LocalDate; import java.time.LocalDateTime; +import java.util.Collection; import java.util.UUID; import javax.persistence.Column; @@ -10,8 +12,8 @@ import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.NamedQueries; import javax.persistence.NamedQuery; +import javax.persistence.OneToMany; import javax.persistence.Table; -import javax.persistence.OneToOne; import com.covas.Classes.Hash; @@ -38,7 +40,7 @@ import io.quarkus.hibernate.orm.panache.PanacheEntityBase; @NamedQuery(name = "Users.bySearchandStatus", query = "from UsersEntity u where (u.pseudo like :search or u.name like :search or u.firstName like :search) and u.status = :status"), @NamedQuery(name = "Users.bySearchandRolesandStatus", query = "from UsersEntity u where (u.pseudo like :search or u.name like :search or u.firstName like :search) and u.roles = :roles and u.status = :status") }) -public class UsersEntity extends PanacheEntityBase { +public class UsersEntity extends PanacheEntityBase implements Serializable { @Id @Column(name = "id") @GeneratedValue(generator = "UUID") @@ -72,8 +74,8 @@ public class UsersEntity extends PanacheEntityBase { public String description; - @OneToOne(mappedBy = "users") - public PublisherEntity publisher; + @OneToMany(mappedBy = "users") + public Collection publisher; public static UsersEntity findByPseudo(String pseudo){ return find("pseudo", pseudo).firstResult(); From 4b4b9ae27771c3dae569c9590c5f069ed4bb9b4b Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Thu, 5 Jan 2023 21:16:52 +0100 Subject: [PATCH 07/18] add column for publisher --- .../java/com/covas/Entity/PublisherEntity.java | 16 +++++++++++++++- src/main/java/com/covas/Enum/Type.java | 6 ++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/covas/Enum/Type.java diff --git a/src/main/java/com/covas/Entity/PublisherEntity.java b/src/main/java/com/covas/Entity/PublisherEntity.java index a79c483..1a877a7 100644 --- a/src/main/java/com/covas/Entity/PublisherEntity.java +++ b/src/main/java/com/covas/Entity/PublisherEntity.java @@ -6,6 +6,8 @@ import java.util.UUID; import javax.persistence.Column; import javax.persistence.Entity; +import javax.persistence.EnumType; +import javax.persistence.Enumerated; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @@ -13,8 +15,11 @@ import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.CascadeType; - +import org.hibernate.annotations.ColumnDefault; import org.hibernate.annotations.GenericGenerator; + +import com.covas.Enum.Type; + import io.quarkus.hibernate.orm.panache.PanacheEntityBase; @@ -30,8 +35,17 @@ public class PublisherEntity extends PanacheEntityBase implements Serializable { @Column(columnDefinition="TEXT") public String description; + @Enumerated(EnumType.STRING) + public Type type; + + public String url; + + @Column(nullable = false) + public LocalDateTime created_at; @Column(nullable = false) public LocalDateTime updated_at; + @ColumnDefault("null") + public LocalDateTime deleted_at; @ManyToOne(cascade = CascadeType.ALL) @JoinColumn(name = "users_id", referencedColumnName = "id") diff --git a/src/main/java/com/covas/Enum/Type.java b/src/main/java/com/covas/Enum/Type.java new file mode 100644 index 0000000..e9d6839 --- /dev/null +++ b/src/main/java/com/covas/Enum/Type.java @@ -0,0 +1,6 @@ +package com.covas.Enum; + +public enum Type { + VIDEO, PHOTO, TEXT, URL + +} From af49540afd5b1eae32c99c5c630547fb82a0bc69 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Thu, 5 Jan 2023 21:17:16 +0100 Subject: [PATCH 08/18] remove class profileSingle --- src/main/java/com/covas/Json/ProfileSingle.java | 15 --------------- 1 file changed, 15 deletions(-) delete mode 100644 src/main/java/com/covas/Json/ProfileSingle.java diff --git a/src/main/java/com/covas/Json/ProfileSingle.java b/src/main/java/com/covas/Json/ProfileSingle.java deleted file mode 100644 index 789c4ad..0000000 --- a/src/main/java/com/covas/Json/ProfileSingle.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.covas.Json; - -import io.quarkus.runtime.annotations.RegisterForReflection; - -@RegisterForReflection -public class ProfileSingle { - - public final String description; - - public ProfileSingle(String description){ - this.description = description; - - } - -} From b1cd8c08fdf16154c435a2d862180c6fefec7e4d Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Thu, 5 Jan 2023 21:26:35 +0100 Subject: [PATCH 09/18] add resources publisher --- .../covas/Resources/PublisherRessources.java | 500 ++++++++++++++++++ 1 file changed, 500 insertions(+) create mode 100644 src/main/java/com/covas/Resources/PublisherRessources.java diff --git a/src/main/java/com/covas/Resources/PublisherRessources.java b/src/main/java/com/covas/Resources/PublisherRessources.java new file mode 100644 index 0000000..92ccc07 --- /dev/null +++ b/src/main/java/com/covas/Resources/PublisherRessources.java @@ -0,0 +1,500 @@ +package com.covas.Resources; + +import java.nio.charset.StandardCharsets; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.util.List; +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.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.POST; +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.Classes.Hash; +import com.covas.Entity.UsersEntity; +import com.covas.Json.UserExist; +import com.covas.Json.UserSingle; + +import io.quarkus.panache.common.Page; +import io.quarkus.panache.common.Parameters; + +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("publisher") +public class PublisherRessources { + 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 getUsers(@CookieParam("user") String userCookie, @Context SecurityContext ctx, + @QueryParam("page") Integer page, @QueryParam("nbPages") Integer nbPages, + @QueryParam("status") Short status, @QueryParam("roles") String roles, + @QueryParam("email") String email, @QueryParam("search") String search, + @QueryParam("uuid") String uuid) { + 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 listUsers = UsersEntity.findAll().page(Page.of(page, nbPages)).list(); + + if((roles != null) && (status == null)){ + listUsers = UsersEntity.find("#Users.byRoles", Parameters.with("roles",roles)).page(Page.of(page, nbPages)).list(); + } + if((roles == null) && (status != null)){ + listUsers = UsersEntity.find("#Users.byStatus", Parameters.with("status",status)).page(Page.of(page, nbPages)).list(); + } + if((roles != null) && (status != null)){ + listUsers = UsersEntity.find("#Users.byRolesandStatus", Parameters.with("roles",roles).and("status", status)).page(Page.of(page, nbPages)).list(); + } + responseHttp = Response.ok(listUsers).build(); + if(uuid != null){ + UsersEntity userSingle = UsersEntity.findById(UUID.fromString(uuid)); + if((roles != null) && (status == null)){ + userSingle = UsersEntity.find("#Users.byUUIDandRoles", Parameters.with("id", UUID.fromString(uuid)).and("roles",roles)).firstResult(); + } + if((roles == null) && (status != null)){ + userSingle = UsersEntity.find("#Users.byUUIDandStatus", Parameters.with("id", UUID.fromString(uuid)).and("status",status)).firstResult(); + } + if((roles != null) && (status != null)){ + userSingle = UsersEntity.find("#Users.byUUIDandRolesandStatus", Parameters.with("id", UUID.fromString(uuid)).and("status",status).and("roles", roles)).firstResult(); + } + responseHttp = Response.ok(userSingle).build(); + + } + if(email != null){ + UsersEntity userSingle = UsersEntity.findByEmail(email); + if((roles != null) && (status == null)){ + userSingle = UsersEntity.find("#Users.byEmailandRoles", Parameters.with("email", email).and("roles",roles)).firstResult(); + } + if((roles == null) && (status != null)){ + userSingle = UsersEntity.find("#Users.byEmailandStatus", Parameters.with("email", email).and("status",status)).firstResult(); + } + if((roles != null) && (status != null)){ + userSingle = UsersEntity.find("#Users.byEmailandRolesandStatus", Parameters.with("email", email).and("status",status).and("roles", roles)).firstResult(); + } + responseHttp = Response.ok(userSingle).build(); + } + if(search != null){ + List usersList = UsersEntity.find("#Users.bySearch", Parameters.with("search", search)).page(Page.of(page, nbPages)).list(); + if((roles != null) && (status == null)){ + usersList = UsersEntity.find("#Users.bySearchandRoles", Parameters.with("search", search).and("roles",roles)).page(Page.of(page, nbPages)).list(); + } + if((roles == null) && (status != null)){ + usersList = UsersEntity.find("#Users.bySearchandStatus", Parameters.with("search", search).and("status",status)).page(Page.of(page, nbPages)).list(); + } + if((roles != null) && (status != null)){ + usersList = UsersEntity.find("#Users.bySearchandRolesandStatus", Parameters.with("search", search).and("status",status).and("roles", roles)).page(Page.of(page, nbPages)).list(); + } + responseHttp = Response.ok(usersList).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(UsersEntity.count()).build(); + } + return responseHttp; + } + + @GET + @RolesAllowed("Admin") + @Path("{id}") + 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); + 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); + responseHttp = Response.status(Response.Status.NOT_FOUND).build(); + if (users != null) { + responseHttp = Response.ok(users).build(); + } + + } + return responseHttp; + } + + @GET + @RolesAllowed("User") + @Path("info") + public Response getInfoUser(@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)) { + if (user == null) { + status = Response.Status.NOT_FOUND; + } + if (!checkUserCookie(userCookie, user)) { + status = Response.Status.FORBIDDEN; + } + } + 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, user.description)) + .build(); + } + return responseHttp; + } + + // POST + @POST + @Consumes(MediaType.APPLICATION_JSON) + @Transactional + public Response createUser(UsersEntity users) { + Response.Status status = Response.Status.OK; + UsersEntity usersPseudo = UsersEntity.findByPseudo(users.pseudo); + UsersEntity usersEmail = UsersEntity.findByEmail(users.email); + Boolean createUserBool = true; + Boolean pseudoExist = false; + Boolean emailExist = false; + if (usersPseudo != null) { + createUserBool = false; + status = Response.Status.UNAUTHORIZED; + pseudoExist = true; + } + if(usersEmail != null){ + createUserBool = false; + status = Response.Status.UNAUTHORIZED; + emailExist = true; + } + if(createUserBool) { + 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 = 2; + usersNew.persist(); + if (usersNew.isPersistent()) { + status = Response.Status.CREATED; + } else { + status = Response.Status.NO_CONTENT; + } + } + return Response.status(status).entity(new UserExist(emailExist, pseudoExist)).build(); + } + + + // PUT + @PUT + @RolesAllowed("Admin") + @Consumes(MediaType.APPLICATION_JSON) + @Transactional + 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); + Response.Status status = getResponseCheck(ctx, userCookie, user); + Boolean pseudoExist = false; + Boolean emailExist = false; + + if (status.equals(Response.Status.OK)) { + + UsersEntity usersPseudo = UsersEntity.findByPseudo(users.pseudo); + UsersEntity usersEmail = UsersEntity.findByEmail(users.email); + Boolean createUserBool = true; + + if (usersPseudo != null) { + createUserBool = false; + status = Response.Status.UNAUTHORIZED; + pseudoExist = true; + } + if(usersEmail != null){ + createUserBool = false; + status = Response.Status.UNAUTHORIZED; + emailExist = true; + } + if(createUserBool) { + 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 = 1; + usersNew.persist(); + if (usersNew.isPersistent()) { + status = Response.Status.CREATED; + } else { + status = Response.Status.NO_CONTENT; + } + } + } + return Response.status(status).entity(new UserExist(emailExist, pseudoExist)).build(); + } + + // Delete + @DELETE + @RolesAllowed("User") + @Transactional + 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 = -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("{id}") + @RolesAllowed("Admin") + @Transactional + 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); + 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 = 0; + 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(); + } + + // PATCH + @PATCH + @RolesAllowed("Admin") + @Consumes(MediaType.APPLICATION_JSON) + @Transactional + @Path("{id}") + public Response updateUserAdmin(@Context SecurityContext ctx, @CookieParam("user") String userCookie, + UsersEntity users, @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 usersOrig = UsersEntity.findById(UUID.fromString(id)); + 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(); + if(users.password.length() > 0){ + usersOrig.password = Hash + .encryptSHA512(Base64.toBase64String(users.password.getBytes(StandardCharsets.UTF_8))); + } + usersOrig.roles = users.roles; + if(users.status == 1){ + usersOrig.deleted_at = null; + usersOrig.status = 1; + } + usersOrig.persist(); + if (!usersOrig.isPersistent()) { + status = Response.Status.NOT_MODIFIED; + } + } + } + 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, @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 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) + @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 dc0428f7b553aa1a15f90b29e564ce94cbd35c3b Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sat, 7 Jan 2023 20:21:55 +0100 Subject: [PATCH 10/18] ajout section sur le statut de la pulication --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index bbc54ea..9488908 100644 --- a/README.md +++ b/README.md @@ -10,3 +10,13 @@ C'est la partie backend du projet COVAS généré par le générateur fourni par 2 : en attente de confirmation +## Statut de la publicatiob + +-1 : suppression +0 : desactivé +1 : activé +2 : censuré + + + + From 3485fbb646ce4cb08c693f7fb58b4f30905d62cc Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sat, 7 Jan 2023 21:36:39 +0100 Subject: [PATCH 11/18] get publishers 75% --- .../com/covas/Entity/PublisherEntity.java | 8 ++ .../java/com/covas/Json/PublisherSingle.java | 21 ++++++ .../covas/Resources/PublisherRessources.java | 74 +++++-------------- 3 files changed, 48 insertions(+), 55 deletions(-) create mode 100644 src/main/java/com/covas/Json/PublisherSingle.java diff --git a/src/main/java/com/covas/Entity/PublisherEntity.java b/src/main/java/com/covas/Entity/PublisherEntity.java index 1a877a7..95c3df0 100644 --- a/src/main/java/com/covas/Entity/PublisherEntity.java +++ b/src/main/java/com/covas/Entity/PublisherEntity.java @@ -13,6 +13,8 @@ import javax.persistence.Id; import javax.persistence.Table; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; +import javax.persistence.NamedQueries; +import javax.persistence.NamedQuery; import javax.persistence.CascadeType; import org.hibernate.annotations.ColumnDefault; @@ -25,6 +27,9 @@ import io.quarkus.hibernate.orm.panache.PanacheEntityBase; @Entity @Table(name = "publisher") +@NamedQueries({ +@NamedQuery(name = "Publishers.bySearch", query = "from PublisherEntity u where u.description like :description"), +}) public class PublisherEntity extends PanacheEntityBase implements Serializable { @Id @Column(name = "id") @@ -47,6 +52,9 @@ public class PublisherEntity extends PanacheEntityBase implements Serializable { @ColumnDefault("null") public LocalDateTime deleted_at; + @ColumnDefault("1") + public Short status; + @ManyToOne(cascade = CascadeType.ALL) @JoinColumn(name = "users_id", referencedColumnName = "id") public UsersEntity users; diff --git a/src/main/java/com/covas/Json/PublisherSingle.java b/src/main/java/com/covas/Json/PublisherSingle.java new file mode 100644 index 0000000..339ee46 --- /dev/null +++ b/src/main/java/com/covas/Json/PublisherSingle.java @@ -0,0 +1,21 @@ +package com.covas.Json; + +import io.quarkus.runtime.annotations.RegisterForReflection; + +@RegisterForReflection +public class PublisherSingle { + + public final String pseudo; + public final String text; + public final Short status; + public final String url; + + public PublisherSingle(String pseudo, String text, Short status, String url){ + this.pseudo = pseudo; + this.text = text; + this.status = status; + this.url = url; + + } + +} diff --git a/src/main/java/com/covas/Resources/PublisherRessources.java b/src/main/java/com/covas/Resources/PublisherRessources.java index 92ccc07..ab2167a 100644 --- a/src/main/java/com/covas/Resources/PublisherRessources.java +++ b/src/main/java/com/covas/Resources/PublisherRessources.java @@ -27,7 +27,9 @@ import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import com.covas.Classes.Hash; +import com.covas.Entity.PublisherEntity; import com.covas.Entity.UsersEntity; +import com.covas.Json.PublisherSingle; import com.covas.Json.UserExist; import com.covas.Json.UserSingle; @@ -75,8 +77,8 @@ public class PublisherRessources { @RolesAllowed("Admin") public Response getUsers(@CookieParam("user") String userCookie, @Context SecurityContext ctx, @QueryParam("page") Integer page, @QueryParam("nbPages") Integer nbPages, - @QueryParam("status") Short status, @QueryParam("roles") String roles, - @QueryParam("email") String email, @QueryParam("search") String search, + @QueryParam("status") Short status, + @QueryParam("search") String search, @QueryParam("uuid") String uuid) { if(nbPages == null){ nbPages = 20; @@ -89,57 +91,19 @@ public class PublisherRessources { Response.Status statusHttp = getResponseCheck(ctx, userCookie, user); Response responseHttp = Response.status(statusHttp).build(); if (statusHttp.equals(Response.Status.OK)) { - List listUsers = UsersEntity.findAll().page(Page.of(page, nbPages)).list(); + List listPublishers = PublisherEntity.findAll().page(Page.of(page, nbPages)).list(); - if((roles != null) && (status == null)){ - listUsers = UsersEntity.find("#Users.byRoles", Parameters.with("roles",roles)).page(Page.of(page, nbPages)).list(); - } - if((roles == null) && (status != null)){ - listUsers = UsersEntity.find("#Users.byStatus", Parameters.with("status",status)).page(Page.of(page, nbPages)).list(); - } - if((roles != null) && (status != null)){ - listUsers = UsersEntity.find("#Users.byRolesandStatus", Parameters.with("roles",roles).and("status", status)).page(Page.of(page, nbPages)).list(); - } - responseHttp = Response.ok(listUsers).build(); + + responseHttp = Response.ok(listPublishers).build(); if(uuid != null){ - UsersEntity userSingle = UsersEntity.findById(UUID.fromString(uuid)); - if((roles != null) && (status == null)){ - userSingle = UsersEntity.find("#Users.byUUIDandRoles", Parameters.with("id", UUID.fromString(uuid)).and("roles",roles)).firstResult(); - } - if((roles == null) && (status != null)){ - userSingle = UsersEntity.find("#Users.byUUIDandStatus", Parameters.with("id", UUID.fromString(uuid)).and("status",status)).firstResult(); - } - if((roles != null) && (status != null)){ - userSingle = UsersEntity.find("#Users.byUUIDandRolesandStatus", Parameters.with("id", UUID.fromString(uuid)).and("status",status).and("roles", roles)).firstResult(); - } - responseHttp = Response.ok(userSingle).build(); - - } - if(email != null){ - UsersEntity userSingle = UsersEntity.findByEmail(email); - if((roles != null) && (status == null)){ - userSingle = UsersEntity.find("#Users.byEmailandRoles", Parameters.with("email", email).and("roles",roles)).firstResult(); - } - if((roles == null) && (status != null)){ - userSingle = UsersEntity.find("#Users.byEmailandStatus", Parameters.with("email", email).and("status",status)).firstResult(); - } - if((roles != null) && (status != null)){ - userSingle = UsersEntity.find("#Users.byEmailandRolesandStatus", Parameters.with("email", email).and("status",status).and("roles", roles)).firstResult(); - } - responseHttp = Response.ok(userSingle).build(); + PublisherEntity publisherSingle = PublisherEntity.findById(UUID.fromString(uuid)); + responseHttp = Response.ok(publisherSingle).build(); } + if(search != null){ - List usersList = UsersEntity.find("#Users.bySearch", Parameters.with("search", search)).page(Page.of(page, nbPages)).list(); - if((roles != null) && (status == null)){ - usersList = UsersEntity.find("#Users.bySearchandRoles", Parameters.with("search", search).and("roles",roles)).page(Page.of(page, nbPages)).list(); - } - if((roles == null) && (status != null)){ - usersList = UsersEntity.find("#Users.bySearchandStatus", Parameters.with("search", search).and("status",status)).page(Page.of(page, nbPages)).list(); - } - if((roles != null) && (status != null)){ - usersList = UsersEntity.find("#Users.bySearchandRolesandStatus", Parameters.with("search", search).and("status",status).and("roles", roles)).page(Page.of(page, nbPages)).list(); - } - responseHttp = Response.ok(usersList).build(); + List publishersList = PublisherEntity.find("#Publisher.bySearch", Parameters.with("description", search)).page(Page.of(page, nbPages)).list(); + + responseHttp = Response.ok(publishersList).build(); } } @@ -155,7 +119,7 @@ public class PublisherRessources { Response.Status status = getResponseCheck(ctx, userCookie, user); Response responseHttp = Response.status(status).build(); if (status.equals(Response.Status.OK)){ - responseHttp = Response.ok(UsersEntity.count()).build(); + responseHttp = Response.ok(PublisherEntity.count()).build(); } return responseHttp; } @@ -171,10 +135,10 @@ public class PublisherRessources { Response responseHttp = Response.status(status).build(); if (status.equals(Response.Status.OK)) { UUID uid = UUID.fromString(id); - UsersEntity users = UsersEntity.findById(uid); + PublisherEntity publishers = PublisherEntity.findById(uid); responseHttp = Response.status(Response.Status.NOT_FOUND).build(); - if (users != null) { - responseHttp = Response.ok(users).build(); + if (publishers != null) { + responseHttp = Response.ok(publishers).build(); } } @@ -198,8 +162,8 @@ public class PublisherRessources { } 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, user.description)) - .build(); + // responseHttp = Response.status(status).entity(new PublisherSingle(user.name, user.pseudo, user.firstName, user.description)) + //.build(); } return responseHttp; } From d0b6cee3ea4e2c30e65c741dc852e06266701920 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sat, 7 Jan 2023 21:40:06 +0100 Subject: [PATCH 12/18] change name of function --- src/main/java/com/covas/Resources/PublisherRessources.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/covas/Resources/PublisherRessources.java b/src/main/java/com/covas/Resources/PublisherRessources.java index ab2167a..5d08820 100644 --- a/src/main/java/com/covas/Resources/PublisherRessources.java +++ b/src/main/java/com/covas/Resources/PublisherRessources.java @@ -75,7 +75,7 @@ public class PublisherRessources { /// GET @GET @RolesAllowed("Admin") - public Response getUsers(@CookieParam("user") String userCookie, @Context SecurityContext ctx, + public Response getPublishers(@CookieParam("user") String userCookie, @Context SecurityContext ctx, @QueryParam("page") Integer page, @QueryParam("nbPages") Integer nbPages, @QueryParam("status") Short status, @QueryParam("search") String search, @@ -127,7 +127,7 @@ public class PublisherRessources { @GET @RolesAllowed("Admin") @Path("{id}") - public Response getSingleUser(@PathParam("id") String id, @CookieParam("user") String userCookie, + public Response getSinglePublisher(@PathParam("id") String id, @CookieParam("user") String userCookie, @Context SecurityContext ctx) { UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); UsersEntity user = UsersEntity.findById(kid); @@ -148,7 +148,7 @@ public class PublisherRessources { @GET @RolesAllowed("User") @Path("info") - public Response getInfoUser(@Context SecurityContext ctx, @CookieParam("user") String userCookie) { + public Response getInfoPublisher(@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); From 7f9a149196421bd7a00ff0ca9cd63f9d81eb74ac Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sat, 7 Jan 2023 21:46:22 +0100 Subject: [PATCH 13/18] update delete for publisher admin --- .../covas/Resources/PublisherRessources.java | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/covas/Resources/PublisherRessources.java b/src/main/java/com/covas/Resources/PublisherRessources.java index 5d08820..07756c7 100644 --- a/src/main/java/com/covas/Resources/PublisherRessources.java +++ b/src/main/java/com/covas/Resources/PublisherRessources.java @@ -294,21 +294,21 @@ public class PublisherRessources { @Path("{id}") @RolesAllowed("Admin") @Transactional - public Response changeStatusSingleUserToDelete(@Context SecurityContext ctx, @CookieParam("user") String userCookie, + public Response changeStatusSinglePublisherToDelete(@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) { + PublisherEntity singlePublisher = PublisherEntity.find("id", UUID.fromString(id)).firstResult(); + if (singlePublisher == 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()) { + singlePublisher.status = -1; + singlePublisher.updated_at = LocalDateTime.now(); + singlePublisher.deleted_at = LocalDateTime.now(); + singlePublisher.persist(); + if (!singlePublisher.isPersistent()) { status = Response.Status.NOT_MODIFIED; } } @@ -344,21 +344,21 @@ public class PublisherRessources { @Path("/disable/{id}") @RolesAllowed("Admin") @Transactional - public Response changeStatusSingleUserToDisable(@Context SecurityContext ctx, @CookieParam("user") String userCookie, + public Response changeStatusSinglePublisherToDisable(@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) { + PublisherEntity singlePublisher = PublisherEntity.find("id", UUID.fromString(id)).firstResult(); + if (singlePublisher == null) { status = Response.Status.NOT_FOUND; } else { - singleUser.status = 0; - singleUser.updated_at = LocalDateTime.now(); - singleUser.deleted_at = LocalDateTime.now(); - singleUser.persist(); - if (!singleUser.isPersistent()) { + singlePublisher.status = 0; + singlePublisher.updated_at = LocalDateTime.now(); + singlePublisher.deleted_at = LocalDateTime.now(); + singlePublisher.persist(); + if (!singlePublisher.isPersistent()) { status = Response.Status.NOT_MODIFIED; } } From 5b30166cbf7a279a103044e097d51096c876f9e6 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sat, 7 Jan 2023 22:02:00 +0100 Subject: [PATCH 14/18] add patch publisher --- .../covas/Resources/PublisherRessources.java | 45 +++++++++---------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/covas/Resources/PublisherRessources.java b/src/main/java/com/covas/Resources/PublisherRessources.java index 07756c7..cbb5164 100644 --- a/src/main/java/com/covas/Resources/PublisherRessources.java +++ b/src/main/java/com/covas/Resources/PublisherRessources.java @@ -372,35 +372,30 @@ public class PublisherRessources { @Consumes(MediaType.APPLICATION_JSON) @Transactional @Path("{id}") - public Response updateUserAdmin(@Context SecurityContext ctx, @CookieParam("user") String userCookie, - UsersEntity users, @PathParam("id") String id) { + public Response updatePublisherAdmin(@Context SecurityContext ctx, @CookieParam("user") String userCookie, + PublisherEntity publishers, @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 usersOrig = UsersEntity.findById(UUID.fromString(id)); - if (usersOrig == null) { + PublisherEntity publishersOrig = PublisherEntity.findById(UUID.fromString(id)); + if (publishersOrig == 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(); - if(users.password.length() > 0){ - usersOrig.password = Hash - .encryptSHA512(Base64.toBase64String(users.password.getBytes(StandardCharsets.UTF_8))); + publishersOrig.description = publishers.description; + publishersOrig.url = publishers.url; + publishersOrig.type = publishers.type; + + publishersOrig.updated_at = LocalDateTime.now(); + + if(publishers.status == 1){ + publishersOrig.deleted_at = null; + publishersOrig.status = 1; } - usersOrig.roles = users.roles; - if(users.status == 1){ - usersOrig.deleted_at = null; - usersOrig.status = 1; - } - usersOrig.persist(); - if (!usersOrig.isPersistent()) { + publishersOrig.persist(); + if (!publishersOrig.isPersistent()) { status = Response.Status.NOT_MODIFIED; } } @@ -421,13 +416,13 @@ public class PublisherRessources { Response.Status status = getResponseCheck(ctx, userCookie, user); if (status.equals(Response.Status.OK)) { - UsersEntity usersOrig = UsersEntity.findById(UUID.fromString(id)); - if (usersOrig == null) { + PublisherEntity publishersOrig = PublisherEntity.findById(UUID.fromString(id)); + if (publishersOrig == null) { status = Response.Status.NOT_FOUND; } else { - usersOrig.status = 1; - usersOrig.persist(); - if (!usersOrig.isPersistent()) { + publishersOrig.status = 1; + publishersOrig.persist(); + if (!publishersOrig.isPersistent()) { status = Response.Status.NOT_MODIFIED; } } From e97a50b635a46c43ae3a17b898a8142a07ec7551 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sun, 8 Jan 2023 11:48:33 +0100 Subject: [PATCH 15/18] get list publishers for a user --- .../com/covas/Entity/PublisherEntity.java | 5 +++++ .../java/com/covas/Json/PublisherByUser.java | 22 +++++++++++++++++++ .../java/com/covas/Json/PublisherSingle.java | 21 ------------------ .../covas/Resources/PublisherRessources.java | 8 +++---- 4 files changed, 30 insertions(+), 26 deletions(-) create mode 100644 src/main/java/com/covas/Json/PublisherByUser.java delete mode 100644 src/main/java/com/covas/Json/PublisherSingle.java diff --git a/src/main/java/com/covas/Entity/PublisherEntity.java b/src/main/java/com/covas/Entity/PublisherEntity.java index 95c3df0..5310c46 100644 --- a/src/main/java/com/covas/Entity/PublisherEntity.java +++ b/src/main/java/com/covas/Entity/PublisherEntity.java @@ -2,6 +2,7 @@ package com.covas.Entity; import java.io.Serializable; import java.time.LocalDateTime; +import java.util.List; import java.util.UUID; import javax.persistence.Column; @@ -58,4 +59,8 @@ public class PublisherEntity extends PanacheEntityBase implements Serializable { @ManyToOne(cascade = CascadeType.ALL) @JoinColumn(name = "users_id", referencedColumnName = "id") public UsersEntity users; + + public static List findByUsers(String uuid){ + return find("users_id", uuid).list(); + } } \ No newline at end of file diff --git a/src/main/java/com/covas/Json/PublisherByUser.java b/src/main/java/com/covas/Json/PublisherByUser.java new file mode 100644 index 0000000..f5a215c --- /dev/null +++ b/src/main/java/com/covas/Json/PublisherByUser.java @@ -0,0 +1,22 @@ +package com.covas.Json; + +import java.util.Collection; + +import com.covas.Entity.PublisherEntity; + +import io.quarkus.runtime.annotations.RegisterForReflection; + +@RegisterForReflection +public class PublisherByUser { + + public final String pseudo; + public final Collection publishers; + + public PublisherByUser(String pseudo, Collection publishers){ + this.pseudo = pseudo; + this.publishers = publishers; + + + } + +} diff --git a/src/main/java/com/covas/Json/PublisherSingle.java b/src/main/java/com/covas/Json/PublisherSingle.java deleted file mode 100644 index 339ee46..0000000 --- a/src/main/java/com/covas/Json/PublisherSingle.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.covas.Json; - -import io.quarkus.runtime.annotations.RegisterForReflection; - -@RegisterForReflection -public class PublisherSingle { - - public final String pseudo; - public final String text; - public final Short status; - public final String url; - - public PublisherSingle(String pseudo, String text, Short status, String url){ - this.pseudo = pseudo; - this.text = text; - this.status = status; - this.url = url; - - } - -} diff --git a/src/main/java/com/covas/Resources/PublisherRessources.java b/src/main/java/com/covas/Resources/PublisherRessources.java index cbb5164..8782c35 100644 --- a/src/main/java/com/covas/Resources/PublisherRessources.java +++ b/src/main/java/com/covas/Resources/PublisherRessources.java @@ -10,7 +10,6 @@ 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; @@ -29,9 +28,8 @@ import javax.ws.rs.core.Response; import com.covas.Classes.Hash; import com.covas.Entity.PublisherEntity; import com.covas.Entity.UsersEntity; -import com.covas.Json.PublisherSingle; +import com.covas.Json.PublisherByUser; import com.covas.Json.UserExist; -import com.covas.Json.UserSingle; import io.quarkus.panache.common.Page; import io.quarkus.panache.common.Parameters; @@ -162,8 +160,8 @@ public class PublisherRessources { } Response responseHttp = Response.status(status).build(); if (status.equals(Response.Status.OK)) { - // responseHttp = Response.status(status).entity(new PublisherSingle(user.name, user.pseudo, user.firstName, user.description)) - //.build(); + responseHttp = Response.status(status).entity(new PublisherByUser(user.pseudo, user.publisher)) + .build(); } return responseHttp; } From a71d6562ca86ce651fe084e065022253f3311a4d Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sun, 8 Jan 2023 11:59:29 +0100 Subject: [PATCH 16/18] Remove function useless --- .../covas/Resources/PublisherRessources.java | 79 +------------------ 1 file changed, 4 insertions(+), 75 deletions(-) diff --git a/src/main/java/com/covas/Resources/PublisherRessources.java b/src/main/java/com/covas/Resources/PublisherRessources.java index 8782c35..bb9ca51 100644 --- a/src/main/java/com/covas/Resources/PublisherRessources.java +++ b/src/main/java/com/covas/Resources/PublisherRessources.java @@ -267,30 +267,9 @@ public class PublisherRessources { } // Delete - @DELETE - @RolesAllowed("User") - @Transactional - 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 = -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("{id}") - @RolesAllowed("Admin") + @RolesAllowed({"Admin", "User"}) @Transactional public Response changeStatusSinglePublisherToDelete(@Context SecurityContext ctx, @CookieParam("user") String userCookie, @PathParam("id") String id) { @@ -314,33 +293,9 @@ public class PublisherRessources { 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") + @RolesAllowed({"Admin", "User"}) @Transactional public Response changeStatusSinglePublisherToDisable(@Context SecurityContext ctx, @CookieParam("user") String userCookie, @PathParam("id") String id) { @@ -366,7 +321,7 @@ public class PublisherRessources { // PATCH @PATCH - @RolesAllowed("Admin") + @RolesAllowed({"Admin", "User"}) @Consumes(MediaType.APPLICATION_JSON) @Transactional @Path("{id}") @@ -404,7 +359,7 @@ public class PublisherRessources { @PATCH - @RolesAllowed("Admin") + @RolesAllowed({"Admin", "User"}) @Consumes(MediaType.APPLICATION_JSON) @Transactional @Path("enable/{id}") @@ -428,30 +383,4 @@ public class PublisherRessources { 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 44dc820a30415a4d336d4b4b1b25676da26dad09 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sun, 8 Jan 2023 12:14:02 +0100 Subject: [PATCH 17/18] POST publisher --- .../covas/Resources/PublisherRessources.java | 53 ++++++------------- 1 file changed, 17 insertions(+), 36 deletions(-) diff --git a/src/main/java/com/covas/Resources/PublisherRessources.java b/src/main/java/com/covas/Resources/PublisherRessources.java index bb9ca51..cd0397c 100644 --- a/src/main/java/com/covas/Resources/PublisherRessources.java +++ b/src/main/java/com/covas/Resources/PublisherRessources.java @@ -170,45 +170,27 @@ public class PublisherRessources { @POST @Consumes(MediaType.APPLICATION_JSON) @Transactional - public Response createUser(UsersEntity users) { - Response.Status status = Response.Status.OK; - UsersEntity usersPseudo = UsersEntity.findByPseudo(users.pseudo); - UsersEntity usersEmail = UsersEntity.findByEmail(users.email); - Boolean createUserBool = true; - Boolean pseudoExist = false; - Boolean emailExist = false; - if (usersPseudo != null) { - createUserBool = false; - status = Response.Status.UNAUTHORIZED; - pseudoExist = true; - } - if(usersEmail != null){ - createUserBool = false; - status = Response.Status.UNAUTHORIZED; - emailExist = true; - } - if(createUserBool) { - 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 = 2; - usersNew.persist(); - if (usersNew.isPersistent()) { + public Response createPublisher(@Context SecurityContext ctx, @CookieParam("user") String userCookie, PublisherEntity publishers) { + + UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); + UsersEntity user = UsersEntity.findById(kid); + Response.Status status = getResponseCheck(ctx, userCookie, user); + PublisherEntity newPublisher = new PublisherEntity(); + if (status.equals(Response.Status.OK)) { + newPublisher.description = publishers.description; + newPublisher.type = publishers.type; + newPublisher.url = publishers.url; + newPublisher.users = user; + newPublisher.created_at = LocalDateTime.now(); + newPublisher.updated_at = LocalDateTime.now(); + newPublisher.persist(); + if (newPublisher.isPersistent()) { status = Response.Status.CREATED; } else { status = Response.Status.NO_CONTENT; } - } - return Response.status(status).entity(new UserExist(emailExist, pseudoExist)).build(); + } + return Response.status(status).entity(newPublisher).build(); } @@ -382,5 +364,4 @@ public class PublisherRessources { } return Response.status(status).build(); } - } From fb48935bbb4027c08c780d54ee27a9a6e56fb0b6 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sun, 8 Jan 2023 12:23:51 +0100 Subject: [PATCH 18/18] replace POST to PUT --- .../covas/Resources/PublisherRessources.java | 61 ++----------------- 1 file changed, 4 insertions(+), 57 deletions(-) diff --git a/src/main/java/com/covas/Resources/PublisherRessources.java b/src/main/java/com/covas/Resources/PublisherRessources.java index cd0397c..4b694d2 100644 --- a/src/main/java/com/covas/Resources/PublisherRessources.java +++ b/src/main/java/com/covas/Resources/PublisherRessources.java @@ -166,9 +166,10 @@ public class PublisherRessources { return responseHttp; } - // POST - @POST + // PUT + @PUT @Consumes(MediaType.APPLICATION_JSON) + @RolesAllowed({"Admin", "User"}) @Transactional public Response createPublisher(@Context SecurityContext ctx, @CookieParam("user") String userCookie, PublisherEntity publishers) { @@ -194,61 +195,7 @@ public class PublisherRessources { } - // PUT - @PUT - @RolesAllowed("Admin") - @Consumes(MediaType.APPLICATION_JSON) - @Transactional - 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); - Response.Status status = getResponseCheck(ctx, userCookie, user); - Boolean pseudoExist = false; - Boolean emailExist = false; - - if (status.equals(Response.Status.OK)) { - - UsersEntity usersPseudo = UsersEntity.findByPseudo(users.pseudo); - UsersEntity usersEmail = UsersEntity.findByEmail(users.email); - Boolean createUserBool = true; - - if (usersPseudo != null) { - createUserBool = false; - status = Response.Status.UNAUTHORIZED; - pseudoExist = true; - } - if(usersEmail != null){ - createUserBool = false; - status = Response.Status.UNAUTHORIZED; - emailExist = true; - } - if(createUserBool) { - 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 = 1; - usersNew.persist(); - if (usersNew.isPersistent()) { - status = Response.Status.CREATED; - } else { - status = Response.Status.NO_CONTENT; - } - } - } - return Response.status(status).entity(new UserExist(emailExist, pseudoExist)).build(); - } - - // Delete + // DELETE @DELETE @Path("{id}") @RolesAllowed({"Admin", "User"})