88-change-password base
This commit is contained in:
parent
ca3159a66b
commit
f8f501b8ff
@ -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;
|
||||
|
@ -141,13 +141,6 @@ public class UserController extends OdinController<UserListDto, UserDto> {
|
||||
return new Response<>(userService.activateUser(activationKey));
|
||||
}
|
||||
|
||||
// TODO: add page for user password change (user-profile)
|
||||
@PostMapping("/change-password")
|
||||
public Response<UserDto> 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<Boolean> requestPasswordReset(@RequestParam("email") String email) {
|
||||
log.debug("REST: UserController.requestPasswordReset( {} )", email);
|
||||
|
@ -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<UserListDto, UserDto> {
|
||||
userService.inviteUser(email);
|
||||
return "redirect:/";
|
||||
}
|
||||
|
||||
@PostMapping("/changePassword")
|
||||
public String changePassword(@RequestParam Map<String, String> 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:/";
|
||||
}
|
||||
}
|
||||
|
@ -114,4 +114,10 @@ public class MailService {
|
||||
public void sendInviteMail(Map<String, Object> 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);
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -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<String, String> 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);
|
||||
|
||||
|
21
src/main/resources/mail_templates/passwordChangeEmail.html
Normal file
21
src/main/resources/mail_templates/passwordChangeEmail.html
Normal file
@ -0,0 +1,21 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<title>Password reset</title>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
|
||||
<link rel="shortcut icon" th:href="@{|${baseUrl}/favicon.ico|}"/>
|
||||
</head>
|
||||
<body>
|
||||
<p>
|
||||
Dear <span th:text="${user.firstName + ' ' + user.lastName}">Ivan Ivanov</span>
|
||||
</p>
|
||||
<p>
|
||||
Your password has been changed.
|
||||
</p>
|
||||
<p>
|
||||
Regards,
|
||||
<br/>
|
||||
<em>Balance Team.</em>
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
@ -70,6 +70,7 @@
|
||||
<a class="dropdown-item" href="/users/profile">Личный кабинет</a>
|
||||
<a class="dropdown-item" href="/logout">Выход</a>
|
||||
<a class="dropdown-item" data-toggle="modal" href="invite.html" data-target="#inviteModal">Пригласить</a>
|
||||
<a class="dropdown-item" data-toggle="modal" data-target="#changePasswordModal">Сменить пароль</a>
|
||||
</div>
|
||||
</li>
|
||||
</ul>
|
||||
@ -77,6 +78,7 @@
|
||||
</div>
|
||||
</nav>
|
||||
<div th:replace="users/inviteModal"/>
|
||||
<div th:replace="users/changePassword"/>
|
||||
<div class="container-fluid">
|
||||
<div class="container">
|
||||
<ul id="messages" class="feedback-panel">
|
||||
|
37
src/main/resources/templates/users/changePassword.html
Normal file
37
src/main/resources/templates/users/changePassword.html
Normal file
@ -0,0 +1,37 @@
|
||||
<!DOCTYPE HTML>
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<head th:fragment="headerfiles">
|
||||
<meta charset="UTF-8"/>
|
||||
</head>
|
||||
<body>
|
||||
<div id="changePasswordModal" class="modal fade text-center">
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-content">
|
||||
<form id="invite-form" method="post" action="/users/changePassword">
|
||||
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title" id="label">Пригласить пользователя</h5>
|
||||
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
|
||||
<span aria-hidden="true">×</span>
|
||||
</button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<input class="form-control" id="oldPassword" type="password"
|
||||
placeholder="Старый пароль" name="oldPassword"/>
|
||||
<br />
|
||||
<input class="form-control" id="password" type="password"
|
||||
placeholder="Новый пароль" name="password"/>
|
||||
<br />
|
||||
<input class="form-control" id="confirmPassword" type="password"
|
||||
placeholder="Подтверждение нового пароля" name="confirmPassword"/>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-secondary" data-dismiss="modal">Закрыть</button>
|
||||
<button type="submit" class="btn btn-primary">Сохранить</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user