From 052e4a1aefb5eb16150c78e09c8a1888c8300f9f Mon Sep 17 00:00:00 2001 From: "Artem.Arefev" Date: Fri, 17 May 2019 03:55:22 +0400 Subject: [PATCH] 88 rest changePassword + ajax --- .../ulstu/core/model/response/Response.java | 2 +- .../core/model/response/ResponseExtended.java | 2 +- .../ulstu/user/controller/UserController.java | 13 ++++++ .../user/controller/UserMvcController.java | 18 -------- ...rPasswordsNotValidOrNotMatchException.java | 3 +- .../ru/ulstu/user/service/UserService.java | 12 ++--- src/main/resources/public/js/users.js | 33 ++++++++++++++ src/main/resources/templates/error/error.html | 21 --------- .../templates/users/changePassword.html | 45 +++++++++---------- 9 files changed, 78 insertions(+), 71 deletions(-) create mode 100644 src/main/resources/public/js/users.js delete mode 100644 src/main/resources/templates/error/error.html diff --git a/src/main/java/ru/ulstu/core/model/response/Response.java b/src/main/java/ru/ulstu/core/model/response/Response.java index 4722010..7c57168 100644 --- a/src/main/java/ru/ulstu/core/model/response/Response.java +++ b/src/main/java/ru/ulstu/core/model/response/Response.java @@ -11,6 +11,6 @@ public class Response extends ResponseEntity { } public Response(ErrorConstants error) { - super(new ControllerResponse(new ControllerResponseError<>(error, null)), HttpStatus.OK); + super(new ControllerResponse(new ControllerResponseError<>(error, null)), HttpStatus.BAD_REQUEST); } } diff --git a/src/main/java/ru/ulstu/core/model/response/ResponseExtended.java b/src/main/java/ru/ulstu/core/model/response/ResponseExtended.java index 1829622..568e9b5 100644 --- a/src/main/java/ru/ulstu/core/model/response/ResponseExtended.java +++ b/src/main/java/ru/ulstu/core/model/response/ResponseExtended.java @@ -7,6 +7,6 @@ import ru.ulstu.core.model.ErrorConstants; public class ResponseExtended extends ResponseEntity { public ResponseExtended(ErrorConstants error, E errorData) { - super(new ControllerResponse(new ControllerResponseError(error, errorData)), HttpStatus.OK); + super(new ControllerResponse(new ControllerResponseError(error, errorData)), HttpStatus.BAD_REQUEST); } } diff --git a/src/main/java/ru/ulstu/user/controller/UserController.java b/src/main/java/ru/ulstu/user/controller/UserController.java index b49ff4f..f674a23 100644 --- a/src/main/java/ru/ulstu/user/controller/UserController.java +++ b/src/main/java/ru/ulstu/user/controller/UserController.java @@ -19,6 +19,7 @@ import ru.ulstu.odin.controller.OdinController; import ru.ulstu.odin.model.OdinMetadata; import ru.ulstu.odin.model.OdinVoid; import ru.ulstu.odin.service.OdinService; +import ru.ulstu.user.model.User; import ru.ulstu.user.model.UserDto; import ru.ulstu.user.model.UserListDto; import ru.ulstu.user.model.UserResetPasswordDto; @@ -28,8 +29,12 @@ import ru.ulstu.user.model.UserSessionListDto; import ru.ulstu.user.service.UserService; import ru.ulstu.user.service.UserSessionService; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpSession; import javax.validation.Valid; +import java.util.Map; + import static ru.ulstu.user.controller.UserController.URL; @RestController @@ -153,4 +158,12 @@ public class UserController extends OdinController { log.debug("REST: UserController.requestPasswordReset( {} )", key); return new Response<>(userService.completeUserPasswordReset(key, userResetPasswordDto)); } + + @PostMapping("/changePassword") + public void changePassword(@RequestBody Map payload, HttpServletRequest request) { + HttpSession session = request.getSession(false); + final String sessionId = session.getAttribute(Constants.SESSION_ID_ATTR).toString(); + User user = userSessionService.getUserBySessionId(sessionId); + userService.changeUserPassword(user, payload); + } } diff --git a/src/main/java/ru/ulstu/user/controller/UserMvcController.java b/src/main/java/ru/ulstu/user/controller/UserMvcController.java index 4a58168..29123e9 100644 --- a/src/main/java/ru/ulstu/user/controller/UserMvcController.java +++ b/src/main/java/ru/ulstu/user/controller/UserMvcController.java @@ -3,9 +3,7 @@ package ru.ulstu.user.controller; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; import org.springframework.ui.ModelMap; -import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; @@ -20,7 +18,6 @@ import ru.ulstu.user.service.UserSessionService; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; -import java.util.Map; @Controller @RequestMapping(value = "/users") @@ -38,12 +35,6 @@ public class UserMvcController extends OdinController { this.userSessionService = userSessionService; } - @ExceptionHandler(Exception.class) - public String errorHandler(Model model, Exception exception) { - model.addAttribute("error", exception.getMessage()); - return "/error/error"; - } - @GetMapping("/profile") public void getUserProfile(ModelMap modelMap, HttpServletRequest request) { HttpSession session = request.getSession(false); @@ -64,13 +55,4 @@ public class UserMvcController extends OdinController { userService.inviteUser(email); return "redirect:/"; } - - @PostMapping("/changePassword") - public String changePassword(@RequestParam Map payload, HttpServletRequest request) { - HttpSession session = request.getSession(false); - final String sessionId = session.getAttribute(Constants.SESSION_ID_ATTR).toString(); - User user = userSessionService.getUserBySessionId(sessionId); - userService.changeUserPassword(user, payload); - return "redirect:/"; - } } diff --git a/src/main/java/ru/ulstu/user/error/UserPasswordsNotValidOrNotMatchException.java b/src/main/java/ru/ulstu/user/error/UserPasswordsNotValidOrNotMatchException.java index 088f999..3edc1fa 100644 --- a/src/main/java/ru/ulstu/user/error/UserPasswordsNotValidOrNotMatchException.java +++ b/src/main/java/ru/ulstu/user/error/UserPasswordsNotValidOrNotMatchException.java @@ -1,6 +1,7 @@ package ru.ulstu.user.error; public class UserPasswordsNotValidOrNotMatchException extends RuntimeException { - public UserPasswordsNotValidOrNotMatchException() { + public UserPasswordsNotValidOrNotMatchException(String message) { + super(message); } } diff --git a/src/main/java/ru/ulstu/user/service/UserService.java b/src/main/java/ru/ulstu/user/service/UserService.java index 63be451..d82d564 100644 --- a/src/main/java/ru/ulstu/user/service/UserService.java +++ b/src/main/java/ru/ulstu/user/service/UserService.java @@ -128,7 +128,7 @@ public class UserService implements UserDetailsService { throw new UserEmailExistsException(userDto.getEmail()); } if (!userDto.isPasswordsValid()) { - throw new UserPasswordsNotValidOrNotMatchException(); + throw new UserPasswordsNotValidOrNotMatchException(""); } User user = userMapper.userDtoToUserEntity(userDto); user.setActivated(false); @@ -198,10 +198,10 @@ public class UserService implements UserDetailsService { : roles); if (!StringUtils.isEmpty(userDto.getOldPassword())) { if (!userDto.isPasswordsValid() || !userDto.isOldPasswordValid()) { - throw new UserPasswordsNotValidOrNotMatchException(); + throw new UserPasswordsNotValidOrNotMatchException(""); } if (!passwordEncoder.matches(userDto.getOldPassword(), user.getPassword())) { - throw new UserPasswordsNotValidOrNotMatchException(); + throw new UserPasswordsNotValidOrNotMatchException(""); } user.setPassword(passwordEncoder.encode(userDto.getPassword())); log.debug("Changed password for User: {}", user.getLogin()); @@ -223,10 +223,10 @@ public class UserService implements UserDetailsService { public void changeUserPassword(User user, Map payload) { if (!payload.get("password").equals(payload.get("confirmPassword"))) { - throw new UserPasswordsNotValidOrNotMatchException(); + throw new UserPasswordsNotValidOrNotMatchException(""); } if (!passwordEncoder.matches(payload.get("oldPassword"), user.getPassword())) { - throw new UserPasswordsNotValidOrNotMatchException(); + throw new UserPasswordsNotValidOrNotMatchException("Старый пароль введен неправильно"); } user.setPassword(passwordEncoder.encode(payload.get("password"))); log.debug("Changed password for User: {}", user.getLogin()); @@ -253,7 +253,7 @@ public class UserService implements UserDetailsService { public boolean completeUserPasswordReset(String key, UserResetPasswordDto userResetPasswordDto) { if (!userResetPasswordDto.isPasswordsValid()) { - throw new UserPasswordsNotValidOrNotMatchException(); + throw new UserPasswordsNotValidOrNotMatchException(""); } User user = userRepository.findOneByResetKey(key); if (user == null) { diff --git a/src/main/resources/public/js/users.js b/src/main/resources/public/js/users.js new file mode 100644 index 0000000..fee4f70 --- /dev/null +++ b/src/main/resources/public/js/users.js @@ -0,0 +1,33 @@ +function changePassword() { + oldPassword = document.getElementById("oldPassword").value + password = document.getElementById("password").value + confirmPassword = document.getElementById("confirmPassword").value + + if ([oldPassword.length, password.length, confirmPassword.length].includes(0)) { + alert("Заполните все поля"); + return; + } + + if (password != confirmPassword) { + alert("Повторный пароль введен неверно"); + return; + } + + $.ajax({ + url:"/api/1.0/users/changePassword", + contentType: "application/json; charset=utf-8", + data: JSON.stringify({ + "oldPassword": document.getElementById("oldPassword").value, + "password": document.getElementById("password").value, + "confirmPassword": document.getElementById("confirmPassword").value, + }), + method: "POST", + success: function() { + document.getElementById("closeModalPassword").click(); + alert("Пароль был обновлен"); + }, + error: function(errorData) { + alert(errorData.responseJSON.error.message) + } + }) +} \ No newline at end of file diff --git a/src/main/resources/templates/error/error.html b/src/main/resources/templates/error/error.html deleted file mode 100644 index 53c9054..0000000 --- a/src/main/resources/templates/error/error.html +++ /dev/null @@ -1,21 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/src/main/resources/templates/users/changePassword.html b/src/main/resources/templates/users/changePassword.html index 845bbcb..bc96b5c 100644 --- a/src/main/resources/templates/users/changePassword.html +++ b/src/main/resources/templates/users/changePassword.html @@ -2,34 +2,33 @@ +