]> WPIA git - gigi.git/blob - src/club/wpia/gigi/pages/wot/VerificationForm.java
upd: replace SomeCA by variable
[gigi.git] / src / club / wpia / gigi / pages / wot / VerificationForm.java
1 package club.wpia.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 club.wpia.gigi.GigiApiException;
15 import club.wpia.gigi.dbObjects.Name;
16 import club.wpia.gigi.dbObjects.User;
17 import club.wpia.gigi.dbObjects.Verification.VerificationType;
18 import club.wpia.gigi.localisation.Language;
19 import club.wpia.gigi.output.ArrayIterable;
20 import club.wpia.gigi.output.CountrySelector;
21 import club.wpia.gigi.output.template.Form;
22 import club.wpia.gigi.output.template.IterableDataset;
23 import club.wpia.gigi.output.template.Outputable;
24 import club.wpia.gigi.output.template.SprintfCommand;
25 import club.wpia.gigi.output.template.Template;
26 import club.wpia.gigi.output.template.TranslateCommand;
27 import club.wpia.gigi.pages.Page;
28 import club.wpia.gigi.pages.PasswordResetPage;
29 import club.wpia.gigi.util.DayDate;
30 import club.wpia.gigi.util.Notary;
31 import club.wpia.gigi.util.ServerConstants;
32
33 public class VerificationForm extends Form {
34
35     public static class ConcatOutputable implements Outputable {
36
37         private Outputable[] outputables;
38
39         public ConcatOutputable(Outputable... outputables) {
40             this.outputables = outputables;
41         }
42
43         @Override
44         public void output(PrintWriter out, Language l, Map<String, Object> vars) {
45             for (int i = 0; i < outputables.length; i++) {
46                 if (i != 0) {
47                     out.println();
48                 }
49                 outputables[i].output(out, l, vars);
50             }
51         }
52     }
53
54     private User applicant;
55
56     private Name[] applicantNames;
57
58     private boolean[] selected;
59
60     private DayDate dob;
61
62     private String location = "";
63
64     private String date = "";
65
66     private String aword;
67
68     private User agent;
69
70     private VerificationType type = VerificationType.FACE_TO_FACE;
71
72     private static final Template templ = new Template(VerificationForm.class.getResource("VerificationForm.templ"));
73
74     private CountrySelector cs;
75
76     public VerificationForm(HttpServletRequest hsr, User applicant) throws GigiApiException {
77         super(hsr);
78         agent = Page.getUser(hsr);
79         this.applicant = applicant;
80
81         if (agent.getId() == applicant.getId()) {
82             throw new GigiApiException("You cannot verify yourself.");
83         }
84         if ( !agent.canVerify()) {
85             throw new GigiApiException("You are not a RA-Agent.");
86         }
87
88         Name[] initialNames = this.applicant.getNonDeprecatedNames();
89         LinkedList<Name> names = new LinkedList<>();
90         for (Name name : initialNames) {
91             if (Notary.checkVerificationIsPossible(agent, name)) {
92                 names.add(name);
93             }
94         }
95         if (names.size() == 0) {
96             throw new GigiApiException(SprintfCommand.createSimple("You have already verified all names of this applicant within the last {0} days.", Notary.LIMIT_DAYS_VERIFICATION));
97         }
98         applicantNames = names.toArray(new Name[names.size()]);
99         dob = this.applicant.getDoB();
100         selected = new boolean[applicantNames.length];
101         cs = new CountrySelector("countryCode", false);
102     }
103
104     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
105
106     SimpleDateFormat sdf2 = new SimpleDateFormat("dd. MMM yyyy");
107
108     @Override
109     public void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
110         HashMap<String, Object> res = new HashMap<String, Object>(vars);
111         res.putAll(vars);
112         res.put("names", new ArrayIterable<Name>(applicantNames) {
113
114             @Override
115             public void apply(Name t, Language l, Map<String, Object> vars) {
116                 vars.put("nameExplicit", t);
117                 vars.put("nameId", t.getId());
118                 vars.put("checked", selected[i] ? " checked" : "");
119             }
120
121         });
122         res.put("name", applicant.getPreferredName().toString());
123         res.put("maxpoints", agent.getMaxVerifyPoints());
124         res.put("dob", sdf.format(applicant.getDoB().toDate()));
125         res.put("dobFmt2", sdf2.format(applicant.getDoB().toDate()));
126         res.put("location", location);
127         res.put("date", date);
128         res.put("aword", aword);
129         res.put("countryCode", cs);
130
131         final LinkedList<VerificationType> ats = new LinkedList<>();
132         for (VerificationType at : VerificationType.values()) {
133             try {
134                 Notary.may(agent, applicant, at);
135                 ats.add(at);
136             } catch (GigiApiException e) {
137             }
138         }
139         res.put("ats", new IterableDataset() {
140
141             Iterator<VerificationType> t = ats.iterator();
142
143             @Override
144             public boolean next(Language l, Map<String, Object> vars) {
145                 if ( !t.hasNext()) {
146                     return false;
147                 }
148                 VerificationType t1 = t.next();
149                 vars.put("type", t1.getDescription());
150                 vars.put("id", t1.toString());
151                 vars.put("sel", t1 == type ? " selected" : "");
152                 return true;
153             }
154         });
155         templ.output(out, l, res);
156     }
157
158     @Override
159     public SubmissionResult submit(HttpServletRequest req) throws GigiApiException {
160         location = req.getParameter("location");
161         date = req.getParameter("date");
162         cs.update(req);
163         GigiApiException gae = new GigiApiException();
164         if (date == null || location == null) {
165             gae.mergeInto(new GigiApiException("You need to enter location and date!"));
166         }
167
168         if ( !"1".equals(req.getParameter("certify")) || !"1".equals(req.getParameter("rules")) || !"1".equals(req.getParameter("assertion"))) {
169             gae.mergeInto(new GigiApiException(SprintfCommand.createSimple("You failed to check all boxes to validate your adherence to the rules and policies of {0}.", ServerConstants.getAppName())));
170         }
171         if ("1".equals(req.getParameter("passwordReset"))) {
172             aword = req.getParameter("passwordResetValue");
173             if ("".equals(aword)) {
174                 aword = null;
175             }
176         } else {
177             aword = null;
178         }
179         String val = req.getParameter("verificationType");
180         if (val != null) {
181             try {
182                 type = VerificationType.valueOf(val);
183             } catch (IllegalArgumentException e) {
184                 gae.mergeInto(new GigiApiException("Verification Type wrong."));
185             }
186         }
187
188         int pointsI = 0;
189         String points = req.getParameter("points");
190         if (points == null || "".equals(points)) {
191             gae.mergeInto(new GigiApiException("For a verification, you need to enter points."));
192         } else {
193             try {
194                 pointsI = Integer.parseInt(points);
195             } catch (NumberFormatException e) {
196                 gae.mergeInto(new GigiApiException("The points entered were not a number."));
197             }
198         }
199         String[] parameterValues = req.getParameterValues("verifiedName");
200         HashSet<String> data = new HashSet<>(Arrays.asList(parameterValues == null ? new String[0] : parameterValues));
201         for (int i = 0; i < applicantNames.length; i++) {
202             selected[i] = data.contains(Integer.toString(applicantNames[i].getId()));
203         }
204
205         if ( !gae.isEmpty()) {
206             throw gae;
207         }
208
209         LinkedList<Name> toAssure = new LinkedList<Name>();
210         for (int i = 0; i < selected.length; i++) {
211             if (selected[i]) {
212                 toAssure.add(applicantNames[i]);
213             }
214         }
215         if (toAssure.size() == 0) {
216             throw new GigiApiException("You must confirm at least one name to verify an account.");
217         }
218
219         Notary.verifyAll(agent, applicant, dob, pointsI, location, req.getParameter("date"), type, toAssure.toArray(new Name[toAssure.size()]), cs.getCountry());
220         Outputable result = new TranslateCommand("Verification complete.");
221         if (isWithPasswordReset()) {
222             Language langApplicant = Language.getInstance(applicant.getPreferredLocale());
223             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:");
224             String subject = langApplicant.getTranslation("Password reset by verification");
225             PasswordResetPage.initPasswordResetProcess(applicant, req, aword, langApplicant, method, subject);
226             result = new ConcatOutputable(result, new TranslateCommand("Password reset successful."));
227         }
228         return new SuccessMessageResult(result);
229     }
230
231     public boolean isWithPasswordReset() {
232         return aword != null && !aword.equals("");
233     }
234
235     public User getApplicant() {
236         return applicant;
237     }
238
239 }