add: send notification to applicant after successful verification
authorINOPIAE <m.maengel@inopiae.de>
Wed, 27 Jul 2016 09:55:34 +0000 (11:55 +0200)
committerFelix Dörre <felix@dogcraft.de>
Fri, 29 Jul 2016 10:48:44 +0000 (12:48 +0200)
fixes issue #94

Change-Id: I1c1459474fbe7dce3bc35df98daceff387055ee5

src/org/cacert/gigi/pages/wot/AssuranceForm.java
src/org/cacert/gigi/util/Notary.java
src/org/cacert/gigi/util/VerificationEntered.templ [new file with mode: 0644]
tests/org/cacert/gigi/pages/account/TestPasswordResetExternal.java

index 7d15e8dac5386d25b7179df81805c97ad8a5e1c2..9e69bd2f4affed6eb79a427d78e379cee153719c 100644 (file)
@@ -175,16 +175,21 @@ public class AssuranceForm extends Form {
         if ( !gae.isEmpty()) {
             throw gae;
         }
+
+        LinkedList<Name> toAssure = new LinkedList<Name>();
         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;
     }
index 620eb89cb456b44bddc1760dfe5459a8b2fc4393..cba93aba8ee78f92a6b40b00252d8bc7c55eaf6b 100644 (file)
@@ -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<String, Object> mailVars = new HashMap<>();
+        mailVars.put("agent", assurer.getPreferredName().toString());
+        mailVars.put("names", new ArrayIterable<Name>(toAssure) {
+
+            @Override
+            public void apply(Name t, Language l, Map<String, Object> 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 (file)
index 0000000..bf76549
--- /dev/null
@@ -0,0 +1,15 @@
+Subject: <?=_Verification entered?>
+
+<?=_Hi?>,
+
+<?=_RA-Agent ${agent} verified your name(s):?>
+<? foreach($names) { ?>
+<?=_${name}: with ${points} to total ${total} Verification Points.?>
+<? if($rem) { ?><?=_To issue client certificates with this name you need ${rem} more Verification Points.?>
+<? } ?><? if($gotGreater) { ?><?=_You can now issue client certificates with this name.?>
+<? } ?>
+<? } ?>
+<? if($remAll) { ?><?=_To apply for RA Agent status or code signing ability you need ${remAll} more Verification Points.?>
+<? } ?><? if($gotGreaterAll) { ?><?=_You can now apply for RA Agent status or code signing ability.?>
+<? } ?>
+<?=_RA DB?>
index 1ec408a5083c9f3ef8159c9883cebb05df6b06f9..8f22ecf7cc48958e0587a3f5e5c6c8e7b54edcfd 100644 (file)
@@ -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);