diff --git a/build.gradle b/build.gradle index b5f8c19..436ff55 100644 --- a/build.gradle +++ b/build.gradle @@ -31,8 +31,13 @@ dependencies { implementation 'org.hibernate.validator:hibernate-validator' + implementation 'org.thymeleaf:thymeleaf' + implementation 'org.springframework.boot:spring-boot-starter-security' + implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6' + implementation 'org.springdoc:springdoc-openapi-ui:1.6.5' implementation 'org.projectlombok:lombok:1.18.22' + implementation 'org.springframework.boot:spring-boot-starter-actuator' testImplementation 'org.springframework.boot:spring-boot-starter-test' diff --git a/src/main/java/com/example/demo/MvcController.java b/src/main/java/com/example/demo/MvcController.java new file mode 100644 index 0000000..71fd97b --- /dev/null +++ b/src/main/java/com/example/demo/MvcController.java @@ -0,0 +1,15 @@ +package com.example.demo; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +@RequestMapping(value = {"", "/"}) +public class MvcController { + @GetMapping("") + public String getUserPage(Model model) { + return "redirect:/master"; + } +} diff --git a/src/main/java/com/example/demo/configuration/PasswordEncoderConfiguration.java b/src/main/java/com/example/demo/configuration/PasswordEncoderConfiguration.java new file mode 100644 index 0000000..6937121 --- /dev/null +++ b/src/main/java/com/example/demo/configuration/PasswordEncoderConfiguration.java @@ -0,0 +1,14 @@ +package com.example.demo.configuration; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.crypto.password.PasswordEncoder; + +@Configuration +public class PasswordEncoderConfiguration { + @Bean + public PasswordEncoder createPasswordEncoder() { + return new BCryptPasswordEncoder(); + } +} diff --git a/src/main/java/com/example/demo/configuration/SecurityConfiguration.java b/src/main/java/com/example/demo/configuration/SecurityConfiguration.java new file mode 100644 index 0000000..60609dd --- /dev/null +++ b/src/main/java/com/example/demo/configuration/SecurityConfiguration.java @@ -0,0 +1,76 @@ +package com.example.demo.configuration; + + +import com.example.demo.master.MasterRole; +import com.example.demo.master.MasterService; +import com.example.demo.master.MasterSignupMvcController; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.HttpMethod; +import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer; +import org.springframework.security.web.SecurityFilterChain; + +@Configuration +@EnableWebSecurity +@EnableMethodSecurity( + securedEnabled = true +) +public class SecurityConfiguration { + private final Logger log = LoggerFactory.getLogger(SecurityConfiguration.class); + private static final String LOGIN_URL = "/login"; + private final MasterService masterService; + + public SecurityConfiguration(MasterService masterService) { + this.masterService = masterService; + createAdminOnStartup(); + } + + private void createAdminOnStartup() { + final String admin = "admin"; + if (masterService.findMaster(admin) == null) { + log.info("Admin user successfully created"); + masterService.addMaster(admin, admin, admin, admin, MasterRole.ADMIN); + } + } + + @Bean + public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { + http.headers().frameOptions().sameOrigin().and() + .cors().and() + .csrf().disable() + .authorizeHttpRequests() + .requestMatchers(MasterSignupMvcController.SIGNUP_URL).permitAll() + .requestMatchers(HttpMethod.GET, LOGIN_URL).permitAll() + .anyRequest().authenticated() + .and() + .formLogin() + .loginPage(LOGIN_URL).permitAll() + .and() + .logout().permitAll(); + return http.build(); + } + + @Bean + public AuthenticationManager authenticationManagerBean(HttpSecurity http) throws Exception { + AuthenticationManagerBuilder authenticationManagerBuilder = http + .getSharedObject(AuthenticationManagerBuilder.class); + authenticationManagerBuilder.userDetailsService(masterService); + return authenticationManagerBuilder.build(); + } + + @Bean + public WebSecurityCustomizer webSecurityCustomizer() { + return (web) -> web.ignoring() + .requestMatchers("/css/**") + .requestMatchers("/js/**") + .requestMatchers("/templates/**") + .requestMatchers("/webjars/**"); + } +} \ No newline at end of file diff --git a/src/main/java/com/example/demo/WebConfiguration.java b/src/main/java/com/example/demo/configuration/WebConfiguration.java similarity index 68% rename from src/main/java/com/example/demo/WebConfiguration.java rename to src/main/java/com/example/demo/configuration/WebConfiguration.java index ba35358..c4db666 100644 --- a/src/main/java/com/example/demo/WebConfiguration.java +++ b/src/main/java/com/example/demo/configuration/WebConfiguration.java @@ -1,4 +1,4 @@ -package com.example.demo; +package com.example.demo.configuration; import org.springframework.boot.web.server.ErrorPage; import org.springframework.boot.web.server.WebServerFactoryCustomizer; @@ -20,15 +20,7 @@ public class WebConfiguration implements WebMvcConfigurer { @Override public void addViewControllers(ViewControllerRegistry registry) { - ViewControllerRegistration registration = registry.addViewController("/notFound"); - registration.setViewName("forward:/index.html"); - registration.setStatusCode(HttpStatus.OK); - } - - @Bean - public WebServerFactoryCustomizer containerCustomizer() { - return container -> { - container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/notFound")); - }; + WebMvcConfigurer.super.addViewControllers(registry); + registry.addViewController("login"); } } \ No newline at end of file diff --git a/src/main/java/com/example/demo/master/Master.java b/src/main/java/com/example/demo/master/Master.java index 0995eae..b63e9e1 100644 --- a/src/main/java/com/example/demo/master/Master.java +++ b/src/main/java/com/example/demo/master/Master.java @@ -1,9 +1,7 @@ package com.example.demo.master; -import com.example.demo.order.Order; import jakarta.persistence.*; -import java.util.List; import java.util.Objects; @Entity @@ -19,14 +17,17 @@ public class Master { private String email; private String password; + private MasterRole role; + public Master() { } - public Master(String firstName, String lastName, String email, String password) { + public Master(String firstName, String lastName, String email, String password, MasterRole role) { this.firstName = firstName; this.lastName = lastName; this.email = email; this.password = password; + this.role = role; } public Long getId() { @@ -45,6 +46,10 @@ public class Master { public String getPassword() { return password; } + public MasterRole getRole() { + return role; + } + public void setFirstName(String firstName) { this.firstName = firstName; } @@ -61,6 +66,10 @@ public class Master { this.password = password; } + public void setRole(MasterRole role) { + this.role = role; + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/src/main/java/com/example/demo/master/MasterController.java b/src/main/java/com/example/demo/master/MasterController.java index ff78f88..02f58f4 100644 --- a/src/main/java/com/example/demo/master/MasterController.java +++ b/src/main/java/com/example/demo/master/MasterController.java @@ -1,8 +1,7 @@ package com.example.demo.master; -import com.example.demo.WebConfiguration; +import com.example.demo.configuration.WebConfiguration; import com.example.demo.order.OrderService; -import com.example.demo.product.ProductDto; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PatchMapping; @@ -17,59 +16,59 @@ import java.util.List; @RestController @RequestMapping(WebConfiguration.REST_API + "/master") public class MasterController { - private final MasterService masterService; - private final OrderService orderService; - - - public MasterController(MasterService masterService, OrderService orderService) { - this.masterService = masterService; - this.orderService = orderService; - } - - @GetMapping("/") - public MasterDto getCurrentMaster() { - return new MasterDto(masterService.findMaster(masterService.getCurrentMasterId())); - - } - @GetMapping("/{email}/{password}") - public MasterDto getCurrentMaster(@PathVariable("email") String email, - @PathVariable("password") String password) { - var master = new MasterDto(masterService.findMaster(email, password)); - masterService.setCurrentMasterId(master.getId()); - return master; - } - @PostMapping("/") - public MasterDto createMaster(@RequestParam("firstName") String firstName, - @RequestParam("lastName") String lastName, - @RequestParam("email") String email, - @RequestParam("password") String password) { - MasterDto master = new MasterDto(masterService.addMaster(firstName, lastName, email, password)); - masterService.setCurrentMasterId(master.getId()); - orderService.addOrder(master.getId()); - return master; - } - - @PatchMapping("/") - public MasterDto updateMaster(@RequestParam("firstName") String firstName, - @RequestParam("lastName") String lastName, - @RequestParam("email") String email, - @RequestParam("password") String password) { - return new MasterDto(masterService.updateMaster(masterService.getCurrentMasterId(), - firstName, lastName, email, password)); - } - - @DeleteMapping("/") - public MasterDto deleteMaster(@PathVariable Long id) { - return new MasterDto(masterService.deleteMaster(masterService.getCurrentMasterId())); - } - - @PostMapping("/log_out") - public void logOut() { - masterService.setCurrentMasterId(0L); - } - - @GetMapping("/all") - public List GetMasters(){ - return masterService.findAllMasters().stream().map(MasterDto::new).toList(); - } +// private final MasterService masterService; +// private final OrderService orderService; +// +// +// public MasterController(MasterService masterService, OrderService orderService) { +// this.masterService = masterService; +// this.orderService = orderService; +// } +// +// @GetMapping("/") +// public MasterDto getCurrentMaster() { +// return new MasterDto(masterService.findMaster(masterService.getCurrentMasterId())); +// +// } +// @GetMapping("/{email}/{password}") +// public MasterDto getCurrentMaster(@PathVariable("email") String email, +// @PathVariable("password") String password) { +// var master = new MasterDto(masterService.findMaster(email, password)); +// masterService.setCurrentMasterId(master.getId()); +// return master; +// } +// @PostMapping("/") +// public MasterDto createMaster(@RequestParam("firstName") String firstName, +// @RequestParam("lastName") String lastName, +// @RequestParam("email") String email, +// @RequestParam("password") String password) { +// MasterDto master = new MasterDto(masterService.addMaster(firstName, lastName, email, password)); +// masterService.setCurrentMasterId(master.getId()); +// orderService.addOrder(master.getId()); +// return master; +// } +// +// @PatchMapping("/") +// public MasterDto updateMaster(@RequestParam("firstName") String firstName, +// @RequestParam("lastName") String lastName, +// @RequestParam("email") String email, +// @RequestParam("password") String password) { +// return new MasterDto(masterService.updateMaster(masterService.getCurrentMasterId(), +// firstName, lastName, email, password)); +// } +// +// @DeleteMapping("/") +// public MasterDto deleteMaster(@PathVariable Long id) { +// return new MasterDto(masterService.deleteMaster(masterService.getCurrentMasterId())); +// } +// +// @PostMapping("/log_out") +// public void logOut() { +// masterService.setCurrentMasterId(0L); +// } +// +// @GetMapping("/all") +// public List GetMasters(){ +// return masterService.findAllMasters().stream().map(MasterDto::new).toList(); +// } } diff --git a/src/main/java/com/example/demo/master/MasterMvcController.java b/src/main/java/com/example/demo/master/MasterMvcController.java index 115decb..a8125d7 100644 --- a/src/main/java/com/example/demo/master/MasterMvcController.java +++ b/src/main/java/com/example/demo/master/MasterMvcController.java @@ -3,10 +3,13 @@ package com.example.demo.master; import com.example.demo.order.OrderService; import com.example.demo.product.ProductDto; import com.example.demo.product.ProductService; +import org.springframework.security.access.annotation.Secured; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; +import java.security.Principal; + @Controller @RequestMapping("/master") public class MasterMvcController { @@ -18,67 +21,16 @@ public class MasterMvcController { this.orderService = orderService; } - @GetMapping("/login") - public String loginPage(Model model) { - if (masterService.getCurrentMasterId() != 0) { - return "redirect:/product"; - } - model.addAttribute("user", new Master()); - return "Login"; - } - - @PostMapping("/login") - public String login(@ModelAttribute Master user) { - var master = new MasterDto(masterService.findMaster(user.getEmail(), user.getPassword())); - masterService.setCurrentMasterId(master.getId()); - return "redirect:/product/my_products"; - } @GetMapping("") - public String getUserPage(Model model) { - if (masterService.getCurrentMasterId() != 0) { - model.addAttribute("user", masterService.findMaster(masterService.getCurrentMasterId())); - } else { - return "redirect:/master/register"; - } + public String getUserPage(Principal principal,Model model) { + Long masterId = masterService.findMaster(principal.getName()).getId(); + model.addAttribute("user", masterService.findMaster(masterId)); return "UserPage"; } @PostMapping(value = "", params = "action=update") public String updateUserPage(@ModelAttribute Master user) { - if (masterService.getCurrentMasterId() == 0) { - return "redirect:/master"; - } - masterService.updateMaster( - masterService.getCurrentMasterId(), - user.getFirstName(), - user.getLastName(), - user.getEmail(), - user.getPassword()); return "redirect:/product"; } - - @PostMapping(value = "", params = "action=log_out") - public String logOut() { - masterService.setCurrentMasterId(0L); - return "redirect:/product"; - } - - @GetMapping("/register") - public String registerPage(Model model) { - model.addAttribute("user", new Master()); - return "UserPage"; - } - - @PostMapping(value = "/register", params = "action=register") - public String register(@ModelAttribute Master user) { - MasterDto master = new MasterDto(masterService.addMaster( - user.getFirstName(), - user.getLastName(), - user.getEmail(), - user.getPassword())); - masterService.setCurrentMasterId(master.getId()); - orderService.addOrder(master.getId()); - return "redirect:/product/my_products"; - } } diff --git a/src/main/java/com/example/demo/master/MasterRepository.java b/src/main/java/com/example/demo/master/MasterRepository.java index 1faca4f..5df3d84 100644 --- a/src/main/java/com/example/demo/master/MasterRepository.java +++ b/src/main/java/com/example/demo/master/MasterRepository.java @@ -7,4 +7,6 @@ import java.util.Optional; public interface MasterRepository extends JpaRepository { Optional findByEmail(String email); + + Master findOneByEmailIgnoreCase(String login); } diff --git a/src/main/java/com/example/demo/master/MasterRole.java b/src/main/java/com/example/demo/master/MasterRole.java new file mode 100644 index 0000000..991bac8 --- /dev/null +++ b/src/main/java/com/example/demo/master/MasterRole.java @@ -0,0 +1,20 @@ +package com.example.demo.master; + +import org.springframework.security.core.GrantedAuthority; + +public enum MasterRole implements GrantedAuthority { + ADMIN, + USER; + + private static final String PREFIX = "ROLE_"; + + @Override + public String getAuthority() { + return PREFIX + this.name(); + } + + public static final class AsString { + public static final String ADMIN = PREFIX + "ADMIN"; + public static final String USER = PREFIX + "USER"; + } +} diff --git a/src/main/java/com/example/demo/master/MasterService.java b/src/main/java/com/example/demo/master/MasterService.java index d45af85..3726e71 100644 --- a/src/main/java/com/example/demo/master/MasterService.java +++ b/src/main/java/com/example/demo/master/MasterService.java @@ -1,32 +1,44 @@ package com.example.demo.master; import com.example.demo.order.Order; -import com.example.demo.order.OrderController; import com.example.demo.order.OrderService; import com.example.demo.util.validation.ValidatorUtil; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.core.userdetails.UsernameNotFoundException; +import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.Collections; import java.util.List; import java.util.Objects; import java.util.Optional; @Service -public class MasterService { +public class MasterService implements UserDetailsService { private final MasterRepository masterRepository; private final ValidatorUtil validatorUtil; + private final PasswordEncoder passwordEncoder; - private Long currentMasterId = 0L; - - public MasterService(MasterRepository masterRepository, ValidatorUtil validatorUtil) { + public MasterService(MasterRepository masterRepository, ValidatorUtil validatorUtil, PasswordEncoder passwordEncoder) { this.masterRepository = masterRepository; this.validatorUtil = validatorUtil; + this.passwordEncoder = passwordEncoder; + } + + @Transactional + public Master addMaster(String firstName, String lastName, String email, String password, MasterRole role) { + final Master master = new Master(firstName, lastName, email, passwordEncoder.encode(password), role); + validatorUtil.validate(master); + + return masterRepository.save(master); } @Transactional public Master addMaster(String firstName, String lastName, String email, String password) { - final Master master = new Master(firstName, lastName, email, password); + final Master master = new Master(firstName, lastName, email, passwordEncoder.encode(password), MasterRole.USER); validatorUtil.validate(master); return masterRepository.save(master); } @@ -48,6 +60,11 @@ public class MasterService { return realMaster; } + @Transactional(readOnly = true) + public Master findMaster(String email) { + return masterRepository.findOneByEmailIgnoreCase(email); + } + @Transactional(readOnly = true) public List findAllMasters() { return masterRepository.findAll(); @@ -76,14 +93,15 @@ public class MasterService { masterRepository.deleteAll(); } - @Transactional - public Long getCurrentMasterId (){ - return currentMasterId; - } - @Transactional - public void setCurrentMasterId(Long masterId) { - currentMasterId = masterId; + @Override + public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { + final Master userEntity = findMaster(username); + if (userEntity == null) { + throw new UsernameNotFoundException(username); + } + return new org.springframework.security.core.userdetails.User( + userEntity.getEmail(), userEntity.getPassword(), Collections.singleton(userEntity.getRole())); } } diff --git a/src/main/java/com/example/demo/master/MasterSignupDto.java b/src/main/java/com/example/demo/master/MasterSignupDto.java new file mode 100644 index 0000000..2b21893 --- /dev/null +++ b/src/main/java/com/example/demo/master/MasterSignupDto.java @@ -0,0 +1,37 @@ +package com.example.demo.master; + +import jakarta.validation.constraints.NotBlank; + +public class MasterSignupDto { + private String firstName; + private String lastName; + private String email; + private String password; + + + public String getFirstName() { + return firstName; + } + public String getLastName() { + return lastName; + } + public String getEmail() {return email; } + + public String getPassword() { return password; } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public void setEmail(String email) { + this.email = email; + } + + public void setPassword(String password) { + this.password = password; + } +} diff --git a/src/main/java/com/example/demo/master/MasterSignupMvcController.java b/src/main/java/com/example/demo/master/MasterSignupMvcController.java new file mode 100644 index 0000000..e5e387b --- /dev/null +++ b/src/main/java/com/example/demo/master/MasterSignupMvcController.java @@ -0,0 +1,49 @@ +package com.example.demo.master; + + +import com.example.demo.order.OrderService; +import com.example.demo.util.validation.ValidationException; +import jakarta.validation.Valid; +import org.aspectj.weaver.ast.Or; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.annotation.*; + +@Controller +@RequestMapping(MasterSignupMvcController.SIGNUP_URL) +public class MasterSignupMvcController { + public static final String SIGNUP_URL = "/signup"; + private final MasterService masterService; + + private final OrderService orderService; + public MasterSignupMvcController(MasterService masterService, OrderService orderService) { + this.masterService = masterService; + this.orderService = orderService; + } + + @GetMapping + public String showSignupForm(Model model) { + model.addAttribute("userDto", new MasterSignupDto()); + return "singup"; + } + + @PostMapping + public String signup(@ModelAttribute("userDto") @Valid MasterSignupDto userSignupDto, + BindingResult bindingResult, + Model model) { + if (bindingResult.hasErrors()) { + model.addAttribute("errors", bindingResult.getAllErrors()); + return "signup"; + } + try { + Master master = masterService.addMaster(userSignupDto.getFirstName(), userSignupDto.getLastName(), + userSignupDto.getEmail(), userSignupDto.getPassword(), MasterRole.USER); + orderService.addOrder(master.getId()); + return "redirect:/login"; + } catch (ValidationException e) { + model.addAttribute("errors", e.getMessage()); + return "signup"; + } + } +} diff --git a/src/main/java/com/example/demo/order/OrderController.java b/src/main/java/com/example/demo/order/OrderController.java index a193878..089ae74 100644 --- a/src/main/java/com/example/demo/order/OrderController.java +++ b/src/main/java/com/example/demo/order/OrderController.java @@ -1,54 +1,54 @@ -package com.example.demo.order; - -import com.example.demo.WebConfiguration; -import com.example.demo.master.MasterService; -import org.springframework.web.bind.annotation.*; - -import java.util.List; - -@RestController -@RequestMapping(WebConfiguration.REST_API + "/order") -public class OrderController { - private final OrderService orderService; - private final MasterService masterService; - - public OrderController(OrderService orderService, MasterService masterService) { - this.orderService = orderService; - this.masterService = masterService; - } - - @GetMapping("/{id}") - public OrderDto getOrder(@PathVariable Long id) { - return new OrderDto(orderService.findOrder(id)); - } - - @DeleteMapping("/") - public void buyProducts() { - orderService.buyProducts(masterService.getCurrentMasterId()); - } - - @GetMapping("/") - public List getOrder() { - return orderService.findAllOrders().stream().map(OrderDto::new).toList(); - } - - @PostMapping("/") - public OrderDto createOrder(@RequestParam("master") Long masterId) { - return new OrderDto(orderService.addOrder(masterId)); - } - - @PostMapping("/{product}") - public void addProduct(@PathVariable("product") Long productId) { - orderService.addProduct(masterService.getCurrentMasterId(), productId); - } - - @DeleteMapping("/{product}") - public void deleteProduct(@PathVariable("product") Long productId) { - orderService.deleteProduct(masterService.getCurrentMasterId(), productId); - } - - @GetMapping("/findOrders/{masterId}") - public List findOrders(@PathVariable("masterId") Long masterId) { - return orderService.findMastersOrders(masterId).stream().map(OrderDto::new).toList(); - } -} +//package com.example.demo.order; +// +//import com.example.demo.configuration.WebConfiguration; +//import com.example.demo.master.MasterService; +//import org.springframework.web.bind.annotation.*; +// +//import java.util.List; +// +//@RestController +//@RequestMapping(WebConfiguration.REST_API + "/order") +//public class OrderController { +// private final OrderService orderService; +// private final MasterService masterService; +// +// public OrderController(OrderService orderService, MasterService masterService) { +// this.orderService = orderService; +// this.masterService = masterService; +// } +// +// @GetMapping("/{id}") +// public OrderDto getOrder(@PathVariable Long id) { +// return new OrderDto(orderService.findOrder(id)); +// } +// +// @DeleteMapping("/") +// public void buyProducts() { +// orderService.buyProducts(masterService.getCurrentMasterId()); +// } +// +// @GetMapping("/") +// public List getOrder() { +// return orderService.findAllOrders().stream().map(OrderDto::new).toList(); +// } +// +// @PostMapping("/") +// public OrderDto createOrder(@RequestParam("master") Long masterId) { +// return new OrderDto(orderService.addOrder(masterId)); +// } +// +// @PostMapping("/{product}") +// public void addProduct(@PathVariable("product") Long productId) { +// orderService.addProduct(masterService.getCurrentMasterId(), productId); +// } +// +// @DeleteMapping("/{product}") +// public void deleteProduct(@PathVariable("product") Long productId) { +// orderService.deleteProduct(masterService.getCurrentMasterId(), productId); +// } +// +// @GetMapping("/findOrders/{masterId}") +// public List findOrders(@PathVariable("masterId") Long masterId) { +// return orderService.findMastersOrders(masterId).stream().map(OrderDto::new).toList(); +// } +//} diff --git a/src/main/java/com/example/demo/order/OrderMvcController.java b/src/main/java/com/example/demo/order/OrderMvcController.java index 1ebbb3a..e73a570 100644 --- a/src/main/java/com/example/demo/order/OrderMvcController.java +++ b/src/main/java/com/example/demo/order/OrderMvcController.java @@ -1,6 +1,8 @@ package com.example.demo.order; +import com.example.demo.master.MasterRole; import com.example.demo.master.MasterService; +import org.springframework.security.access.annotation.Secured; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @@ -8,6 +10,7 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; +import java.security.Principal; import java.util.ArrayList; import java.util.Objects; import java.util.concurrent.atomic.AtomicInteger; @@ -24,34 +27,33 @@ public class OrderMvcController { } @GetMapping("") - public String getOrder(Model model) { - if (masterService.getCurrentMasterId() == 0) { - return "redirect:/master/login"; - } - model.addAttribute("user", masterService.findMaster(masterService.getCurrentMasterId())); - model.addAttribute("order", orderService.findOrder(masterService.getCurrentMasterId())); + public String getOrder(Model model, Principal principal) { + Long masterId = masterService.findMaster(principal.getName()).getId(); + model.addAttribute("order", orderService.findOrder(masterId)); AtomicInteger fullCost = new AtomicInteger(); - orderService.findOrder(masterService.getCurrentMasterId()).getProducts().forEach( + orderService.findOrder(masterId).getProducts().forEach( item -> fullCost.addAndGet(item.getCost())); model.addAttribute("fullCost", fullCost); return "OrderPage"; } @PostMapping(value = "", params = "action=delete") - public String deleteProduct(@RequestParam(value = "id", required = true) Long id) { - orderService.deleteProduct(masterService.getCurrentMasterId(), id); + public String deleteProduct(@RequestParam(value = "id", required = true) Long id, Principal principal) { + Long masterId = masterService.findMaster(principal.getName()).getId(); + orderService.deleteProduct(masterId, id); return "redirect:/order"; } @PostMapping(value = "", params = "action=buy") - public String buyProducts() { - orderService.buyProducts(masterService.getCurrentMasterId()); + public String buyProducts(Principal principal) { + Long masterId = masterService.findMaster(principal.getName()).getId(); + orderService.buyProducts(masterId); return "redirect:/product"; } @GetMapping("/masters_order") + @Secured(MasterRole.AsString.ADMIN) public String MastersOrders(Model model, @RequestParam(value = "master_id", defaultValue = "-1") String masterId) { - model.addAttribute("user", masterService.findMaster(masterService.getCurrentMasterId())); model.addAttribute("masters", masterService.findAllMasters()); if (!Objects.equals(masterId, "-1")) { diff --git a/src/main/java/com/example/demo/product/ProductController.java b/src/main/java/com/example/demo/product/ProductController.java index ab975a8..cf6e284 100644 --- a/src/main/java/com/example/demo/product/ProductController.java +++ b/src/main/java/com/example/demo/product/ProductController.java @@ -1,7 +1,6 @@ package com.example.demo.product; -import com.example.demo.WebConfiguration; -import org.springframework.beans.factory.annotation.Autowired; +import com.example.demo.configuration.WebConfiguration; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PatchMapping; diff --git a/src/main/java/com/example/demo/product/ProductMvcController.java b/src/main/java/com/example/demo/product/ProductMvcController.java index 4b83fdd..9836581 100644 --- a/src/main/java/com/example/demo/product/ProductMvcController.java +++ b/src/main/java/com/example/demo/product/ProductMvcController.java @@ -7,6 +7,8 @@ import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; +import java.security.Principal; + @Controller @RequestMapping("/product") @@ -24,67 +26,46 @@ public class ProductMvcController { @GetMapping("") public String getProducts(Model model) { - if (masterService.getCurrentMasterId() != 0) { - Master user = masterService.findMaster(masterService.getCurrentMasterId()); - model.addAttribute("user", user); - } - else { - model.addAttribute("user", new Master()); - } model.addAttribute("products", productService.findAllProducts() .stream().map(ProductDto::new).toList()); return "Products"; } @PostMapping("") - public String addProductToOrder(@RequestParam(value = "id", required = true) Long id) { - if (masterService.getCurrentMasterId() == 0) { - return "redirect:/master/login"; - } - orderService.addProduct(masterService.getCurrentMasterId(), id); + public String addProductToOrder(@RequestParam(value = "id", required = true) Long id, Principal principal) { + Long masterId = masterService.findMaster(principal.getName()).getId(); + orderService.addProduct(masterId, id); return "redirect:/product"; } - @GetMapping("/my_products") - public String getMasterProduct(Model model) { - if (masterService.getCurrentMasterId() == 0) { - return "redirect:/product"; - } - model.addAttribute("user", - masterService.findMaster(masterService.getCurrentMasterId())); + public String getMasterProduct(Model model, Principal principal) { + Long masterId = masterService.findMaster(principal.getName()).getId(); model.addAttribute("products", - productService.findProducts(masterService.getCurrentMasterId()).stream().map(ProductDto::new).toList()); + productService.findProducts(masterId).stream().map(ProductDto::new).toList()); return "UserProducts"; } @GetMapping("/create_product") public String createProductPage(Model model) { - if (masterService.getCurrentMasterId() == 0) { - return "redirect:/product"; - } - model.addAttribute("user", masterService.findMaster(masterService.getCurrentMasterId())); model.addAttribute("product", new Product()); model.addAttribute("buttonText", "Create"); return "ProductCreate"; } @PostMapping("/create_product") - public String createProduct(@ModelAttribute Product product) { + public String createProduct(@ModelAttribute Product product, Principal principal) { + Long masterId = masterService.findMaster(principal.getName()).getId(); productService.addProduct( product.getName(), product.getCost(), - masterService.getCurrentMasterId() + masterId ); return "redirect:/product/my_products"; } @GetMapping("/update_product/{id}") public String updateProductPage(Model model, @PathVariable("id") Long id) { - if (masterService.getCurrentMasterId() == 0) { - return "redirect:/product"; - } - model.addAttribute("user", masterService.findMaster(masterService.getCurrentMasterId())); model.addAttribute("product", productService.findProduct(id)); model.addAttribute("buttonText", "Update"); return "ProductCreate"; diff --git a/src/main/resources/static/style.css b/src/main/resources/static/style.css deleted file mode 100644 index 07b9627..0000000 --- a/src/main/resources/static/style.css +++ /dev/null @@ -1,25 +0,0 @@ -body { - background: #f54d9a; -} - -.logo { - background: #FF9CCE; -} - -main { - padding: 2%; - margin: 10% 5%; -} - -main img { - width: 100%; - object-fit: cover; -} - -form { - padding: 1%; -} - -.product-div { - background: #e874ac; -} \ No newline at end of file diff --git a/src/main/resources/templates/MastersOrders.html b/src/main/resources/templates/MastersOrders.html index 9816fcc..b97f8ff 100644 --- a/src/main/resources/templates/MastersOrders.html +++ b/src/main/resources/templates/MastersOrders.html @@ -1,5 +1,5 @@ - +> Login @@ -15,15 +15,15 @@ crossorigin="anonymous"> -
-
+ +

Masters

- + diff --git a/src/main/resources/templates/OrderPage.html b/src/main/resources/templates/OrderPage.html index e23acc9..8a2c182 100644 --- a/src/main/resources/templates/OrderPage.html +++ b/src/main/resources/templates/OrderPage.html @@ -1,5 +1,5 @@ - +> Login @@ -15,7 +15,8 @@ crossorigin="anonymous"> -
+ +

Order

@@ -44,5 +45,6 @@
+
\ No newline at end of file diff --git a/src/main/resources/templates/ProductCreate.html b/src/main/resources/templates/ProductCreate.html index dbb20ad..7e617c9 100644 --- a/src/main/resources/templates/ProductCreate.html +++ b/src/main/resources/templates/ProductCreate.html @@ -1,5 +1,5 @@ - +> Login @@ -15,7 +15,8 @@ crossorigin="anonymous"> -
+ +
@@ -23,5 +24,6 @@
+
\ No newline at end of file diff --git a/src/main/resources/templates/Products.html b/src/main/resources/templates/Products.html index 9b701d2..636c83a 100644 --- a/src/main/resources/templates/Products.html +++ b/src/main/resources/templates/Products.html @@ -1,5 +1,5 @@ - +> Title @@ -15,8 +15,8 @@ crossorigin="anonymous"> -
- + +
@@ -33,6 +33,6 @@
- +
\ No newline at end of file diff --git a/src/main/resources/templates/UserPage.html b/src/main/resources/templates/UserPage.html deleted file mode 100644 index 413e22a..0000000 --- a/src/main/resources/templates/UserPage.html +++ /dev/null @@ -1,55 +0,0 @@ - - - - - Title - - - - - - -
-
-
-
- -
-
- -
-
- -
-
- -
-
-
- -
-
- -
-
-
- -
-
- Sing In -
-
-
- - \ No newline at end of file diff --git a/src/main/resources/templates/UserProducts.html b/src/main/resources/templates/UserProducts.html index 909ce73..bac7780 100644 --- a/src/main/resources/templates/UserProducts.html +++ b/src/main/resources/templates/UserProducts.html @@ -1,5 +1,5 @@ - +> Title @@ -16,7 +16,7 @@ crossorigin="anonymous"> -
+
Create @@ -33,5 +33,6 @@
+
\ No newline at end of file diff --git a/src/main/resources/templates/default.html b/src/main/resources/templates/default.html new file mode 100644 index 0000000..8aff8a2 --- /dev/null +++ b/src/main/resources/templates/default.html @@ -0,0 +1,46 @@ + + + + + Title + + + + + + + + + + + diff --git a/src/main/resources/templates/login.html b/src/main/resources/templates/login.html new file mode 100644 index 0000000..ad3f3a3 --- /dev/null +++ b/src/main/resources/templates/login.html @@ -0,0 +1,44 @@ + +> + + + + + +
+
+ User not found +
+
+ Logout success +
+
+ User '' was successfully created +
+
+
+

Login

+ +
+
+

Password

+ +
+
+ +
+
+

+ Not a member yet? + Sing Up here +

+
+
+
+ + \ No newline at end of file diff --git a/src/main/resources/templates/Login.html b/src/main/resources/templates/singup.html similarity index 59% rename from src/main/resources/templates/Login.html rename to src/main/resources/templates/singup.html index 4a492e4..3721186 100644 --- a/src/main/resources/templates/Login.html +++ b/src/main/resources/templates/singup.html @@ -1,8 +1,11 @@ - + + - Login + Title -
-
+
+
+
+ +
+
+ +
+ id="email" placeholder="Email" th:field="${userDto.email}">
+ id="password" placeholder="Password" th:field="${userDto.password}">
- +
- +
\ No newline at end of file