From ea46dd664cd3ea193dfd754c5e6a6199fa84b79e Mon Sep 17 00:00:00 2001 From: Bartha Maxime <231026@umons.ac.be> Date: Sun, 17 Mar 2024 16:02:30 +0100 Subject: [PATCH 1/4] added a todo to send an email for every state changement of request --- .../Clyde/EndPoints/InscriptionController.java | 16 ---------------- .../Clyde/Services/InscriptionService.java | 10 ++++++++++ 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/InscriptionController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/InscriptionController.java index 814c185..1c273ba 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/InscriptionController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/InscriptionController.java @@ -58,22 +58,6 @@ public class InscriptionController { return new ResponseEntity<>(requestWithoutPassword(foundInscriptionRequest), HttpStatus.OK); } - /** - @GetMapping("request/user") - public ResponseEntity getUserInscriptionRequest(@RequestHeader("Authorization") String token){ - //todo return l'inscriptionRequest ACTUELLE du user (check si le poster est bien le même que id target ou secretariat) - - if (authServ.IsNotIn(new Role[]{Role.Student,Role.Admin},token)) - return new UnauthorizedResponse<>(null); - - User poster = authServ.getUserFromToken(token); - - inscriptionServ.getById() - - - return null; - } **/ - @PatchMapping("/request/register/{id}") public ResponseEntity changeRequestState(@PathVariable long id, @RequestHeader("Authorization") String token, diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/InscriptionService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/InscriptionService.java index 31b18a6..4712f8f 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/InscriptionService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/InscriptionService.java @@ -32,6 +32,16 @@ public class InscriptionService { if (inscriptionRequest == null) return false; + // if th state is the same we don't send an email + if (requestState == inscriptionRequest.getState()) + return false; + + /** todo send an email to tell the poster of the inscriptionRequest (inscriptionRequest.getEmail()) + * to notify them that the state of their request changed + * FooEmailFormat toSend = (String.format("Your request state changed from %s to %s"), + * inscriptionRequest.getState(), requestState) + * FooEmailSender.send(toSend, inscriptionRequest.getEmail()) + */ inscriptionRequest.setState(requestState); save(inscriptionRequest); return true; From 37f8a3ac4e9170b7ec3a605bf2d0c57596de5903 Mon Sep 17 00:00:00 2001 From: Bartha Maxime <231026@umons.ac.be> Date: Sun, 17 Mar 2024 16:25:00 +0100 Subject: [PATCH 2/4] removed an unused variable --- .../ovh/herisson/Clyde/EndPoints/ApplicationsController.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/ApplicationsController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/ApplicationsController.java index a708ec1..f09e92e 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/ApplicationsController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/ApplicationsController.java @@ -54,8 +54,6 @@ public class ApplicationsController { // if authed authorizedApps.add(Applications.Profile); - Role posterRole = user.getRole(); - if (!authServ.isNotIn(new Role[]{Role.Teacher,Role.Student,Role.Admin},token)) { authorizedApps.add(Applications.Msg); authorizedApps.add(Applications.Forum); From 76f5a39a8f91c47448182f447374d96d4e1ff6b7 Mon Sep 17 00:00:00 2001 From: Bartha Maxime <231026@umons.ac.be> Date: Sun, 17 Mar 2024 16:26:30 +0100 Subject: [PATCH 3/4] GET /users doesn't return Admins if the poster isn't an admin --- .../ovh/herisson/Clyde/EndPoints/UserController.java | 12 +++++++++++- .../herisson/Clyde/Repositories/UserRepository.java | 5 +++++ .../ovh/herisson/Clyde/Services/UserService.java | 4 ++++ 3 files changed, 20 insertions(+), 1 deletion(-) 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 aee09b6..2ace404 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java @@ -9,6 +9,8 @@ import ovh.herisson.Clyde.Services.ProtectionService; import ovh.herisson.Clyde.Services.UserService; import ovh.herisson.Clyde.Tables.Role; import ovh.herisson.Clyde.Tables.User; + +import java.util.ArrayList; import java.util.HashMap; import java.util.Map; @@ -53,7 +55,15 @@ public class UserController { if (authServ.isNotIn(new Role[]{Role.Admin,Role.Secretary},token)) return new UnauthorizedResponse<>(null); - Iterable users = userService.getAll(); + Role posterRole = authServ.getUserFromToken(token).getRole(); + + Iterable users = new ArrayList<>(); + + if (posterRole == Role.Admin) + users = userService.getAll(); + + else if (posterRole == Role.Secretary) + users = userService.getAllExceptAdmins(); return new ResponseEntity<>(ProtectionService.usersWithoutPasswords(users), HttpStatus.OK); } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Repositories/UserRepository.java b/backend/src/main/java/ovh/herisson/Clyde/Repositories/UserRepository.java index a275948..413f090 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Repositories/UserRepository.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Repositories/UserRepository.java @@ -10,9 +10,14 @@ public interface UserRepository extends CrudRepository { User findByEmail(String email); + + @Query("select u from User u where u.role = ovh.herisson.Clyde.Tables.Role.Teacher") Iterable findAllTeachers(); @Query("select u from User u where u.role = ovh.herisson.Clyde.Tables.Role.Student") Iterable findAllStudents(); + + @Query("select u from User u where u.role <> ovh.herisson.Clyde.Tables.Role.Admin") + Iterable findAllExceptAdmins(); } \ No newline at end of file diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/UserService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/UserService.java index 52078dc..3d30a89 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/UserService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/UserService.java @@ -114,6 +114,10 @@ public class UserService { return userRepo.findAll(); } + public Iterable getAllExceptAdmins(){ + return userRepo.findAllExceptAdmins(); + } + public Iterable getAllTeachers (){return userRepo.findAllTeachers();} From ea4a0745e0fbc4a835f80361df74ddc9dd035af3 Mon Sep 17 00:00:00 2001 From: Bartha Maxime <231026@umons.ac.be> Date: Sun, 17 Mar 2024 17:15:33 +0100 Subject: [PATCH 4/4] creation of the user when request accepted --- .../EndPoints/InscriptionController.java | 2 +- .../Clyde/EndPoints/LoginController.java | 2 +- .../UserCurriculumRepository.java | 7 +++ .../Clyde/Services/AuthenticatorService.java | 2 - .../Clyde/Services/InscriptionService.java | 61 ++++++++++++++++--- .../Clyde/Tables/InscriptionRequest.java | 16 +++-- 6 files changed, 67 insertions(+), 23 deletions(-) create mode 100644 backend/src/main/java/ovh/herisson/Clyde/Repositories/UserCurriculumRepository.java diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/InscriptionController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/InscriptionController.java index 1c273ba..6c71fd3 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/InscriptionController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/InscriptionController.java @@ -83,7 +83,7 @@ public class InscriptionController { toReturn.put("email",inscriptionRequest.getEmail()); toReturn.put("birthDate", inscriptionRequest.getBirthDate()); toReturn.put("country", inscriptionRequest.getCountry()); - toReturn.put("curriculum", inscriptionRequest.getCurriculum()); + toReturn.put("curriculum", inscriptionRequest.getCurriculumId()); toReturn.put("state", inscriptionRequest.getState()); toReturn.put("profilePictureUrl", inscriptionRequest.getProfilePicture()); diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/LoginController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/LoginController.java index 6e0b4fa..9367484 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/LoginController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/LoginController.java @@ -44,7 +44,7 @@ public class LoginController { return ResponseEntity.ok().headers(responseHeaders).build(); } - @PostMapping("/request/register") + @PostMapping("/register") public ResponseEntity register(@RequestBody InscriptionRequest inscriptionRequest){ return new ResponseEntity<>(authServ.register(inscriptionRequest), HttpStatus.CREATED); } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Repositories/UserCurriculumRepository.java b/backend/src/main/java/ovh/herisson/Clyde/Repositories/UserCurriculumRepository.java new file mode 100644 index 0000000..93cb10f --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/Repositories/UserCurriculumRepository.java @@ -0,0 +1,7 @@ +package ovh.herisson.Clyde.Repositories; + +import org.springframework.data.repository.CrudRepository; +import ovh.herisson.Clyde.Tables.UserCurriculum; + +public interface UserCurriculumRepository extends CrudRepository { +} 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 a73182a..fc29fb6 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/AuthenticatorService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/AuthenticatorService.java @@ -2,9 +2,7 @@ package ovh.herisson.Clyde.Services; import org.springframework.stereotype.Service; import ovh.herisson.Clyde.Tables.*; - import java.util.Date; -import java.util.HashMap; @Service public class AuthenticatorService { diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/InscriptionService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/InscriptionService.java index 4712f8f..7ae6e74 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/InscriptionService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/InscriptionService.java @@ -1,20 +1,39 @@ package ovh.herisson.Clyde.Services; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.stereotype.Service; +import ovh.herisson.Clyde.Repositories.CurriculumRepository; import ovh.herisson.Clyde.Repositories.InscriptionRepository; +import ovh.herisson.Clyde.Repositories.UserCurriculumRepository; +import ovh.herisson.Clyde.Repositories.UserRepository; import ovh.herisson.Clyde.Tables.InscriptionRequest; import ovh.herisson.Clyde.Tables.RequestState; +import ovh.herisson.Clyde.Tables.User; +import ovh.herisson.Clyde.Tables.UserCurriculum; @Service public class InscriptionService { - InscriptionRepository inscriptionRepo; + private final InscriptionRepository inscriptionRepo; - public InscriptionService(InscriptionRepository inscriptionRepo){ + private final UserRepository userRepo; + + private final UserCurriculumRepository userCurriculumRepo; + + private final CurriculumRepository curriculumRepo; + + private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); + + + public InscriptionService(InscriptionRepository inscriptionRepo, UserRepository userRepo, UserCurriculumRepository userCurriculumRepo, CurriculumRepository curriculumRepo){ this.inscriptionRepo = inscriptionRepo; + this.userRepo = userRepo; + this.userCurriculumRepo = userCurriculumRepo; + this.curriculumRepo = curriculumRepo; } public InscriptionRequest save(InscriptionRequest inscriptionRequest){ + inscriptionRequest.setPassword(passwordEncoder.encode(inscriptionRequest.getPassword())); return inscriptionRepo.save(inscriptionRequest); } @@ -27,23 +46,45 @@ public class InscriptionService { } public boolean modifyState(long id, RequestState requestState) { - InscriptionRequest inscriptionRequest = getById(id); + InscriptionRequest inscrRequest = getById(id); - if (inscriptionRequest == null) + if (inscrRequest == null) return false; // if th state is the same we don't send an email - if (requestState == inscriptionRequest.getState()) + if (requestState == inscrRequest.getState()) return false; - /** todo send an email to tell the poster of the inscriptionRequest (inscriptionRequest.getEmail()) + /** todo send an email to tell the poster of the inscrRequest (inscrRequest.getEmail()) * to notify them that the state of their request changed * FooEmailFormat toSend = (String.format("Your request state changed from %s to %s"), - * inscriptionRequest.getState(), requestState) - * FooEmailSender.send(toSend, inscriptionRequest.getEmail()) + * inscrRequest.getState(), requestState) + * FooEmailSender.send(toSend, inscrRequest.getEmail()) */ - inscriptionRequest.setState(requestState); - save(inscriptionRequest); + + + //saves the user from the request if accepted + if (requestState == RequestState.Accepted) + { + if (curriculumRepo.findById(inscrRequest.getCurriculumId()) == null) + return false; + + User userFromRequest = new User( + inscrRequest.getLastName(), + inscrRequest.getFirstName(), + inscrRequest.getEmail(), + inscrRequest.getAddress(), + inscrRequest.getCountry(), + inscrRequest.getBirthDate(), + inscrRequest.getProfilePicture(), + inscrRequest.getPassword() + ); + + userRepo.save(userFromRequest); + userCurriculumRepo.save(new UserCurriculum(userFromRequest, curriculumRepo.findById(inscrRequest.getCurriculumId()))); + } + inscrRequest.setState(requestState); + save(inscrRequest); return true; } } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/InscriptionRequest.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/InscriptionRequest.java index b7bfea3..18e20d0 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/InscriptionRequest.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/InscriptionRequest.java @@ -16,22 +16,20 @@ public class InscriptionRequest { private String country; private Date birthDate; - @ManyToOne(fetch = FetchType.EAGER) - @JoinColumn(name="Curriculum") - private Curriculum curriculum; + private Long curriculumId; private RequestState state; private String profilePicture; private String password; public InscriptionRequest(){} - public InscriptionRequest(String lastName, String firstName, String address, String email, String country, Date birthDate,Curriculum curriculum, RequestState state, String profilePicture, String password){ + public InscriptionRequest(String lastName, String firstName, String address, String email, String country, Date birthDate,Long curriculumId, RequestState state, String profilePicture, String password){ this.lastName = lastName; this.firstName = firstName; this.address = address; this.email = email; this.country = country; this.birthDate = birthDate; - this.curriculum = curriculum; + this.curriculumId = curriculumId; this.state = state; this.profilePicture = profilePicture; this.password = password; @@ -89,12 +87,12 @@ public class InscriptionRequest { this.birthDate = birthDate; } - public Curriculum getCurriculum() { - return curriculum; + public long getCurriculumId() { + return curriculumId; } - public void setCurriculum(Curriculum curriculum) { - this.curriculum = curriculum; + public void setCurriculumId(long curriculum) { + this.curriculumId = curriculum; } public RequestState getState() {