From adaf867aa0aebdf1278b46eba300acc1a0760a9e Mon Sep 17 00:00:00 2001 From: INOPIAE Date: Wed, 27 Jul 2016 11:55:34 +0200 Subject: [PATCH] add: send notification to applicant after successful verification fixes issue #94 Change-Id: I1c1459474fbe7dce3bc35df98daceff387055ee5 --- .../cacert/gigi/pages/wot/AssuranceForm.java | 15 +++-- src/org/cacert/gigi/util/Notary.java | 56 +++++++++++++++++++ .../gigi/util/VerificationEntered.templ | 15 +++++ .../account/TestPasswordResetExternal.java | 3 + 4 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 src/org/cacert/gigi/util/VerificationEntered.templ diff --git a/src/org/cacert/gigi/pages/wot/AssuranceForm.java b/src/org/cacert/gigi/pages/wot/AssuranceForm.java index 7d15e8da..9e69bd2f 100644 --- a/src/org/cacert/gigi/pages/wot/AssuranceForm.java +++ b/src/org/cacert/gigi/pages/wot/AssuranceForm.java @@ -175,16 +175,21 @@ public class AssuranceForm extends Form { if ( !gae.isEmpty()) { throw gae; } + + LinkedList toAssure = new LinkedList(); for (int i = 0; i < selected.length; i++) { if (selected[i]) { - Notary.assure(assurer, assuree, assureeNames[i], dob, pointsI, location, req.getParameter("date"), type); + toAssure.add(assureeNames[i]); } } + + Notary.assureAll(assurer, assuree, dob, pointsI, location, req.getParameter("date"), type, toAssure.toArray(new Name[toAssure.size()])); + if (aword != null && !aword.equals("")) { - Language l = Language.getInstance(assuree.getPreferredLocale()); - String method = l.getTranslation("A password reset was triggered. If you did a password reset by assurance, please enter your secret password using this form:"); - String subject = l.getTranslation("Password reset by assurance"); - PasswordResetPage.initPasswordResetProcess(out, assuree, req, aword, l, method, subject); + Language langApplicant = Language.getInstance(assuree.getPreferredLocale()); + String method = langApplicant.getTranslation("A password reset was triggered. If you did a password reset by assurance, please enter your secret password using this form:"); + String subject = langApplicant.getTranslation("Password reset by assurance"); + PasswordResetPage.initPasswordResetProcess(out, assuree, req, aword, langApplicant, method, subject); } return true; } diff --git a/src/org/cacert/gigi/util/Notary.java b/src/org/cacert/gigi/util/Notary.java index 620eb89c..cba93aba 100644 --- a/src/org/cacert/gigi/util/Notary.java +++ b/src/org/cacert/gigi/util/Notary.java @@ -1,9 +1,12 @@ package org.cacert.gigi.util; +import java.io.IOException; import java.text.ParseException; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; +import java.util.HashMap; +import java.util.Map; import org.cacert.gigi.GigiApiException; import org.cacert.gigi.database.GigiPreparedStatement; @@ -12,7 +15,10 @@ import org.cacert.gigi.dbObjects.Assurance.AssuranceType; import org.cacert.gigi.dbObjects.Group; import org.cacert.gigi.dbObjects.Name; import org.cacert.gigi.dbObjects.User; +import org.cacert.gigi.localisation.Language; +import org.cacert.gigi.output.ArrayIterable; import org.cacert.gigi.output.DateSelector; +import org.cacert.gigi.output.template.MailTemplate; import org.cacert.gigi.output.template.SprintfCommand; public class Notary { @@ -231,4 +237,54 @@ public class Notary { ps.execute(); } } + + public synchronized static void assureAll(User assurer, User assuree, DayDate dob, int awarded, String location, String date, AssuranceType type, Name[] toAssure) throws GigiApiException { + boolean[] hadLessThan50Points = new boolean[toAssure.length]; + boolean hadTotalLessThan100 = assuree.getAssurancePoints() < 100; + for (int i = 0; i < toAssure.length; i++) { + hadLessThan50Points[i] = toAssure[i].getAssurancePoints() < 50; + + assure(assurer, assuree, toAssure[i], dob, awarded, location, date, type); + } + sendVerificationNotificationApplicant(assurer, assuree, toAssure, awarded, hadLessThan50Points, hadTotalLessThan100); + } + + private static final MailTemplate verificationEntered = new MailTemplate(Notary.class.getResource("VerificationEntered.templ")); + + private static void sendVerificationNotificationApplicant(User assurer, User assuree, Name[] toAssure, final int awarded, final boolean[] hadLessThan50Points, boolean hadTotalLessThan100) { + HashMap mailVars = new HashMap<>(); + mailVars.put("agent", assurer.getPreferredName().toString()); + mailVars.put("names", new ArrayIterable(toAssure) { + + @Override + public void apply(Name t, Language l, Map vars) { + int totalVP = t.getAssurancePoints(); + vars.put("name", t.toString()); + vars.put("points", Integer.toString(awarded)); + vars.put("total", totalVP); + if (totalVP < 50) { + vars.put("rem", (50 - totalVP)); + vars.remove("gotGreater"); + } else if (hadLessThan50Points[i]) { + vars.put("gotGreater", true); + vars.remove("rem"); + } + } + + }); + + int grandTotalVP = assuree.getAssurancePoints(); + if (grandTotalVP >= 50 && grandTotalVP < 100) { + mailVars.put("remAll", (100 - grandTotalVP)); + mailVars.remove("gotGreaterAll"); + } else if (hadTotalLessThan100) { + mailVars.put("gotGreaterAll", true); + mailVars.remove("remAll"); + } + try { + verificationEntered.sendMail(Language.getInstance(assuree.getPreferredLocale()), mailVars, assuree.getEmail()); + } catch (IOException e) { + e.printStackTrace(); + } + } } diff --git a/src/org/cacert/gigi/util/VerificationEntered.templ b/src/org/cacert/gigi/util/VerificationEntered.templ new file mode 100644 index 00000000..bf765492 --- /dev/null +++ b/src/org/cacert/gigi/util/VerificationEntered.templ @@ -0,0 +1,15 @@ +Subject: + +, + + + + + + + + + + + + diff --git a/tests/org/cacert/gigi/pages/account/TestPasswordResetExternal.java b/tests/org/cacert/gigi/pages/account/TestPasswordResetExternal.java index 1ec408a5..8f22ecf7 100644 --- a/tests/org/cacert/gigi/pages/account/TestPasswordResetExternal.java +++ b/tests/org/cacert/gigi/pages/account/TestPasswordResetExternal.java @@ -1,5 +1,6 @@ package org.cacert.gigi.pages.account; +import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.*; import java.io.IOException; @@ -34,6 +35,8 @@ public class TestPasswordResetExternal extends ClientTest { assertNull(error); TestMail mail = getMailReceiver().receive(); + assertThat(mail.getSubject(), containsString("Verification")); + mail = getMailReceiver().receive(); String link = mail.extractLink(); String npw = TEST_PASSWORD + "'"; System.out.println(link); -- 2.39.2