|
10 | 10 | import org.springframework.http.ResponseEntity; |
11 | 11 | import org.springframework.security.core.annotation.AuthenticationPrincipal; |
12 | 12 | import org.springframework.security.core.context.SecurityContextHolder; |
| 13 | +import org.springframework.web.bind.annotation.DeleteMapping; |
13 | 14 | import org.springframework.web.bind.annotation.PostMapping; |
14 | 15 | import org.springframework.web.bind.annotation.RequestMapping; |
15 | 16 | import org.springframework.web.bind.annotation.RestController; |
|
25 | 26 | import com.digitalsanctuary.spring.user.service.UserService.TokenValidationResult; |
26 | 27 | import com.digitalsanctuary.spring.user.util.JSONResponse; |
27 | 28 | import com.digitalsanctuary.spring.user.util.UserUtils; |
| 29 | +import jakarta.servlet.ServletException; |
28 | 30 | import jakarta.servlet.http.HttpServletRequest; |
29 | 31 | import lombok.RequiredArgsConstructor; |
30 | 32 | import lombok.extern.slf4j.Slf4j; |
@@ -64,6 +66,10 @@ public class UserAPI { |
64 | 66 | @Value("${user.security.forgotPasswordChangeURI}") |
65 | 67 | private String forgotPasswordChangeURI; |
66 | 68 |
|
| 69 | + @Value("${user.actuallyDeleteAccount:false}") |
| 70 | + private boolean actuallyDeleteAccount; |
| 71 | + |
| 72 | + |
67 | 73 | /** |
68 | 74 | * Register a new user account. |
69 | 75 | * |
@@ -293,4 +299,40 @@ public ResponseEntity<JSONResponse> changeUserPassword(@AuthenticationPrincipal |
293 | 299 | HttpStatus.OK); |
294 | 300 | } |
295 | 301 |
|
| 302 | + /** |
| 303 | + * Deletes the current user's account. |
| 304 | + * |
| 305 | + * @param locale the locale |
| 306 | + * @param request the request |
| 307 | + * @return the generic response |
| 308 | + */ |
| 309 | + @DeleteMapping("/deleteAccount") |
| 310 | + public ResponseEntity<JSONResponse> deleteAccount(@AuthenticationPrincipal DSUserDetails userDetails, final Locale locale, |
| 311 | + final HttpServletRequest request) { |
| 312 | + |
| 313 | + if (userDetails == null || userDetails.getUser() == null) { |
| 314 | + log.error("UserAPI.deleteAccount:" + "deleteAccount called with null userDetails or user."); |
| 315 | + return new ResponseEntity<JSONResponse>( |
| 316 | + JSONResponse.builder().success(false).code(2).message(messages.getMessage("message.error", null, locale)).build(), |
| 317 | + HttpStatus.INTERNAL_SERVER_ERROR); |
| 318 | + } |
| 319 | + final User user = userDetails.getUser(); |
| 320 | + |
| 321 | + if (actuallyDeleteAccount) { |
| 322 | + userService.deleteUser(user); |
| 323 | + } else { |
| 324 | + user.setEnabled(false); |
| 325 | + userService.saveRegisteredUser(user); |
| 326 | + } |
| 327 | + try { |
| 328 | + SecurityContextHolder.clearContext(); |
| 329 | + request.logout(); |
| 330 | + } catch (ServletException e) { |
| 331 | + log.warn("UserAPI.deleteAccount:" + "Exception on logout!", e); |
| 332 | + } |
| 333 | + |
| 334 | + return new ResponseEntity<JSONResponse>(JSONResponse.builder().success(true).message("Account Deleted").build(), HttpStatus.OK); |
| 335 | + } |
| 336 | + |
| 337 | + |
296 | 338 | } |
0 commit comments