|
6 | 6 | import java.util.Date; |
7 | 7 | import java.util.HashMap; |
8 | 8 | import java.util.Map; |
| 9 | +import org.springframework.context.ApplicationEventPublisher; |
9 | 10 | import org.springframework.context.annotation.Profile; |
10 | 11 | import org.springframework.http.HttpStatus; |
11 | 12 | import org.springframework.http.ResponseEntity; |
|
19 | 20 | import org.springframework.web.bind.annotation.RequestParam; |
20 | 21 | import org.springframework.web.bind.annotation.RestController; |
21 | 22 | import com.digitalsanctuary.spring.demo.user.profile.DemoUserProfileRepository; |
| 23 | +import com.digitalsanctuary.spring.user.event.UserPreDeleteEvent; |
22 | 24 | import com.digitalsanctuary.spring.user.persistence.model.PasswordResetToken; |
23 | 25 | import com.digitalsanctuary.spring.user.persistence.model.Role; |
24 | 26 | import com.digitalsanctuary.spring.user.persistence.model.User; |
@@ -50,6 +52,7 @@ public class TestDataController { |
50 | 52 | private final RoleRepository roleRepository; |
51 | 53 | private final PasswordEncoder passwordEncoder; |
52 | 54 | private final DemoUserProfileRepository demoUserProfileRepository; |
| 55 | + private final ApplicationEventPublisher eventPublisher; |
53 | 56 |
|
54 | 57 | /** |
55 | 58 | * Check if a user exists by email. |
@@ -231,6 +234,10 @@ public ResponseEntity<Map<String, Object>> deleteTestUser(@RequestParam String e |
231 | 234 | return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response); |
232 | 235 | } |
233 | 236 |
|
| 237 | + // Let framework listeners clean up their data first (e.g. WebAuthn credentials and user |
| 238 | + // entities, which have a foreign key on the user account) |
| 239 | + eventPublisher.publishEvent(new UserPreDeleteEvent(this, user)); |
| 240 | + |
234 | 241 | // Delete related entities first to avoid foreign key constraints |
235 | 242 | demoUserProfileRepository.findById(user.getId()).ifPresent(demoUserProfileRepository::delete); |
236 | 243 |
|
|
0 commit comments