diff --git a/prototype/src/main/java/org/hso/ecommerce/app/RequestController.java b/prototype/src/main/java/org/hso/ecommerce/app/RequestController.java index 36b47ce..bb0b5d6 100644 --- a/prototype/src/main/java/org/hso/ecommerce/app/RequestController.java +++ b/prototype/src/main/java/org/hso/ecommerce/app/RequestController.java @@ -46,11 +46,17 @@ public class RequestController { return "login"; } - if (!user.get().validatePassword(password)) { + if (!user.get().validatePassword(password)) { request.setAttribute("error", "Passwort falsch."); response.setStatus(HttpServletResponse.SC_EXPECTATION_FAILED); return "login"; } + + if (!user.get().isActive) { + request.setAttribute("error", "User ist deaktiviert."); + response.setStatus(HttpServletResponse.SC_EXPECTATION_FAILED); + return "login"; + } session.setAttribute("userId", user.get().getId()); @@ -69,22 +75,6 @@ public class RequestController { return "redirect:/"; } - @GetMapping("/register") - public String register() { - return "register"; - } - - @PostMapping("/register") - public String registerPost( - @RequestParam("username") String username, - @RequestParam("password") String password, - @RequestParam("password2") String password2, - @RequestParam("type") String type - ) { - - return "redirect:/"; - } - @GetMapping("/intern/") public String intern() { return "intern/index"; diff --git a/prototype/src/main/java/org/hso/ecommerce/components/LoginIntercepter.java b/prototype/src/main/java/org/hso/ecommerce/components/LoginIntercepter.java index 8e1fc1d..e537867 100644 --- a/prototype/src/main/java/org/hso/ecommerce/components/LoginIntercepter.java +++ b/prototype/src/main/java/org/hso/ecommerce/components/LoginIntercepter.java @@ -24,6 +24,7 @@ public class LoginIntercepter implements HandlerInterceptor { HttpSession session = request.getSession(); Object userId = session.getAttribute("userId"); + Optional user = null; if (request.getRequestURI().startsWith("/user/")) { System.out.println("USER"); @@ -43,10 +44,24 @@ public class LoginIntercepter implements HandlerInterceptor { response.sendRedirect("/login"); return false; } + + user = userRepository.findById((Long) userId); + + if(user.isPresent() && !user.get().isEmployee) + { + session.setAttribute("afterLogin", request.getRequestURI()); + response.sendRedirect("/"); + return false; + } + } + + if (!request.getRequestURI().startsWith("/login")) { + session.removeAttribute("afterLogin"); } if (userId != null) { - Optional user = userRepository.findById((Long) userId); + if (user == null) + user = userRepository.findById((Long) userId); user.ifPresent(value -> request.setAttribute("user", value)); } diff --git a/prototype/src/main/java/org/hso/ecommerce/controller/RegisterController.java b/prototype/src/main/java/org/hso/ecommerce/controller/RegisterController.java new file mode 100644 index 0000000..00ade5d --- /dev/null +++ b/prototype/src/main/java/org/hso/ecommerce/controller/RegisterController.java @@ -0,0 +1,74 @@ +package org.hso.ecommerce.controller; + +import org.hso.ecommerce.entities.shop.Address; +import org.hso.ecommerce.entities.user.User; +import org.hso.ecommerce.repos.user.UserRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.util.Optional; + +@Controller +public class RegisterController { + + @Autowired + private final UserRepository userRepository = null; + + @PostMapping("/register") + public String registerPost( + HttpServletRequest request, + HttpServletResponse response, + @RequestParam("username") String username, + @RequestParam("password") String password, + @RequestParam("password2") String password2, + @RequestParam("salutation") String salutation, + @RequestParam("name") String name, + @RequestParam("address") String address, + @RequestParam("type") String type, + @RequestParam("ad") String ad + ) + { + Optional user = userRepository.findByEmail(username); + if (user.isPresent()) { + request.setAttribute("error", "Email Adresse existiert bereits!"); + response.setStatus(HttpServletResponse.SC_EXPECTATION_FAILED); + return "register"; + } + + if (!password.equals(password2)){ + request.setAttribute("error", "Passwörter sind nicht gleich"); + response.setStatus(HttpServletResponse.SC_BAD_REQUEST); + return "register"; + } + + //set values for new user + User newUser = new User(); + newUser.email = username; + newUser.setPassword(password); + newUser.email = username; + newUser.isEmployee = false; + //TODO for salutation, type, ad are no attributes/fields in the class/database. Add when they are there. + + newUser.isActive = true; + newUser.created = new java.sql.Timestamp(System.currentTimeMillis()); + + Address newAddress = new Address(); + newAddress.name = name; + newAddress.addressString = address; + newUser.defaultDeliveryAddress = newAddress; + + userRepository.save(newUser); // save newUser + + return "login"; + } + + @GetMapping("/register") + public String register() { + return "register"; + } +} diff --git a/prototype/src/main/java/org/hso/ecommerce/entities/booking/PaymentMethod.java b/prototype/src/main/java/org/hso/ecommerce/entities/booking/PaymentMethod.java index 4f81062..af0a9c0 100644 --- a/prototype/src/main/java/org/hso/ecommerce/entities/booking/PaymentMethod.java +++ b/prototype/src/main/java/org/hso/ecommerce/entities/booking/PaymentMethod.java @@ -1,11 +1,10 @@ package org.hso.ecommerce.entities.booking; import javax.persistence.Embeddable; -import javax.validation.constraints.NotNull; @Embeddable public class PaymentMethod { - @NotNull + public String creditCardNumber; public static PaymentMethod fromCreditCarNumber(String cardnumber) {