From 4ff2f7a462e9d6ef5171b7d0a4c8d3cf3f26c024 Mon Sep 17 00:00:00 2001 From: INOPIAE Date: Tue, 23 Aug 2016 18:43:31 +0200 Subject: [PATCH] add: send notification to support and user after support actions notifications will be send to support and the user after revoking certificates, changing DoB and support groups Change-Id: Iccdcb2799fa617a98b140bbfe9531a882e61b1ae --- .../SupportUserNotificationMail.templ | 7 +++ .../cacert/gigi/dbObjects/SupportedUser.java | 24 ++++++++++ .../SupportRevokeCertificatesForm.java | 11 ++++- .../admin/support/SupportUserDetailsForm.java | 19 ++++++-- .../admin/TestSEAdminNotificationMail.java | 48 +++++++++++++++---- 5 files changed, 93 insertions(+), 16 deletions(-) create mode 100644 src/org/cacert/gigi/dbObjects/SupportUserNotificationMail.templ diff --git a/src/org/cacert/gigi/dbObjects/SupportUserNotificationMail.templ b/src/org/cacert/gigi/dbObjects/SupportUserNotificationMail.templ new file mode 100644 index 00000000..6b69f184 --- /dev/null +++ b/src/org/cacert/gigi/dbObjects/SupportUserNotificationMail.templ @@ -0,0 +1,7 @@ +Subject: [] Support action: + +, + + + + diff --git a/src/org/cacert/gigi/dbObjects/SupportedUser.java b/src/org/cacert/gigi/dbObjects/SupportedUser.java index a4a3ba12..e5daeb31 100644 --- a/src/org/cacert/gigi/dbObjects/SupportedUser.java +++ b/src/org/cacert/gigi/dbObjects/SupportedUser.java @@ -10,6 +10,7 @@ import org.cacert.gigi.dbObjects.Certificate.CertificateStatus; import org.cacert.gigi.localisation.Language; import org.cacert.gigi.output.template.MailTemplate; import org.cacert.gigi.output.template.Outputable; +import org.cacert.gigi.output.template.SprintfCommand; import org.cacert.gigi.util.DayDate; import org.cacert.gigi.util.ServerConstants; @@ -53,6 +54,14 @@ public class SupportedUser { if (cert.getStatus() == CertificateStatus.ISSUED) { writeSELog("SE Revoke certificate"); cert.revoke().waitFor(60000); + // send notification to support + String subject = "Revoke certificate"; + Outputable message = SprintfCommand.createSimple("Certificate with serial number {0} for {1} <{2}>, has been revoked.", cert.getSerial(), target.getPreferredName().toString(), target.getEmail()); + sendSupportNotification(subject, message); + // send notification to user + subject = "Revoke certificate"; + message = SprintfCommand.createSimple("Certificate with serial number {0} with subject distinguished name {1} has been revoked.", cert.getSerial(), cert.getDistinguishedName()); + sendSupportUserNotification(subject, message); } } @@ -109,4 +118,19 @@ public class SupportedUser { e.printStackTrace(); } } + + private static final MailTemplate supportUserNotification = new MailTemplate(SupportedUser.class.getResource("SupportUserNotificationMail.templ")); + + public void sendSupportUserNotification(String subject, Outputable message) { + try { + HashMap vars = new HashMap<>(); + vars.put("action", message); + vars.put("ticket", this.getTicket()); + vars.put("subject", subject); + + supportUserNotification.sendMail(Language.getInstance(Locale.ENGLISH), vars, target.getEmail()); + } catch (IOException e) { + e.printStackTrace(); + } + } } diff --git a/src/org/cacert/gigi/pages/admin/support/SupportRevokeCertificatesForm.java b/src/org/cacert/gigi/pages/admin/support/SupportRevokeCertificatesForm.java index ff64c6f5..8c106dcb 100644 --- a/src/org/cacert/gigi/pages/admin/support/SupportRevokeCertificatesForm.java +++ b/src/org/cacert/gigi/pages/admin/support/SupportRevokeCertificatesForm.java @@ -13,12 +13,13 @@ import org.cacert.gigi.dbObjects.Certificate; import org.cacert.gigi.dbObjects.Certificate.CertificateStatus; import org.cacert.gigi.dbObjects.CertificateProfile; import org.cacert.gigi.dbObjects.SupportedUser; +import org.cacert.gigi.dbObjects.User; import org.cacert.gigi.localisation.Language; import org.cacert.gigi.output.template.Form; import org.cacert.gigi.output.template.IterableDataset; import org.cacert.gigi.output.template.Outputable; +import org.cacert.gigi.output.template.SprintfCommand; import org.cacert.gigi.output.template.Template; -import org.cacert.gigi.output.template.TranslateCommand; public class SupportRevokeCertificatesForm extends Form { @@ -35,9 +36,15 @@ public class SupportRevokeCertificatesForm extends Form { public boolean submit(PrintWriter out, HttpServletRequest req) throws GigiApiException { if (user.getTicket() != null) { user.revokeAllCertificates(); + User target = user.getTargetUser(); + // send notification to support String subject = "Revoke certificates"; - Outputable message = new TranslateCommand("All certificates in the account have been revoked."); + Outputable message = SprintfCommand.createSimple("All certificates in the account {0} <{1}> have been revoked.", target.getPreferredName().toString(), target.getEmail()); user.sendSupportNotification(subject, message); + // send notification to user + subject = "Revoke certificate"; + message = SprintfCommand.createSimple("All certificates in your account have been revoked."); + user.sendSupportUserNotification(subject, message); return true; } return false; diff --git a/src/org/cacert/gigi/pages/admin/support/SupportUserDetailsForm.java b/src/org/cacert/gigi/pages/admin/support/SupportUserDetailsForm.java index ccfb3cc7..57382036 100644 --- a/src/org/cacert/gigi/pages/admin/support/SupportUserDetailsForm.java +++ b/src/org/cacert/gigi/pages/admin/support/SupportUserDetailsForm.java @@ -48,18 +48,23 @@ public class SupportUserDetailsForm extends Form { throw new GigiApiException("More than one action requested!"); } if (req.getParameter("addGroup") != null || req.getParameter("removeGroup") != null) { - String actionType = "granted"; value.update(req); Group toMod = value.getGroup(); + boolean grant; if (req.getParameter("addGroup") != null) { + grant = true; user.grant(toMod); } else { - actionType = "revoked"; + grant = false; user.revoke(toMod); } String subject = "Change Group Permissions"; - Outputable message = SprintfCommand.createSimple("The group permission {0} was {1}.", toMod.getDatabaseName(), actionType); + // send notification to support + Outputable message = SprintfCommand.createSimple(grant ? "The group permission '{0}' was granted." : "The group permission '{0}' was revoked.", toMod.getName()); user.sendSupportNotification(subject, message); + // send notification to user + message = SprintfCommand.createSimple(grant ? "The group permission '{0}' was granted to your account." : "The group permission '{0}' was revoked from your account.", toMod.getName()); + user.sendSupportUserNotification(subject, message); return true; } if (req.getParameter("resetPass") != null) { @@ -81,9 +86,13 @@ public class SupportUserDetailsForm extends Form { } user.setDob(dobSelector.getDate()); - String subject = "Change Account Data"; - Outputable message = new TranslateCommand("The account data was changed."); + String subject = "Change DoB Data"; + // send notification to support + Outputable message = new TranslateCommand("The DoB was changed."); user.sendSupportNotification(subject, message); + // send notification to user + message = SprintfCommand.createSimple("The DoB in your account was changed to {0}.", dobSelector.getDate()); + user.sendSupportUserNotification(subject, message); return true; } diff --git a/tests/org/cacert/gigi/pages/admin/TestSEAdminNotificationMail.java b/tests/org/cacert/gigi/pages/admin/TestSEAdminNotificationMail.java index c51e5cc4..dac9053e 100644 --- a/tests/org/cacert/gigi/pages/admin/TestSEAdminNotificationMail.java +++ b/tests/org/cacert/gigi/pages/admin/TestSEAdminNotificationMail.java @@ -4,10 +4,17 @@ import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.*; import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; import java.net.MalformedURLException; +import java.net.URLEncoder; +import java.util.HashMap; +import java.util.Locale; import org.cacert.gigi.GigiApiException; import org.cacert.gigi.dbObjects.Group; +import org.cacert.gigi.dbObjects.User; +import org.cacert.gigi.localisation.Language; import org.cacert.gigi.pages.admin.support.SupportEnterTicketPage; import org.cacert.gigi.pages.admin.support.SupportUserDetailsPage; import org.cacert.gigi.testUtils.ClientTest; @@ -35,10 +42,13 @@ public class TestSEAdminNotificationMail extends ClientTest { executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + targetID + "/", "dobd=1&dobm=2&doby=2000&detailupdate", 0); + // mail to support String message = getMailReceiver().receive().getMessage(); - assertThat(message, containsString("The account data was changed.")); + assertThat(message, containsString("The DoB was changed")); assertThat(message, containsString("supporter " + u.getPreferredName().toString() + " triggered:")); - + // mail to user + message = getMailReceiver().receive().getMessage(); + assertThat(message, containsString("The DoB in your account was changed to 2000-02-01.")); } @Test @@ -54,26 +64,46 @@ public class TestSEAdminNotificationMail extends ClientTest { @Test public void testGrantUserGroup() throws MalformedURLException, IOException { - executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + targetID + "/", "addGroup&groupToModify=supporter", 0); + executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + targetID + "/", "addGroup&groupToModify=" + URLEncoder.encode(Group.SUPPORTER.getDatabaseName(), "UTF-8"), 0); + + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + Group.SUPPORTER.getName().output(pw, Language.getInstance(Locale.ENGLISH), new HashMap()); + // mail to support String message = getMailReceiver().receive().getMessage(); - assertThat(message, containsString("The group permission supporter was granted.")); + assertThat(message, containsString("The group permission '" + sw.toString() + "' was granted.")); + // mail to user + message = getMailReceiver().receive().getMessage(); + assertThat(message, containsString("The group permission '" + sw.toString() + "' was granted to your account.")); } @Test public void testRemoveUserGroup() throws MalformedURLException, IOException { - executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + targetID + "/", "removeGroup&groupToModify=supporter", 0); + executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + targetID + "/", "removeGroup&groupToModify=" + URLEncoder.encode(Group.SUPPORTER.getDatabaseName(), "UTF-8"), 0); + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + Group.SUPPORTER.getName().output(pw, Language.getInstance(Locale.ENGLISH), new HashMap()); + + // mail to support String message = getMailReceiver().receive().getMessage(); - assertThat(message, containsString("The group permission supporter was revoked.")); + assertThat(message, containsString("The group permission '" + sw.toString() + "' was revoked.")); + // mail to user + message = getMailReceiver().receive().getMessage(); + assertThat(message, containsString("The group permission '" + sw.toString() + "' was revoked from your account.")); } @Test - public void testRevokeCertificates() throws MalformedURLException, IOException { + public void testRevokeAllCertificates() throws MalformedURLException, IOException { executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + targetID + "/", "revokeall", 1); + User user = User.getById(targetID); + // mail to support String message = getMailReceiver().receive().getMessage(); - assertThat(message, containsString("All certificates in the account have been revoked.")); - + assertThat(message, containsString("All certificates in the account " + user.getPreferredName().toString())); + // mail to user + message = getMailReceiver().receive().getMessage(); + assertThat(message, containsString("All certificates in your account have been revoked.")); } } -- 2.39.2