88 rest changePassword + ajax
This commit is contained in:
parent
4fc8bc3717
commit
052e4a1aef
@ -11,6 +11,6 @@ public class Response<D> extends ResponseEntity<Object> {
|
||||
}
|
||||
|
||||
public Response(ErrorConstants error) {
|
||||
super(new ControllerResponse<Void, Void>(new ControllerResponseError<>(error, null)), HttpStatus.OK);
|
||||
super(new ControllerResponse<Void, Void>(new ControllerResponseError<>(error, null)), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,6 @@ import ru.ulstu.core.model.ErrorConstants;
|
||||
public class ResponseExtended<E> extends ResponseEntity<Object> {
|
||||
|
||||
public ResponseExtended(ErrorConstants error, E errorData) {
|
||||
super(new ControllerResponse<Void, E>(new ControllerResponseError<E>(error, errorData)), HttpStatus.OK);
|
||||
super(new ControllerResponse<Void, E>(new ControllerResponseError<E>(error, errorData)), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
}
|
||||
|
@ -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<UserListDto, UserDto> {
|
||||
log.debug("REST: UserController.requestPasswordReset( {} )", key);
|
||||
return new Response<>(userService.completeUserPasswordReset(key, userResetPasswordDto));
|
||||
}
|
||||
|
||||
@PostMapping("/changePassword")
|
||||
public void changePassword(@RequestBody 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);
|
||||
}
|
||||
}
|
||||
|
@ -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<UserListDto, UserDto> {
|
||||
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<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:/";
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package ru.ulstu.user.error;
|
||||
|
||||
public class UserPasswordsNotValidOrNotMatchException extends RuntimeException {
|
||||
public UserPasswordsNotValidOrNotMatchException() {
|
||||
public UserPasswordsNotValidOrNotMatchException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
@ -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<String, String> 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) {
|
||||
|
33
src/main/resources/public/js/users.js
Normal file
33
src/main/resources/public/js/users.js
Normal file
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en"
|
||||
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
|
||||
layout:decorator="default" xmlns:th="http://www.w3.org/1999/xhtml">
|
||||
<head>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container" layout:fragment="content">
|
||||
<section id="services">
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
<div class="col-lg-12 text-center">
|
||||
<h2 class="section-heading text-uppercase"><span th:text="${error}"></span></h2>
|
||||
<a href="/"><h3>Вернуться на главную</h3></a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
@ -2,34 +2,33 @@
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<head th:fragment="headerfiles">
|
||||
<meta charset="UTF-8"/>
|
||||
<script type='text/javascript' src="js/users.js"></script>
|
||||
</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 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="Старый пароль" required="required" name="oldPassword"/>
|
||||
<br/>
|
||||
<input class="form-control" id="password" type="password"
|
||||
placeholder="Новый пароль" required="required" name="password"/>
|
||||
<br/>
|
||||
<input class="form-control" id="confirmPassword" type="password"
|
||||
placeholder="Подтверждение нового пароля" required="required" name="confirmPassword"/>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button id="closeModalPassword" type="button" class="btn btn-secondary" data-dismiss="modal">Закрыть
|
||||
</button>
|
||||
<button type="button" onclick="changePassword()" class="btn btn-primary">Сохранить</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
Loading…
Reference in New Issue
Block a user