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.SprintfCommand;
24 import org.cacert.gigi.output.template.Template;
25 import org.cacert.gigi.pages.Page;
26 import org.cacert.gigi.pages.PasswordResetPage;
27 import org.cacert.gigi.util.DayDate;
28 import org.cacert.gigi.util.Notary;
30 public class AssuranceForm extends Form {
34 private Name[] assureeNames;
36 private boolean[] selected;
40 private String location = "";
42 private String date = "";
48 private AssuranceType type = AssuranceType.FACE_TO_FACE;
50 private static final Template templ = new Template(AssuranceForm.class.getResource("AssuranceForm.templ"));
52 private CountrySelector cs;
54 public AssuranceForm(HttpServletRequest hsr, User assuree) throws GigiApiException {
56 assurer = Page.getUser(hsr);
57 this.assuree = assuree;
59 if (assurer.getId() == assuree.getId()) {
60 throw new GigiApiException("You cannot verify yourself.");
62 if ( !assurer.canAssure()) {
63 throw new GigiApiException("You are not a RA-Agent.");
66 Name[] initialNames = this.assuree.getNonDeprecatedNames();
67 LinkedList<Name> names = new LinkedList<>();
68 for (Name name : initialNames) {
69 if (Notary.checkAssuranceIsPossible(assurer, name)) {
73 if (names.size() == 0) {
74 throw new GigiApiException(SprintfCommand.createSimple("You have already verified all names of this applicant within the last {0} days.", Notary.LIMIT_DAYS_VERIFICATION));
76 assureeNames = names.toArray(new Name[names.size()]);
77 dob = this.assuree.getDoB();
78 selected = new boolean[assureeNames.length];
79 cs = new CountrySelector("countryCode", false);
82 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
84 SimpleDateFormat sdf2 = new SimpleDateFormat("dd. MMM yyyy");
87 public void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
88 HashMap<String, Object> res = new HashMap<String, Object>();
90 res.put("names", new ArrayIterable<Name>(assureeNames) {
93 public void apply(Name t, Language l, Map<String, Object> vars) {
94 vars.put("nameExplicit", t);
95 vars.put("nameId", t.getId());
96 vars.put("checked", selected[i] ? " checked" : "");
100 res.put("name", assuree.getPreferredName().toString());
101 res.put("maxpoints", assurer.getMaxAssurePoints());
102 res.put("dob", sdf.format(assuree.getDoB().toDate()));
103 res.put("dobFmt2", sdf2.format(assuree.getDoB().toDate()));
104 res.put("location", location);
105 res.put("date", date);
106 res.put("aword", aword);
107 res.put("countryCode", cs);
109 final LinkedList<AssuranceType> ats = new LinkedList<>();
110 for (AssuranceType at : AssuranceType.values()) {
112 Notary.may(assurer, assuree, at);
114 } catch (GigiApiException e) {
117 res.put("ats", new IterableDataset() {
119 Iterator<AssuranceType> t = ats.iterator();
122 public boolean next(Language l, Map<String, Object> vars) {
126 AssuranceType t1 = t.next();
127 vars.put("type", t1.getDescription());
128 vars.put("id", t1.toString());
129 vars.put("sel", t1 == type ? " selected" : "");
133 templ.output(out, l, res);
137 public boolean submit(HttpServletRequest req) throws GigiApiException {
138 location = req.getParameter("location");
139 date = req.getParameter("date");
141 GigiApiException gae = new GigiApiException();
142 if (date == null || location == null) {
143 gae.mergeInto(new GigiApiException("You need to enter location and date!"));
146 if ( !"1".equals(req.getParameter("certify")) || !"1".equals(req.getParameter("rules")) || !"1".equals(req.getParameter("assertion"))) {
147 gae.mergeInto(new GigiApiException("You failed to check all boxes to validate" + " your adherence to the rules and policies of SomeCA"));
149 if ("1".equals(req.getParameter("passwordReset"))) {
150 aword = req.getParameter("passwordResetValue");
151 if ("".equals(aword)) {
157 String val = req.getParameter("assuranceType");
160 type = AssuranceType.valueOf(val);
161 } catch (IllegalArgumentException e) {
162 gae.mergeInto(new GigiApiException("Verification Type wrong."));
167 String points = req.getParameter("points");
168 if (points == null || "".equals(points)) {
169 gae.mergeInto(new GigiApiException("For a verification, you need to enter points."));
172 pointsI = Integer.parseInt(points);
173 } catch (NumberFormatException e) {
174 gae.mergeInto(new GigiApiException("The points entered were not a number."));
177 String[] parameterValues = req.getParameterValues("assuredName");
178 HashSet<String> data = new HashSet<>(Arrays.asList(parameterValues == null ? new String[0] : parameterValues));
179 for (int i = 0; i < assureeNames.length; i++) {
180 selected[i] = data.contains(Integer.toString(assureeNames[i].getId()));
183 if ( !gae.isEmpty()) {
187 LinkedList<Name> toAssure = new LinkedList<Name>();
188 for (int i = 0; i < selected.length; i++) {
190 toAssure.add(assureeNames[i]);
193 if (toAssure.size() == 0) {
194 throw new GigiApiException("You must confirm at least one name to verify an account.");
197 Notary.assureAll(assurer, assuree, dob, pointsI, location, req.getParameter("date"), type, toAssure.toArray(new Name[toAssure.size()]), cs.getCountry());
199 if (isWithPasswordReset()) {
200 Language langApplicant = Language.getInstance(assuree.getPreferredLocale());
201 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:");
202 String subject = langApplicant.getTranslation("Password reset by verification");
203 PasswordResetPage.initPasswordResetProcess(assuree, req, aword, langApplicant, method, subject);
208 public boolean isWithPasswordReset() {
209 return aword != null && !aword.equals("");
212 public User getAssuree() {