Add patch request
This commit is contained in:
parent
5cb64e205f
commit
3e45ad224d
@ -9,11 +9,11 @@ import javax.annotation.security.RolesAllowed;
|
|||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.transaction.Transactional;
|
import javax.transaction.Transactional;
|
||||||
import javax.ws.rs.core.SecurityContext;
|
import javax.ws.rs.core.SecurityContext;
|
||||||
import javax.ws.rs.core.Response.Status;
|
|
||||||
import javax.ws.rs.Consumes;
|
import javax.ws.rs.Consumes;
|
||||||
import javax.ws.rs.CookieParam;
|
import javax.ws.rs.CookieParam;
|
||||||
import javax.ws.rs.DELETE;
|
import javax.ws.rs.DELETE;
|
||||||
import javax.ws.rs.GET;
|
import javax.ws.rs.GET;
|
||||||
|
import javax.ws.rs.PATCH;
|
||||||
import javax.ws.rs.PUT;
|
import javax.ws.rs.PUT;
|
||||||
import javax.ws.rs.Path;
|
import javax.ws.rs.Path;
|
||||||
import javax.ws.rs.PathParam;
|
import javax.ws.rs.PathParam;
|
||||||
@ -40,7 +40,7 @@ public class UsersRessources {
|
|||||||
|
|
||||||
/// Function
|
/// Function
|
||||||
private Boolean checkUserCookie(String userCookie, UsersEntity users) {
|
private Boolean checkUserCookie(String userCookie, UsersEntity users) {
|
||||||
if (userCookie == null) {
|
if ((userCookie == null) || (users == null)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
String name = new String(Base64.decode(userCookie), StandardCharsets.UTF_8);
|
String name = new String(Base64.decode(userCookie), StandardCharsets.UTF_8);
|
||||||
@ -70,7 +70,7 @@ public class UsersRessources {
|
|||||||
UsersEntity user = UsersEntity.findById(kid);
|
UsersEntity user = UsersEntity.findById(kid);
|
||||||
Response.Status status = getResponseCheck(ctx, userCookie, user);
|
Response.Status status = getResponseCheck(ctx, userCookie, user);
|
||||||
Response responseHttp = Response.status(status).build();
|
Response responseHttp = Response.status(status).build();
|
||||||
if(status.equals(Response.Status.OK)){
|
if (status.equals(Response.Status.OK)) {
|
||||||
responseHttp = Response.ok(UsersEntity.listAll()).build();
|
responseHttp = Response.ok(UsersEntity.listAll()).build();
|
||||||
}
|
}
|
||||||
return responseHttp;
|
return responseHttp;
|
||||||
@ -194,8 +194,7 @@ public class UsersRessources {
|
|||||||
UsersEntity singleUser = UsersEntity.find("id", UUID.fromString(id)).firstResult();
|
UsersEntity singleUser = UsersEntity.find("id", UUID.fromString(id)).firstResult();
|
||||||
if (singleUser == null) {
|
if (singleUser == null) {
|
||||||
status = Response.Status.NOT_FOUND;
|
status = Response.Status.NOT_FOUND;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
singleUser.status = false;
|
singleUser.status = false;
|
||||||
singleUser.persist();
|
singleUser.persist();
|
||||||
if (!singleUser.isPersistent()) {
|
if (!singleUser.isPersistent()) {
|
||||||
@ -205,4 +204,67 @@ public class UsersRessources {
|
|||||||
}
|
}
|
||||||
return Response.status(status).build();
|
return Response.status(status).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PATCH
|
||||||
|
@PATCH
|
||||||
|
@RolesAllowed("Admin")
|
||||||
|
@Consumes(MediaType.APPLICATION_JSON)
|
||||||
|
@Transactional
|
||||||
|
public Response updateUserAdmin(@Context SecurityContext ctx, @CookieParam("user") String userCookie,
|
||||||
|
UsersEntity users) {
|
||||||
|
UUID kid = UUID.fromString(jwt.getClaim(Claims.kid));
|
||||||
|
UsersEntity user = UsersEntity.findById(kid);
|
||||||
|
Response.Status status = getResponseCheck(ctx, userCookie, user);
|
||||||
|
|
||||||
|
if (status.equals(Response.Status.OK)) {
|
||||||
|
|
||||||
|
UsersEntity usersOrig = UsersEntity.findByPseudo(users.pseudo);
|
||||||
|
if (usersOrig == null) {
|
||||||
|
status = Response.Status.NOT_FOUND;
|
||||||
|
} else {
|
||||||
|
usersOrig.name = users.name;
|
||||||
|
usersOrig.firstName = users.firstName;
|
||||||
|
usersOrig.email = users.email;
|
||||||
|
usersOrig.birth = LocalDate.of(users.birth.getYear(), users.birth.getMonth(),
|
||||||
|
users.birth.getDayOfMonth());
|
||||||
|
usersOrig.updated_at = LocalDateTime.now();
|
||||||
|
usersOrig.password = Hash
|
||||||
|
.encryptSHA512(Base64.toBase64String(users.password.getBytes(StandardCharsets.UTF_8)));
|
||||||
|
usersOrig.roles = users.roles;
|
||||||
|
usersOrig.persist();
|
||||||
|
if (!usersOrig.isPersistent()) {
|
||||||
|
status = Response.Status.NOT_MODIFIED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Response.status(status).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@PATCH
|
||||||
|
@RolesAllowed("User")
|
||||||
|
@Consumes(MediaType.APPLICATION_JSON)
|
||||||
|
@Transactional
|
||||||
|
public Response updateSingleUser(@Context SecurityContext ctx, @CookieParam("user") String userCookie,
|
||||||
|
UsersEntity users) {
|
||||||
|
UUID kid = UUID.fromString(jwt.getClaim(Claims.kid));
|
||||||
|
UsersEntity user = UsersEntity.findById(kid);
|
||||||
|
Response.Status status = getResponseCheck(ctx, userCookie, user);
|
||||||
|
|
||||||
|
if (status.equals(Response.Status.OK)) {
|
||||||
|
|
||||||
|
user.name = users.name;
|
||||||
|
user.firstName = users.firstName;
|
||||||
|
user.email = users.email;
|
||||||
|
user.birth = LocalDate.of(users.birth.getYear(), users.birth.getMonth(),
|
||||||
|
users.birth.getDayOfMonth());
|
||||||
|
user.updated_at = LocalDateTime.now();
|
||||||
|
user.password = Hash.encryptSHA512(Base64.toBase64String(users.password.getBytes(StandardCharsets.UTF_8)));
|
||||||
|
user.persist();
|
||||||
|
if (!user.isPersistent()) {
|
||||||
|
status = Response.Status.NOT_MODIFIED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Response.status(status).build();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user