From f8f501b8ff0d2b0b6a40120401af36bf1e0c4258 Mon Sep 17 00:00:00 2001 From: "Artem.Arefev" Date: Thu, 16 May 2019 00:38:53 +0400 Subject: [PATCH 1/4] 88-change-password base --- .../ru/ulstu/configuration/Constants.java | 1 + .../ulstu/user/controller/UserController.java | 7 ---- .../user/controller/UserControllerV2.java | 12 ++++++ .../ru/ulstu/user/service/MailService.java | 6 +++ .../ru/ulstu/user/service/UserService.java | 24 +++++------- .../mail_templates/passwordChangeEmail.html | 21 +++++++++++ src/main/resources/templates/default.html | 2 + .../templates/users/changePassword.html | 37 +++++++++++++++++++ 8 files changed, 88 insertions(+), 22 deletions(-) create mode 100644 src/main/resources/mail_templates/passwordChangeEmail.html create mode 100644 src/main/resources/templates/users/changePassword.html diff --git a/src/main/java/ru/ulstu/configuration/Constants.java b/src/main/java/ru/ulstu/configuration/Constants.java index 51806b6..f910108 100644 --- a/src/main/java/ru/ulstu/configuration/Constants.java +++ b/src/main/java/ru/ulstu/configuration/Constants.java @@ -6,6 +6,7 @@ public class Constants { public static final String MAIL_ACTIVATE = "Account activation"; public static final String MAIL_RESET = "Password reset"; public static final String MAIL_INVITE = "Account registration"; + public static final String MAIL_CHANGE_PASSWORD = "Password has been changed"; public static final int MIN_PASSWORD_LENGTH = 6; public static final int MAX_PASSWORD_LENGTH = 32; diff --git a/src/main/java/ru/ulstu/user/controller/UserController.java b/src/main/java/ru/ulstu/user/controller/UserController.java index 6806d19..b49ff4f 100644 --- a/src/main/java/ru/ulstu/user/controller/UserController.java +++ b/src/main/java/ru/ulstu/user/controller/UserController.java @@ -141,13 +141,6 @@ public class UserController extends OdinController { return new Response<>(userService.activateUser(activationKey)); } - // TODO: add page for user password change (user-profile) - @PostMapping("/change-password") - public Response changePassword(@Valid @RequestBody UserDto userDto) { - log.debug("REST: UserController.changePassword( {} )", userDto.getLogin()); - return new Response<>(userService.changeUserPassword(userDto)); - } - @PostMapping(PASSWORD_RESET_REQUEST_URL) public Response requestPasswordReset(@RequestParam("email") String email) { log.debug("REST: UserController.requestPasswordReset( {} )", email); diff --git a/src/main/java/ru/ulstu/user/controller/UserControllerV2.java b/src/main/java/ru/ulstu/user/controller/UserControllerV2.java index eee98a1..84c0349 100644 --- a/src/main/java/ru/ulstu/user/controller/UserControllerV2.java +++ b/src/main/java/ru/ulstu/user/controller/UserControllerV2.java @@ -2,10 +2,12 @@ package ru.ulstu.user.controller; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import ru.ulstu.configuration.Constants; @@ -19,6 +21,7 @@ import ru.ulstu.user.service.UserSessionService; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; +import java.util.Map; @Controller @RequestMapping(value = "/users") @@ -56,4 +59,13 @@ public class UserControllerV2 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/service/MailService.java b/src/main/java/ru/ulstu/user/service/MailService.java index 59d7e51..9c71c10 100644 --- a/src/main/java/ru/ulstu/user/service/MailService.java +++ b/src/main/java/ru/ulstu/user/service/MailService.java @@ -114,4 +114,10 @@ public class MailService { public void sendInviteMail(Map variables, String email) throws MessagingException { sendEmailFromTemplate(variables, "userInviteEmail", Constants.MAIL_INVITE, email); } + + @Async + public void sendChangePasswordMail(User user) { + sendEmailFromTemplate(user, "passwordChangeEmail", Constants.MAIL_CHANGE_PASSWORD); + + } } diff --git a/src/main/java/ru/ulstu/user/service/UserService.java b/src/main/java/ru/ulstu/user/service/UserService.java index db20868..02f0ddb 100644 --- a/src/main/java/ru/ulstu/user/service/UserService.java +++ b/src/main/java/ru/ulstu/user/service/UserService.java @@ -218,24 +218,18 @@ public class UserService implements UserDetailsService { return userMapper.userEntityToUserDto(user); } - public UserDto changeUserPassword(UserDto userDto) { - if (userDto.getId() == null) { - throw new EntityIdIsNullException(); - } - if (!userDto.isPasswordsValid() || !userDto.isOldPasswordValid()) { + public void changeUserPassword(User user, Map payload) { + if (!payload.get("password").equals(payload.get("confirmPassword"))) { throw new UserPasswordsNotValidOrNotMatchException(); } - final String login = UserUtils.getCurrentUserLogin(); - final User user = userRepository.findOneByLoginIgnoreCase(login); - if (user == null) { - throw new UserNotFoundException(login); - } - if (!passwordEncoder.matches(userDto.getOldPassword(), user.getPassword())) { + if (!passwordEncoder.matches(payload.get("oldPassword"), user.getPassword())) { throw new UserPasswordsNotValidOrNotMatchException(); } - user.setPassword(passwordEncoder.encode(userDto.getPassword())); + user.setPassword(passwordEncoder.encode(payload.get("password"))); log.debug("Changed password for User: {}", user.getLogin()); - return userMapper.userEntityToUserDto(userRepository.save(user)); + userRepository.save(user); + + mailService.sendChangePasswordMail(user); } public boolean requestUserPasswordReset(String email) { @@ -331,8 +325,8 @@ public class UserService implements UserDetailsService { user.setPassword(passwordEncoder.encode(password)); user.setLogin(email); user.setEmail(email); - user.setFirstName(""); - user.setLastName(""); + user.setFirstName("user"); + user.setLastName("user"); user.setActivated(true); userRepository.save(user); diff --git a/src/main/resources/mail_templates/passwordChangeEmail.html b/src/main/resources/mail_templates/passwordChangeEmail.html new file mode 100644 index 0000000..ec15a36 --- /dev/null +++ b/src/main/resources/mail_templates/passwordChangeEmail.html @@ -0,0 +1,21 @@ + + + + Password reset + + + + +

+ Dear Ivan Ivanov +

+

+ Your password has been changed. +

+

+ Regards, +
+ Balance Team. +

+ + diff --git a/src/main/resources/templates/default.html b/src/main/resources/templates/default.html index 0c92865..32939e7 100644 --- a/src/main/resources/templates/default.html +++ b/src/main/resources/templates/default.html @@ -70,6 +70,7 @@ Личный кабинет Выход Пригласить + Сменить пароль @@ -77,6 +78,7 @@
+