From 6a62e61caf9ad45a28f527fccd1fc02c66881879 Mon Sep 17 00:00:00 2001 From: "Artem.Arefev" Date: Fri, 17 May 2019 04:34:03 +0400 Subject: [PATCH 1/4] 87 error handling + ajax + rest --- .../core/controller/AdviceController.java | 6 ++++ .../ru/ulstu/core/model/ErrorConstants.java | 7 ++-- .../ulstu/user/controller/UserController.java | 6 ++++ .../user/controller/UserMvcController.java | 6 ---- .../ru/ulstu/user/service/MailService.java | 4 +-- .../ru/ulstu/user/service/UserService.java | 5 +-- src/main/resources/public/js/users.js | 24 +++++++++++++ .../templates/users/inviteModal.html | 34 +++++++++---------- 8 files changed, 61 insertions(+), 31 deletions(-) diff --git a/src/main/java/ru/ulstu/core/controller/AdviceController.java b/src/main/java/ru/ulstu/core/controller/AdviceController.java index 27ba6c9..b7ccace 100644 --- a/src/main/java/ru/ulstu/core/controller/AdviceController.java +++ b/src/main/java/ru/ulstu/core/controller/AdviceController.java @@ -20,6 +20,7 @@ import ru.ulstu.user.error.UserNotActivatedException; import ru.ulstu.user.error.UserNotFoundException; import ru.ulstu.user.error.UserPasswordsNotValidOrNotMatchException; import ru.ulstu.user.error.UserResetKeyError; +import ru.ulstu.user.error.UserSendingMailException; import ru.ulstu.user.service.UserService; import java.util.Set; @@ -112,4 +113,9 @@ public class AdviceController { public ResponseExtended handleUserIsUndeadException(Throwable e) { return handleException(ErrorConstants.USER_UNDEAD_ERROR, e.getMessage()); } + + @ExceptionHandler(UserSendingMailException.class) + public ResponseExtended handleUserSendingMailException(Throwable e ) { + return handleException(ErrorConstants.USER_SENDING_MAIL_EXCEPTION, e.getMessage()); + } } diff --git a/src/main/java/ru/ulstu/core/model/ErrorConstants.java b/src/main/java/ru/ulstu/core/model/ErrorConstants.java index 4347dfd..55832f3 100644 --- a/src/main/java/ru/ulstu/core/model/ErrorConstants.java +++ b/src/main/java/ru/ulstu/core/model/ErrorConstants.java @@ -6,14 +6,15 @@ public enum ErrorConstants { VALIDATION_ERROR(2, "Validation error"), USER_ID_EXISTS(100, "New user can't have id"), USER_ACTIVATION_ERROR(101, "Invalid activation key"), - USER_EMAIL_EXISTS(102, "User with same email already exists"), - USER_LOGIN_EXISTS(103, "User with same login already exists"), + USER_EMAIL_EXISTS(102, "Пользователь с таким почтовым ящиком уже существует"), + USER_LOGIN_EXISTS(103, "Пользователь с таким логином уже существует"), USER_PASSWORDS_NOT_VALID_OR_NOT_MATCH(104, "Пароли введены неверно"), USER_NOT_FOUND(105, "User is not found"), USER_NOT_ACTIVATED(106, "User is not activated"), USER_RESET_ERROR(107, "Invalid reset key"), USER_UNDEAD_ERROR(108, "Can't edit/delete that user"), - FILE_UPLOAD_ERROR(110, "File upload error"); + FILE_UPLOAD_ERROR(110, "File upload error"), + USER_SENDING_MAIL_EXCEPTION(111, "Во время отправки приглашения пользователю произошла ошибка"); private int code; private String message; diff --git a/src/main/java/ru/ulstu/user/controller/UserController.java b/src/main/java/ru/ulstu/user/controller/UserController.java index f674a23..59e023b 100644 --- a/src/main/java/ru/ulstu/user/controller/UserController.java +++ b/src/main/java/ru/ulstu/user/controller/UserController.java @@ -3,6 +3,7 @@ package ru.ulstu.user.controller; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.security.access.annotation.Secured; +import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -166,4 +167,9 @@ public class UserController extends OdinController { User user = userSessionService.getUserBySessionId(sessionId); userService.changeUserPassword(user, payload); } + + @PostMapping("/invite") + public void inviteUser(@RequestParam("email") String email) { + userService.inviteUser(email); + } } diff --git a/src/main/java/ru/ulstu/user/controller/UserMvcController.java b/src/main/java/ru/ulstu/user/controller/UserMvcController.java index 29123e9..bf5daba 100644 --- a/src/main/java/ru/ulstu/user/controller/UserMvcController.java +++ b/src/main/java/ru/ulstu/user/controller/UserMvcController.java @@ -49,10 +49,4 @@ public class UserMvcController extends OdinController { User user = userSessionService.getUserBySessionId(sessionId); modelMap.addAttribute("userDto", userService.updateUserInformation(user, userDto)); } - - @PostMapping("/invite") - public String inviteUser(@RequestParam(value = "email") String email, ModelMap modelMap) { - userService.inviteUser(email); - 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 9c71c10..ec91739 100644 --- a/src/main/java/ru/ulstu/user/service/MailService.java +++ b/src/main/java/ru/ulstu/user/service/MailService.java @@ -40,7 +40,7 @@ public class MailService { } @Async - public void sendEmail(String to, String subject, String content) throws MessagingException { + public void sendEmail(String to, String subject, String content) throws MessagingException, MailException { log.debug("Send email to '{}' with subject '{}'", to, subject); MimeMessage mimeMessage = javaMailSender.createMimeMessage(); MimeMessageHelper message = new MimeMessageHelper(mimeMessage, false, StandardCharsets.UTF_8.name()); @@ -111,7 +111,7 @@ public class MailService { sendEmailFromTemplate(user, "passwordResetEmail", Constants.MAIL_RESET); } - public void sendInviteMail(Map variables, String email) throws MessagingException { + public void sendInviteMail(Map variables, String email) throws MessagingException, MailException { sendEmailFromTemplate(variables, "userInviteEmail", Constants.MAIL_INVITE, email); } diff --git a/src/main/java/ru/ulstu/user/service/UserService.java b/src/main/java/ru/ulstu/user/service/UserService.java index d82d564..c889caa 100644 --- a/src/main/java/ru/ulstu/user/service/UserService.java +++ b/src/main/java/ru/ulstu/user/service/UserService.java @@ -5,6 +5,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.data.domain.Page; import org.springframework.data.domain.Sort; +import org.springframework.mail.MailException; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; @@ -336,8 +337,8 @@ public class UserService implements UserDetailsService { Map variables = ImmutableMap.of("password", password, "email", email); try { mailService.sendInviteMail(variables, email); - } catch (MessagingException e) { - throw new UserSendingMailException(INVITE_USER_EXCEPTION); + } catch (MessagingException | MailException e) { + throw new UserSendingMailException(email); } } } diff --git a/src/main/resources/public/js/users.js b/src/main/resources/public/js/users.js index fee4f70..ec297fc 100644 --- a/src/main/resources/public/js/users.js +++ b/src/main/resources/public/js/users.js @@ -30,4 +30,28 @@ function changePassword() { alert(errorData.responseJSON.error.message) } }) +} + +function inviteUser() { + email = document.getElementById("email").value; + re = /\S+@\S+\.\S+/; + + + if (!re.test(email)) { + alert("Некорректный почтовый ящик") + return; + } + + $.ajax({ + url:"/api/1.0/users/invite?email=" + email, + contentType: "application/json; charset=utf-8", + method: "POST", + success: function() { + document.getElementById("closeModalInvite").click(); + alert("Пользователь был успешно приглашен"); + }, + error: function(errorData) { + alert(errorData.responseJSON.error.message) + } + }) } \ No newline at end of file diff --git a/src/main/resources/templates/users/inviteModal.html b/src/main/resources/templates/users/inviteModal.html index 5909c61..8fd67e5 100644 --- a/src/main/resources/templates/users/inviteModal.html +++ b/src/main/resources/templates/users/inviteModal.html @@ -2,29 +2,27 @@ + From 76d3adb4649cb3de9fb0d378e74794d3d740d6e5 Mon Sep 17 00:00:00 2001 From: "Artem.Arefev" Date: Fri, 17 May 2019 08:58:27 +0400 Subject: [PATCH 2/4] 87 small fix whitespace --- src/main/java/ru/ulstu/core/controller/AdviceController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ru/ulstu/core/controller/AdviceController.java b/src/main/java/ru/ulstu/core/controller/AdviceController.java index b7ccace..27c9d5b 100644 --- a/src/main/java/ru/ulstu/core/controller/AdviceController.java +++ b/src/main/java/ru/ulstu/core/controller/AdviceController.java @@ -115,7 +115,7 @@ public class AdviceController { } @ExceptionHandler(UserSendingMailException.class) - public ResponseExtended handleUserSendingMailException(Throwable e ) { + public ResponseExtended handleUserSendingMailException(Throwable e) { return handleException(ErrorConstants.USER_SENDING_MAIL_EXCEPTION, e.getMessage()); } } From 7163937a53275c7fa14358d0f5b44df85afe1514 Mon Sep 17 00:00:00 2001 From: "Artem.Arefev" Date: Fri, 17 May 2019 12:20:08 +0400 Subject: [PATCH 3/4] 87 using showFeedBackMessage --- .../java/ru/ulstu/user/controller/UserMvcController.java | 1 + src/main/resources/public/js/users.js | 9 +++++---- src/main/resources/templates/users/inviteModal.html | 1 + 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/main/java/ru/ulstu/user/controller/UserMvcController.java b/src/main/java/ru/ulstu/user/controller/UserMvcController.java index bf5daba..db30cb1 100644 --- a/src/main/java/ru/ulstu/user/controller/UserMvcController.java +++ b/src/main/java/ru/ulstu/user/controller/UserMvcController.java @@ -4,6 +4,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; 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; diff --git a/src/main/resources/public/js/users.js b/src/main/resources/public/js/users.js index ec297fc..3f4af95 100644 --- a/src/main/resources/public/js/users.js +++ b/src/main/resources/public/js/users.js @@ -24,10 +24,11 @@ function changePassword() { method: "POST", success: function() { document.getElementById("closeModalPassword").click(); - alert("Пароль был обновлен"); + showFeedbackMessage("Пароль был обновлен", MessageTypesEnum.SUCCESS) + }, error: function(errorData) { - alert(errorData.responseJSON.error.message) + showFeedbackMessage(errorData.responseJSON.error.message, MessageTypesEnum.WARNING) } }) } @@ -48,10 +49,10 @@ function inviteUser() { method: "POST", success: function() { document.getElementById("closeModalInvite").click(); - alert("Пользователь был успешно приглашен"); + showFeedbackMessage("Пользователь был успешно приглашен", MessageTypesEnum.SUCCESS) }, error: function(errorData) { - alert(errorData.responseJSON.error.message) + showFeedbackMessage(errorData.responseJSON.error.message, MessageTypesEnum.WARNING) } }) } \ No newline at end of file diff --git a/src/main/resources/templates/users/inviteModal.html b/src/main/resources/templates/users/inviteModal.html index 8fd67e5..c91b397 100644 --- a/src/main/resources/templates/users/inviteModal.html +++ b/src/main/resources/templates/users/inviteModal.html @@ -3,6 +3,7 @@ +