]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/wot/AssuranceForm.java
8ad735fbd8b78e48f0c261a05d73c1fa0c235443
[gigi.git] / src / org / cacert / gigi / pages / wot / AssuranceForm.java
1 package org.cacert.gigi.pages.wot;
2
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;
10 import java.util.Map;
11
12 import javax.servlet.http.HttpServletRequest;
13
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;
29
30 public class AssuranceForm extends Form {
31
32     private User assuree;
33
34     private Name[] assureeNames;
35
36     private boolean[] selected;
37
38     private DayDate dob;
39
40     private String location = "";
41
42     private String date = "";
43
44     private String aword;
45
46     private User assurer;
47
48     private AssuranceType type = AssuranceType.FACE_TO_FACE;
49
50     private static final Template templ = new Template(AssuranceForm.class.getResource("AssuranceForm.templ"));
51
52     private CountrySelector cs;
53
54     public AssuranceForm(HttpServletRequest hsr, User assuree) throws GigiApiException {
55         super(hsr);
56         assurer = Page.getUser(hsr);
57         this.assuree = assuree;
58
59         if (assurer.getId() == assuree.getId()) {
60             throw new GigiApiException("You cannot verify yourself.");
61         }
62         if ( !assurer.canAssure()) {
63             throw new GigiApiException("You are not a RA-Agent.");
64         }
65
66         Name[] initialNames = this.assuree.getNonDeprecatedNames();
67         LinkedList<Name> names = new LinkedList<>();
68         for (Name name : initialNames) {
69             if (Notary.checkAssuranceIsPossible(assurer, name)) {
70                 names.add(name);
71             }
72         }
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));
75         }
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);
80     }
81
82     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
83
84     SimpleDateFormat sdf2 = new SimpleDateFormat("dd. MMM yyyy");
85
86     @Override
87     public void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
88         HashMap<String, Object> res = new HashMap<String, Object>();
89         res.putAll(vars);
90         res.put("names", new ArrayIterable<Name>(assureeNames) {
91
92             @Override
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" : "");
97             }
98
99         });
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);
108
109         final LinkedList<AssuranceType> ats = new LinkedList<>();
110         for (AssuranceType at : AssuranceType.values()) {
111             try {
112                 Notary.may(assurer, assuree, at);
113                 ats.add(at);
114             } catch (GigiApiException e) {
115             }
116         }
117         res.put("ats", new IterableDataset() {
118
119             Iterator<AssuranceType> t = ats.iterator();
120
121             @Override
122             public boolean next(Language l, Map<String, Object> vars) {
123                 if ( !t.hasNext()) {
124                     return false;
125                 }
126                 AssuranceType t1 = t.next();
127                 vars.put("type", t1.getDescription());
128                 vars.put("id", t1.toString());
129                 vars.put("sel", t1 == type ? " selected" : "");
130                 return true;
131             }
132         });
133         templ.output(out, l, res);
134     }
135
136     @Override
137     public boolean submit(PrintWriter out, HttpServletRequest req) throws GigiApiException {
138         location = req.getParameter("location");
139         date = req.getParameter("date");
140         cs.update(req);
141         GigiApiException gae = new GigiApiException();
142         if (date == null || location == null) {
143             gae.mergeInto(new GigiApiException("You need to enter location and date!"));
144         }
145
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"));
148         }
149         if ("1".equals(req.getParameter("passwordReset"))) {
150             aword = req.getParameter("passwordResetValue");
151             if ("".equals(aword)) {
152                 aword = null;
153             }
154         } else {
155             aword = null;
156         }
157         String val = req.getParameter("assuranceType");
158         if (val != null) {
159             try {
160                 type = AssuranceType.valueOf(val);
161             } catch (IllegalArgumentException e) {
162                 gae.mergeInto(new GigiApiException("Verification Type wrong."));
163             }
164         }
165
166         int pointsI = 0;
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."));
170         } else {
171             try {
172                 pointsI = Integer.parseInt(points);
173             } catch (NumberFormatException e) {
174                 gae.mergeInto(new GigiApiException("The points entered were not a number."));
175             }
176         }
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()));
181         }
182
183         if ( !gae.isEmpty()) {
184             throw gae;
185         }
186
187         LinkedList<Name> toAssure = new LinkedList<Name>();
188         for (int i = 0; i < selected.length; i++) {
189             if (selected[i]) {
190                 toAssure.add(assureeNames[i]);
191             }
192         }
193         if (toAssure.size() == 0) {
194             throw new GigiApiException("You must confirm at least one name to verify an account.");
195         }
196
197         Notary.assureAll(assurer, assuree, dob, pointsI, location, req.getParameter("date"), type, toAssure.toArray(new Name[toAssure.size()]), cs.getCountry());
198
199         if (aword != null && !aword.equals("")) {
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(out, assuree, req, aword, langApplicant, method, subject);
204         }
205         return true;
206     }
207
208     public User getAssuree() {
209         return assuree;
210     }
211
212 }