diff --git a/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java b/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java index 542e41f26..25364f7b0 100644 --- a/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java +++ b/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java @@ -35,6 +35,10 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter { http.authorizeRequests().antMatchers("/resources/**", "/webjars/**", "/h2-console/**").permitAll() .antMatchers(HttpMethod.GET, "/", "/oups").permitAll() .antMatchers("/users/new").permitAll() + + .antMatchers("/clients/new").permitAll() + .antMatchers("/clients/edit").hasAnyAuthority("client") + .antMatchers("/clients/disable").hasAnyAuthority("client") .antMatchers("/login/**").anonymous() .antMatchers("/logout").authenticated() @@ -48,7 +52,6 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter { .antMatchers("/offers/**/new").hasAnyAuthority("admin", "client") .antMatchers("/offers/**/activate").hasAnyAuthority("admin","client") - .antMatchers("/clients/new").permitAll() .antMatchers("/offers").permitAll() .antMatchers("/offersCreate").hasAuthority("client") diff --git a/src/main/java/org/springframework/cheapy/model/Client.java b/src/main/java/org/springframework/cheapy/model/Client.java index e3f939728..2981c9d98 100644 --- a/src/main/java/org/springframework/cheapy/model/Client.java +++ b/src/main/java/org/springframework/cheapy/model/Client.java @@ -1,7 +1,6 @@ package org.springframework.cheapy.model; import java.util.List; -import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Entity; diff --git a/src/main/java/org/springframework/cheapy/model/User.java b/src/main/java/org/springframework/cheapy/model/User.java index e9c232562..4ef2dec13 100644 --- a/src/main/java/org/springframework/cheapy/model/User.java +++ b/src/main/java/org/springframework/cheapy/model/User.java @@ -36,4 +36,12 @@ public class User{ this.password = password; } + public boolean getEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + } diff --git a/src/main/java/org/springframework/cheapy/repository/ClientRepository.java b/src/main/java/org/springframework/cheapy/repository/ClientRepository.java index 1e04f6f3a..54c696f00 100644 --- a/src/main/java/org/springframework/cheapy/repository/ClientRepository.java +++ b/src/main/java/org/springframework/cheapy/repository/ClientRepository.java @@ -2,13 +2,15 @@ package org.springframework.cheapy.repository; import org.springframework.cheapy.model.Client; import org.springframework.data.jpa.repository.Query; -import org.springframework.data.repository.CrudRepository; +import org.springframework.data.repository.Repository; import org.springframework.transaction.annotation.Transactional; -public interface ClientRepository extends CrudRepository { +public interface ClientRepository extends Repository { @Query("SELECT client FROM Client client WHERE username =:username") @Transactional(readOnly = true) Client findByUsername(String username); + void save(Client client); + } diff --git a/src/main/java/org/springframework/cheapy/service/ClientService.java b/src/main/java/org/springframework/cheapy/service/ClientService.java index d65649680..c8404afb9 100644 --- a/src/main/java/org/springframework/cheapy/service/ClientService.java +++ b/src/main/java/org/springframework/cheapy/service/ClientService.java @@ -26,4 +26,8 @@ public class ClientService { return this.clientRepository.findByUsername(username); } + public void saveClient(final Client client) throws DataAccessException { + this.clientRepository.save(client); + } + } diff --git a/src/main/java/org/springframework/cheapy/web/ClientController.java b/src/main/java/org/springframework/cheapy/web/ClientController.java new file mode 100644 index 000000000..47ba8a918 --- /dev/null +++ b/src/main/java/org/springframework/cheapy/web/ClientController.java @@ -0,0 +1,115 @@ + +package org.springframework.cheapy.web; + +import java.util.Map; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.validation.Valid; + +import org.springframework.beans.BeanUtils; +import org.springframework.cheapy.model.Client; +import org.springframework.cheapy.model.FoodOffer; +import org.springframework.cheapy.service.ClientService; +import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; + +@Controller +public class ClientController { + + private static final String VIEWS_CREATE_OR_UPDATE_CLIENT = "clients/createOrUpdateClientForm"; + + private final ClientService clientService; + + public ClientController(final ClientService clientService) { + this.clientService = clientService; + } + + + + private boolean checkDates(final FoodOffer foodOffer) { + boolean res = false; + if(foodOffer.getEnd()==null || foodOffer.getStart()==null || foodOffer.getEnd().isAfter(foodOffer.getStart())) { + res = true; + } + return res; + } + + + + + @GetMapping("/clients/show") + public String processShowForm(Map model) { + + Client client = this.clientService.getCurrentClient(); + + + model.put("client", client); + return "clients/clientShow"; + + + + } + + @GetMapping(value = "/clients/edit") + public String updateClient( final ModelMap model, HttpServletRequest request) { + + Client client = this.clientService.getCurrentClient(); + + model.addAttribute("client", client); + + return ClientController.VIEWS_CREATE_OR_UPDATE_CLIENT; + } + + @PostMapping(value = "/clients/edit") + public String updateClient(@Valid final Client clientEdit, final BindingResult result, + final ModelMap model, HttpServletRequest request) { + + + Client clienteSesion = this.clientService.getCurrentClient(); + + + + if (result.hasErrors()) { + model.addAttribute("client", clientEdit); + return ClientController.VIEWS_CREATE_OR_UPDATE_CLIENT; + } + + BeanUtils.copyProperties(clienteSesion, clientEdit, "name", "email", "address","init", "finish","telephone", "description","food","usuar"); + clientEdit.getUsuar().setUsername(clienteSesion.getUsuar().getUsername()); + clientEdit.getUsuar().setEnabled(true); + this.clientService.saveClient(clientEdit); + return "redirect:/clients/show"; + + } + + @GetMapping(value = "/clients/disable") + public String disableClient(final ModelMap model) { + + Client client = this.clientService.getCurrentClient(); + model.put("client", client); + return "/clients/clientDisable"; + } + + @PostMapping(value = "/clients/disable") + public String disableClientForm(final ModelMap model, HttpServletRequest request) { + + + Client client = this.clientService.getCurrentClient(); + + client.getUsuar().setEnabled(false); + + this.clientService.saveClient(client); + + try { + request.logout(); + } catch (ServletException e) { + + } + return "redirect:/login"; + + } +} diff --git a/src/main/java/org/springframework/cheapy/web/OfertaController.java b/src/main/java/org/springframework/cheapy/web/OfertaController.java index 60567c491..996773042 100644 --- a/src/main/java/org/springframework/cheapy/web/OfertaController.java +++ b/src/main/java/org/springframework/cheapy/web/OfertaController.java @@ -13,9 +13,6 @@ 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.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; diff --git a/src/main/webapp/WEB-INF/jsp/clients/clientDisable.jsp b/src/main/webapp/WEB-INF/jsp/clients/clientDisable.jsp new file mode 100644 index 000000000..5961d075c --- /dev/null +++ b/src/main/webapp/WEB-INF/jsp/clients/clientDisable.jsp @@ -0,0 +1,25 @@ +<%@ page session="false" trimDirectiveWhitespaces="true"%> +<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> +<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> +<%@ taglib prefix="cheapy" tagdir="/WEB-INF/tags"%> +<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%> +<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> + + + + +

¿Está seguro de que quiere eliminar su cuenta?

+ + + +
+ +
+
+ +
+ +
diff --git a/src/main/webapp/WEB-INF/jsp/clients/clientShow.jsp b/src/main/webapp/WEB-INF/jsp/clients/clientShow.jsp new file mode 100644 index 000000000..398ba4d07 --- /dev/null +++ b/src/main/webapp/WEB-INF/jsp/clients/clientShow.jsp @@ -0,0 +1,78 @@ +<%@ 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="cheapy" tagdir="/WEB-INF/tags" %> +<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %> + + + + +

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + +
+ + + + + + + + + + +
+
+
+ + +
diff --git a/src/main/webapp/WEB-INF/jsp/clients/createOrUpdateClientForm.jsp b/src/main/webapp/WEB-INF/jsp/clients/createOrUpdateClientForm.jsp new file mode 100644 index 000000000..bd21204d0 --- /dev/null +++ b/src/main/webapp/WEB-INF/jsp/clients/createOrUpdateClientForm.jsp @@ -0,0 +1,51 @@ +<%@ 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="cheapy" tagdir="/WEB-INF/tags" %> + + + +

+ +

+ + +
+ + + + + + + + + + + + + +
+
+
+
+ + + + + + + + +
+
+
+
+ +
diff --git a/src/main/webapp/WEB-INF/jsp/welcome.jsp b/src/main/webapp/WEB-INF/jsp/welcome.jsp index a2622ed5a..c5bc812e3 100644 --- a/src/main/webapp/WEB-INF/jsp/welcome.jsp +++ b/src/main/webapp/WEB-INF/jsp/welcome.jsp @@ -33,6 +33,11 @@ +
+ +