diff --git a/src/main/java/org/springframework/cheapy/web/FoodOfferController.java b/src/main/java/org/springframework/cheapy/web/FoodOfferController.java index 0af5a857c..187d27e62 100644 --- a/src/main/java/org/springframework/cheapy/web/FoodOfferController.java +++ b/src/main/java/org/springframework/cheapy/web/FoodOfferController.java @@ -30,6 +30,17 @@ public class FoodOfferController { this.foodOfferService = foodOfferService; this.clientService = clientService; } + + private boolean checkIdentity(final int foodOfferId) { + boolean res = false; + Client client = this.clientService.getCurrentClient(); + FoodOffer foodOffer = this.foodOfferService.findFoodOfferById(foodOfferId); + Client clientOffer = foodOffer.getClient(); + if (client.equals(clientOffer)) { + res = true; + } + return res; + } @GetMapping("/offers/food/new") public String initCreationForm(Map model) { @@ -75,7 +86,6 @@ public class FoodOfferController { model.put("localDateTimeFormat", DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm")); - return "offers/food/foodOffersShow"; } @@ -83,14 +93,21 @@ public class FoodOfferController { @GetMapping(value = "/offers/food/{foodOfferId}/edit") public String updateFoodOffer(@PathVariable("foodOfferId") final int foodOfferId, final ModelMap model) { + if (!this.checkIdentity(foodOfferId)) { + return "error"; + } + FoodOffer foodOffer = this.foodOfferService.findFoodOfferById(foodOfferId); model.addAttribute("foodOffer", foodOffer); return FoodOfferController.VIEWS_FOOD_OFFER_CREATE_OR_UPDATE_FORM; } @PostMapping(value = "/offers/food/{foodOfferId}/edit") - public String updateFoodOffer(@Valid final FoodOffer foodOfferEdit, final BindingResult result, - final ModelMap model) { + public String updateFoodOffer(@Valid final FoodOffer foodOfferEdit, final BindingResult result, final ModelMap model) { + + if (!this.checkIdentity(foodOfferEdit.getId())) { + return "error"; + } if (result.hasErrors()) { model.addAttribute("foodOffer", foodOfferEdit); @@ -104,16 +121,22 @@ public class FoodOfferController { @GetMapping(value = "/offers/food/{foodOfferId}/disable") public String disableFoodOffer(@PathVariable("foodOfferId") final int foodOfferId, final ModelMap model) { - + + if (!this.checkIdentity(foodOfferId)) { + return "error"; + } FoodOffer foodOffer = this.foodOfferService.findFoodOfferById(foodOfferId); model.put("foodOffer", foodOffer); - return "foodOffers/foodOffersDisable"; + return "offers/food/foodOffersDisable"; } @PostMapping(value = "/offers/food/{foodOfferId}/disable") public String disableFoodOfferForm(@PathVariable("foodOfferId") final int foodOfferId, final ModelMap model) { - + + if (!this.checkIdentity(foodOfferId)) { + return "error"; + } FoodOffer foodOffer = this.foodOfferService.findFoodOfferById(foodOfferId); @@ -121,7 +144,7 @@ public class FoodOfferController { this.foodOfferService.saveFoodOffer(foodOffer); - return "redirect:/offers"; + return "redirect:/myOffers"; } } diff --git a/src/main/java/org/springframework/cheapy/web/NuOfferController.java b/src/main/java/org/springframework/cheapy/web/NuOfferController.java index d8533842d..ec1b25dd4 100644 --- a/src/main/java/org/springframework/cheapy/web/NuOfferController.java +++ b/src/main/java/org/springframework/cheapy/web/NuOfferController.java @@ -38,6 +38,17 @@ public class NuOfferController { model.put("nuOffer", nuOffer); return VIEWS_NU_OFFER_CREATE_OR_UPDATE_FORM; } + + private boolean checkIdentity(final int nuOfferId) { + boolean res = false; + Client client = this.clientService.getCurrentClient(); + NuOffer nuOffer = this.nuOfferService.findNuOfferById(nuOfferId); + Client clientOffer = nuOffer.getClient(); + if (client.equals(clientOffer)) { + res = true; + } + return res; + } @PostMapping("/offers/nu/new") public String processCreationForm(@Valid NuOffer nuOffer, BindingResult result) { @@ -83,7 +94,10 @@ public class NuOfferController { @GetMapping(value = "/offers/nu/{nuOfferId}/edit") public String updateNuOffer(@PathVariable("nuOfferId") final int nuOfferId, final ModelMap model) { - + + if (!this.checkIdentity(nuOfferId)) { + return "error"; + } NuOffer nuOffer = this.nuOfferService.findNuOfferById(nuOfferId); model.addAttribute("nuOffer", nuOffer); @@ -92,6 +106,10 @@ public class NuOfferController { @PostMapping(value = "/offers/nu/{nuOfferId}/edit") public String updateNuOffer(@Valid final NuOffer nuOfferEdit, final BindingResult result, final ModelMap model) { + + if (!this.checkIdentity(nuOfferEdit.getId())) { + return "error"; + } if (result.hasErrors()) { model.addAttribute("nuOffer", nuOfferEdit); @@ -104,24 +122,28 @@ public class NuOfferController { } @GetMapping(value = "/offers/nu/{nuOfferId}/disable") - public String disableNuOffer(@PathVariable("nuOfferId") final int nuOfferId, final Principal principal, - final ModelMap model) { + public String disableNuOffer(@PathVariable("nuOfferId") final int nuOfferId, final Principal principal, final ModelMap model) { + if (!this.checkIdentity(nuOfferId)) { + return "error"; + } NuOffer nuOffer = this.nuOfferService.findNuOfferById(nuOfferId); model.put("nuOffer", nuOffer); - return "nuOffers/nuOffersDisable"; + return "offers/nu/nuOffersDisable"; } @PostMapping(value = "/offers/nu/{nuOfferId}/disable") - public String disableNuOfferForm(@PathVariable("nuOfferId") final int nuOfferId, final Principal principal, - final ModelMap model) { - + public String disableNuOfferForm(@PathVariable("nuOfferId") final int nuOfferId, final Principal principal, final ModelMap model) { + if (!this.checkIdentity(nuOfferId)) { + return "error"; + } + NuOffer nuOffer = this.nuOfferService.findNuOfferById(nuOfferId); nuOffer.setStatus(StatusOffer.inactive); this.nuOfferService.saveNuOffer(nuOffer); - return "redirect:/offers"; + return "redirect:/myOffers"; } diff --git a/src/main/java/org/springframework/cheapy/web/SpeedOfferController.java b/src/main/java/org/springframework/cheapy/web/SpeedOfferController.java index 2162e8558..8739479d4 100644 --- a/src/main/java/org/springframework/cheapy/web/SpeedOfferController.java +++ b/src/main/java/org/springframework/cheapy/web/SpeedOfferController.java @@ -29,7 +29,17 @@ public class SpeedOfferController { this.speedOfferService = speedOfferService; this.clientService = clientService; } - + + private boolean checkIdentity(final int speedOfferId) { + boolean res = false; + Client client = this.clientService.getCurrentClient(); + SpeedOffer speedOffer = this.speedOfferService.findSpeedOfferById(speedOfferId); + Client clientOffer = speedOffer.getClient(); + if (client.equals(clientOffer)) { + res = true; + } + return res; + } @GetMapping("/offers/speed/new") public String initCreationForm(Map model) { @@ -79,6 +89,10 @@ public class SpeedOfferController { @GetMapping(value = "/offers/speed/{speedOfferId}/edit") public String updateSpeedOffer(@PathVariable("speedOfferId") final int speedOfferId, final ModelMap model) { + if (!this.checkIdentity(speedOfferId)) { + return "error"; + } + SpeedOffer speedOffer = this.speedOfferService.findSpeedOfferById(speedOfferId); model.addAttribute("speedOffer", speedOffer); return SpeedOfferController.VIEWS_SPEED_OFFER_CREATE_OR_UPDATE_FORM; @@ -87,6 +101,10 @@ public class SpeedOfferController { @PostMapping(value = "/offers/speed/{speedOfferId}/edit") public String updateSpeedOffer(@Valid final SpeedOffer speedOfferEdit, final BindingResult result, final ModelMap model) { + if (!this.checkIdentity(speedOfferEdit.getId())) { + return "error"; + } + if (result.hasErrors()) { model.addAttribute("speedOffer", speedOfferEdit); return SpeedOfferController.VIEWS_SPEED_OFFER_CREATE_OR_UPDATE_FORM; @@ -100,16 +118,22 @@ public class SpeedOfferController { @GetMapping(value = "/offers/speed/{speedOfferId}/disable") public String disableSpeedOffer(@PathVariable("speedOfferId") final int speedOfferId, final ModelMap model) { - + + if (!this.checkIdentity(speedOfferId)) { + return "error"; + } SpeedOffer speedOffer = this.speedOfferService.findSpeedOfferById(speedOfferId); model.put("speedOffer", speedOffer); - return "speedOffers/speedOffersDisable"; + return "offers/speed/speedOffersDisable"; } @PostMapping(value = "/offers/speed/{speedOfferId}/disable") public String disableSpeedOfferForm(@PathVariable("speedOfferId") final int speedOfferId, final ModelMap model) { + if (!this.checkIdentity(speedOfferId)) { + return "error"; + } SpeedOffer speedOffer = this.speedOfferService.findSpeedOfferById(speedOfferId); @@ -117,7 +141,7 @@ public class SpeedOfferController { this.speedOfferService.saveSpeedOffer(speedOffer); - return "redirect:/offers"; + return "redirect:/myOffers"; } } diff --git a/src/main/java/org/springframework/cheapy/web/TimeOfferController.java b/src/main/java/org/springframework/cheapy/web/TimeOfferController.java index 41fcb27ce..eb795cf35 100644 --- a/src/main/java/org/springframework/cheapy/web/TimeOfferController.java +++ b/src/main/java/org/springframework/cheapy/web/TimeOfferController.java @@ -21,7 +21,6 @@ import org.springframework.web.bind.annotation.PostMapping; @Controller public class TimeOfferController { - private static final String VIEWS_TIME_OFFER_CREATE_OR_UPDATE_FORM = "offers/time/createOrUpdateTimeOfferForm"; private final TimeOfferService timeOfferService; private final ClientService clientService; @@ -30,6 +29,17 @@ public class TimeOfferController { this.timeOfferService = timeOfferService; this.clientService = clientService; } + + private boolean checkIdentity(final int timeOfferId) { + boolean res = false; + Client client = this.clientService.getCurrentClient(); + TimeOffer timeOffer = this.timeOfferService.findTimeOfferById(timeOfferId); + Client clientOffer = timeOffer.getClient(); + if (client.equals(clientOffer)) { + res = true; + } + return res; + } @GetMapping("/offers/time/new") public String initCreationForm(Map model) { @@ -88,6 +98,9 @@ public class TimeOfferController { @GetMapping(value = "/offers/time/{timeOfferId}/edit") public String updateTimeOffer(@PathVariable("timeOfferId") final int timeOfferId, final ModelMap model) { + if (!this.checkIdentity(timeOfferId)) { + return "error"; + } TimeOffer timeOffer = this.timeOfferService.findTimeOfferById(timeOfferId); model.addAttribute("timeOffer", timeOffer); @@ -97,6 +110,9 @@ public class TimeOfferController { @PostMapping(value = "/offers/time/{timeOfferId}/edit") public String updateTimeOffer(@Valid final TimeOffer timeOfferEdit, final BindingResult result, final ModelMap model) { + if (!this.checkIdentity(timeOfferEdit.getId())) { + return "error"; + } if (result.hasErrors()) { model.addAttribute("timeOffer", timeOfferEdit); @@ -111,16 +127,22 @@ public class TimeOfferController { @GetMapping(value = "/offers/time/{timeOfferId}/disable") public String disableTimeOffer(@PathVariable("timeOfferId") final int timeOfferId, final ModelMap model) { - + + if (!this.checkIdentity(timeOfferId)) { + return "error"; + } TimeOffer timeOffer = this.timeOfferService.findTimeOfferById(timeOfferId); model.put("timeOffer", timeOffer); - return "timeOffers/timeOffersDisable"; + return "offers/time/timeOffersDisable"; } @PostMapping(value = "/offers/time/{timeOfferId}/disable") public String disableTimeOfferForm(@PathVariable("timeOfferId") final int timeOfferId, final ModelMap model) { + if (!this.checkIdentity(timeOfferId)) { + return "error"; + } TimeOffer timeOffer = this.timeOfferService.findTimeOfferById(timeOfferId); @@ -128,7 +150,7 @@ public class TimeOfferController { this.timeOfferService.saveTimeOffer(timeOffer); - return "redirect:/offers"; + return "redirect:/myOffers"; } diff --git a/src/main/webapp/WEB-INF/jsp/offers/food/foodOffersShow.jsp b/src/main/webapp/WEB-INF/jsp/offers/food/foodOffersShow.jsp index ec9e21931..ce758a77c 100644 --- a/src/main/webapp/WEB-INF/jsp/offers/food/foodOffersShow.jsp +++ b/src/main/webapp/WEB-INF/jsp/offers/food/foodOffersShow.jsp @@ -4,6 +4,7 @@ <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> <%@ taglib prefix="cheapy" tagdir="/WEB-INF/tags" %> +<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %> @@ -44,15 +45,19 @@ - + + Editar oferta + + Desactivar oferta + diff --git a/src/main/webapp/WEB-INF/jsp/offers/nu/nuOffersShow.jsp b/src/main/webapp/WEB-INF/jsp/offers/nu/nuOffersShow.jsp index 5fa0f146b..50433e2da 100644 --- a/src/main/webapp/WEB-INF/jsp/offers/nu/nuOffersShow.jsp +++ b/src/main/webapp/WEB-INF/jsp/offers/nu/nuOffersShow.jsp @@ -4,6 +4,7 @@ <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="cheapy" tagdir="/WEB-INF/tags" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> +<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %> @@ -55,15 +56,19 @@ - + + Editar oferta + + Desactivar oferta + diff --git a/src/main/webapp/WEB-INF/jsp/offers/speed/speedOffersShow.jsp b/src/main/webapp/WEB-INF/jsp/offers/speed/speedOffersShow.jsp index 2d6a1c59d..fca775aec 100644 --- a/src/main/webapp/WEB-INF/jsp/offers/speed/speedOffersShow.jsp +++ b/src/main/webapp/WEB-INF/jsp/offers/speed/speedOffersShow.jsp @@ -4,6 +4,7 @@ <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="cheapy" tagdir="/WEB-INF/tags" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> +<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %> @@ -56,14 +57,18 @@ + Editar oferta + + Desactivar oferta + diff --git a/src/main/webapp/WEB-INF/jsp/offers/time/timeOffersShow.jsp b/src/main/webapp/WEB-INF/jsp/offers/time/timeOffersShow.jsp index 52cb38763..892b592fe 100644 --- a/src/main/webapp/WEB-INF/jsp/offers/time/timeOffersShow.jsp +++ b/src/main/webapp/WEB-INF/jsp/offers/time/timeOffersShow.jsp @@ -4,6 +4,7 @@ <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="cheapy" tagdir="/WEB-INF/tags" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> +<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %> @@ -31,16 +32,20 @@ - + + Editar oferta + + Desactivar oferta +