diff --git a/src/main/java/com/webproglabs/lab1/mvc/UserMvcController.java b/src/main/java/com/webproglabs/lab1/mvc/UserMvcController.java index 9a8d3de..f88679f 100644 --- a/src/main/java/com/webproglabs/lab1/mvc/UserMvcController.java +++ b/src/main/java/com/webproglabs/lab1/mvc/UserMvcController.java @@ -1,11 +1,19 @@ package com.webproglabs.lab1.mvc; import com.webproglabs.lab1.dto.UserDto; +import com.webproglabs.lab1.dto.UserSignupDto; +import com.webproglabs.lab1.models.User; +import com.webproglabs.lab1.models.UserRole; import com.webproglabs.lab1.services.UserService; +import org.springframework.security.access.annotation.Secured; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.core.userdetails.UserDetails; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.annotation.*; + +import javax.validation.Valid; @Controller @RequestMapping("/users") @@ -17,8 +25,55 @@ public class UserMvcController { } @GetMapping + @Secured({UserRole.AsString.ADMIN}) public String getUsersPage(Model model){ - model.addAttribute("users", userService.findAllUsers().stream().map(UserDto::new).toList()); + model.addAttribute("profiles", userService.findAllUsers().stream().map(UserDto::new).toList()); return "users"; } + + @PostMapping(value = {"/{id}"}) + @Secured({UserRole.AsString.ADMIN}) + public String deleteUser(@PathVariable Long id) { + userService.deleteUser(id); + return "redirect:/users"; + } + + @GetMapping(value = {"/{login}"}) + public String getUserPage(@PathVariable String login, Model model) { + model.addAttribute("user", new UserDto(userService.findUserByLogin(login))); + return "userPage"; + } + + @GetMapping(value = {"/settings"}) + public String getUserEditPage(Model model) { + UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); + var currentUser = userService.findUserByLogin(principal.getUsername()); + model.addAttribute("user", new UserDto(userService.findUserById(currentUser.getId()))); + model.addAttribute("userDto", new UserSignupDto()); + + return "userEditPage"; + } + + @PostMapping(value = {"/edit/{id}"}) + public String editUserData(@PathVariable Long id, @ModelAttribute("userDto") @Valid UserSignupDto userSignupDto, + BindingResult bindingResult, + Model model) { + UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); + var currentUser = userService.findUserByLogin(principal.getUsername()); + model.addAttribute("user", new UserDto(userService.findUserById(currentUser.getId()))); + model.addAttribute("userDto", new UserSignupDto()); + + if (bindingResult.hasErrors()) { + model.addAttribute("errors", bindingResult.getAllErrors()); + return "userEditPage"; + } + try { + final User user = userService.updateUser(id, userSignupDto.getLogin(), userSignupDto.getPassword(), userSignupDto.getPasswordConfirm()); + model.addAttribute("success", "Данные успешно изменены"); + return "userEditPage"; + } catch (Exception e) { + model.addAttribute("errors", e.getMessage()); + return "userEditPage"; + } + } } diff --git a/src/main/java/com/webproglabs/lab1/services/UserService.java b/src/main/java/com/webproglabs/lab1/services/UserService.java index d5ebb5a..371a68a 100644 --- a/src/main/java/com/webproglabs/lab1/services/UserService.java +++ b/src/main/java/com/webproglabs/lab1/services/UserService.java @@ -62,13 +62,23 @@ public class UserService implements UserDetailsService { } @Transactional - public User updateUser(Long id, String login, String password) { + public User updateUser(Long id, String login, String password, String passwordConfirm) { if (!StringUtils.hasText(login) || !StringUtils.hasText(password)) { throw new IllegalArgumentException("User data is null or empty"); } + final User currentUser = findUserById(id); + + if (Objects.equals(password, currentUser.getPassword())) { + throw new IllegalArgumentException("New password is the same as old"); + } + + if (!Objects.equals(password, passwordConfirm)) { + throw new IllegalArgumentException("Password mismatch"); + } + currentUser.setLogin(login); - currentUser.setPassword(password); + currentUser.setPassword(passwordEncoder.encode(password)); return userRepository.save(currentUser); } diff --git a/src/main/resources/templates/default.html b/src/main/resources/templates/default.html index 4d06ca0..acf7fbc 100644 --- a/src/main/resources/templates/default.html +++ b/src/main/resources/templates/default.html @@ -20,13 +20,14 @@
-

СоцСеточка

+

СоцСеточка

Топики Пользователи Лента + Настройки Выход diff --git a/src/main/resources/templates/feed.html b/src/main/resources/templates/feed.html index 02d1a6a..5b53682 100644 --- a/src/main/resources/templates/feed.html +++ b/src/main/resources/templates/feed.html @@ -11,7 +11,7 @@