From 868c77baae67f3fccd5e46d2c0ddb950246741c8 Mon Sep 17 00:00:00 2001 From: 0xFF Date: Fri, 11 Nov 2016 17:21:43 +0300 Subject: [PATCH] JC-2173 Added email sending when password was changed --- .../service/nontransactional/MailService.java | 22 +++++++++++++++++++ .../TransactionalUserService.java | 1 + .../bundle/TemplatesMessages_en.properties | 2 ++ .../bundle/TemplatesMessages_es.properties | 2 ++ .../bundle/TemplatesMessages_ru.properties | 2 ++ .../bundle/TemplatesMessages_uk.properties | 2 ++ .../html/passwordUpdatedNotification.vm | 22 +++++++++++++++++++ .../plaintext/passwordUpdatedNotification.vm | 22 +++++++++++++++++++ .../nontransactional/MailServiceTest.java | 16 ++++++++++++++ .../web/controller/UserProfileController.java | 17 ++++++++++---- 10 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 jcommune-service/src/main/resources/org/jtalks/jcommune/service/templates/html/passwordUpdatedNotification.vm create mode 100644 jcommune-service/src/main/resources/org/jtalks/jcommune/service/templates/plaintext/passwordUpdatedNotification.vm diff --git a/jcommune-service/src/main/java/org/jtalks/jcommune/service/nontransactional/MailService.java b/jcommune-service/src/main/java/org/jtalks/jcommune/service/nontransactional/MailService.java index babad6c83c..8c9b2d97bb 100644 --- a/jcommune-service/src/main/java/org/jtalks/jcommune/service/nontransactional/MailService.java +++ b/jcommune-service/src/main/java/org/jtalks/jcommune/service/nontransactional/MailService.java @@ -129,6 +129,28 @@ public void sendPasswordRecoveryMail(JCUser user, String newPassword) throws Mai LOGGER.info("Password recovery email sent for {}", name); } + /** + * Sends a notification to user that his e-mail was changed. + * + * @param user a person we will send a mail + * @throws MailingFailedException when mailing failed + */ + public void sendPasswordUpdatedMail(final JCUser user) { + String name = user.getUsername(); + Locale locale = user.getLanguage().getLocale(); + Map model = new HashMap<>(); + model.put(NAME, name); + model.put(RECIPIENT_LOCALE, locale); + try { + this.sendEmail(user.getEmail(), + messageSource.getMessage("passwordUpdatedNotification.subject", new Object[]{}, locale), + model, "passwordUpdatedNotification.vm"); + LOGGER.info("Password updated email sent for {}", name); + } catch (MailingFailedException e) { + LOGGER.info("Password update for '{}' wasn't sent because {}", name, e.getMessage()); + } + } + /** * Sends update notification to user specified if * {@link SubscriptionAwareEntity} was updated, e.g. when some new diff --git a/jcommune-service/src/main/java/org/jtalks/jcommune/service/transactional/TransactionalUserService.java b/jcommune-service/src/main/java/org/jtalks/jcommune/service/transactional/TransactionalUserService.java index 203e9565c9..1477879bb8 100644 --- a/jcommune-service/src/main/java/org/jtalks/jcommune/service/transactional/TransactionalUserService.java +++ b/jcommune-service/src/main/java/org/jtalks/jcommune/service/transactional/TransactionalUserService.java @@ -214,6 +214,7 @@ public JCUser saveEditedUserSecurity(long editedUserId, UserSecurityContainer us editedUser.setPassword(encryptedPassword); } + mailService.sendPasswordUpdatedMail(editedUser); this.getDao().saveOrUpdate(editedUser); LOGGER.info("Updated user security settings. Username: {}", editedUser.getUsername()); return editedUser; diff --git a/jcommune-service/src/main/resources/org/jtalks/jcommune/service/bundle/TemplatesMessages_en.properties b/jcommune-service/src/main/resources/org/jtalks/jcommune/service/bundle/TemplatesMessages_en.properties index 883a80bea6..eb4830d829 100644 --- a/jcommune-service/src/main/resources/org/jtalks/jcommune/service/bundle/TemplatesMessages_en.properties +++ b/jcommune-service/src/main/resources/org/jtalks/jcommune/service/bundle/TemplatesMessages_en.properties @@ -17,6 +17,8 @@ passwordRecovery.subject=Password recovery passwordRecovery.content.part1=This is a password recovery mail from JTalks forum. passwordRecovery.content.part2=Your new password is: passwordRecovery.link=Feel free to log in at +passwordUpdatedNotification.subject=Password updated +passwordUpdatedNotification.content=Your password has been successfully changed. If you didn't change it, please get into touch with the admin admin@jcommune.com moveTopic.subject=Topic was moved moveTopic.content=Topic was moved to another branch moveTopic.by=by diff --git a/jcommune-service/src/main/resources/org/jtalks/jcommune/service/bundle/TemplatesMessages_es.properties b/jcommune-service/src/main/resources/org/jtalks/jcommune/service/bundle/TemplatesMessages_es.properties index 352f73c7aa..7a6e517097 100644 --- a/jcommune-service/src/main/resources/org/jtalks/jcommune/service/bundle/TemplatesMessages_es.properties +++ b/jcommune-service/src/main/resources/org/jtalks/jcommune/service/bundle/TemplatesMessages_es.properties @@ -17,6 +17,8 @@ passwordRecovery.subject=Recuperaci\u00f3n de contrase\u00f1a passwordRecovery.content.part1=Este es un e-mail de recuperaci\u00f3n de contrase\u00f1a del foro JTalks. passwordRecovery.content.part2=Tu nueva contrase\u00f1a es: passwordRecovery.link=Puedes acceder en: +passwordUpdatedNotification.subject=Recuperaci actualizado +passwordUpdatedNotification.content=Su contrase\u00F1a ha sido cambiada con \u00E9xito. Si no cambia, por favor p\u00F3ngase en contacto con el administrador admin@jcommune.com moveTopic.subject=El tema ha sido eliminado moveTopic.content=El tema ha sido movido a otra categor\u00eda moveTopic.by=por diff --git a/jcommune-service/src/main/resources/org/jtalks/jcommune/service/bundle/TemplatesMessages_ru.properties b/jcommune-service/src/main/resources/org/jtalks/jcommune/service/bundle/TemplatesMessages_ru.properties index 52605c7b40..ece35e43a2 100644 --- a/jcommune-service/src/main/resources/org/jtalks/jcommune/service/bundle/TemplatesMessages_ru.properties +++ b/jcommune-service/src/main/resources/org/jtalks/jcommune/service/bundle/TemplatesMessages_ru.properties @@ -17,6 +17,8 @@ passwordRecovery.subject=\u0412\u043E\u0441\u0441\u0442\u0430\u043D\u043E\u0432\ passwordRecovery.content.part1=\u0417\u0430\u0431\u044B\u043B\u0438 \u043F\u0430\u0440\u043E\u043B\u044C \u043E\u0442 \u0430\u043A\u043A\u0430\u0443\u043D\u0442\u0430 \u043D\u0430 JTalks? \u041D\u0435 \u0441\u0442\u0440\u0430\u0448\u043D\u043E, \u043C\u044B \u0441\u0433\u0435\u043D\u0435\u0440\u0438\u0440\u043E\u0432\u0430\u043B\u0438 \u043D\u043E\u0432\u044B\u0439. passwordRecovery.content.part2=\u0412\u0430\u0448 \u043D\u043E\u0432\u044B\u0439 \u043F\u0430\u0440\u043E\u043B\u044C: passwordRecovery.link=\u0410\u0432\u0442\u043E\u0440\u0438\u0437\u043E\u0432\u0430\u0442\u044C\u0441\u044F \u043C\u043E\u0436\u043D\u043E \u0437\u0434\u0435\u0441\u044C +passwordUpdatedNotification.subject=\u041F\u0430\u0440\u043E\u043B\u044C \u043E\u0431\u043D\u043E\u0432\u043B\u0435\u043D +passwordUpdatedNotification.content=\u0412\u0430\u0448 \u043F\u0430\u0440\u043E\u043B\u044C \u0431\u044B\u043B \u0443\u0441\u043F\u0435\u0448\u043D\u043E \u0438\u0437\u043C\u0435\u043D\u0435\u043D. \u0415\u0441\u043B\u0438 \u0412\u044B \u043D\u0435 \u043C\u0435\u043D\u044F\u043B\u0438 \u0435\u0433\u043E, \u043F\u043E\u0436\u0430\u043B\u0443\u0439\u0441\u0442\u0430, \u0441\u0432\u044F\u0436\u0438\u0442\u0435\u0441\u044C \u0441 \u0430\u0434\u043C\u0438\u043D\u0438\u0441\u0442\u0440\u0430\u0442\u043E\u0440\u043E\u043C admin@jcommune.com moveTopic.subject=\u0422\u043E\u043F\u0438\u043A \u0431\u044B\u043B \u043F\u0435\u0440\u0435\u043D\u0435\u0441\u0435\u043D moveTopic.content=\u0422\u043E\u043F\u0438\u043A \u0431\u044B\u043B \u043F\u0435\u0440\u0435\u043D\u0435\u0441\u0435\u043D \u0432 \u0434\u0440\u0443\u0433\u0443\u044E \u0432\u0435\u0442\u043A\u0443 moveTopic.by=\u043F\u043E\u043B\u044C\u0437\u043E\u0432\u0430\u0442\u0435\u043B\u0435\u043C diff --git a/jcommune-service/src/main/resources/org/jtalks/jcommune/service/bundle/TemplatesMessages_uk.properties b/jcommune-service/src/main/resources/org/jtalks/jcommune/service/bundle/TemplatesMessages_uk.properties index 62a7d39165..6e7aae2a7d 100644 --- a/jcommune-service/src/main/resources/org/jtalks/jcommune/service/bundle/TemplatesMessages_uk.properties +++ b/jcommune-service/src/main/resources/org/jtalks/jcommune/service/bundle/TemplatesMessages_uk.properties @@ -17,6 +17,8 @@ passwordRecovery.subject=\u0412\u0456\u0434\u043D\u043E\u0432\u043B\u0435\u043D\ passwordRecovery.content.part1=\u0426\u0435 \u043B\u0438\u0441\u0442 \u0434\u043B\u044F \u0432\u0456\u0434\u043D\u043E\u0432\u043B\u0435\u043D\u043D\u044F \u043F\u0430\u0440\u043E\u043B\u044E \u043D\u0430 \u0444\u043E\u0440\u0443\u043C\u0456 JTalks. passwordRecovery.content.part2=\u0412\u0430\u0448 \u043D\u043E\u0432\u0438\u0439 \u043F\u0430\u0440\u043E\u043B\u044C: passwordRecovery.link=\u0410\u0432\u0442\u043E\u0440\u0438\u0437\u0443\u0432\u0430\u0442\u0438\u0441\u044C \u043C\u043E\u0436\u043D\u0430 \u0442\u0443\u0442 +passwordUpdatedNotification.subject=\u041F\u0430\u0440\u043E\u043B\u044C \u043E\u043D\u043E\u0432\u043B\u0435\u043D\u043E +passwordUpdatedNotification.content=\u0412\u0430\u0448 \u043F\u0430\u0440\u043E\u043B\u044C \u0431\u0443\u043B\u043E \u0443\u043F\u0456\u0448\u043D\u043E \u0437\u043C\u0456\u043D\u0435\u043D\u043E. \u042F\u043A\u0449\u043E \u0412\u0438 \u043D\u0435 \u0437\u043C\u0456\u043D\u044E\u0432\u0430\u043B\u0438 \u0439\u043E\u0433\u043E, \u0431\u0443\u0434\u044C \u043B\u0430\u0441\u043A\u0430 \u0437\u0432\u2019\u044F\u0436\u0456\u0442\u044C\u0441\u044F \u0437 \u0430\u0434\u043C\u0456\u043D\u0456\u0441\u0442\u0440\u0430\u0442\u043E\u0440\u043E\u043C admin@jcommune.com moveTopic.subject=\u0422\u0435\u043C\u0430 \u0431\u0443\u043B\u0430 \u043F\u0435\u0440\u0435\u043C\u0456\u0449\u0435\u043D\u0430 moveTopic.content=\u0422\u0435\u043C\u0430 \u0431\u0443\u043B\u0430 \u043F\u0435\u0440\u0435\u043C\u0456\u0449\u0435\u043D\u0430 \u0434\u043E \u0456\u043D\u0448\u043E\u0457 \u0433\u0456\u043B\u043A\u0438 moveTopic.by=\u043A\u043E\u0440\u0438\u0441\u0442\u0443\u0432\u0430\u0447\u0435\u043C diff --git a/jcommune-service/src/main/resources/org/jtalks/jcommune/service/templates/html/passwordUpdatedNotification.vm b/jcommune-service/src/main/resources/org/jtalks/jcommune/service/templates/html/passwordUpdatedNotification.vm new file mode 100644 index 0000000000..18da0a1778 --- /dev/null +++ b/jcommune-service/src/main/resources/org/jtalks/jcommune/service/templates/html/passwordUpdatedNotification.vm @@ -0,0 +1,22 @@ +#* + +Copyright (C) 2011 JTalks.org Team +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +*# +

${messageSource.getMessage("greeting", $noArgs, $locale)} ${escape.html($name)}!

+
+

${messageSource.getMessage("passwordUpdatedNotification.content", $noArgs, $locale)}

+
+

${messageSource.getMessage("wish", $noArgs, $locale)}

+

${messageSource.getMessage("signature", $noArgs, $locale)}

\ No newline at end of file diff --git a/jcommune-service/src/main/resources/org/jtalks/jcommune/service/templates/plaintext/passwordUpdatedNotification.vm b/jcommune-service/src/main/resources/org/jtalks/jcommune/service/templates/plaintext/passwordUpdatedNotification.vm new file mode 100644 index 0000000000..dce7b79b50 --- /dev/null +++ b/jcommune-service/src/main/resources/org/jtalks/jcommune/service/templates/plaintext/passwordUpdatedNotification.vm @@ -0,0 +1,22 @@ +#* + +Copyright (C) 2011 JTalks.org Team +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +*# +${messageSource.getMessage("greeting", $noArgs, $locale)} ${name}! + +${messageSource.getMessage("passwordUpdatedNotification.content", $noArgs, $locale)} + +${messageSource.getMessage("wish", $noArgs, $locale)} +${messageSource.getMessage("signature", $noArgs, $locale)} \ No newline at end of file diff --git a/jcommune-service/src/test/java/org/jtalks/jcommune/service/nontransactional/MailServiceTest.java b/jcommune-service/src/test/java/org/jtalks/jcommune/service/nontransactional/MailServiceTest.java index 66e4987699..94ef68fe89 100644 --- a/jcommune-service/src/test/java/org/jtalks/jcommune/service/nontransactional/MailServiceTest.java +++ b/jcommune-service/src/test/java/org/jtalks/jcommune/service/nontransactional/MailServiceTest.java @@ -136,6 +136,22 @@ public void passwordRecoveryMailShouldNotBeSentIfNotificationsAreDisabled() thro verify(sender, never()).send(any(MimeMessage.class)); } + @Test + public void testSendPasswordUpdatedMail() throws Exception { + enableEmailNotifications(); + service.sendPasswordUpdatedMail(user); + + this.checkMailCredentials(); + assertTrue(this.getMimeMailBody().contains(USERNAME)); + } + + @Test + public void testSendPasswordUpdatedMailWhenNotificationsAreDisabled() throws Exception { + disableEmailNotifications(); + service.sendPasswordUpdatedMail(user); + verify(sender, never()).send(any(MimeMessage.class)); + } + @Test public void testSendTopicUpdatesEmail() throws Exception { enableEmailNotifications(); diff --git a/jcommune-view/jcommune-web-controller/src/main/java/org/jtalks/jcommune/web/controller/UserProfileController.java b/jcommune-view/jcommune-web-controller/src/main/java/org/jtalks/jcommune/web/controller/UserProfileController.java index f04a0e8d29..95239c0560 100644 --- a/jcommune-view/jcommune-web-controller/src/main/java/org/jtalks/jcommune/web/controller/UserProfileController.java +++ b/jcommune-view/jcommune-web-controller/src/main/java/org/jtalks/jcommune/web/controller/UserProfileController.java @@ -17,15 +17,19 @@ import org.jtalks.jcommune.model.entity.JCUser; import org.jtalks.jcommune.model.entity.Language; import org.jtalks.jcommune.model.entity.Post; +import org.jtalks.jcommune.plugin.api.exceptions.NotFoundException; +import org.jtalks.jcommune.plugin.api.web.util.BreadcrumbBuilder; import org.jtalks.jcommune.service.PostService; import org.jtalks.jcommune.service.UserContactsService; import org.jtalks.jcommune.service.UserService; -import org.jtalks.jcommune.plugin.api.exceptions.NotFoundException; import org.jtalks.jcommune.service.dto.EntityToDtoConverter; import org.jtalks.jcommune.service.nontransactional.ImageConverter; import org.jtalks.jcommune.service.nontransactional.ImageService; -import org.jtalks.jcommune.web.dto.*; -import org.jtalks.jcommune.plugin.api.web.util.BreadcrumbBuilder; +import org.jtalks.jcommune.web.dto.EditUserProfileDto; +import org.jtalks.jcommune.web.dto.UserContactsDto; +import org.jtalks.jcommune.web.dto.UserNotificationsDto; +import org.jtalks.jcommune.web.dto.UserProfileDto; +import org.jtalks.jcommune.web.dto.UserSecurityDto; import org.jtalks.jcommune.web.validation.editors.DefaultAvatarEditor; import org.jtalks.jcommune.web.validation.editors.DefaultStringEditor; import org.jtalks.jcommune.web.validation.editors.PageSizeEditor; @@ -41,7 +45,12 @@ import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; import org.springframework.web.bind.WebDataBinder; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.InitBinder; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.LocaleResolver; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.support.RedirectAttributes;