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é + + + + diff --git a/src/main/java/com/covas/Entity/PublisherEntity.java b/src/main/java/com/covas/Entity/PublisherEntity.java new file mode 100644 index 0000000..5310c46 --- /dev/null +++ b/src/main/java/com/covas/Entity/PublisherEntity.java @@ -0,0 +1,66 @@ +package com.covas.Entity; + +import java.io.Serializable; +import java.time.LocalDateTime; +import java.util.List; +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; +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; +import org.hibernate.annotations.GenericGenerator; + +import com.covas.Enum.Type; + +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") + @GeneratedValue(generator = "UUID") + @GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator") + public UUID id; + + @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; + + @ColumnDefault("1") + public Short status; + + @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/Entity/UsersEntity.java b/src/main/java/com/covas/Entity/UsersEntity.java index 08843f7..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,6 +12,7 @@ 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 com.covas.Classes.Hash; @@ -37,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") @@ -69,6 +72,11 @@ public class UsersEntity extends PanacheEntityBase { @ColumnDefault("null") public LocalDateTime connected_at; + public String description; + + @OneToMany(mappedBy = "users") + public Collection publisher; + public static UsersEntity findByPseudo(String pseudo){ return find("pseudo", pseudo).firstResult(); } @@ -90,6 +98,9 @@ public class UsersEntity extends PanacheEntityBase { users.roles = roles; users.created_at = LocalDateTime.now(); users.updated_at = LocalDateTime.now(); + users.description = ""; + users.persist(); + } } \ No newline at end of file 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 + +} 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/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/PublisherRessources.java b/src/main/java/com/covas/Resources/PublisherRessources.java new file mode 100644 index 0000000..4b694d2 --- /dev/null +++ b/src/main/java/com/covas/Resources/PublisherRessources.java @@ -0,0 +1,314 @@ +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.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.PublisherEntity; +import com.covas.Entity.UsersEntity; +import com.covas.Json.PublisherByUser; +import com.covas.Json.UserExist; + +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 getPublishers(@CookieParam("user") String userCookie, @Context SecurityContext ctx, + @QueryParam("page") Integer page, @QueryParam("nbPages") Integer nbPages, + @QueryParam("status") Short status, + @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 listPublishers = PublisherEntity.findAll().page(Page.of(page, nbPages)).list(); + + + responseHttp = Response.ok(listPublishers).build(); + if(uuid != null){ + PublisherEntity publisherSingle = PublisherEntity.findById(UUID.fromString(uuid)); + responseHttp = Response.ok(publisherSingle).build(); + } + + if(search != null){ + List publishersList = PublisherEntity.find("#Publisher.bySearch", Parameters.with("description", search)).page(Page.of(page, nbPages)).list(); + + responseHttp = Response.ok(publishersList).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(PublisherEntity.count()).build(); + } + return responseHttp; + } + + @GET + @RolesAllowed("Admin") + @Path("{id}") + 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); + Response.Status status = getResponseCheck(ctx, userCookie, user); + Response responseHttp = Response.status(status).build(); + if (status.equals(Response.Status.OK)) { + UUID uid = UUID.fromString(id); + PublisherEntity publishers = PublisherEntity.findById(uid); + responseHttp = Response.status(Response.Status.NOT_FOUND).build(); + if (publishers != null) { + responseHttp = Response.ok(publishers).build(); + } + + } + return responseHttp; + } + + @GET + @RolesAllowed("User") + @Path("info") + 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); + 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 PublisherByUser(user.pseudo, user.publisher)) + .build(); + } + return responseHttp; + } + + // PUT + @PUT + @Consumes(MediaType.APPLICATION_JSON) + @RolesAllowed({"Admin", "User"}) + @Transactional + 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(newPublisher).build(); + } + + + // DELETE + @DELETE + @Path("{id}") + @RolesAllowed({"Admin", "User"}) + @Transactional + 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)) { + PublisherEntity singlePublisher = PublisherEntity.find("id", UUID.fromString(id)).firstResult(); + if (singlePublisher == null) { + status = Response.Status.NOT_FOUND; + } else { + singlePublisher.status = -1; + singlePublisher.updated_at = LocalDateTime.now(); + singlePublisher.deleted_at = LocalDateTime.now(); + singlePublisher.persist(); + if (!singlePublisher.isPersistent()) { + status = Response.Status.NOT_MODIFIED; + } + } + } + return Response.status(status).build(); + } + + @DELETE + @Path("/disable/{id}") + @RolesAllowed({"Admin", "User"}) + @Transactional + 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)) { + PublisherEntity singlePublisher = PublisherEntity.find("id", UUID.fromString(id)).firstResult(); + if (singlePublisher == null) { + status = Response.Status.NOT_FOUND; + } else { + singlePublisher.status = 0; + singlePublisher.updated_at = LocalDateTime.now(); + singlePublisher.deleted_at = LocalDateTime.now(); + singlePublisher.persist(); + if (!singlePublisher.isPersistent()) { + status = Response.Status.NOT_MODIFIED; + } + } + } + return Response.status(status).build(); + } + + // PATCH + @PATCH + @RolesAllowed({"Admin", "User"}) + @Consumes(MediaType.APPLICATION_JSON) + @Transactional + @Path("{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)) { + + PublisherEntity publishersOrig = PublisherEntity.findById(UUID.fromString(id)); + if (publishersOrig == null) { + status = Response.Status.NOT_FOUND; + } else { + 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; + } + publishersOrig.persist(); + if (!publishersOrig.isPersistent()) { + status = Response.Status.NOT_MODIFIED; + } + } + } + return Response.status(status).build(); + } + + + + @PATCH + @RolesAllowed({"Admin", "User"}) + @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)) { + + PublisherEntity publishersOrig = PublisherEntity.findById(UUID.fromString(id)); + if (publishersOrig == null) { + status = Response.Status.NOT_FOUND; + } else { + publishersOrig.status = 1; + publishersOrig.persist(); + if (!publishersOrig.isPersistent()) { + status = Response.Status.NOT_MODIFIED; + } + } + } + return Response.status(status).build(); + } +} 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; 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