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 @@ +