From 74593ca851eff4454058fe4066b3b606a09f47fa Mon Sep 17 00:00:00 2001 From: Bartha Maxime <231026@umons.ac.be> Date: Sat, 16 Mar 2024 09:34:16 +0100 Subject: [PATCH] added the GET /teachers had to fix the mock and the UserController isAdminOrSecretary --- .../Clyde/EndPoints/MockController.java | 2 +- .../Clyde/EndPoints/UserController.java | 23 ++++++++++++++++++- .../Clyde/Repositories/UserRepository.java | 4 ++++ .../herisson/Clyde/Services/UserService.java | 4 ++++ 4 files changed, 31 insertions(+), 2 deletions(-) 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 e254b8c..116026b 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java @@ -49,7 +49,7 @@ public class MockController { User herobrine = new User("brine","hero","admin@admin.com","in your WalLs","ShadowsLand",new Date(0), null,Role.Admin,passwordEncoder.encode("admin")); 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 meh = new User("Inspiration","lackOf","secretary@secretary.com","a Box","the street",new Date(0), null,Role.Secretary,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)); 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 d936b47..9a8c7db 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.AuthenticatorService; import ovh.herisson.Clyde.Services.UserService; import ovh.herisson.Clyde.Tables.Role; import ovh.herisson.Clyde.Tables.User; + +import java.security.Key; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; @@ -72,6 +74,23 @@ public class UserController { return new ResponseEntity<>("data modified", HttpStatus.OK); } + + @GetMapping("/teachers") + public ResponseEntity>> getAllTeachers(@RequestHeader("Authorization") String token){ + if (authServ.getUserFromToken(token) == null) + return new UnauthorizedResponse<>(null); + Iterable teachers = userService.getAllTeachers(); + ArrayList> withoutPassword = new ArrayList<>(); + + for (User t: teachers){ + withoutPassword.add(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 @@ -91,13 +110,15 @@ public class UserController { } private boolean isSecretaryOrAdmin(String authorization){ + System.out.println(authorization); if (authorization ==null) return false; User poster = authServ.getUserFromToken(authorization); if (poster == null) return false; + System.out.println(poster.getRole()); - return poster.getRole() == Role.Secretary && poster.getRole() == Role.Admin; + return poster.getRole() == Role.Secretary || poster.getRole() == Role.Admin; } } 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 af2bd08..b2643e0 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Repositories/UserRepository.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Repositories/UserRepository.java @@ -15,4 +15,8 @@ public interface UserRepository extends CrudRepository { /** @Query(value = "select a.* from Users a ",nativeQuery = true) Iterable findAllUsers();**/ + + @Query("select u from User u where u.role = ovh.herisson.Clyde.Tables.Role.Teacher") + Iterable findAllTeachers(); + } \ 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 55a2f92..a561512 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/UserService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/UserService.java @@ -103,4 +103,8 @@ public class UserService { public Iterable getAll(){ return userRepo.findAll(); } + + + + public Iterable getAllTeachers (){return userRepo.findAllTeachers();} } \ No newline at end of file