add self-action check in FriendshipController to prevent users from blocking or requesting friendship with themselves
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good

This commit is contained in:
2026-01-03 18:57:23 +01:00
parent 1daa400a90
commit 45cf83aa78
@@ -61,6 +61,7 @@ public class FriendshipController {
@PostMapping("/request/{friendId}")
public ResponseEntity<Void> requestFriendship(@PathVariable Long friendId) {
User user = getAuthenticatedUser();
checkSelfAction(user.getId(), friendId);
friendshipService.requestFriendship(user, friendId);
return ResponseEntity.ok().build();
}
@@ -93,6 +94,7 @@ public class FriendshipController {
@PostMapping("/block/{friendId}")
public ResponseEntity<Void> blockUser(@PathVariable Long friendId) {
User user = getAuthenticatedUser();
checkSelfAction(user.getId(), friendId);
friendshipService.blockUser(user, friendId);
return ResponseEntity.ok().build();
}
@@ -111,4 +113,10 @@ public class FriendshipController {
User user = getAuthenticatedUser();
return ResponseEntity.ok(friendshipService.getBlockedUsers(user.getId()));
}
private void checkSelfAction(Long userId, Long targetUserId) {
if (userId.equals(targetUserId)) {
throw new IllegalArgumentException("Action cannot be performed on oneself.");
}
}
}