1 package org.cacert.gigi.pages.wot;
3 import java.io.PrintWriter;
4 import java.text.SimpleDateFormat;
5 import java.util.Arrays;
6 import java.util.HashMap;
7 import java.util.HashSet;
8 import java.util.Iterator;
9 import java.util.LinkedList;
12 import javax.servlet.http.HttpServletRequest;
14 import org.cacert.gigi.GigiApiException;
15 import org.cacert.gigi.dbObjects.Assurance.AssuranceType;
16 import org.cacert.gigi.dbObjects.Name;
17 import org.cacert.gigi.dbObjects.User;
18 import org.cacert.gigi.localisation.Language;
19 import org.cacert.gigi.output.ArrayIterable;
20 import org.cacert.gigi.output.CountrySelector;
21 import org.cacert.gigi.output.template.Form;
22 import org.cacert.gigi.output.template.IterableDataset;
23 import org.cacert.gigi.output.template.Outputable;
24 import org.cacert.gigi.output.template.SprintfCommand;
25 import org.cacert.gigi.output.template.Template;
26 import org.cacert.gigi.output.template.TranslateCommand;
27 import org.cacert.gigi.pages.Page;
28 import org.cacert.gigi.pages.PasswordResetPage;
29 import org.cacert.gigi.util.DayDate;
30 import org.cacert.gigi.util.Notary;
32 public class AssuranceForm extends Form {
34 public static class ConcatOutputable implements Outputable {
36 private Outputable[] outputables;
38 public ConcatOutputable(Outputable... outputables) {
39 this.outputables = outputables;
43 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
44 for (int i = 0; i < outputables.length; i++) {
48 outputables[i].output(out, l, vars);
55 private Name[] assureeNames;
57 private boolean[] selected;
61 private String location = "";
63 private String date = "";
69 private AssuranceType type = AssuranceType.FACE_TO_FACE;
71 private static final Template templ = new Template(AssuranceForm.class.getResource("AssuranceForm.templ"));
73 private CountrySelector cs;
75 public AssuranceForm(HttpServletRequest hsr, User assuree) throws GigiApiException {
77 assurer = Page.getUser(hsr);
78 this.assuree = assuree;
80 if (assurer.getId() == assuree.getId()) {
81 throw new GigiApiException("You cannot verify yourself.");
83 if ( !assurer.canAssure()) {
84 throw new GigiApiException("You are not a RA-Agent.");
87 Name[] initialNames = this.assuree.getNonDeprecatedNames();
88 LinkedList<Name> names = new LinkedList<>();
89 for (Name name : initialNames) {
90 if (Notary.checkAssuranceIsPossible(assurer, name)) {
94 if (names.size() == 0) {
95 throw new GigiApiException(SprintfCommand.createSimple("You have already verified all names of this applicant within the last {0} days.", Notary.LIMIT_DAYS_VERIFICATION));
97 assureeNames = names.toArray(new Name[names.size()]);
98 dob = this.assuree.getDoB();
99 selected = new boolean[assureeNames.length];
100 cs = new CountrySelector("countryCode", false);
103 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
105 SimpleDateFormat sdf2 = new SimpleDateFormat("dd. MMM yyyy");
108 public void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
109 HashMap<String, Object> res = new HashMap<String, Object>();
111 res.put("names", new ArrayIterable<Name>(assureeNames) {
114 public void apply(Name t, Language l, Map<String, Object> vars) {
115 vars.put("nameExplicit", t);
116 vars.put("nameId", t.getId());
117 vars.put("checked", selected[i] ? " checked" : "");
121 res.put("name", assuree.getPreferredName().toString());
122 res.put("maxpoints", assurer.getMaxAssurePoints());
123 res.put("dob", sdf.format(assuree.getDoB().toDate()));
124 res.put("dobFmt2", sdf2.format(assuree.getDoB().toDate()));
125 res.put("location", location);
126 res.put("date", date);
127 res.put("aword", aword);
128 res.put("countryCode", cs);
130 final LinkedList<AssuranceType> ats = new LinkedList<>();
131 for (AssuranceType at : AssuranceType.values()) {
133 Notary.may(assurer, assuree, at);
135 } catch (GigiApiException e) {
138 res.put("ats", new IterableDataset() {
140 Iterator<AssuranceType> t = ats.iterator();
143 public boolean next(Language l, Map<String, Object> vars) {
147 AssuranceType t1 = t.next();
148 vars.put("type", t1.getDescription());
149 vars.put("id", t1.toString());
150 vars.put("sel", t1 == type ? " selected" : "");
154 templ.output(out, l, res);
158 public SubmissionResult submit(HttpServletRequest req) throws GigiApiException {
159 location = req.getParameter("location");
160 date = req.getParameter("date");
162 GigiApiException gae = new GigiApiException();
163 if (date == null || location == null) {
164 gae.mergeInto(new GigiApiException("You need to enter location and date!"));
167 if ( !"1".equals(req.getParameter("certify")) || !"1".equals(req.getParameter("rules")) || !"1".equals(req.getParameter("assertion"))) {
168 gae.mergeInto(new GigiApiException("You failed to check all boxes to validate" + " your adherence to the rules and policies of SomeCA"));
170 if ("1".equals(req.getParameter("passwordReset"))) {
171 aword = req.getParameter("passwordResetValue");
172 if ("".equals(aword)) {
178 String val = req.getParameter("assuranceType");
181 type = AssuranceType.valueOf(val);
182 } catch (IllegalArgumentException e) {
183 gae.mergeInto(new GigiApiException("Verification Type wrong."));
188 String points = req.getParameter("points");
189 if (points == null || "".equals(points)) {
190 gae.mergeInto(new GigiApiException("For a verification, you need to enter points."));
193 pointsI = Integer.parseInt(points);
194 } catch (NumberFormatException e) {
195 gae.mergeInto(new GigiApiException("The points entered were not a number."));
198 String[] parameterValues = req.getParameterValues("assuredName");
199 HashSet<String> data = new HashSet<>(Arrays.asList(parameterValues == null ? new String[0] : parameterValues));
200 for (int i = 0; i < assureeNames.length; i++) {
201 selected[i] = data.contains(Integer.toString(assureeNames[i].getId()));
204 if ( !gae.isEmpty()) {
208 LinkedList<Name> toAssure = new LinkedList<Name>();
209 for (int i = 0; i < selected.length; i++) {
211 toAssure.add(assureeNames[i]);
214 if (toAssure.size() == 0) {
215 throw new GigiApiException("You must confirm at least one name to verify an account.");
218 Notary.assureAll(assurer, assuree, dob, pointsI, location, req.getParameter("date"), type, toAssure.toArray(new Name[toAssure.size()]), cs.getCountry());
219 Outputable result = new TranslateCommand("Verification complete.");
220 if (isWithPasswordReset()) {
221 Language langApplicant = Language.getInstance(assuree.getPreferredLocale());
222 String method = langApplicant.getTranslation("A password reset was triggered. If you did a password reset by verification, please enter your secret password using this form:");
223 String subject = langApplicant.getTranslation("Password reset by verification");
224 PasswordResetPage.initPasswordResetProcess(assuree, req, aword, langApplicant, method, subject);
225 result = new ConcatOutputable(result, new TranslateCommand("Password reset successful."));
227 return new SuccessMessageResult(result);
230 public boolean isWithPasswordReset() {
231 return aword != null && !aword.equals("");
234 public User getAssuree() {