From 4241f1573103b77d432edfcc6f456363377d4570 Mon Sep 17 00:00:00 2001 From: Bartha Maxime <231026@umons.ac.be> Date: Wed, 13 Mar 2024 19:12:46 +0100 Subject: [PATCH 1/3] j'ai envie de me pendre --- .../Clyde/EndPoints/LoginController.java | 23 ++++++++++ .../Repositories/InscriptionRepository.java | 7 ++++ .../Clyde/Services/AuthenticatorService.java | 7 ++++ .../Clyde/Services/InscriptionService.java | 23 ++++++++++ .../ovh/herisson/Clyde/Tables/Cursus.java | 6 +++ .../ovh/herisson/Clyde/Tables/CursusType.java | 8 ++++ .../Clyde/Tables/InscriptionRequest.java | 18 ++++---- .../ovh/herisson/Clyde/Tables/Secretary.java | 42 ------------------- .../ovh/herisson/Clyde/Tables/UserCursus.java | 4 +- 9 files changed, 87 insertions(+), 51 deletions(-) create mode 100644 backend/src/main/java/ovh/herisson/Clyde/Repositories/InscriptionRepository.java create mode 100644 backend/src/main/java/ovh/herisson/Clyde/Services/InscriptionService.java create mode 100644 backend/src/main/java/ovh/herisson/Clyde/Tables/CursusType.java delete mode 100644 backend/src/main/java/ovh/herisson/Clyde/Tables/Secretary.java 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 e43f0a1..5c04ebe 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/LoginController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/LoginController.java @@ -1,10 +1,15 @@ package ovh.herisson.Clyde.EndPoints; import com.fasterxml.jackson.annotation.JsonFormat; +import jakarta.persistence.Column; import org.springframework.http.HttpHeaders; 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.Tables.Cursus; +import ovh.herisson.Clyde.Tables.CursusType; +import ovh.herisson.Clyde.Tables.InscriptionRequest; +import ovh.herisson.Clyde.Tables.User; import java.util.Date; @@ -40,6 +45,24 @@ public class LoginController { responseHeaders.set("Set-Cookie",String.format("session_token=%s",sessionToken)); return ResponseEntity.ok().headers(responseHeaders).build(); } + + @PostMapping("/register") + public ResponseEntity register(@RequestBody InscriptionRequest inscriptionRequest){ + + + authServ.register(inscriptionRequest); + + + + String sessionToken = authServ.login(); + if (sessionToken == null){ + return new UnauthorizedResponse<>("Identifier or Password incorrect"); + } + + HttpHeaders responseHeaders = new HttpHeaders(); + responseHeaders.set("Set-Cookie",String.format("session_token=%s",sessionToken)); + return ResponseEntity.ok().headers(responseHeaders).build(); + } } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Repositories/InscriptionRepository.java b/backend/src/main/java/ovh/herisson/Clyde/Repositories/InscriptionRepository.java new file mode 100644 index 0000000..9f09b4b --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/Repositories/InscriptionRepository.java @@ -0,0 +1,7 @@ +package ovh.herisson.Clyde.Repositories; + +import org.springframework.data.repository.CrudRepository; +import ovh.herisson.Clyde.Tables.InscriptionRequest; + +public interface InscriptionRepository 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 4cd1939..13d0fce 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/AuthenticatorService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/AuthenticatorService.java @@ -1,6 +1,8 @@ package ovh.herisson.Clyde.Services; import org.springframework.stereotype.Service; +import ovh.herisson.Clyde.EndPoints.LoginController; +import ovh.herisson.Clyde.Tables.InscriptionRequest; import ovh.herisson.Clyde.Tables.Token; import ovh.herisson.Clyde.Tables.User; @@ -30,4 +32,9 @@ public class AuthenticatorService { tokenService.saveToken(new Token(user, token,expirationDate)); return token; } + + public void register(InscriptionRequest inscriptionRequest) { + + + } } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/InscriptionService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/InscriptionService.java new file mode 100644 index 0000000..b0739d1 --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/InscriptionService.java @@ -0,0 +1,23 @@ +package ovh.herisson.Clyde.Services; + +import org.springframework.stereotype.Service; +import ovh.herisson.Clyde.Repositories.InscriptionRepository; +import ovh.herisson.Clyde.Tables.InscriptionRequest; + +@Service +public class InscriptionService { + + InscriptionRepository incriptionRepo; + public void save(InscriptionRequest inscriptionRequest){ + incriptionRepo.save(inscriptionRequest); + } + + public InscriptionService(InscriptionRepository inscriptionRepo){ + this.incriptionRepo = inscriptionRepo; + } + + //todo return sans le mdp + public InscriptionRequest getById(int id){ + return null; + } +} \ No newline at end of file diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/Cursus.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/Cursus.java index 5167197..172ec7d 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/Cursus.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/Cursus.java @@ -13,6 +13,12 @@ public class Cursus { private int year; private String option; + public static Cursus infoBab1 = new Cursus(1,"info"); + + public static Cursus chemistryBab1 = new Cursus(1,"chemistry"); + + public static Cursus psychologyBab1 = new Cursus(1,"psychology"); + public Cursus(int year, String option){ this.year = year; this.option = option; diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/CursusType.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/CursusType.java new file mode 100644 index 0000000..5e9c50f --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/CursusType.java @@ -0,0 +1,8 @@ +package ovh.herisson.Clyde.Tables; + +public enum CursusType { + + infoBab1, + chemistryBab1, + psychologyBab1; +} 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 2e5bf0d..9b6ef95 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/InscriptionRequest.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/InscriptionRequest.java @@ -9,7 +9,7 @@ public class InscriptionRequest { private int id; private String firstName; private String lastName; - private String adress; + private String address; private String email; private String country; private Date birthDate; @@ -19,15 +19,19 @@ public class InscriptionRequest { private Cursus cursus; private RequestState state; private String profilePicture; + + private String password; public InscriptionRequest(){} - public InscriptionRequest(String lastName, String firstName, String adress, String email, String country, Date birthDate, RequestState state, String profilePicture){ + public InscriptionRequest(String lastName, String firstName, String address, String email, String country, Date birthDate, RequestState state, String profilePicture, String password){ this.lastName = lastName; this.firstName = firstName; - this.adress = adress; + this.address = address; this.email = email; this.country = country; this.birthDate = birthDate; this.state = state; + this.profilePicture = profilePicture; + this.password = password; } public int getId() { @@ -50,12 +54,12 @@ public class InscriptionRequest { this.lastName = lastName; } - public String getAdress() { - return adress; + public String getAddress() { + return address; } - public void setAdress(String adress) { - this.adress = adress; + public void setAddress(String address) { + this.address = address; } public String getEmail() { diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/Secretary.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/Secretary.java deleted file mode 100644 index cc34841..0000000 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/Secretary.java +++ /dev/null @@ -1,42 +0,0 @@ -package ovh.herisson.Clyde.Tables; - -import jakarta.persistence.*; - -@Entity -public class Secretary { - @Id - @GeneratedValue(strategy = GenerationType.AUTO) - private int id; - - @OneToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "Users") - private User user; - private String faculty; - - public Secretary(User user, String faculty){ - this.user = user; - this.faculty = faculty; - } - - public Secretary() {} - - public int getId() { - return id; - } - - public User getUser() { - return user; - } - - public void setUser(User user) { - this.user = user; - } - - public String getFaculty() { - return faculty; - } - - public void setFaculty(String faculty) { - this.faculty = faculty; - } -} diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/UserCursus.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/UserCursus.java index a5c5153..6590b2d 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/UserCursus.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/UserCursus.java @@ -9,11 +9,11 @@ public class UserCursus { private int id; //Un étudiant peut avoir plusieurs cursus - @ManyToOne(fetch = FetchType.LAZY) + @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "Users") private User user; - @OneToOne(fetch = FetchType.LAZY) + @OneToOne(fetch = FetchType.EAGER) @JoinColumn(name = "Cursus") private Cursus cursus; From 157e5951fca216d0d9a128c9cfcb889a4d7c7b3a Mon Sep 17 00:00:00 2001 From: Bartha Maxime <231026@umons.ac.be> Date: Wed, 13 Mar 2024 19:14:22 +0100 Subject: [PATCH 2/3] =?UTF-8?q?ups=20je=20change=20un=20copi=C3=A9=20coll?= =?UTF-8?q?=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../herisson/Clyde/EndPoints/LoginController.java | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) 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 5c04ebe..0b96535 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/LoginController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/LoginController.java @@ -2,6 +2,7 @@ package ovh.herisson.Clyde.EndPoints; import com.fasterxml.jackson.annotation.JsonFormat; import jakarta.persistence.Column; import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import ovh.herisson.Clyde.Responses.UnauthorizedResponse; @@ -49,19 +50,9 @@ public class LoginController { @PostMapping("/register") public ResponseEntity register(@RequestBody InscriptionRequest inscriptionRequest){ - authServ.register(inscriptionRequest); - - - String sessionToken = authServ.login(); - if (sessionToken == null){ - return new UnauthorizedResponse<>("Identifier or Password incorrect"); - } - - HttpHeaders responseHeaders = new HttpHeaders(); - responseHeaders.set("Set-Cookie",String.format("session_token=%s",sessionToken)); - return ResponseEntity.ok().headers(responseHeaders).build(); + return new ResponseEntity<>("Is OK", HttpStatus.OK); } } From f0a411c03173a2470413a53db000ced81e610c9e Mon Sep 17 00:00:00 2001 From: Bartha Maxime <231026@umons.ac.be> Date: Thu, 14 Mar 2024 16:36:09 +0100 Subject: [PATCH 3/3] added InscriptionController --- .../EndPoints/InscriptionController.java | 84 +++++++++++++++++++ .../Clyde/EndPoints/LoginController.java | 2 +- .../Repositories/InscriptionRepository.java | 3 + .../Clyde/Services/AuthenticatorService.java | 8 +- .../Clyde/Services/InscriptionService.java | 17 +++- 5 files changed, 107 insertions(+), 7 deletions(-) create mode 100644 backend/src/main/java/ovh/herisson/Clyde/EndPoints/InscriptionController.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 new file mode 100644 index 0000000..a997c54 --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/InscriptionController.java @@ -0,0 +1,84 @@ +package ovh.herisson.Clyde.EndPoints; + +import org.springframework.http.HttpStatus; +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.InscriptionService; +import ovh.herisson.Clyde.Tables.InscriptionRequest; +import ovh.herisson.Clyde.Tables.Role; +import ovh.herisson.Clyde.Tables.User; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; + +@RestController +@CrossOrigin(originPatterns = "*", allowCredentials = "true") + +public class InscriptionController { + + + private final InscriptionService inscriptionServ; + private final AuthenticatorService authServ; + + public InscriptionController(InscriptionService inscriptionServ, AuthenticatorService authServ){ + this.inscriptionServ = inscriptionServ; + this.authServ = authServ; + } + + + @GetMapping("/inscriptionRequests") + public ResponseEntity>> getAllRequests(@RequestHeader("Authorization") String token){ + + if (!isSecretaryOrAdmin(token)){return new UnauthorizedResponse<>(null);} + + Iterable inscriptionRequests = inscriptionServ.getAll(); + ArrayList> toReturn = new ArrayList<>(); + + for (InscriptionRequest i:inscriptionRequests){ + toReturn.add(requestWithoutPassword(i)); + } + + return new ResponseEntity<>(toReturn, HttpStatus.OK); + } + + + @GetMapping("/inscriptionRequest/{id}") + public ResponseEntity> getById(@PathVariable long id){ + + + + InscriptionRequest inscriptionRequest = inscriptionServ.getById(id); + if (inscriptionRequest == null) {return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST);} + + return new ResponseEntity<>(requestWithoutPassword(inscriptionRequest), HttpStatus.OK); + } + + + private Map requestWithoutPassword(InscriptionRequest inscriptionRequest) { + Map toReturn = new HashMap<>(); + + toReturn.put("id", inscriptionRequest.getId()); + toReturn.put("firstName", inscriptionRequest.getFirstName()); + toReturn.put("lastName", inscriptionRequest.getLastName()); + toReturn.put("address", inscriptionRequest.getAddress()); + toReturn.put("birthDate", inscriptionRequest.getBirthDate()); + toReturn.put("country", inscriptionRequest.getCountry()); + toReturn.put("cursus", inscriptionRequest.getCursus()); + toReturn.put("profilePictureUrl", inscriptionRequest.getProfilePicture()); + toReturn.put("state", inscriptionRequest.getState()); + return toReturn; + } + + + private boolean isSecretaryOrAdmin(String authorization){ + if (authorization ==null) + return false; + + User poster = authServ.getUserFromToken(authorization); + if (poster == null) return false; + + return poster.getRole() == Role.Secretary && poster.getRole() == Role.Admin; + } +} 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 0b96535..20542b5 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/LoginController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/LoginController.java @@ -13,6 +13,7 @@ import ovh.herisson.Clyde.Tables.InscriptionRequest; import ovh.herisson.Clyde.Tables.User; import java.util.Date; +import java.util.HashMap; @RestController @CrossOrigin(originPatterns = "*", allowCredentials = "true") @@ -51,7 +52,6 @@ public class LoginController { public ResponseEntity register(@RequestBody InscriptionRequest inscriptionRequest){ authServ.register(inscriptionRequest); - return new ResponseEntity<>("Is OK", HttpStatus.OK); } } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Repositories/InscriptionRepository.java b/backend/src/main/java/ovh/herisson/Clyde/Repositories/InscriptionRepository.java index 9f09b4b..0d83e6b 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Repositories/InscriptionRepository.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Repositories/InscriptionRepository.java @@ -3,5 +3,8 @@ package ovh.herisson.Clyde.Repositories; import org.springframework.data.repository.CrudRepository; import ovh.herisson.Clyde.Tables.InscriptionRequest; + public interface InscriptionRepository extends CrudRepository { + + InscriptionRequest findById(long aLong); } 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 13d0fce..60dc6bc 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/AuthenticatorService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/AuthenticatorService.java @@ -2,6 +2,7 @@ package ovh.herisson.Clyde.Services; import org.springframework.stereotype.Service; import ovh.herisson.Clyde.EndPoints.LoginController; +import ovh.herisson.Clyde.Repositories.InscriptionRepository; import ovh.herisson.Clyde.Tables.InscriptionRequest; import ovh.herisson.Clyde.Tables.Token; import ovh.herisson.Clyde.Tables.User; @@ -13,10 +14,12 @@ public class AuthenticatorService { private final TokenService tokenService; private final UserService userService; + private final InscriptionService inscriptionService; - public AuthenticatorService(TokenService tokenService, UserService userService){ + public AuthenticatorService(TokenService tokenService, UserService userService, InscriptionService inscriptionService){ this.tokenService = tokenService; this.userService = userService; + this.inscriptionService = inscriptionService; } public User getUserFromToken(String token){ @@ -34,7 +37,6 @@ public class AuthenticatorService { } public void register(InscriptionRequest inscriptionRequest) { - - + inscriptionService.save(inscriptionRequest); } } 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 b0739d1..45495b7 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/InscriptionService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/InscriptionService.java @@ -4,6 +4,9 @@ import org.springframework.stereotype.Service; import ovh.herisson.Clyde.Repositories.InscriptionRepository; import ovh.herisson.Clyde.Tables.InscriptionRequest; +import java.util.HashMap; +import java.util.Map; + @Service public class InscriptionService { @@ -16,8 +19,16 @@ public class InscriptionService { this.incriptionRepo = inscriptionRepo; } - //todo return sans le mdp - public InscriptionRequest getById(int id){ - return null; + public InscriptionRequest getById(long id){ + InscriptionRequest inscriptionRequest = incriptionRepo.findById(id); + + if (inscriptionRequest == null){ + return null; + } + return inscriptionRequest; + } + + public Iterable getAll(){ + return incriptionRepo.findAll(); } } \ No newline at end of file