From f7df234312ebed12e03d100d9578ba08cd356042 Mon Sep 17 00:00:00 2001 From: Bartha Maxime <231026@umons.ac.be> Date: Sun, 17 Mar 2024 03:06:19 +0100 Subject: [PATCH] moved portective method to Static ProtectiveService --- .../Clyde/EndPoints/CourseController.java | 30 +++++++-------- .../Clyde/EndPoints/UserController.java | 9 +++-- .../Clyde/Services/AuthenticatorService.java | 25 +----------- .../Services/CurriculumCourseService.java | 5 +-- .../Clyde/Services/ProtectionService.java | 38 +++++++++++++++++++ 5 files changed, 59 insertions(+), 48 deletions(-) create mode 100644 backend/src/main/java/ovh/herisson/Clyde/Services/ProtectionService.java diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CourseController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CourseController.java index a7a9719..82e66da 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CourseController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CourseController.java @@ -6,10 +6,12 @@ import org.springframework.web.bind.annotation.*; import ovh.herisson.Clyde.Responses.UnauthorizedResponse; import ovh.herisson.Clyde.Services.AuthenticatorService; import ovh.herisson.Clyde.Services.CourseService; +import ovh.herisson.Clyde.Services.ProtectionService; import ovh.herisson.Clyde.Services.TeacherCourseService; import ovh.herisson.Clyde.Tables.Course; import ovh.herisson.Clyde.Tables.Role; +import java.util.ArrayList; import java.util.HashMap; import java.util.Map; @@ -39,21 +41,27 @@ public class CourseController { if (foundCourse == null) return new ResponseEntity<>(HttpStatus.BAD_REQUEST); - return new ResponseEntity<>(courseWithoutPassword(foundCourse), HttpStatus.OK); + return new ResponseEntity<>(ProtectionService.courseWithoutPassword(foundCourse), HttpStatus.OK); } @GetMapping("/courses") - public ResponseEntity> getAllCourses(@RequestHeader("Authorization") String token){ + public ResponseEntity>> getAllCourses(@RequestHeader("Authorization") String token){ if (authServ.isNotIn(new Role[]{Role.Admin,Role.Secretary},token)) return new UnauthorizedResponse<>(null); + Iterable courses = courseServ.findAll(); + ArrayList> coursesWithoutPassword = new ArrayList<>(); - return new ResponseEntity<>(courseServ.findAll(),HttpStatus.OK); + for (Course course: courses){ + coursesWithoutPassword.add(ProtectionService.courseWithoutPassword(course)); + } + + return new ResponseEntity<>(coursesWithoutPassword,HttpStatus.OK); } @PostMapping("/course") - public ResponseEntity postCourse(@RequestHeader("Authorization") String token, + public ResponseEntity> postCourse(@RequestHeader("Authorization") String token, @RequestBody Course course) { @@ -64,7 +72,7 @@ public class CourseController { if (createdCourse == null) return new ResponseEntity<>(null,HttpStatus.BAD_REQUEST); - return new ResponseEntity<>(createdCourse, HttpStatus.CREATED); + return new ResponseEntity<>(ProtectionService.courseWithoutPassword(createdCourse), HttpStatus.CREATED); } @@ -100,16 +108,4 @@ public class CourseController { return new ResponseEntity<>(HttpStatus.OK); } - - - - private HashMap courseWithoutPassword(Course course){ - HashMap toReturn = new HashMap<>(); - - toReturn.put("courseId",course.getCourseID()); - toReturn.put("credits",course.getCredits()); - toReturn.put("title", course.getTitle()); - toReturn.put("owner", authServ.userWithoutPassword(course.getOwner())); - return toReturn; - } } 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 3f68141..c449a27 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java @@ -5,6 +5,7 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import ovh.herisson.Clyde.Responses.UnauthorizedResponse; import ovh.herisson.Clyde.Services.AuthenticatorService; +import ovh.herisson.Clyde.Services.ProtectionService; import ovh.herisson.Clyde.Services.UserService; import ovh.herisson.Clyde.Tables.Role; import ovh.herisson.Clyde.Tables.User; @@ -35,7 +36,7 @@ public class UserController { User user = authServ.getUserFromToken(token); if (user == null) return new UnauthorizedResponse<>(null); - return new ResponseEntity<>(authServ.userWithoutPassword(user), HttpStatus.OK); + return new ResponseEntity<>(ProtectionService.userWithoutPassword(user), HttpStatus.OK); } @PostMapping("/user") @@ -44,7 +45,7 @@ public class UserController { if (authServ.isNotIn(new Role[]{Role.Admin,Role.InscriptionService,Role.Secretary},token)) return new UnauthorizedResponse<>(null); - return new ResponseEntity<>(authServ.userWithoutPassword(userService.save(user)),HttpStatus.CREATED); + return new ResponseEntity<>(ProtectionService.userWithoutPassword(userService.save(user)),HttpStatus.CREATED); } @GetMapping("/users") @@ -57,7 +58,7 @@ public class UserController { ArrayList> withoutPassword = new ArrayList<>(); for (User u :users){ - withoutPassword.add(authServ.userWithoutPassword(u)); + withoutPassword.add(ProtectionService.userWithoutPassword(u)); } return new ResponseEntity<>(withoutPassword, HttpStatus.OK); } @@ -95,7 +96,7 @@ public class UserController { ArrayList> withoutPassword = new ArrayList<>(); for (User t: teachers){ - withoutPassword.add(authServ.userWithoutPassword(t)); + withoutPassword.add(ProtectionService.userWithoutPassword(t)); } return new ResponseEntity<>(withoutPassword, HttpStatus.OK); 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 cbd0a6d..a73182a 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/AuthenticatorService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/AuthenticatorService.java @@ -1,10 +1,7 @@ package ovh.herisson.Clyde.Services; import org.springframework.stereotype.Service; -import ovh.herisson.Clyde.Tables.InscriptionRequest; -import ovh.herisson.Clyde.Tables.Role; -import ovh.herisson.Clyde.Tables.Token; -import ovh.herisson.Clyde.Tables.User; +import ovh.herisson.Clyde.Tables.*; import java.util.Date; import java.util.HashMap; @@ -53,25 +50,5 @@ 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; - } } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/CurriculumCourseService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/CurriculumCourseService.java index a32e9d6..19549d0 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/CurriculumCourseService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/CurriculumCourseService.java @@ -36,11 +36,11 @@ public class CurriculumCourseService { return null; HashMap toReturn = new HashMap<>(); - ArrayList courses = new ArrayList<>(); + ArrayList> courses = new ArrayList<>(); Iterable foundCourses = curriculumCourseRepo.findCoursesByCurriculum(curriculum); for (Course c: foundCourses){ - courses.add(c); + courses.add(ProtectionService.courseWithoutPassword(c)); } toReturn.put("courses",courses); toReturn.put("curriculumId", curriculum.getCurriculumId()); @@ -89,6 +89,5 @@ public class CurriculumCourseService { curriculumCourseRepo.save(new CurriculumCourse(curriculum,course)); } return true; - } } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/ProtectionService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/ProtectionService.java new file mode 100644 index 0000000..8c778e1 --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/ProtectionService.java @@ -0,0 +1,38 @@ +package ovh.herisson.Clyde.Services; + +import ovh.herisson.Clyde.Tables.Course; +import ovh.herisson.Clyde.Tables.User; + +import java.util.HashMap; + +public class ProtectionService { + + /** return user's data except password + * @param user the user to return + * @return all the user data without the password + */ + public static 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; + } + public static HashMap courseWithoutPassword(Course course){ + HashMap toReturn = new HashMap<>(); + + toReturn.put("courseId",course.getCourseID()); + toReturn.put("credits",course.getCredits()); + toReturn.put("title", course.getTitle()); + toReturn.put("owner", userWithoutPassword(course.getOwner())); + return toReturn; + } + +} +