6084c321f694dafa7c814fabf040dacc97713914
[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.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;
28
29 public class AssuranceForm extends Form {
30
31     private User assuree;
32
33     private Name[] assureeNames;
34
35     private boolean[] selected;
36
37     private DayDate dob;
38
39     private String location = "";
40
41     private String date = "";
42
43     private String aword;
44
45     private User assurer;
46
47     private AssuranceType type = AssuranceType.FACE_TO_FACE;
48
49     private static final Template templ = new Template(AssuranceForm.class.getResource("AssuranceForm.templ"));
50
51     public AssuranceForm(HttpServletRequest hsr, User assuree) throws GigiApiException {
52         super(hsr);
53         assurer = Page.getUser(hsr);
54         this.assuree = assuree;
55
56         if (assurer.getId() == assuree.getId()) {
57             throw new GigiApiException("You cannot verify yourself.");
58         }
59         if ( !assurer.canAssure()) {
60             throw new GigiApiException("You are not a RA-Agent.");
61         }
62
63         Name[] initialNames = this.assuree.getNonDeprecatedNames();
64         LinkedList<Name> names = new LinkedList<>();
65         for (Name name : initialNames) {
66             if (Notary.checkAssuranceIsPossible(assurer, name)) {
67                 names.add(name);
68             }
69         }
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));
72         }
73         assureeNames = names.toArray(new Name[names.size()]);
74         dob = this.assuree.getDoB();
75         selected = new boolean[assureeNames.length];
76     }
77
78     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
79
80     SimpleDateFormat sdf2 = new SimpleDateFormat("dd. MMM yyyy");
81
82     @Override
83     public void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
84         HashMap<String, Object> res = new HashMap<String, Object>();
85         res.putAll(vars);
86         res.put("names", new ArrayIterable<Name>(assureeNames) {
87
88             @Override
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" : "");
93             }
94
95         });
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()) {
105             try {
106                 Notary.may(assurer, assuree, at);
107                 ats.add(at);
108             } catch (GigiApiException e) {
109             }
110         }
111         res.put("ats", new IterableDataset() {
112
113             Iterator<AssuranceType> t = ats.iterator();
114
115             @Override
116             public boolean next(Language l, Map<String, Object> vars) {
117                 if ( !t.hasNext()) {
118                     return false;
119                 }
120                 AssuranceType t1 = t.next();
121                 vars.put("type", t1.getDescription());
122                 vars.put("id", t1.toString());
123                 vars.put("sel", t1 == type ? " selected" : "");
124                 return true;
125             }
126         });
127         templ.output(out, l, res);
128     }
129
130     @Override
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!"));
137         }
138
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"));
141         }
142         if ("1".equals(req.getParameter("passwordReset"))) {
143             aword = req.getParameter("passwordResetValue");
144             if ("".equals(aword)) {
145                 aword = null;
146             }
147         } else {
148             aword = null;
149         }
150         String val = req.getParameter("assuranceType");
151         if (val != null) {
152             try {
153                 type = AssuranceType.valueOf(val);
154             } catch (IllegalArgumentException e) {
155                 gae.mergeInto(new GigiApiException("Assurance Type wrong."));
156             }
157         }
158
159         int pointsI = 0;
160         String points = req.getParameter("points");
161         if (points == null || "".equals(points)) {
162             gae.mergeInto(new GigiApiException("For an assurance, you need to enter points."));
163         } else {
164             try {
165                 pointsI = Integer.parseInt(points);
166             } catch (NumberFormatException e) {
167                 gae.mergeInto(new GigiApiException("The points entered were not a number."));
168             }
169         }
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()));
174         }
175
176         if ( !gae.isEmpty()) {
177             throw gae;
178         }
179
180         LinkedList<Name> toAssure = new LinkedList<Name>();
181         for (int i = 0; i < selected.length; i++) {
182             if (selected[i]) {
183                 toAssure.add(assureeNames[i]);
184             }
185         }
186
187         Notary.assureAll(assurer, assuree, dob, pointsI, location, req.getParameter("date"), type, toAssure.toArray(new Name[toAssure.size()]));
188
189         if (aword != null && !aword.equals("")) {
190             Language langApplicant = Language.getInstance(assuree.getPreferredLocale());
191             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:");
192             String subject = langApplicant.getTranslation("Password reset by assurance");
193             PasswordResetPage.initPasswordResetProcess(out, assuree, req, aword, langApplicant, method, subject);
194         }
195         return true;
196     }
197
198     public User getAssuree() {
199         return assuree;
200     }
201
202 }