Add table publisher and add column description for table users

This commit is contained in:
Valentin CZERYBA 2023-01-05 20:12:23 +01:00
parent df15b0d19e
commit 50db0ab1f2
5 changed files with 10 additions and 125 deletions

View File

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

View File

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

View File

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

View File

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

View File

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