From 208c6c63e841daf5d2ff49f09dc1dbaa24c25163 Mon Sep 17 00:00:00 2001 From: Bartha Maxime <231026@umons.ac.be> Date: Sat, 16 Mar 2024 00:31:03 +0100 Subject: [PATCH 1/7] moved the isSecretaryOrAdmin method --- .../Clyde/EndPoints/InscriptionController.java | 15 ++------------- .../herisson/Clyde/EndPoints/UserController.java | 16 ++-------------- .../Clyde/Services/AuthenticatorService.java | 14 ++++++++++++++ 3 files changed, 18 insertions(+), 27 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 a997c54..67ca1ee 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/InscriptionController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/InscriptionController.java @@ -31,7 +31,7 @@ public class InscriptionController { @GetMapping("/inscriptionRequests") public ResponseEntity>> getAllRequests(@RequestHeader("Authorization") String token){ - if (!isSecretaryOrAdmin(token)){return new UnauthorizedResponse<>(null);} + if (authServ.isNotSecretaryOrAdmin(token)){return new UnauthorizedResponse<>(null);} Iterable inscriptionRequests = inscriptionServ.getAll(); ArrayList> toReturn = new ArrayList<>(); @@ -70,15 +70,4 @@ public class InscriptionController { 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; - } -} +} \ No newline at end of file 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 844e82e..d797284 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java @@ -43,7 +43,7 @@ public class UserController { @PostMapping("/user") public ResponseEntity postUser(@RequestBody User user,@RequestHeader("Authorization") String authorization){ - if (!isSecretaryOrAdmin(authorization)) + if (authServ.isNotSecretaryOrAdmin(authorization)) return new UnauthorizedResponse<>(null); userService.save(user); @@ -53,7 +53,7 @@ public class UserController { @GetMapping("/users") public ResponseEntity>> getAllUsers(@RequestHeader("Authorization") String authorization){ - if (!isSecretaryOrAdmin(authorization)) + if (authServ.isNotSecretaryOrAdmin(authorization)) return new UnauthorizedResponse<>(null); Iterable users = userService.getAll(); @@ -85,7 +85,6 @@ public class UserController { */ private HashMap userWithoutPassword(User user){ HashMap toReturn = new HashMap<>(); - toReturn.put("regNo",user.getRegNo()); toReturn.put("firstName",user.getFirstName()); toReturn.put("lastName",user.getLastName()); @@ -93,18 +92,7 @@ public class UserController { toReturn.put("country",user.getCountry()); toReturn.put("address",user.getAddress()); toReturn.put("role",user.getRole()); - 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/Services/AuthenticatorService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/AuthenticatorService.java index 60dc6bc..1cc18e1 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/AuthenticatorService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/AuthenticatorService.java @@ -4,6 +4,7 @@ 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.Role; import ovh.herisson.Clyde.Tables.Token; import ovh.herisson.Clyde.Tables.User; @@ -39,4 +40,17 @@ public class AuthenticatorService { public void register(InscriptionRequest inscriptionRequest) { inscriptionService.save(inscriptionRequest); } + + + public boolean isNotSecretaryOrAdmin(String authorization){ + if (authorization ==null) + return true; + + User poster = getUserFromToken(authorization); + if (poster == null) return true; + + return poster.getRole() != Role.Secretary || poster.getRole() != Role.Admin; + } + } + From 25009ba149938c74594d53088e7c52ea5ad0fd16 Mon Sep 17 00:00:00 2001 From: Bartha Maxime <231026@umons.ac.be> Date: Sat, 16 Mar 2024 00:34:20 +0100 Subject: [PATCH 2/7] Enum in CamelCase --- .../Clyde/EndPoints/ApplicationsController.java | 14 +++++++------- .../ovh/herisson/Clyde/Tables/Applications.java | 17 +++++++---------- .../ovh/herisson/Clyde/Tables/CursusType.java | 6 +++--- .../ovh/herisson/Clyde/Tables/RequestState.java | 2 +- .../java/ovh/herisson/Clyde/Tables/Role.java | 4 ++-- 5 files changed, 20 insertions(+), 23 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 d044bf7..9bd32c1 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/ApplicationsController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/ApplicationsController.java @@ -44,17 +44,17 @@ public class ApplicationsController { Role posterRole = authServ.getUserFromToken(token).getRole(); ArrayList authorizedApps = new ArrayList<>(); - authorizedApps.add(Applications.LOGIN); - authorizedApps.add(Applications.PROFILE); - authorizedApps.add(Applications.MSG); - authorizedApps.add(Applications.FORUM); - authorizedApps.add(Applications.RDV); + authorizedApps.add(Applications.Login); + authorizedApps.add(Applications.Profile); + authorizedApps.add(Applications.Msg); + authorizedApps.add(Applications.Forum); + authorizedApps.add(Applications.Rdv); if (posterRole == Role.Student || posterRole == Role.Admin) return authorizedApps; - if (posterRole == Role.Teacher || posterRole == Role.Secretary || posterRole == Role.Admin) authorizedApps.add(Applications.MANAGECOURSES); + if (posterRole == Role.Teacher || posterRole == Role.Secretary || posterRole == Role.Admin) authorizedApps.add(Applications.ManageCourses); - if (posterRole == Role.InscriptionService || posterRole == Role.Admin) authorizedApps.add(Applications.INSCRIPTION); + if (posterRole == Role.InscriptionService || posterRole == Role.Admin) authorizedApps.add(Applications.Inscription); return authorizedApps; } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/Applications.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/Applications.java index 445a928..6ad6567 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/Applications.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/Applications.java @@ -2,23 +2,20 @@ package ovh.herisson.Clyde.Tables; public enum Applications { // without any token - LOGIN, + Login, // with any token - PROFILE, + Profile, // Students and higher authorization - MSG, - FORUM, - RDV, + Msg, + Forum, + Rdv, // teachers and Secretary authorization - MANAGECOURSES, + ManageCourses, // InscriptionService authorization - INSCRIPTION; - - - + Inscription } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/CursusType.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/CursusType.java index 5e9c50f..d99d47e 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/CursusType.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/CursusType.java @@ -2,7 +2,7 @@ package ovh.herisson.Clyde.Tables; public enum CursusType { - infoBab1, - chemistryBab1, - psychologyBab1; + InfoBab1, + ChemistryBab1, + PsychologyBab1 } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/RequestState.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/RequestState.java index f0345c1..d52f1c9 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/RequestState.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/RequestState.java @@ -3,5 +3,5 @@ package ovh.herisson.Clyde.Tables; public enum RequestState { Accepted, Refused, - Pending; + Pending } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/Role.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/Role.java index 4e4469b..f6f8967 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/Role.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/Role.java @@ -5,5 +5,5 @@ public enum Role { Student, Admin, InscriptionService, - Secretary; -} + Secretary +} \ No newline at end of file From 13fd048cd28f2efc59a6b7a4112343761aed28b9 Mon Sep 17 00:00:00 2001 From: Bartha Maxime <231026@umons.ac.be> Date: Sat, 16 Mar 2024 09:01:36 +0100 Subject: [PATCH 3/7] added a inscriptionService mock user --- .../main/java/ovh/herisson/Clyde/EndPoints/MockController.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java index 136cecd..f3ea51d 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java @@ -43,7 +43,8 @@ public class MockController { User joe = new User("Mama","Joe","student@student.com","roundabout","DaWarudo",new Date(0), null,Role.Student,passwordEncoder.encode("student")); User meh = new User("Inspiration","lackOf","secretary@secretary.com","a Box","the street",new Date(0), null,Role.Teacher,passwordEncoder.encode("secretary")); User joke = new User("CthemBalls","Lemme","teacher@teacher.com","lab","faculty",new Date(0), null,Role.Teacher,passwordEncoder.encode("teacher")); - mockUsers = new ArrayList(Arrays.asList(herobrine,joe,meh,joke)); + User lena = new User("Louille","Lena","inscriptionService@InscriptionService.com","no","yes",new Date(0), null,Role.Teacher,passwordEncoder.encode("inscriptionService")); + mockUsers = new ArrayList<>(Arrays.asList(herobrine,joe,meh,joke)); userRepo.saveAll(mockUsers); } From c6198b7220931156195ebeb7901cd3cda13dd926 Mon Sep 17 00:00:00 2001 From: Bartha Maxime <231026@umons.ac.be> Date: Sat, 16 Mar 2024 09:02:08 +0100 Subject: [PATCH 4/7] cleaned Services + added private final to all repos --- .../Clyde/Services/AuthenticatorService.java | 2 -- .../herisson/Clyde/Services/InscriptionService.java | 13 +++++-------- .../ovh/herisson/Clyde/Services/StorageService.java | 2 -- .../ovh/herisson/Clyde/Services/TokenService.java | 4 +--- .../main/java/ovh/herisson/Clyde/Tables/Course.java | 2 +- 5 files changed, 7 insertions(+), 16 deletions(-) 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 1cc18e1..a3301c1 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/AuthenticatorService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/AuthenticatorService.java @@ -1,8 +1,6 @@ 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.Role; import ovh.herisson.Clyde.Tables.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 45495b7..318dfc1 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/InscriptionService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/InscriptionService.java @@ -4,23 +4,20 @@ 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 { - InscriptionRepository incriptionRepo; + private final InscriptionRepository inscriptionRepo; public void save(InscriptionRequest inscriptionRequest){ - incriptionRepo.save(inscriptionRequest); + inscriptionRepo.save(inscriptionRequest); } public InscriptionService(InscriptionRepository inscriptionRepo){ - this.incriptionRepo = inscriptionRepo; + this.inscriptionRepo = inscriptionRepo; } public InscriptionRequest getById(long id){ - InscriptionRequest inscriptionRequest = incriptionRepo.findById(id); + InscriptionRequest inscriptionRequest = inscriptionRepo.findById(id); if (inscriptionRequest == null){ return null; @@ -29,6 +26,6 @@ public class InscriptionService { } public Iterable getAll(){ - return incriptionRepo.findAll(); + return inscriptionRepo.findAll(); } } \ No newline at end of file diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/StorageService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/StorageService.java index c7f8d1b..fb04f68 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/StorageService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/StorageService.java @@ -4,10 +4,8 @@ import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import ovh.herisson.Clyde.Repositories.FileRepository; import ovh.herisson.Clyde.Tables.*; - import java.io.File; import java.io.IOException; - import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/TokenService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/TokenService.java index 50ddcbf..ce0d109 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/TokenService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/TokenService.java @@ -5,16 +5,14 @@ import org.springframework.stereotype.Service; import ovh.herisson.Clyde.Repositories.TokenRepository; import ovh.herisson.Clyde.Tables.Token; import ovh.herisson.Clyde.Tables.User; - import java.io.UnsupportedEncodingException; import java.security.SecureRandom; import java.util.ArrayList; import java.util.Calendar; -import java.util.Date; @Service public class TokenService { - TokenRepository tokenRepo; + private final TokenRepository tokenRepo; public TokenService(TokenRepository tokenRepo){ this.tokenRepo = tokenRepo; diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/Course.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/Course.java index 54e167a..fb11604 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/Course.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/Course.java @@ -1,6 +1,6 @@ package ovh.herisson.Clyde.Tables; -import jakarta.persistence.Entity; +import ja karta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; From 8f2fb041129f83e717d2b382b831f47b4eef1a7f Mon Sep 17 00:00:00 2001 From: Bartha Maxime <231026@umons.ac.be> Date: Sat, 16 Mar 2024 09:03:03 +0100 Subject: [PATCH 5/7] I pressed tab by accident --- backend/src/main/java/ovh/herisson/Clyde/Tables/Course.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/Course.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/Course.java index fb11604..54e167a 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/Course.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/Course.java @@ -1,6 +1,6 @@ package ovh.herisson.Clyde.Tables; -import ja karta.persistence.Entity; +import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; From 45fbef52edd3034c6036a8f7055ee6dbfede031f Mon Sep 17 00:00:00 2001 From: Anthony Debucquoy Date: Sat, 16 Mar 2024 12:29:51 +0100 Subject: [PATCH 6/7] fix tokens characters --- .../main/java/ovh/herisson/Clyde/Services/TokenService.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/TokenService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/TokenService.java index 50ddcbf..8c9e2d6 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/TokenService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/TokenService.java @@ -9,6 +9,7 @@ import ovh.herisson.Clyde.Tables.User; import java.io.UnsupportedEncodingException; import java.security.SecureRandom; import java.util.ArrayList; +import java.util.Base64; import java.util.Calendar; import java.util.Date; @@ -30,13 +31,10 @@ public class TokenService { new SecureRandom().nextBytes(bytes); for (int i = 0; i < bytes.length; i++) { bytes[i] = (byte) (((bytes[i]+256)%256 %95+ 32)); - while ((char)bytes[i] == ';'){ - bytes[i] = new SecureRandom().generateSeed(1)[0]; - } } // will never end up in the catch because of the way that SecureRandom.nextBytes is implemented try { - return new String(bytes,"ISO_8859_1"); + return new String(Base64.getEncoder().encode(bytes),"ISO_8859_1"); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } From da3f8c47f95972f78ca760b9328b2ff235b43e44 Mon Sep 17 00:00:00 2001 From: Anthony Debucquoy Date: Sat, 16 Mar 2024 14:22:13 +0100 Subject: [PATCH 7/7] Fix merge because i'm bad --- .../Clyde/EndPoints/CurriculumController.java | 24 ------------------- .../EndPoints/InscriptionController.java | 4 ++-- .../Clyde/EndPoints/LoginController.java | 2 +- 3 files changed, 3 insertions(+), 27 deletions(-) diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CurriculumController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CurriculumController.java index bc83924..8e9b256 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CurriculumController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CurriculumController.java @@ -43,28 +43,4 @@ public class CurriculumController { public ResponseEntity> findAll(){ return new ResponseEntity<>(curriculumCourseServ.findAll(),HttpStatus.OK); } - - /**@PostMapping("/curriculum") - public ResponseEntity postCurriculum(@RequestHeader("Authorization") String token,@RequestBody Curriculum curriculum){ - - if (!isSecretaryOrAdmin(token)){ - return new UnauthorizedResponse<>("you're not allowed to post a Curriculum"); - } - - CurriculumServ.save(Curriculum); - - return new ResponseEntity<>("created !",HttpStatus.CREATED); - }**/ - - - - 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/InscriptionController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/InscriptionController.java index addd01b..36946b5 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/InscriptionController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/InscriptionController.java @@ -32,7 +32,7 @@ public class InscriptionController { @GetMapping("/requests/register") public ResponseEntity>> getAllRequests(@RequestHeader("Authorization") String token){ - if (!isSecretaryOrAdmin(token)){return new UnauthorizedResponse<>(null);} + if (authServ.isNotSecretaryOrAdmin(token)){return new UnauthorizedResponse<>(null);} Iterable inscriptionRequests = inscriptionServ.getAll(); ArrayList> toReturn = new ArrayList<>(); @@ -64,7 +64,7 @@ public class InscriptionController { @RequestHeader("Authorize") String token, @RequestBody RequestState requestState) { - if (!isSecretaryOrAdmin(token)) return new UnauthorizedResponse<>(null); + if (authServ.isNotSecretaryOrAdmin(token)) return new UnauthorizedResponse<>(null); inscriptionServ.modifyState(id, requestState); return null; } 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 a47603b..1e761ec 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/LoginController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/LoginController.java @@ -49,4 +49,4 @@ public class LoginController { authServ.register(inscriptionRequest); return new ResponseEntity<>("Is OK", HttpStatus.OK); } -} \ No newline at end of file +}