added the post /user Endpoint
This commit is contained in:
		| @ -41,10 +41,10 @@ public class MockController { | |||||||
|     @PostMapping("/mock") |     @PostMapping("/mock") | ||||||
|     public void postMock(){ |     public void postMock(){ | ||||||
|  |  | ||||||
|         User herobrine = new User("brine","hero","admin@admin.com","in your WalLs","ShadowsLand",new Date(0), "none",Role.Admin,"admin"); |         User herobrine = new User("brine","hero","admin@admin.com","in your WalLs","ShadowsLand",new Date(0), "none",Role.Admin,passwordEncoder.encode("admin")); | ||||||
|         User joe = new User("Mama","Joe","student@student.com","roundabout","DaWarudo",new Date(0), "None",Role.Student,"student"); |         User joe = new User("Mama","Joe","student@student.com","roundabout","DaWarudo",new Date(0), "None",Role.Student,passwordEncoder.encode("student")); | ||||||
|         User meh = new User("Inspiration","lackOf","secretary@secretary.com","a Box","the street",new Date(0),"none", Role.Teacher,"secretary"); |         User meh = new User("Inspiration","lackOf","secretary@secretary.com","a Box","the street",new Date(0),"none", Role.Teacher, passwordEncoder.encode("secretary")); | ||||||
|         User joke = new User("CthemBalls","Lemme","teacher@teacher.com","lab","faculty",new Date(0), "none",Role.Teacher,"teacher"); |         User joke = new User("CthemBalls","Lemme","teacher@teacher.com","lab","faculty",new Date(0), "none",Role.Teacher, passwordEncoder.encode("teacher")); | ||||||
|  |  | ||||||
|         mockUsers = new ArrayList<User>(Arrays.asList(herobrine,joe,meh,joke)); |         mockUsers = new ArrayList<User>(Arrays.asList(herobrine,joe,meh,joke)); | ||||||
|  |  | ||||||
|  | |||||||
| @ -1,8 +1,10 @@ | |||||||
| package ovh.herisson.Clyde.EndPoints; | package ovh.herisson.Clyde.EndPoints; | ||||||
|  |  | ||||||
|  |  | ||||||
|  | import jakarta.servlet.http.HttpServletRequest; | ||||||
| import org.springframework.http.HttpStatus; | import org.springframework.http.HttpStatus; | ||||||
|  |  | ||||||
|  | import org.springframework.http.MediaType; | ||||||
| import org.springframework.http.ResponseEntity; | import org.springframework.http.ResponseEntity; | ||||||
| import org.springframework.web.bind.annotation.*; | import org.springframework.web.bind.annotation.*; | ||||||
| import ovh.herisson.Clyde.Responses.UnauthorizedResponse; | import ovh.herisson.Clyde.Responses.UnauthorizedResponse; | ||||||
| @ -11,7 +13,9 @@ import ovh.herisson.Clyde.Services.UserService; | |||||||
| import ovh.herisson.Clyde.Tables.Role; | import ovh.herisson.Clyde.Tables.Role; | ||||||
| import ovh.herisson.Clyde.Tables.User; | import ovh.herisson.Clyde.Tables.User; | ||||||
|  |  | ||||||
|  | import java.io.IOException; | ||||||
| import java.util.ArrayList; | import java.util.ArrayList; | ||||||
|  | import java.util.Map; | ||||||
|  |  | ||||||
|  |  | ||||||
| @RestController | @RestController | ||||||
| @ -38,13 +42,9 @@ public class UserController { | |||||||
|     @PostMapping("/user") |     @PostMapping("/user") | ||||||
|     public ResponseEntity<String> postUser(@RequestBody User user,@RequestHeader("Authorization") String authorization){ |     public ResponseEntity<String> postUser(@RequestBody User user,@RequestHeader("Authorization") String authorization){ | ||||||
|  |  | ||||||
|         if (authorization == null) return new UnauthorizedResponse<>(null); |         if (!isSecretaryOrAdmin(authorization)) | ||||||
|         User poster = authServ.getUserFromToken(authorization); |  | ||||||
|  |  | ||||||
|         if (poster.getRole() != Role.Secretary || poster.getRole() != Role.Admin) |  | ||||||
|             return new UnauthorizedResponse<>(null); |             return new UnauthorizedResponse<>(null); | ||||||
|  |  | ||||||
|  |  | ||||||
|         userService.save(user); |         userService.save(user); | ||||||
|         return new ResponseEntity<>(String.format("Account created with ID:%s",user.getRegNo()),HttpStatus.CREATED); |         return new ResponseEntity<>(String.format("Account created with ID:%s",user.getRegNo()),HttpStatus.CREATED); | ||||||
|     } |     } | ||||||
| @ -52,12 +52,7 @@ public class UserController { | |||||||
|     @GetMapping("/users") |     @GetMapping("/users") | ||||||
|     public ResponseEntity<Iterable<Object[]>> getAllUsers(@RequestHeader("Authorization") String authorization){ |     public ResponseEntity<Iterable<Object[]>> getAllUsers(@RequestHeader("Authorization") String authorization){ | ||||||
|  |  | ||||||
|         if (authorization == null) return new UnauthorizedResponse<>(null); |         if (!isSecretaryOrAdmin(authorization)) | ||||||
|         User poster = authServ.getUserFromToken(authorization); |  | ||||||
|  |  | ||||||
|         if (poster == null) return new UnauthorizedResponse<>(null); |  | ||||||
|  |  | ||||||
|         if (poster.getRole() != Role.Secretary || poster.getRole() != Role.Admin) |  | ||||||
|             return new UnauthorizedResponse<>(null); |             return new UnauthorizedResponse<>(null); | ||||||
|  |  | ||||||
|         Iterable<User> users = userService.getAll(); |         Iterable<User> users = userService.getAll(); | ||||||
| @ -68,7 +63,19 @@ public class UserController { | |||||||
|         } |         } | ||||||
|         return new ResponseEntity<>(withoutPassword, HttpStatus.OK); |         return new ResponseEntity<>(withoutPassword, HttpStatus.OK); | ||||||
|     } |     } | ||||||
|  |     @PatchMapping("/user") | ||||||
|  |     public ResponseEntity<String> patchUser(@RequestBody Map<String,Object> updates, @RequestHeader("Authorization") String authorization) { | ||||||
|  |  | ||||||
|  |         if (authorization == null) return new UnauthorizedResponse<>(null); | ||||||
|  |  | ||||||
|  |         User poster = authServ.getUserFromToken(authorization); | ||||||
|  |         if (poster == null) {return new UnauthorizedResponse<>("bad authorization");} | ||||||
|  |  | ||||||
|  |         if (!userService.modifyData(poster, updates, poster)) | ||||||
|  |             return new UnauthorizedResponse<>("there was an issue with the updates requested"); | ||||||
|  |  | ||||||
|  |         return new ResponseEntity<>("data modified", HttpStatus.OK); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |  | ||||||
|         /** return user's data except password |         /** return user's data except password | ||||||
| @ -78,5 +85,15 @@ public class UserController { | |||||||
|     private Object[] userWithoutPassword(User user){ |     private Object[] userWithoutPassword(User user){ | ||||||
|         return new Object[] {user.getRegNo(),user.getFirstName(),user.getLastName(),user.getBirthDate(),user.getCountry(),user.getAddress(),user.getRole()}; |         return new Object[] {user.getRegNo(),user.getFirstName(),user.getLastName(),user.getBirthDate(),user.getCountry(),user.getAddress(),user.getRole()}; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     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; | ||||||
|  |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | |||||||
| @ -28,6 +28,68 @@ public class UserService { | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     /** modify the target data | ||||||
|  |      * verify the permission of modifying from the poster | ||||||
|  |      * | ||||||
|  |      * @param poster the user wanting to modify target's data | ||||||
|  |      * @param updates the changes to be made | ||||||
|  |      * @param target the user to update | ||||||
|  |      * @return if the changes were done or not | ||||||
|  |      */ | ||||||
|  |     public boolean modifyData(User poster, Map<String ,Object> updates, User target){ | ||||||
|  |  | ||||||
|  |         System.out.printf("%s and %s",poster.getRegNo(),target.getRegNo()); | ||||||
|  |         if (poster.getRegNo().equals(target.getRegNo())){ | ||||||
|  |             for (Map.Entry<String, Object> entry : updates.entrySet()){ | ||||||
|  |  | ||||||
|  |                 if ( entry.getKey().equals("regNo") || entry.getKey().equals("role")) {return false;} | ||||||
|  |  | ||||||
|  |                 switch (entry.getKey()){ | ||||||
|  |                     case "firstName": | ||||||
|  |                         target.setFirstName((String) entry.getValue()); | ||||||
|  |                         break; | ||||||
|  |                     case "lastName": | ||||||
|  |                         target.setLastName((String) entry.getValue()); | ||||||
|  |                         break; | ||||||
|  |                     case "email": | ||||||
|  |                         target.setEmail((String) entry.getValue()); | ||||||
|  |                         break; | ||||||
|  |                     case "address": | ||||||
|  |                         target.setAddress((String) entry.getValue()); | ||||||
|  |                         break; | ||||||
|  |                     case "country": | ||||||
|  |                         target.setCountry((String) entry.getValue()); | ||||||
|  |                         break; | ||||||
|  |                     case "birthDate": | ||||||
|  |                         target.setBirthDate((Date) entry.getValue()); | ||||||
|  |                         break; | ||||||
|  |                     case "profilePictureUrl": | ||||||
|  |                         target.setProfilePictureUrl((String) entry.getValue()); | ||||||
|  |                         break; | ||||||
|  |                     case "password": | ||||||
|  |                         target.setPassword(encodePassword((String) entry.getValue())); | ||||||
|  |                         break; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             userRepo.save(target); | ||||||
|  |             return true; | ||||||
|  |         } | ||||||
|  |         // the secretary can change roles (for example if a student becomes a teacher) | ||||||
|  |         else if (poster.getRole() == Role.Secretary) | ||||||
|  |         { | ||||||
|  |             for (Map.Entry<String, Object> entry : updates.entrySet()){ | ||||||
|  |  | ||||||
|  |                 if ( !entry.getKey().equals("role")) {return false;} | ||||||
|  |  | ||||||
|  |                 if (entry.getValue() == Role.Admin){return false;} | ||||||
|  |  | ||||||
|  |                 target.setRole((Role) entry.getValue()); | ||||||
|  |                 userRepo.save(target); | ||||||
|  |                 return true; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |         return false; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |  | ||||||
|     public boolean checkPassword(User user, String tryingPassword){ |     public boolean checkPassword(User user, String tryingPassword){ | ||||||
|  | |||||||
| @ -12,7 +12,7 @@ import java.util.Date; | |||||||
| public class User { | public class User { | ||||||
|     @Id |     @Id | ||||||
|     @GeneratedValue(strategy = GenerationType.AUTO) |     @GeneratedValue(strategy = GenerationType.AUTO) | ||||||
|     private int regNo; |     private Long regNo; | ||||||
|     private String lastName; |     private String lastName; | ||||||
|     private String firstName; |     private String firstName; | ||||||
|     private String email; |     private String email; | ||||||
| @ -63,7 +63,7 @@ public class User { | |||||||
|     } |     } | ||||||
|     public User() {} |     public User() {} | ||||||
|  |  | ||||||
|     public int getRegNo(){ |     public Long getRegNo(){ | ||||||
|         return this.regNo; |         return this.regNo; | ||||||
|     } |     } | ||||||
|     public String getLastName() { |     public String getLastName() { | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user