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
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good
This commit is contained in:
@@ -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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user