From 346a049fe2834ac7d72e5f427e72d881d81f7bd8 Mon Sep 17 00:00:00 2001 From: Thiloparn <48439369+Thiloparn@users.noreply.github.com> Date: Wed, 24 Mar 2021 14:42:38 +0100 Subject: [PATCH 01/14] Pubicaciones de speedOffer y foodOffer hechas. Falta comprobar que funcionan correctamente. --- .../configuration/SecurityConfiguration.java | 2 + .../springframework/cheapy/model/Offer.java | 9 +- .../cheapy/repository/ClientRepository.java | 11 +++ .../repository/FoodOfferRepository.java | 33 +++++++ .../repository/SpeedOfferRepository.java | 33 +++++++ .../cheapy/service/ClientService.java | 47 ++++++++++ .../cheapy/service/FoodOfferService.java | 27 ++++++ .../cheapy/service/SpeedOfferService.java | 27 ++++++ .../cheapy/web/FoodOfferController.java | 89 +++++++++++++++++++ .../cheapy/web/SpeedOfferController.java | 89 +++++++++++++++++++ .../createOrUpdateFoodOfferForm.jsp | 31 +++++++ .../createOrUpdateSpeedOfferForm.jsp | 34 +++++++ .../cheapy/web/OwnerControllerTests.java | 4 +- 13 files changed, 433 insertions(+), 3 deletions(-) create mode 100644 src/main/java/org/springframework/cheapy/repository/ClientRepository.java create mode 100644 src/main/java/org/springframework/cheapy/repository/FoodOfferRepository.java create mode 100644 src/main/java/org/springframework/cheapy/repository/SpeedOfferRepository.java create mode 100644 src/main/java/org/springframework/cheapy/service/ClientService.java create mode 100644 src/main/java/org/springframework/cheapy/service/FoodOfferService.java create mode 100644 src/main/java/org/springframework/cheapy/service/SpeedOfferService.java create mode 100644 src/main/java/org/springframework/cheapy/web/FoodOfferController.java create mode 100644 src/main/java/org/springframework/cheapy/web/SpeedOfferController.java create mode 100644 src/main/webapp/WEB-INF/jsp/foodOffers/createOrUpdateFoodOfferForm.jsp create mode 100644 src/main/webapp/WEB-INF/jsp/speedOffers/createOrUpdateSpeedOfferForm.jsp diff --git a/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java b/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java index 2fbdc84ad..156867728 100644 --- a/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java +++ b/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java @@ -39,6 +39,8 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter { .antMatchers("/users/new").permitAll() .antMatchers("/usuarios/new").permitAll() .antMatchers("/admin/**").hasAnyAuthority("admin") + .antMatchers("/speedOffers/**").hasAnyAuthority("admin", "client") + .antMatchers("/foodOffers/**").hasAnyAuthority("admin", "client") .antMatchers("/owners/**").hasAnyAuthority("owner", "admin") .antMatchers("/vets/**").authenticated().anyRequest().denyAll() .and().formLogin() diff --git a/src/main/java/org/springframework/cheapy/model/Offer.java b/src/main/java/org/springframework/cheapy/model/Offer.java index 4c3921b1b..7ee58ff40 100644 --- a/src/main/java/org/springframework/cheapy/model/Offer.java +++ b/src/main/java/org/springframework/cheapy/model/Offer.java @@ -42,7 +42,6 @@ public class Offer extends BaseEntity { @Future private LocalDateTime end; - @NotBlank private String code; @Enumerated(value = EnumType.STRING) @@ -83,5 +82,13 @@ public class Offer extends BaseEntity { public void setType(StatusOffer type) { this.type = type; } + + public Client getClient() { + return client; + } + + public void setClient(Client client) { + this.client = client; + } } diff --git a/src/main/java/org/springframework/cheapy/repository/ClientRepository.java b/src/main/java/org/springframework/cheapy/repository/ClientRepository.java new file mode 100644 index 000000000..36841b6a9 --- /dev/null +++ b/src/main/java/org/springframework/cheapy/repository/ClientRepository.java @@ -0,0 +1,11 @@ + +package org.springframework.cheapy.repository; + +import org.springframework.data.repository.CrudRepository; +import org.springframework.cheapy.model.Client; + +public interface ClientRepository extends CrudRepository { + + Client findByUsername(String currentPrincipalName); + +} diff --git a/src/main/java/org/springframework/cheapy/repository/FoodOfferRepository.java b/src/main/java/org/springframework/cheapy/repository/FoodOfferRepository.java new file mode 100644 index 000000000..b3487f216 --- /dev/null +++ b/src/main/java/org/springframework/cheapy/repository/FoodOfferRepository.java @@ -0,0 +1,33 @@ +/* + * Copyright 2012-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.cheapy.repository; + +import org.springframework.cheapy.model.FoodOffer; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.Repository; +import org.springframework.data.repository.query.Param; +import org.springframework.transaction.annotation.Transactional; + +public interface FoodOfferRepository extends Repository { + + + @Query("SELECT foodOffer FROM FoodOffer foodOffer WHERE id =:id") + @Transactional(readOnly = true) + FoodOffer findById(@Param("id") Integer id); + + void save(FoodOffer foodOffer); + +} diff --git a/src/main/java/org/springframework/cheapy/repository/SpeedOfferRepository.java b/src/main/java/org/springframework/cheapy/repository/SpeedOfferRepository.java new file mode 100644 index 000000000..a37405e61 --- /dev/null +++ b/src/main/java/org/springframework/cheapy/repository/SpeedOfferRepository.java @@ -0,0 +1,33 @@ +/* + * Copyright 2012-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.cheapy.repository; + +import org.springframework.cheapy.model.SpeedOffer; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.Repository; +import org.springframework.data.repository.query.Param; +import org.springframework.transaction.annotation.Transactional; + +public interface SpeedOfferRepository extends Repository { + + + @Query("SELECT speedOffer FROM SpeedOffer speedOffer WHERE id =:id") + @Transactional(readOnly = true) + SpeedOffer findById(@Param("id") Integer id); + + void save(SpeedOffer speedOffer); + +} diff --git a/src/main/java/org/springframework/cheapy/service/ClientService.java b/src/main/java/org/springframework/cheapy/service/ClientService.java new file mode 100644 index 000000000..edc9773a9 --- /dev/null +++ b/src/main/java/org/springframework/cheapy/service/ClientService.java @@ -0,0 +1,47 @@ +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.cheapy.service; + + +import org.springframework.cheapy.model.Client; +import org.springframework.cheapy.repository.ClientRepository; +import org.springframework.dao.DataAccessException; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + + +/** + * Mostly used as a facade for all Petclinic controllers Also a placeholder + * for @Transactional and @Cacheable annotations + * + * @author Michael Isvy + */ + +@Service +public class ClientService { + + private ClientRepository clientRepository; + + @Transactional + public Client getCurrentclient() throws DataAccessException { + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + String currentPrincipalName = authentication.getName(); + return this.clientRepository.findByUsername(currentPrincipalName); + } + +} diff --git a/src/main/java/org/springframework/cheapy/service/FoodOfferService.java b/src/main/java/org/springframework/cheapy/service/FoodOfferService.java new file mode 100644 index 000000000..952a27f98 --- /dev/null +++ b/src/main/java/org/springframework/cheapy/service/FoodOfferService.java @@ -0,0 +1,27 @@ +package org.springframework.cheapy.service; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cheapy.model.FoodOffer; +import org.springframework.cheapy.repository.FoodOfferRepository; +import org.springframework.dao.DataAccessException; +import org.springframework.stereotype.Service; + +@Service +public class FoodOfferService { + private FoodOfferRepository foodOfferRepository; + + + @Autowired + public FoodOfferService(final FoodOfferRepository foodOfferRepository) { + this.foodOfferRepository = foodOfferRepository; + } + + public FoodOffer findFoodOfferById(final int id) { + return this.foodOfferRepository.findById(id); + } + + public void saveFoodOffer(final FoodOffer foodOffer) throws DataAccessException { + this.foodOfferRepository.save(foodOffer); + + } +} diff --git a/src/main/java/org/springframework/cheapy/service/SpeedOfferService.java b/src/main/java/org/springframework/cheapy/service/SpeedOfferService.java new file mode 100644 index 000000000..34f7fd1e2 --- /dev/null +++ b/src/main/java/org/springframework/cheapy/service/SpeedOfferService.java @@ -0,0 +1,27 @@ +package org.springframework.cheapy.service; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cheapy.model.SpeedOffer; +import org.springframework.cheapy.repository.SpeedOfferRepository; +import org.springframework.dao.DataAccessException; +import org.springframework.stereotype.Service; + +@Service +public class SpeedOfferService { + + private SpeedOfferRepository speedOfferRepository; + + @Autowired + public SpeedOfferService(final SpeedOfferRepository speedOfferRepository) { + this.speedOfferRepository = speedOfferRepository; + } + + public SpeedOffer findSpeedOfferById(final int id) { + return this.speedOfferRepository.findById(id); + } + + public void saveSpeedOffer(final SpeedOffer speedOffer) throws DataAccessException { + this.speedOfferRepository.save(speedOffer); + + } +} diff --git a/src/main/java/org/springframework/cheapy/web/FoodOfferController.java b/src/main/java/org/springframework/cheapy/web/FoodOfferController.java new file mode 100644 index 000000000..28a050574 --- /dev/null +++ b/src/main/java/org/springframework/cheapy/web/FoodOfferController.java @@ -0,0 +1,89 @@ +/* + * Copyright 2012-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.cheapy.web; + +import java.util.Map; + +import javax.validation.Valid; + +import org.springframework.cheapy.model.Client; +import org.springframework.cheapy.model.FoodOffer; +import org.springframework.cheapy.model.StatusOffer; +import org.springframework.cheapy.service.ClientService; +import org.springframework.cheapy.service.FoodOfferService; +import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.WebDataBinder; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.InitBinder; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; + +@Controller +public class FoodOfferController { + + private static final String VIEWS_FOOD_OFFER_CREATE_OR_UPDATE_FORM = "foodOffers/createOrUpdateFoodOfferForm"; + + private final FoodOfferService foodOfferService; + private final ClientService clientService; + + + public FoodOfferController(final FoodOfferService foodOfferService, final ClientService clientService) { + this.foodOfferService = foodOfferService; + this.clientService = clientService; + } + + @InitBinder + public void setAllowedFields(WebDataBinder dataBinder) { + dataBinder.setDisallowedFields("id"); + } + + @GetMapping("/foodOffers/new") + public String initCreationForm(Map model) { + FoodOffer foodOffer = new FoodOffer(); + model.put("foodOffer", foodOffer); + return VIEWS_FOOD_OFFER_CREATE_OR_UPDATE_FORM; + } + + @PostMapping("/foodOffers/new") + public String processCreationForm(@Valid FoodOffer foodOffer, BindingResult result) { + if (result.hasErrors()) { + return VIEWS_FOOD_OFFER_CREATE_OR_UPDATE_FORM; + } + else { + Client client = this.clientService.getCurrentclient(); + foodOffer.setClient(client); + foodOffer.setType(StatusOffer.hidden); + this.foodOfferService.saveFoodOffer(foodOffer); + return "redirect:/foodOffers/" + foodOffer.getId(); + } + } + + @GetMapping(value = "/foodOffers/{foodOfferid}/activate") + public String activateFoodOffer(@PathVariable("foodOffer") final int foodOfferId, final ModelMap modelMap) { + FoodOffer foodOffer = this.foodOfferService.findFoodOfferById(foodOfferId); + Client client = this.clientService.getCurrentclient(); + if(foodOffer.getClient().equals(client)) { + foodOffer.setType(StatusOffer.active); + foodOffer.setCode("SE-"+foodOfferId); + this.foodOfferService.saveFoodOffer(foodOffer); + } else { + modelMap.addAttribute("message", "You don't have access to this food offer"); + } + return "redirect:/foodOffers/" + foodOffer.getId(); + } +} diff --git a/src/main/java/org/springframework/cheapy/web/SpeedOfferController.java b/src/main/java/org/springframework/cheapy/web/SpeedOfferController.java new file mode 100644 index 000000000..f633dfaba --- /dev/null +++ b/src/main/java/org/springframework/cheapy/web/SpeedOfferController.java @@ -0,0 +1,89 @@ +/* + * Copyright 2012-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.cheapy.web; + +import java.util.Map; + +import javax.validation.Valid; + +import org.springframework.cheapy.model.Client; +import org.springframework.cheapy.model.SpeedOffer; +import org.springframework.cheapy.model.StatusOffer; +import org.springframework.cheapy.service.ClientService; +import org.springframework.cheapy.service.SpeedOfferService; +import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.WebDataBinder; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.InitBinder; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; + +@Controller +public class SpeedOfferController { + + private static final String VIEWS_SPEED_OFFER_CREATE_OR_UPDATE_FORM = "speedOffers/createOrUpdateSpeedOfferForm"; + + private final SpeedOfferService speedOfferService; + private final ClientService clientService; + + public SpeedOfferController(final SpeedOfferService speedOfferService, final ClientService clientService) { + this.speedOfferService = speedOfferService; + this.clientService = clientService; + + } + + @InitBinder + public void setAllowedFields(WebDataBinder dataBinder) { + dataBinder.setDisallowedFields("id"); + } + + @GetMapping("/speedOffers/new") + public String initCreationForm(Map model) { + SpeedOffer speedOffer = new SpeedOffer(); + model.put("speedOffer", speedOffer); + return VIEWS_SPEED_OFFER_CREATE_OR_UPDATE_FORM; + } + + @PostMapping("/speedOffers/new") + public String processCreationForm(@Valid SpeedOffer speedOffer, BindingResult result) { + if (result.hasErrors()) { + return VIEWS_SPEED_OFFER_CREATE_OR_UPDATE_FORM; + } + else { + Client client = this.clientService.getCurrentclient(); + speedOffer.setClient(client); + speedOffer.setType(StatusOffer.hidden); + this.speedOfferService.saveSpeedOffer(speedOffer); + return "redirect:/speedOffers/" + speedOffer.getId(); + } + } + + @GetMapping(value = "/speedOffers/{speedOfferid}/activate") + public String activateSpeedOffer(@PathVariable("speedOffer") final int speedOfferId, final ModelMap modelMap) { + SpeedOffer speedOffer = this.speedOfferService.findSpeedOfferById(speedOfferId); + Client client = this.clientService.getCurrentclient(); + if(speedOffer.getClient().equals(client)) { + speedOffer.setType(StatusOffer.active); + speedOffer.setCode("SE-"+speedOfferId); + this.speedOfferService.saveSpeedOffer(speedOffer); + } else { + modelMap.addAttribute("message", "You don't have access to this speed offer"); + } + return "redirect:/speedOffers/" + speedOffer.getId(); + } +} diff --git a/src/main/webapp/WEB-INF/jsp/foodOffers/createOrUpdateFoodOfferForm.jsp b/src/main/webapp/WEB-INF/jsp/foodOffers/createOrUpdateFoodOfferForm.jsp new file mode 100644 index 000000000..ff6097fe2 --- /dev/null +++ b/src/main/webapp/WEB-INF/jsp/foodOffers/createOrUpdateFoodOfferForm.jsp @@ -0,0 +1,31 @@ +<%@ page session="false" trimDirectiveWhitespaces="true" %> +<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> +<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> +<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> +<%@ taglib prefix="petclinic" tagdir="/WEB-INF/tags" %> + + +

+ New FoodOffer +

+ +
+ + + + + +
+
+
+ + + + + +
+
+
+
diff --git a/src/main/webapp/WEB-INF/jsp/speedOffers/createOrUpdateSpeedOfferForm.jsp b/src/main/webapp/WEB-INF/jsp/speedOffers/createOrUpdateSpeedOfferForm.jsp new file mode 100644 index 000000000..0e35b6ed3 --- /dev/null +++ b/src/main/webapp/WEB-INF/jsp/speedOffers/createOrUpdateSpeedOfferForm.jsp @@ -0,0 +1,34 @@ +<%@ page session="false" trimDirectiveWhitespaces="true" %> +<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> +<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> +<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> +<%@ taglib prefix="petclinic" tagdir="/WEB-INF/tags" %> + + +

+ New SpeedOffer +

+ +
+ + + + + + + + +
+
+
+ + + + + +
+
+
+
diff --git a/src/test/java/org/springframework/cheapy/web/OwnerControllerTests.java b/src/test/java/org/springframework/cheapy/web/OwnerControllerTests.java index 9e5997489..9f258005f 100644 --- a/src/test/java/org/springframework/cheapy/web/OwnerControllerTests.java +++ b/src/test/java/org/springframework/cheapy/web/OwnerControllerTests.java @@ -43,7 +43,7 @@ import org.springframework.test.web.servlet.MockMvc; @WebMvcTest(OwnerController.class) class OwnerControllerTests { - private static final int TEST_OWNER_ID = 1; + /*private static final int TEST_OWNER_ID = 1; @Autowired private MockMvc mockMvc; @@ -158,6 +158,6 @@ class OwnerControllerTests { .andExpect(model().attribute("owner", hasProperty("city", is("Madison")))) .andExpect(model().attribute("owner", hasProperty("telephone", is("6085551023")))) .andExpect(view().name("owners/ownerDetails")); - } + }*/ } From bb86bbc6bab7973fce3ee1fa2b096bdced2c0c2f Mon Sep 17 00:00:00 2001 From: David Date: Wed, 24 Mar 2021 21:03:28 +0100 Subject: [PATCH 02/14] Cambio de vista del login --- .../configuration/SecurityConfiguration.java | 15 +- .../cheapy/system/LoginController.java | 32 ++ .../cheapy/system/WelcomeController.java | 2 + src/main/webapp/WEB-INF/jsp/login.jsp | 300 ++++++++++++++++++ src/main/webapp/WEB-INF/tags/menu.tag | 5 - 5 files changed, 342 insertions(+), 12 deletions(-) create mode 100644 src/main/java/org/springframework/cheapy/system/LoginController.java create mode 100644 src/main/webapp/WEB-INF/jsp/login.jsp diff --git a/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java b/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java index 2fbdc84ad..677bb736d 100644 --- a/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java +++ b/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java @@ -20,9 +20,7 @@ import org.springframework.security.crypto.password.PasswordEncoder; * and open the template in the editor. */ -/** - * @author japarejo - */ + @Configuration @EnableWebSecurity public class SecurityConfiguration extends WebSecurityConfigurerAdapter { @@ -37,19 +35,22 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter { http.authorizeRequests().antMatchers("/resources/**", "/webjars/**", "/h2-console/**").permitAll() .antMatchers(HttpMethod.GET, "/", "/oups").permitAll() .antMatchers("/users/new").permitAll() + .antMatchers("/login/**").anonymous() .antMatchers("/usuarios/new").permitAll() .antMatchers("/admin/**").hasAnyAuthority("admin") .antMatchers("/owners/**").hasAnyAuthority("owner", "admin") - .antMatchers("/vets/**").authenticated().anyRequest().denyAll() + .antMatchers("/vets/**").authenticated().anyRequest().anonymous() .and().formLogin() - /* .loginPage("/login") */ - .failureUrl("/login-error").and().logout().logoutSuccessUrl("/"); + .loginPage("/login") + .successForwardUrl("/") + .failureUrl("/login?error") + .and().logout().logoutUrl("/login?logout"); // Configuración para que funcione la consola de administración // de la BD H2 (deshabilitar las cabeceras de protección contra // ataques de tipo csrf y habilitar los framesets si su contenido // se sirve desde esta misma página. - http.csrf().ignoringAntMatchers("/h2-console/**"); + //http.csrf().ignoringAntMatchers("/h2-console/**"); http.headers().frameOptions().sameOrigin(); } diff --git a/src/main/java/org/springframework/cheapy/system/LoginController.java b/src/main/java/org/springframework/cheapy/system/LoginController.java new file mode 100644 index 000000000..e0e0fa7a9 --- /dev/null +++ b/src/main/java/org/springframework/cheapy/system/LoginController.java @@ -0,0 +1,32 @@ +/* + * Copyright 2012-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cheapy.system; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; + +@Controller +class LoginController { + + @GetMapping("/login") + public String login() { + return "login"; + } + + + +} diff --git a/src/main/java/org/springframework/cheapy/system/WelcomeController.java b/src/main/java/org/springframework/cheapy/system/WelcomeController.java index 85782e967..1f3b04637 100644 --- a/src/main/java/org/springframework/cheapy/system/WelcomeController.java +++ b/src/main/java/org/springframework/cheapy/system/WelcomeController.java @@ -27,4 +27,6 @@ class WelcomeController { return "welcome"; } + + } diff --git a/src/main/webapp/WEB-INF/jsp/login.jsp b/src/main/webapp/WEB-INF/jsp/login.jsp new file mode 100644 index 000000000..bf718d6e4 --- /dev/null +++ b/src/main/webapp/WEB-INF/jsp/login.jsp @@ -0,0 +1,300 @@ +<%@ page session="false" trimDirectiveWhitespaces="true" %> +<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> +<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> +<%@ taglib prefix="cheapy" tagdir="/WEB-INF/tags" %> +<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %> + + + + + + + + + + +
+
+ + + +
+ +
+ + +
+
+ +
diff --git a/src/main/webapp/WEB-INF/tags/menu.tag b/src/main/webapp/WEB-INF/tags/menu.tag index 144904a60..d398ef39b 100644 --- a/src/main/webapp/WEB-INF/tags/menu.tag +++ b/src/main/webapp/WEB-INF/tags/menu.tag @@ -41,11 +41,6 @@ - - - Login - From 83897d84d56086988f6cd0951059bcc9a03b3f27 Mon Sep 17 00:00:00 2001 From: abemorcardc Date: Wed, 24 Mar 2021 21:58:46 +0100 Subject: [PATCH 03/14] =?UTF-8?q?A=C3=B1adidas=20ofertas=20de=20numero=20d?= =?UTF-8?q?e=20clientes=20y=20de=20franja=20horaria?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../configuration/SecurityConfiguration.java | 3 +- .../springframework/cheapy/model/NuOffer.java | 7 +- .../springframework/cheapy/model/Offer.java | 18 ++-- .../cheapy/model/TimeOffer.java | 23 ++++- .../cheapy/repository/ClientRepository.java | 11 +++ .../cheapy/repository/NuOfferRepository.java | 18 ++++ .../repository/TimeOfferRepository.java | 19 +++++ .../cheapy/service/ClientService.java | 41 +++++++++ .../cheapy/service/NuOfferService.java | 28 +++++++ .../cheapy/service/TimeOfferService.java | 28 +++++++ .../cheapy/web/NuOfferController.java | 84 +++++++++++++++++++ .../cheapy/web/TimeOfferController.java | 84 +++++++++++++++++++ .../nuOffers/createOrUpdateNuOfferForm.jsp | 39 +++++++++ .../createOrUpdateTimeOfferForm.jsp | 36 ++++++++ .../cheapy/web/OwnerControllerTests.java | 4 +- 15 files changed, 428 insertions(+), 15 deletions(-) create mode 100644 src/main/java/org/springframework/cheapy/repository/ClientRepository.java create mode 100644 src/main/java/org/springframework/cheapy/repository/NuOfferRepository.java create mode 100644 src/main/java/org/springframework/cheapy/repository/TimeOfferRepository.java create mode 100644 src/main/java/org/springframework/cheapy/service/ClientService.java create mode 100644 src/main/java/org/springframework/cheapy/service/NuOfferService.java create mode 100644 src/main/java/org/springframework/cheapy/service/TimeOfferService.java create mode 100644 src/main/java/org/springframework/cheapy/web/NuOfferController.java create mode 100644 src/main/java/org/springframework/cheapy/web/TimeOfferController.java create mode 100644 src/main/webapp/WEB-INF/jsp/nuOffers/createOrUpdateNuOfferForm.jsp create mode 100644 src/main/webapp/WEB-INF/jsp/timeOffers/createOrUpdateTimeOfferForm.jsp diff --git a/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java b/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java index 2fbdc84ad..c6e3c7440 100644 --- a/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java +++ b/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java @@ -37,10 +37,11 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter { http.authorizeRequests().antMatchers("/resources/**", "/webjars/**", "/h2-console/**").permitAll() .antMatchers(HttpMethod.GET, "/", "/oups").permitAll() .antMatchers("/users/new").permitAll() + .antMatchers("/nuOffers/new").hasAnyAuthority("admin","client") + .antMatchers("/timeOffers/new").hasAnyAuthority("admin","client") .antMatchers("/usuarios/new").permitAll() .antMatchers("/admin/**").hasAnyAuthority("admin") .antMatchers("/owners/**").hasAnyAuthority("owner", "admin") - .antMatchers("/vets/**").authenticated().anyRequest().denyAll() .and().formLogin() /* .loginPage("/login") */ .failureUrl("/login-error").and().logout().logoutSuccessUrl("/"); diff --git a/src/main/java/org/springframework/cheapy/model/NuOffer.java b/src/main/java/org/springframework/cheapy/model/NuOffer.java index 05d66a688..9593be032 100644 --- a/src/main/java/org/springframework/cheapy/model/NuOffer.java +++ b/src/main/java/org/springframework/cheapy/model/NuOffer.java @@ -19,26 +19,27 @@ import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Table; import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; @Entity @Table(name = "nu_offers") public class NuOffer extends Offer { - @NotBlank + @NotNull private Integer gold; @Column(name = "discount_gold") @NotBlank private String discountGold; - @NotBlank + @NotNull private Integer silver; @Column(name = "discount_silver") @NotBlank private String discountSilver; - @NotBlank + @NotNull private Integer bronze; @Column(name = "discount_bronze") diff --git a/src/main/java/org/springframework/cheapy/model/Offer.java b/src/main/java/org/springframework/cheapy/model/Offer.java index 4c3921b1b..5d0772def 100644 --- a/src/main/java/org/springframework/cheapy/model/Offer.java +++ b/src/main/java/org/springframework/cheapy/model/Offer.java @@ -17,15 +17,13 @@ package org.springframework.cheapy.model; import java.time.LocalDateTime; -import javax.persistence.Entity; import javax.persistence.EnumType; import javax.persistence.Enumerated; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.MappedSuperclass; -import javax.persistence.Table; import javax.validation.constraints.Future; -import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; import org.springframework.format.annotation.DateTimeFormat; @@ -33,16 +31,16 @@ import org.springframework.format.annotation.DateTimeFormat; public class Offer extends BaseEntity { @DateTimeFormat(pattern = "dd/MM/yyyy HH:mm") - @NotBlank + @NotNull @Future private LocalDateTime start; @DateTimeFormat(pattern = "dd/MM/yyyy HH:mm") - @NotBlank + @NotNull @Future private LocalDateTime end; - @NotBlank + private String code; @Enumerated(value = EnumType.STRING) @@ -83,5 +81,13 @@ public class Offer extends BaseEntity { public void setType(StatusOffer type) { this.type = type; } + + public Client getClient() { + return client; + } + + public void setClient(Client client) { + this.client = client; + } } diff --git a/src/main/java/org/springframework/cheapy/model/TimeOffer.java b/src/main/java/org/springframework/cheapy/model/TimeOffer.java index 7f1d3cc6d..60eb17820 100644 --- a/src/main/java/org/springframework/cheapy/model/TimeOffer.java +++ b/src/main/java/org/springframework/cheapy/model/TimeOffer.java @@ -15,13 +15,12 @@ */ package org.springframework.cheapy.model; -import java.time.LocalDateTime; import java.time.LocalTime; import javax.persistence.Entity; import javax.persistence.Table; -import javax.validation.constraints.Future; import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; import org.springframework.format.annotation.DateTimeFormat; @@ -30,11 +29,11 @@ import org.springframework.format.annotation.DateTimeFormat; public class TimeOffer extends Offer { @DateTimeFormat(pattern = "HH:mm") - @NotBlank + @NotNull private LocalTime init; @DateTimeFormat(pattern = "HH:mm") - @NotBlank + @NotNull private LocalTime finish; @NotBlank @@ -42,6 +41,22 @@ public class TimeOffer extends Offer { + public LocalTime getInit() { + return init; + } + + public void setInit(LocalTime init) { + this.init = init; + } + + public LocalTime getFinish() { + return finish; + } + + public void setFinish(LocalTime finish) { + this.finish = finish; + } + public String getDiscount() { return discount; } diff --git a/src/main/java/org/springframework/cheapy/repository/ClientRepository.java b/src/main/java/org/springframework/cheapy/repository/ClientRepository.java new file mode 100644 index 000000000..5ac6769a4 --- /dev/null +++ b/src/main/java/org/springframework/cheapy/repository/ClientRepository.java @@ -0,0 +1,11 @@ + +package org.springframework.cheapy.repository; + +import org.springframework.cheapy.model.Client; +import org.springframework.data.repository.CrudRepository; + +public interface ClientRepository extends CrudRepository { + + Client findByUsername(String currentPrincipalName); + +} diff --git a/src/main/java/org/springframework/cheapy/repository/NuOfferRepository.java b/src/main/java/org/springframework/cheapy/repository/NuOfferRepository.java new file mode 100644 index 000000000..e2c1c127d --- /dev/null +++ b/src/main/java/org/springframework/cheapy/repository/NuOfferRepository.java @@ -0,0 +1,18 @@ +package org.springframework.cheapy.repository; + +import org.springframework.cheapy.model.NuOffer; +import org.springframework.data.repository.Repository; + + +public interface NuOfferRepository extends Repository { + + + + + + NuOffer findNuOfferById(int nuOfferId); + + + void save(NuOffer nuOffer); + +} diff --git a/src/main/java/org/springframework/cheapy/repository/TimeOfferRepository.java b/src/main/java/org/springframework/cheapy/repository/TimeOfferRepository.java new file mode 100644 index 000000000..c8d2684d4 --- /dev/null +++ b/src/main/java/org/springframework/cheapy/repository/TimeOfferRepository.java @@ -0,0 +1,19 @@ +package org.springframework.cheapy.repository; + +import org.springframework.cheapy.model.NuOffer; +import org.springframework.cheapy.model.TimeOffer; +import org.springframework.data.repository.Repository; + + +public interface TimeOfferRepository extends Repository { + + + + + + TimeOffer findTimeOfferById(int timeOfferId); + + + void save(TimeOffer timeOffer); + +} diff --git a/src/main/java/org/springframework/cheapy/service/ClientService.java b/src/main/java/org/springframework/cheapy/service/ClientService.java new file mode 100644 index 000000000..9e8071e9b --- /dev/null +++ b/src/main/java/org/springframework/cheapy/service/ClientService.java @@ -0,0 +1,41 @@ +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.cheapy.service; + + +import org.springframework.cheapy.model.Client; +import org.springframework.cheapy.repository.ClientRepository; +import org.springframework.dao.DataAccessException; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + + + +@Service +public class ClientService { + + private ClientRepository clientRepository; + + @Transactional + public Client getCurrentClient() throws DataAccessException { + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + String currentPrincipalName = authentication.getName(); + return this.clientRepository.findByUsername(currentPrincipalName); + } + +} diff --git a/src/main/java/org/springframework/cheapy/service/NuOfferService.java b/src/main/java/org/springframework/cheapy/service/NuOfferService.java new file mode 100644 index 000000000..2e72549ec --- /dev/null +++ b/src/main/java/org/springframework/cheapy/service/NuOfferService.java @@ -0,0 +1,28 @@ +package org.springframework.cheapy.service; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cheapy.model.NuOffer; +import org.springframework.cheapy.repository.NuOfferRepository; +import org.springframework.dao.DataAccessException; +import org.springframework.stereotype.Service; + +@Service +public class NuOfferService { + private NuOfferRepository nuOfferRepository; + + + @Autowired + public NuOfferService(final NuOfferRepository nuOfferRepository) { + this.nuOfferRepository = nuOfferRepository; + } + + public NuOffer findNuOfferById(final int id) { + return this.nuOfferRepository.findNuOfferById(id); + } + + + public void saveNuOffer(final NuOffer nuOffer) throws DataAccessException { // + this.nuOfferRepository.save(nuOffer); + + } +} diff --git a/src/main/java/org/springframework/cheapy/service/TimeOfferService.java b/src/main/java/org/springframework/cheapy/service/TimeOfferService.java new file mode 100644 index 000000000..4f1fd6867 --- /dev/null +++ b/src/main/java/org/springframework/cheapy/service/TimeOfferService.java @@ -0,0 +1,28 @@ +package org.springframework.cheapy.service; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cheapy.model.TimeOffer; +import org.springframework.cheapy.repository.TimeOfferRepository; +import org.springframework.dao.DataAccessException; +import org.springframework.stereotype.Service; + +@Service +public class TimeOfferService { + private TimeOfferRepository TimeOfferRepository; + + + @Autowired + public TimeOfferService(final TimeOfferRepository TimeOfferRepository) { + this.TimeOfferRepository = TimeOfferRepository; + } + + public TimeOffer findTimeOfferById(final int id) { + return this.TimeOfferRepository.findTimeOfferById(id); + } + + + public void saveTimeOffer(final TimeOffer TimeOffer) throws DataAccessException { // + this.TimeOfferRepository.save(TimeOffer); + + } +} diff --git a/src/main/java/org/springframework/cheapy/web/NuOfferController.java b/src/main/java/org/springframework/cheapy/web/NuOfferController.java new file mode 100644 index 000000000..5beb60eac --- /dev/null +++ b/src/main/java/org/springframework/cheapy/web/NuOfferController.java @@ -0,0 +1,84 @@ +package org.springframework.cheapy.web; + +import java.util.Map; + +import javax.validation.Valid; + +import org.springframework.cheapy.model.Client; +import org.springframework.cheapy.model.NuOffer; +import org.springframework.cheapy.model.StatusOffer; +import org.springframework.cheapy.service.ClientService; +import org.springframework.cheapy.service.NuOfferService; +import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.WebDataBinder; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.InitBinder; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; + + +@Controller +public class NuOfferController { + + private static final String VIEWS_NU_OFFER_CREATE_OR_UPDATE_FORM = "nuOffers/createOrUpdateNuOfferForm"; + + private final NuOfferService nuOfferService; + private final ClientService clientService; + + + + public NuOfferController(final NuOfferService nuOfferService,ClientService clientService) { + this.nuOfferService = nuOfferService; + this.clientService = clientService; + + } + + @InitBinder + public void setAllowedFields(WebDataBinder dataBinder) { + dataBinder.setDisallowedFields("id"); + } + + @GetMapping("/nuOffers/new") + public String initCreationForm(Map model) { + NuOffer nuOffer = new NuOffer(); + model.put("nuOffer", nuOffer); + return VIEWS_NU_OFFER_CREATE_OR_UPDATE_FORM; + } + + @PostMapping("/nuOffers/new") + public String processCreationForm(@Valid NuOffer nuOffer, BindingResult result) { + if (result.hasErrors()) { + return VIEWS_NU_OFFER_CREATE_OR_UPDATE_FORM; + } + else { + nuOffer.setType(StatusOffer.hidden); + + Client client = this.clientService.getCurrentClient(); + + nuOffer.setClient(client); + + + this.nuOfferService.saveNuOffer(nuOffer); + return "redirect:/nuOffers/" + nuOffer.getId(); + } + } + @GetMapping(value ="/nuOffers/{nuOfferId}/activate") + public String activateNuOffer(@PathVariable("nuOfferId") final int nuOfferId, final ModelMap modelMap) { + Client client = this.clientService.getCurrentClient(); + NuOffer nuOffer=this.nuOfferService.findNuOfferById(nuOfferId); + if(nuOffer.getClient().equals(client)) { + nuOffer.setType(StatusOffer.active); + nuOffer.setCode("NU-"+nuOfferId); + + return "redirect:/nuOffers/" + nuOffer.getId(); + } else { + modelMap.addAttribute("message", "You don't have access to this number offer"); + } + return "redirect:/nuOffers/" + nuOffer.getId(); + + + } + +} diff --git a/src/main/java/org/springframework/cheapy/web/TimeOfferController.java b/src/main/java/org/springframework/cheapy/web/TimeOfferController.java new file mode 100644 index 000000000..89412985b --- /dev/null +++ b/src/main/java/org/springframework/cheapy/web/TimeOfferController.java @@ -0,0 +1,84 @@ +package org.springframework.cheapy.web; + +import java.util.Map; + +import javax.validation.Valid; + +import org.springframework.cheapy.model.Client; +import org.springframework.cheapy.model.TimeOffer; +import org.springframework.cheapy.model.StatusOffer; +import org.springframework.cheapy.service.ClientService; +import org.springframework.cheapy.service.TimeOfferService; +import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.WebDataBinder; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.InitBinder; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; + + +@Controller +public class TimeOfferController { + + private static final String VIEWS_NU_OFFER_CREATE_OR_UPDATE_FORM = "timeOffers/createOrUpdateTimeOfferForm"; + + private final TimeOfferService timeOfferService; + private final ClientService clientService; + + + + public TimeOfferController(final TimeOfferService timeOfferService,ClientService clientService) { + this.timeOfferService = timeOfferService; + this.clientService = clientService; + + } + + @InitBinder + public void setAllowedFields(WebDataBinder dataBinder) { + dataBinder.setDisallowedFields("id"); + } + + @GetMapping("/timeOffers/new") + public String initCreationForm(Map model) { + TimeOffer timeOffer = new TimeOffer(); + model.put("timeOffer", timeOffer); + return VIEWS_NU_OFFER_CREATE_OR_UPDATE_FORM; + } + + @PostMapping("/timeOffers/new") + public String processCreationForm(@Valid TimeOffer timeOffer, BindingResult result) { + if (result.hasErrors()) { + return VIEWS_NU_OFFER_CREATE_OR_UPDATE_FORM; + } + else { + timeOffer.setType(StatusOffer.hidden); + + Client client = this.clientService.getCurrentClient(); + + timeOffer.setClient(client); + + + this.timeOfferService.saveTimeOffer(timeOffer); + return "redirect:/TimeOffers/" + timeOffer.getId(); + } + } + @GetMapping(value ="/timeOffers/{timeOfferId}/activate") + public String activateTimeOffer(@PathVariable("timeOfferId") final int timeOfferId, final ModelMap modelMap) { + Client client = this.clientService.getCurrentClient(); + TimeOffer timeOffer=this.timeOfferService.findTimeOfferById(timeOfferId); + if(timeOffer.getClient().equals(client)) { + timeOffer.setType(StatusOffer.active); + timeOffer.setCode("TI-"+timeOfferId); + + return "redirect:/timeOffers/" + timeOffer.getId(); + } else { + modelMap.addAttribute("message", "You don't have access to this number offer"); + } + return "redirect:/timeOffers/" + timeOffer.getId(); + + + } + +} diff --git a/src/main/webapp/WEB-INF/jsp/nuOffers/createOrUpdateNuOfferForm.jsp b/src/main/webapp/WEB-INF/jsp/nuOffers/createOrUpdateNuOfferForm.jsp new file mode 100644 index 000000000..e0d2366cd --- /dev/null +++ b/src/main/webapp/WEB-INF/jsp/nuOffers/createOrUpdateNuOfferForm.jsp @@ -0,0 +1,39 @@ +<%@ page session="false" trimDirectiveWhitespaces="true" %> +<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> +<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> +<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> +<%@ taglib prefix="petclinic" tagdir="/WEB-INF/tags" %> + + +

+ New NuOffer +

+ +
+ + + + + + + + + + +
+
+
+ + + + + + + + +
+
+
+
diff --git a/src/main/webapp/WEB-INF/jsp/timeOffers/createOrUpdateTimeOfferForm.jsp b/src/main/webapp/WEB-INF/jsp/timeOffers/createOrUpdateTimeOfferForm.jsp new file mode 100644 index 000000000..0dc37f439 --- /dev/null +++ b/src/main/webapp/WEB-INF/jsp/timeOffers/createOrUpdateTimeOfferForm.jsp @@ -0,0 +1,36 @@ +<%@ page session="false" trimDirectiveWhitespaces="true" %> +<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> +<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> +<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> +<%@ taglib prefix="petclinic" tagdir="/WEB-INF/tags" %> + + +

+ New TimeOffer +

+ +
+ + + + + + + +
+
+
+ + + + + + + + +
+
+
+
diff --git a/src/test/java/org/springframework/cheapy/web/OwnerControllerTests.java b/src/test/java/org/springframework/cheapy/web/OwnerControllerTests.java index 9e5997489..62f38dca5 100644 --- a/src/test/java/org/springframework/cheapy/web/OwnerControllerTests.java +++ b/src/test/java/org/springframework/cheapy/web/OwnerControllerTests.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - +/* package org.springframework.cheapy.web; import static org.hamcrest.Matchers.hasProperty; @@ -40,6 +40,7 @@ import org.springframework.test.web.servlet.MockMvc; * * @author Colin But */ +/* @WebMvcTest(OwnerController.class) class OwnerControllerTests { @@ -161,3 +162,4 @@ class OwnerControllerTests { } } +*/ \ No newline at end of file From 88b304f72abb0f9b9a610a6f9740cb07dd1ccb45 Mon Sep 17 00:00:00 2001 From: Antonio Vidal Date: Thu, 25 Mar 2021 18:00:19 +0100 Subject: [PATCH 04/14] Los shows funcionan pero hay un error con las TimeOffer, parece que los jsp no reconocen el tipo LocalTime --- .../cheapy/web/FoodOfferController.java | 67 +++++++++++++++++++ .../cheapy/web/TimeOfferController.java | 54 +++++++++++++++ .../WEB-INF/jsp/foodOffers/foodOffersShow.jsp | 44 ++++++++++++ .../WEB-INF/jsp/nuOffers/nuOffersShow.jsp | 2 +- .../WEB-INF/jsp/timeOffers/timeOffersShow.jsp | 31 +++++++++ 5 files changed, 197 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/springframework/cheapy/web/FoodOfferController.java create mode 100644 src/main/java/org/springframework/cheapy/web/TimeOfferController.java create mode 100644 src/main/webapp/WEB-INF/jsp/foodOffers/foodOffersShow.jsp create mode 100644 src/main/webapp/WEB-INF/jsp/timeOffers/timeOffersShow.jsp diff --git a/src/main/java/org/springframework/cheapy/web/FoodOfferController.java b/src/main/java/org/springframework/cheapy/web/FoodOfferController.java new file mode 100644 index 000000000..06afaed26 --- /dev/null +++ b/src/main/java/org/springframework/cheapy/web/FoodOfferController.java @@ -0,0 +1,67 @@ +/* + * Copyright 2012-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.cheapy.web; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import org.springframework.cheapy.model.FoodOffer; +import org.springframework.cheapy.model.NuOffer; +import org.springframework.cheapy.model.SpeedOffer; +import org.springframework.cheapy.model.TimeOffer; +import org.springframework.cheapy.service.FoodOfferService; +import org.springframework.cheapy.service.NuOfferService; +import org.springframework.cheapy.service.SpeedOfferService; +import org.springframework.cheapy.service.TimeOfferService; +import org.springframework.stereotype.Controller; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; + + +/** + * @author Juergen Hoeller + * @author Ken Krebs + * @author Arjen Poutsma + * @author Michael Isvy + */ +@Controller +public class FoodOfferController { + + //private static final String VIEWS_OWNER_CREATE_OR_UPDATE_FORM = "owners/createOrUpdateOwnerForm"; + + private final FoodOfferService foodOfferService; + + + + public FoodOfferController(final FoodOfferService foodOfferService) { + this.foodOfferService = foodOfferService; + + } + + @GetMapping("/offers/food/{foodOfferId}") + public String processShowForm(@PathVariable("foodOfferId") int foodOfferId, Map model) { + + FoodOffer foodOffer=this.foodOfferService.findFoodOfferById(foodOfferId); + + model.put("foodOffer", foodOffer); + + return "foodOffers/foodOffersShow"; + + } + +} diff --git a/src/main/java/org/springframework/cheapy/web/TimeOfferController.java b/src/main/java/org/springframework/cheapy/web/TimeOfferController.java new file mode 100644 index 000000000..703b2b846 --- /dev/null +++ b/src/main/java/org/springframework/cheapy/web/TimeOfferController.java @@ -0,0 +1,54 @@ +/* + * Copyright 2012-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.cheapy.web; + +import java.util.Map; + +import org.springframework.cheapy.model.TimeOffer; +import org.springframework.cheapy.service.TimeOfferService; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; + + +/** + * @author Juergen Hoeller + * @author Ken Krebs + * @author Arjen Poutsma + * @author Michael Isvy + */ +@Controller +public class TimeOfferController { + + //private static final String VIEWS_OWNER_CREATE_OR_UPDATE_FORM = "owners/createOrUpdateOwnerForm"; + private final TimeOfferService timeOfferService; + + public TimeOfferController(final TimeOfferService timeOfferService) { + this.timeOfferService = timeOfferService; + + } + + @GetMapping("/offers/time/{timeOfferId}") + public String processShowForm(@PathVariable("timeOfferId") int timeOfferId, Map model) { + + TimeOffer timeOffer=this.timeOfferService.findTimeOfferById(timeOfferId); + + model.put("timeOffer", timeOffer); + + return "timeOffers/timeOffersShow"; + + } +} diff --git a/src/main/webapp/WEB-INF/jsp/foodOffers/foodOffersShow.jsp b/src/main/webapp/WEB-INF/jsp/foodOffers/foodOffersShow.jsp new file mode 100644 index 000000000..0b7c8785a --- /dev/null +++ b/src/main/webapp/WEB-INF/jsp/foodOffers/foodOffersShow.jsp @@ -0,0 +1,44 @@ +<%@ page session="false" trimDirectiveWhitespaces="true" %> +<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> +<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ taglib prefix="cheapy" tagdir="/WEB-INF/tags" %> + + + +

Oferta por plato específico

+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
Inicio de la oferta
Fin de la oferta
Plato en oferta
Descuento
Cantidad
Codigo de la oferta
+ + <%-- + + + Edit Owner --%> + +
diff --git a/src/main/webapp/WEB-INF/jsp/nuOffers/nuOffersShow.jsp b/src/main/webapp/WEB-INF/jsp/nuOffers/nuOffersShow.jsp index 9c4eca9f3..431d2a4d6 100644 --- a/src/main/webapp/WEB-INF/jsp/nuOffers/nuOffersShow.jsp +++ b/src/main/webapp/WEB-INF/jsp/nuOffers/nuOffersShow.jsp @@ -4,7 +4,7 @@ <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="cheapy" tagdir="/WEB-INF/tags" %> - +

Oferta por número de comensales

diff --git a/src/main/webapp/WEB-INF/jsp/timeOffers/timeOffersShow.jsp b/src/main/webapp/WEB-INF/jsp/timeOffers/timeOffersShow.jsp new file mode 100644 index 000000000..a344a6221 --- /dev/null +++ b/src/main/webapp/WEB-INF/jsp/timeOffers/timeOffersShow.jsp @@ -0,0 +1,31 @@ +<%@ page session="false" trimDirectiveWhitespaces="true" %> +<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> +<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ taglib prefix="cheapy" tagdir="/WEB-INF/tags" %> + + + +

Oferta por franja horária

+ + + + + + + + + + + + + + + + + + + +
Inicio de la oferta
Fin de la oferta
Descuento
Codigo de la oferta
+ +
From a041e31a504b00071f6d8cbb94b2e012d3f62023 Mon Sep 17 00:00:00 2001 From: Antonio Vidal Date: Thu, 25 Mar 2021 18:30:33 +0100 Subject: [PATCH 05/14] Actualizado el listar. El show de TimeOffer no le gusta los LocalTime --- .../webapp/WEB-INF/jsp/offers/offersList.jsp | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/main/webapp/WEB-INF/jsp/offers/offersList.jsp b/src/main/webapp/WEB-INF/jsp/offers/offersList.jsp index b7df4694d..54efc214d 100644 --- a/src/main/webapp/WEB-INF/jsp/offers/offersList.jsp +++ b/src/main/webapp/WEB-INF/jsp/offers/offersList.jsp @@ -15,18 +15,12 @@ Plato Fecha inicio Fecha fin - + - <%-- - - - - - --%> @@ -36,6 +30,12 @@ + + + + + Enlace + @@ -115,11 +115,11 @@ Fecha inicio Fecha fin - + - + @@ -128,12 +128,12 @@ - <%-- - + + - - --%> + Enlace + From f9452671bb88644a2da6ce26755d92f825d91c6c Mon Sep 17 00:00:00 2001 From: Thiloparn <48439369+Thiloparn@users.noreply.github.com> Date: Thu, 25 Mar 2021 19:12:46 +0100 Subject: [PATCH 06/14] Corrercion de errores --- .../java/org/springframework/cheapy/model/FoodOffer.java | 3 ++- .../java/org/springframework/cheapy/model/Offer.java | 9 ++++----- .../org/springframework/cheapy/model/SpeedOffer.java | 7 ++++--- .../springframework/cheapy/service/ClientService.java | 7 +++++++ .../springframework/cheapy/web/FoodOfferController.java | 8 ++++---- .../springframework/cheapy/web/SpeedOfferController.java | 6 +++--- src/main/resources/db/mysql/data.sql | 7 +++---- 7 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/main/java/org/springframework/cheapy/model/FoodOffer.java b/src/main/java/org/springframework/cheapy/model/FoodOffer.java index b82fed73b..ac3838a8a 100644 --- a/src/main/java/org/springframework/cheapy/model/FoodOffer.java +++ b/src/main/java/org/springframework/cheapy/model/FoodOffer.java @@ -18,6 +18,7 @@ package org.springframework.cheapy.model; import javax.persistence.Entity; import javax.persistence.Table; import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; @Entity @Table(name = "food_offers") @@ -29,7 +30,7 @@ public class FoodOffer extends Offer { @NotBlank private String discount; - @NotBlank + @NotNull private Integer units; // revisar public String getFood() { diff --git a/src/main/java/org/springframework/cheapy/model/Offer.java b/src/main/java/org/springframework/cheapy/model/Offer.java index 7ee58ff40..70a765526 100644 --- a/src/main/java/org/springframework/cheapy/model/Offer.java +++ b/src/main/java/org/springframework/cheapy/model/Offer.java @@ -17,15 +17,13 @@ package org.springframework.cheapy.model; import java.time.LocalDateTime; -import javax.persistence.Entity; import javax.persistence.EnumType; import javax.persistence.Enumerated; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.MappedSuperclass; -import javax.persistence.Table; import javax.validation.constraints.Future; -import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; import org.springframework.format.annotation.DateTimeFormat; @@ -33,12 +31,12 @@ import org.springframework.format.annotation.DateTimeFormat; public class Offer extends BaseEntity { @DateTimeFormat(pattern = "dd/MM/yyyy HH:mm") - @NotBlank + @NotNull @Future private LocalDateTime start; @DateTimeFormat(pattern = "dd/MM/yyyy HH:mm") - @NotBlank + @NotNull @Future private LocalDateTime end; @@ -47,6 +45,7 @@ public class Offer extends BaseEntity { @Enumerated(value = EnumType.STRING) private StatusOffer type; + @ManyToOne @JoinColumn(name="client_id") private Client client; diff --git a/src/main/java/org/springframework/cheapy/model/SpeedOffer.java b/src/main/java/org/springframework/cheapy/model/SpeedOffer.java index 0399d4baf..9d8ee0373 100644 --- a/src/main/java/org/springframework/cheapy/model/SpeedOffer.java +++ b/src/main/java/org/springframework/cheapy/model/SpeedOffer.java @@ -19,26 +19,27 @@ import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Table; import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; @Entity @Table(name = "speed_offers") public class SpeedOffer extends Offer { - @NotBlank + @NotNull private Integer gold; // x minutos @Column(name = "discount_gold") @NotBlank private String discountGold; - @NotBlank + @NotNull private Integer silver; @Column(name = "discount_silver") @NotBlank private String discountSilver; - @NotBlank + @NotNull private Integer bronze; @Column(name = "discount_bronze") diff --git a/src/main/java/org/springframework/cheapy/service/ClientService.java b/src/main/java/org/springframework/cheapy/service/ClientService.java index edc9773a9..7994110ad 100644 --- a/src/main/java/org/springframework/cheapy/service/ClientService.java +++ b/src/main/java/org/springframework/cheapy/service/ClientService.java @@ -16,8 +16,10 @@ package org.springframework.cheapy.service; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cheapy.model.Client; import org.springframework.cheapy.repository.ClientRepository; +import org.springframework.cheapy.repository.SpeedOfferRepository; import org.springframework.dao.DataAccessException; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; @@ -36,6 +38,11 @@ import org.springframework.transaction.annotation.Transactional; public class ClientService { private ClientRepository clientRepository; + + @Autowired + public ClientService(final ClientRepository clientRepository) { + this.clientRepository = clientRepository; + } @Transactional public Client getCurrentclient() throws DataAccessException { diff --git a/src/main/java/org/springframework/cheapy/web/FoodOfferController.java b/src/main/java/org/springframework/cheapy/web/FoodOfferController.java index 28a050574..f221a3600 100644 --- a/src/main/java/org/springframework/cheapy/web/FoodOfferController.java +++ b/src/main/java/org/springframework/cheapy/web/FoodOfferController.java @@ -73,17 +73,17 @@ public class FoodOfferController { } } - @GetMapping(value = "/foodOffers/{foodOfferid}/activate") - public String activateFoodOffer(@PathVariable("foodOffer") final int foodOfferId, final ModelMap modelMap) { + @GetMapping(value = "/foodOffers/{foodOfferId}/activate") + public String activateFoodOffer(@PathVariable("foodOfferId") final int foodOfferId, ModelMap modelMap) { FoodOffer foodOffer = this.foodOfferService.findFoodOfferById(foodOfferId); Client client = this.clientService.getCurrentclient(); if(foodOffer.getClient().equals(client)) { foodOffer.setType(StatusOffer.active); - foodOffer.setCode("SE-"+foodOfferId); + foodOffer.setCode("FE-"+foodOfferId); this.foodOfferService.saveFoodOffer(foodOffer); } else { modelMap.addAttribute("message", "You don't have access to this food offer"); } - return "redirect:/foodOffers/" + foodOffer.getId(); + return "redirect:/foodOffers/"; } } diff --git a/src/main/java/org/springframework/cheapy/web/SpeedOfferController.java b/src/main/java/org/springframework/cheapy/web/SpeedOfferController.java index f633dfaba..0456f9742 100644 --- a/src/main/java/org/springframework/cheapy/web/SpeedOfferController.java +++ b/src/main/java/org/springframework/cheapy/web/SpeedOfferController.java @@ -73,8 +73,8 @@ public class SpeedOfferController { } } - @GetMapping(value = "/speedOffers/{speedOfferid}/activate") - public String activateSpeedOffer(@PathVariable("speedOffer") final int speedOfferId, final ModelMap modelMap) { + @GetMapping(value = "/speedOffers/{speedOfferId}/activate") + public String activateSpeedOffer(@PathVariable("speedOfferId") final int speedOfferId, ModelMap modelMap) { SpeedOffer speedOffer = this.speedOfferService.findSpeedOfferById(speedOfferId); Client client = this.clientService.getCurrentclient(); if(speedOffer.getClient().equals(client)) { @@ -84,6 +84,6 @@ public class SpeedOfferController { } else { modelMap.addAttribute("message", "You don't have access to this speed offer"); } - return "redirect:/speedOffers/" + speedOffer.getId(); + return "redirect:/speedOffers/"; } } diff --git a/src/main/resources/db/mysql/data.sql b/src/main/resources/db/mysql/data.sql index 3dd3b74c7..5db5228c9 100644 --- a/src/main/resources/db/mysql/data.sql +++ b/src/main/resources/db/mysql/data.sql @@ -10,13 +10,12 @@ INSERT INTO owners VALUES (9, 'David', 'Schroeder', '2749 Blackhawk Trail', 'Mad INSERT INTO owners VALUES (10, 'Carlos', 'Estaban', '2335 Independence La.', 'Waunakee', '6085555487'); -INSERT INTO food_offers(start, end, code, type, client_id, food, discount, units) VALUES ('2021-06-15 12:00:00', '2021-06-16 12:00:00', 'jkhlljk', 'active', null, 'macarrones', '15%', 10); - +INSERT INTO food_offers(start, end, code, type, client_id, food, discount, units) VALUES ('2021-06-15 12:00:00', '2021-06-16 12:00:00', 'FE-1', 'active', null, 'macarrones', '15%', 10); +INSERT INTO speed_offers(start, end, code, type, client_id, gold, discount_gold, silver, discount_silver, bronze, discount_bronze) VALUES ('2021-06-15 12:00:00', '2021-06-16 12:00:00', 'SE-1', 'active', null, 5, '15%', 10, '10%', 15, '5%'); INSERT INTO time_offers(start, end, code, type, client_id, init, finish, discount) VALUES ('2021-06-15 12:00:00', '2021-06-16 12:00:00', 'jkhlljk', 'active', null, '12:00:00', '13:00:00', '10%'); --insert into usuarios(username, password, enabled) values ('admin3', 'admin', true); --insert into authorities(id ,usuario, authority) values (42,'admin3', 'admin'); INSERT INTO users(username,password,enabled) VALUES ('admin1','4dm1n',TRUE); -INSERT INTO authorities(id,username,authority) VALUES (1,'admin1','admin'); - +INSERT INTO authorities(id,username,authority) VALUES (1,'admin1','admin'); \ No newline at end of file From f2324efb9faa15e31f34a3d3904cdc0086b21473 Mon Sep 17 00:00:00 2001 From: Thiloparn <48439369+Thiloparn@users.noreply.github.com> Date: Thu, 25 Mar 2021 19:42:18 +0100 Subject: [PATCH 07/14] Update FoodOfferController.java --- .../org/springframework/cheapy/web/FoodOfferController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/springframework/cheapy/web/FoodOfferController.java b/src/main/java/org/springframework/cheapy/web/FoodOfferController.java index f221a3600..de3bdd8ee 100644 --- a/src/main/java/org/springframework/cheapy/web/FoodOfferController.java +++ b/src/main/java/org/springframework/cheapy/web/FoodOfferController.java @@ -79,7 +79,7 @@ public class FoodOfferController { Client client = this.clientService.getCurrentclient(); if(foodOffer.getClient().equals(client)) { foodOffer.setType(StatusOffer.active); - foodOffer.setCode("FE-"+foodOfferId); + foodOffer.setCode("FO-"+foodOfferId); this.foodOfferService.saveFoodOffer(foodOffer); } else { modelMap.addAttribute("message", "You don't have access to this food offer"); From 042f8f2731f961d9070a238b7feaf3876a12ef14 Mon Sep 17 00:00:00 2001 From: Thiloparn <48439369+Thiloparn@users.noreply.github.com> Date: Thu, 25 Mar 2021 19:42:41 +0100 Subject: [PATCH 08/14] Update SpeedOfferController.java --- .../org/springframework/cheapy/web/SpeedOfferController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/springframework/cheapy/web/SpeedOfferController.java b/src/main/java/org/springframework/cheapy/web/SpeedOfferController.java index 0456f9742..1db36201a 100644 --- a/src/main/java/org/springframework/cheapy/web/SpeedOfferController.java +++ b/src/main/java/org/springframework/cheapy/web/SpeedOfferController.java @@ -79,7 +79,7 @@ public class SpeedOfferController { Client client = this.clientService.getCurrentclient(); if(speedOffer.getClient().equals(client)) { speedOffer.setType(StatusOffer.active); - speedOffer.setCode("SE-"+speedOfferId); + speedOffer.setCode("SP-"+speedOfferId); this.speedOfferService.saveSpeedOffer(speedOffer); } else { modelMap.addAttribute("message", "You don't have access to this speed offer"); From e80e782d134aaafa41ba9f3c3d81ee08435e9c8a Mon Sep 17 00:00:00 2001 From: Thiloparn <48439369+Thiloparn@users.noreply.github.com> Date: Thu, 25 Mar 2021 19:43:28 +0100 Subject: [PATCH 09/14] Update data.sql --- src/main/resources/db/mysql/data.sql | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/resources/db/mysql/data.sql b/src/main/resources/db/mysql/data.sql index 5db5228c9..c2ded26d7 100644 --- a/src/main/resources/db/mysql/data.sql +++ b/src/main/resources/db/mysql/data.sql @@ -10,12 +10,12 @@ INSERT INTO owners VALUES (9, 'David', 'Schroeder', '2749 Blackhawk Trail', 'Mad INSERT INTO owners VALUES (10, 'Carlos', 'Estaban', '2335 Independence La.', 'Waunakee', '6085555487'); -INSERT INTO food_offers(start, end, code, type, client_id, food, discount, units) VALUES ('2021-06-15 12:00:00', '2021-06-16 12:00:00', 'FE-1', 'active', null, 'macarrones', '15%', 10); -INSERT INTO speed_offers(start, end, code, type, client_id, gold, discount_gold, silver, discount_silver, bronze, discount_bronze) VALUES ('2021-06-15 12:00:00', '2021-06-16 12:00:00', 'SE-1', 'active', null, 5, '15%', 10, '10%', 15, '5%'); +INSERT INTO food_offers(start, end, code, type, client_id, food, discount, units) VALUES ('2021-06-15 12:00:00', '2021-06-16 12:00:00', 'FO-1', 'active', null, 'macarrones', '15%', 10); +INSERT INTO speed_offers(start, end, code, type, client_id, gold, discount_gold, silver, discount_silver, bronze, discount_bronze) VALUES ('2021-06-15 12:00:00', '2021-06-16 12:00:00', 'SP-1', 'active', null, 5, '15%', 10, '10%', 15, '5%'); INSERT INTO time_offers(start, end, code, type, client_id, init, finish, discount) VALUES ('2021-06-15 12:00:00', '2021-06-16 12:00:00', 'jkhlljk', 'active', null, '12:00:00', '13:00:00', '10%'); --insert into usuarios(username, password, enabled) values ('admin3', 'admin', true); --insert into authorities(id ,usuario, authority) values (42,'admin3', 'admin'); INSERT INTO users(username,password,enabled) VALUES ('admin1','4dm1n',TRUE); -INSERT INTO authorities(id,username,authority) VALUES (1,'admin1','admin'); \ No newline at end of file +INSERT INTO authorities(id,username,authority) VALUES (1,'admin1','admin'); From fbd5813f863f8b879603c4aabb1bc245b05efbd0 Mon Sep 17 00:00:00 2001 From: David Date: Thu, 25 Mar 2021 20:15:34 +0100 Subject: [PATCH 10/14] Vista de login --- pom.xml | 3 +++ .../cheapy/configuration/SecurityConfiguration.java | 8 ++++---- .../cheapy/system/LoginController.java | 9 ++++++++- src/main/webapp/WEB-INF/jsp/login.jsp | 12 +++++++----- .../WEB-INF/jsp/owners/createOrUpdateOwnerForm.jsp | 3 +-- src/main/webapp/WEB-INF/jsp/owners/findOwners.jsp | 3 +-- src/main/webapp/WEB-INF/jsp/owners/ownerDetails.jsp | 3 +-- src/main/webapp/WEB-INF/jsp/owners/ownersList.jsp | 3 +-- .../webapp/WEB-INF/jsp/users/createOwnerForm.jsp | 3 +-- src/main/webapp/WEB-INF/tags/menu.tag | 8 ++++---- src/main/webapp/WEB-INF/tags/menuItem.tag | 3 +-- 11 files changed, 32 insertions(+), 26 deletions(-) diff --git a/pom.xml b/pom.xml index 60be53522..38e0cd40b 100644 --- a/pom.xml +++ b/pom.xml @@ -135,7 +135,10 @@ spring-boot-devtools true + + + diff --git a/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java b/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java index 677bb736d..673ad7a35 100644 --- a/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java +++ b/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java @@ -36,15 +36,15 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter { .antMatchers(HttpMethod.GET, "/", "/oups").permitAll() .antMatchers("/users/new").permitAll() .antMatchers("/login/**").anonymous() + .antMatchers("/logout").permitAll() .antMatchers("/usuarios/new").permitAll() .antMatchers("/admin/**").hasAnyAuthority("admin") .antMatchers("/owners/**").hasAnyAuthority("owner", "admin") .antMatchers("/vets/**").authenticated().anyRequest().anonymous() .and().formLogin() - .loginPage("/login") - .successForwardUrl("/") - .failureUrl("/login?error") - .and().logout().logoutUrl("/login?logout"); + .loginPage("/login").permitAll() + .failureUrl("/login?error") + .and().logout().logoutSuccessUrl("/login"); // Configuración para que funcione la consola de administración // de la BD H2 (deshabilitar las cabeceras de protección contra diff --git a/src/main/java/org/springframework/cheapy/system/LoginController.java b/src/main/java/org/springframework/cheapy/system/LoginController.java index e0e0fa7a9..e26025570 100644 --- a/src/main/java/org/springframework/cheapy/system/LoginController.java +++ b/src/main/java/org/springframework/cheapy/system/LoginController.java @@ -16,6 +16,9 @@ package org.springframework.cheapy.system; +import org.springframework.security.authentication.AnonymousAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @@ -24,7 +27,11 @@ class LoginController { @GetMapping("/login") public String login() { - return "login"; + Authentication authentication= SecurityContextHolder.getContext().getAuthentication(); + if(authentication==null || authentication instanceof AnonymousAuthenticationToken) { + return "login"; + } + return "redirect:/"; } diff --git a/src/main/webapp/WEB-INF/jsp/login.jsp b/src/main/webapp/WEB-INF/jsp/login.jsp index bf718d6e4..796cdceeb 100644 --- a/src/main/webapp/WEB-INF/jsp/login.jsp +++ b/src/main/webapp/WEB-INF/jsp/login.jsp @@ -4,9 +4,9 @@ <%@ taglib prefix="cheapy" tagdir="/WEB-INF/tags" %> <%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %> - +