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.template.Form;
21 import org.cacert.gigi.output.template.IterableDataset;
22 import org.cacert.gigi.output.template.SprintfCommand;
23 import org.cacert.gigi.output.template.Template;
24 import org.cacert.gigi.pages.Page;
25 import org.cacert.gigi.pages.PasswordResetPage;
26 import org.cacert.gigi.util.DayDate;
27 import org.cacert.gigi.util.Notary;
29 public class AssuranceForm extends Form {
33 private Name[] assureeNames;
35 private boolean[] selected;
39 private String location = "";
41 private String date = "";
47 private AssuranceType type = AssuranceType.FACE_TO_FACE;
49 private static final Template templ = new Template(AssuranceForm.class.getResource("AssuranceForm.templ"));
51 public AssuranceForm(HttpServletRequest hsr, User assuree) throws GigiApiException {
53 assurer = Page.getUser(hsr);
54 this.assuree = assuree;
56 if (assurer.getId() == assuree.getId()) {
57 throw new GigiApiException("You cannot verify yourself.");
59 if ( !assurer.canAssure()) {
60 throw new GigiApiException("You are not a RA-Agent.");
63 Name[] initialNames = this.assuree.getNonDeprecatedNames();
64 LinkedList<Name> names = new LinkedList<>();
65 for (Name name : initialNames) {
66 if (Notary.checkAssuranceIsPossible(assurer, name)) {
70 if (names.size() == 0) {
71 throw new GigiApiException(SprintfCommand.createSimple("You have already verified all names of this applicant within the last {0} days.", Notary.LIMIT_DAYS_VERIFICATION));
73 assureeNames = names.toArray(new Name[names.size()]);
74 dob = this.assuree.getDoB();
75 selected = new boolean[assureeNames.length];
78 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
80 SimpleDateFormat sdf2 = new SimpleDateFormat("dd. MMM yyyy");
83 public void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
84 HashMap<String, Object> res = new HashMap<String, Object>();
86 res.put("names", new ArrayIterable<Name>(assureeNames) {
89 public void apply(Name t, Language l, Map<String, Object> vars) {
90 vars.put("nameExplicit", t);
91 vars.put("nameId", t.getId());
92 vars.put("checked", selected[i] ? " checked" : "");
96 res.put("name", assuree.getPreferredName().toString());
97 res.put("maxpoints", assurer.getMaxAssurePoints());
98 res.put("dob", sdf.format(assuree.getDoB().toDate()));
99 res.put("dobFmt2", sdf2.format(assuree.getDoB().toDate()));
100 res.put("location", location);
101 res.put("date", date);
102 res.put("aword", aword);
103 final LinkedList<AssuranceType> ats = new LinkedList<>();
104 for (AssuranceType at : AssuranceType.values()) {
106 Notary.may(assurer, assuree, at);
108 } catch (GigiApiException e) {
111 res.put("ats", new IterableDataset() {
113 Iterator<AssuranceType> t = ats.iterator();
116 public boolean next(Language l, Map<String, Object> vars) {
120 AssuranceType t1 = t.next();
121 vars.put("type", t1.getDescription());
122 vars.put("id", t1.toString());
123 vars.put("sel", t1 == type ? " selected" : "");
127 templ.output(out, l, res);
131 public boolean submit(PrintWriter out, HttpServletRequest req) throws GigiApiException {
132 location = req.getParameter("location");
133 date = req.getParameter("date");
134 GigiApiException gae = new GigiApiException();
135 if (date == null || location == null) {
136 gae.mergeInto(new GigiApiException("You need to enter location and date!"));
139 if ( !"1".equals(req.getParameter("certify")) || !"1".equals(req.getParameter("rules")) || !"1".equals(req.getParameter("assertion"))) {
140 gae.mergeInto(new GigiApiException("You failed to check all boxes to validate" + " your adherence to the rules and policies of SomeCA"));
142 if ("1".equals(req.getParameter("passwordReset"))) {
143 aword = req.getParameter("passwordResetValue");
144 if ("".equals(aword)) {
150 String val = req.getParameter("assuranceType");
153 type = AssuranceType.valueOf(val);
154 } catch (IllegalArgumentException e) {
155 gae.mergeInto(new GigiApiException("Verification Type wrong."));
160 String points = req.getParameter("points");
161 if (points == null || "".equals(points)) {
162 gae.mergeInto(new GigiApiException("For a verification, you need to enter points."));
165 pointsI = Integer.parseInt(points);
166 } catch (NumberFormatException e) {
167 gae.mergeInto(new GigiApiException("The points entered were not a number."));
170 String[] parameterValues = req.getParameterValues("assuredName");
171 HashSet<String> data = new HashSet<>(Arrays.asList(parameterValues == null ? new String[0] : parameterValues));
172 for (int i = 0; i < assureeNames.length; i++) {
173 selected[i] = data.contains(Integer.toString(assureeNames[i].getId()));
176 if ( !gae.isEmpty()) {
180 LinkedList<Name> toAssure = new LinkedList<Name>();
181 for (int i = 0; i < selected.length; i++) {
183 toAssure.add(assureeNames[i]);
186 if (toAssure.size() == 0) {
187 throw new GigiApiException("You must confirm at least one name to verify an account.");
190 Notary.assureAll(assurer, assuree, dob, pointsI, location, req.getParameter("date"), type, toAssure.toArray(new Name[toAssure.size()]));
192 if (aword != null && !aword.equals("")) {
193 Language langApplicant = Language.getInstance(assuree.getPreferredLocale());
194 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:");
195 String subject = langApplicant.getTranslation("Password reset by verification");
196 PasswordResetPage.initPasswordResetProcess(out, assuree, req, aword, langApplicant, method, subject);
201 public User getAssuree() {