Skip to content

Commit

Permalink
fix: user delete method
Browse files Browse the repository at this point in the history
  • Loading branch information
LAPLACE4A committed Nov 6, 2024
1 parent 2c7be6d commit fa4a947
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/kert/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
.requestMatchers("/admin/**").hasRole("ADMIN")
.requestMatchers(HttpMethod.POST, "/posts/**", "/histories/**").hasRole("ADMIN")
.requestMatchers(HttpMethod.PUT, "/posts/**", "/histories/**").hasRole("ADMIN")
.requestMatchers(HttpMethod.DELETE, "/users/**", "/posts/**", "/histories/**").hasRole("ADMIN")
.requestMatchers(HttpMethod.DELETE, "/posts/**", "/histories/**").hasRole("ADMIN")
.requestMatchers(HttpMethod.GET, "/posts/**", "/histories/**").permitAll()
.anyRequest().authenticated()
)
Expand Down
20 changes: 16 additions & 4 deletions src/main/java/com/kert/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,20 @@ public ResponseEntity<User> updateUser(@PathVariable Long studentId, @RequestHea
}

@DeleteMapping("/{studentId}")
public ResponseEntity<Void> deleteUser(@PathVariable Long studentId) {
userService.deleteUser(studentId);
return ResponseEntity.noContent().build();
public ResponseEntity<Void> deleteUser(@PathVariable Long studentId, @RequestHeader("Authorization") String authHeader) {
String token = authHeader.replace("Bearer ", "");

User user = userService.getUserById(studentId);
Long currentUserId = jwtTokenProvider.getUserIdFromJWT(token);
if (user == null) {
return ResponseEntity.notFound().build();
}
boolean isAdmin = adminService.getAdminByStudentId(currentUserId) != null;
if (isAdmin || currentUserId.equals(studentId)) {
userService.deleteUser(studentId);
return ResponseEntity.noContent().build();
} else {
return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
}
}
}
}

0 comments on commit fa4a947

Please sign in to comment.