From 1d793cef4eae3b62fda3530be8ddf22fad9b3dbb Mon Sep 17 00:00:00 2001 From: Bartha Maxime <231026@umons.ac.be> Date: Sun, 17 Mar 2024 02:40:05 +0100 Subject: [PATCH] moved UserWithouPaswword to authenticatorService --- .../Clyde/EndPoints/UserController.java | 28 +++---------------- .../Clyde/Services/AuthenticatorService.java | 21 ++++++++++++++ 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java index f4782e5..3f68141 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java @@ -35,7 +35,7 @@ public class UserController { User user = authServ.getUserFromToken(token); if (user == null) return new UnauthorizedResponse<>(null); - return new ResponseEntity<>(userWithoutPassword(user), HttpStatus.OK); + return new ResponseEntity<>(authServ.userWithoutPassword(user), HttpStatus.OK); } @PostMapping("/user") @@ -44,7 +44,7 @@ public class UserController { if (authServ.isNotIn(new Role[]{Role.Admin,Role.InscriptionService,Role.Secretary},token)) return new UnauthorizedResponse<>(null); - return new ResponseEntity<>(userWithoutPassword(userService.save(user)),HttpStatus.CREATED); + return new ResponseEntity<>(authServ.userWithoutPassword(userService.save(user)),HttpStatus.CREATED); } @GetMapping("/users") @@ -57,7 +57,7 @@ public class UserController { ArrayList> withoutPassword = new ArrayList<>(); for (User u :users){ - withoutPassword.add(userWithoutPassword(u)); + withoutPassword.add(authServ.userWithoutPassword(u)); } return new ResponseEntity<>(withoutPassword, HttpStatus.OK); } @@ -95,30 +95,10 @@ public class UserController { ArrayList> withoutPassword = new ArrayList<>(); for (User t: teachers){ - withoutPassword.add(userWithoutPassword(t)); + withoutPassword.add(authServ.userWithoutPassword(t)); } return new ResponseEntity<>(withoutPassword, HttpStatus.OK); } - - - - /** return user's data except password - * @param user the user to return - * @return all the user data without the password - */ - private HashMap userWithoutPassword(User user){ - HashMap toReturn = new HashMap<>(); - toReturn.put("regNo",user.getRegNo()); - toReturn.put("lastName",user.getLastName()); - toReturn.put("firstName",user.getFirstName()); - toReturn.put("email", user.getEmail()); - toReturn.put("address",user.getAddress()); - toReturn.put("birthDate",user.getBirthDate()); - toReturn.put("country",user.getCountry()); - toReturn.put("profilePictureUrl",user.getProfilePictureUrl()); - toReturn.put("role",user.getRole()); - return toReturn; - } } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/AuthenticatorService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/AuthenticatorService.java index 15ae7eb..cbd0a6d 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/AuthenticatorService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/AuthenticatorService.java @@ -7,6 +7,7 @@ import ovh.herisson.Clyde.Tables.Token; import ovh.herisson.Clyde.Tables.User; import java.util.Date; +import java.util.HashMap; @Service public class AuthenticatorService { @@ -52,5 +53,25 @@ public class AuthenticatorService { } return true; } + + + + /** return user's data except password + * @param user the user to return + * @return all the user data without the password + */ + public HashMap userWithoutPassword(User user){ + HashMap toReturn = new HashMap<>(); + toReturn.put("regNo",user.getRegNo()); + toReturn.put("lastName",user.getLastName()); + toReturn.put("firstName",user.getFirstName()); + toReturn.put("email", user.getEmail()); + toReturn.put("address",user.getAddress()); + toReturn.put("birthDate",user.getBirthDate()); + toReturn.put("country",user.getCountry()); + toReturn.put("profilePictureUrl",user.getProfilePictureUrl()); + toReturn.put("role",user.getRole()); + return toReturn; + } }