Skip to content
Snippets Groups Projects
Commit bbf72c7c authored by Birk Øvstetun Narvhus's avatar Birk Øvstetun Narvhus
Browse files

added authentication to delete amount endpoint

parent ee09321e
No related branches found
No related tags found
No related merge requests found
......@@ -80,15 +80,10 @@ public class FridgeController {
@DeleteMapping("/group/delete/product/{fridgeProductId}/{amount}")
public ResponseEntity<?> deleteAmountFridgeProduct(@PathVariable("fridgeProductId") long fridgeProductId,
@PathVariable("amount") String amountStr, Authentication authentication) {
Optional<Fridge> fridge = fridgeService.getFridgeByFridgeId(fridgeProductId);
if (fridge.isEmpty()) {
return ResponseEntity.status(404).body("Fridge not found");
}
if (fridge.get().getGroup().getUser().stream().map(user -> user.getUser().getUsername())
.noneMatch(username -> username.equals(authentication.getName()))
&& authentication.getAuthorities().contains(new SimpleGrantedAuthority(Authority.ADMIN.name()))){
if (!fridgeService.isUserInGroupWithFridgeProduct( authentication.getName(), fridgeProductId)
&& !authentication.getAuthorities().contains(new SimpleGrantedAuthority(Authority.ADMIN.name()))){
return ResponseEntity.status(403).body("You are not a member of this group");
}
......
......@@ -212,15 +212,15 @@ public class FridgeService {
/**
* Delete all products in a fridge
* @param fridgeId the id of the fridge
* @return true if the fridge was deleted
*
public boolean deleteAllProductsInFridge(long fridgeId) {
Optional<Fridge> fridge = fridgeRepository.findById(fridgeId);
if(fridge.isEmpty()) return false;
fridgeProductAssoService.deleteAllFridgeProducts(fridgeId);
return true;
* Get all the fridge products of a group
* @param username the username of the user
* @param fridgeProductId the id of the fridge product
* @return true if the user is in the group of the fridge product
*/
public boolean isUserInGroupWithFridgeProduct(String username, long fridgeProductId) {
Optional<Fridge> fridge = fridgeProductAssoRepo.findById(fridgeProductId)
.map(FridgeProductAsso::getFridgeId);
return fridge.map(value -> value.getGroup().getUser().stream()
.anyMatch(user -> user.getUser().getUsername().equals(username))).orElse(false);
}
*/
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment