1 package club.wpia.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 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;
33 public class VerificationForm extends Form {
35 public static class ConcatOutputable implements Outputable {
37 private Outputable[] outputables;
39 public ConcatOutputable(Outputable... outputables) {
40 this.outputables = outputables;
44 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
45 for (int i = 0; i < outputables.length; i++) {
49 outputables[i].output(out, l, vars);
54 private User applicant;
56 private Name[] applicantNames;
58 private boolean[] selected;
62 private String location = "";
64 private String date = "";
70 private VerificationType type = VerificationType.FACE_TO_FACE;
72 private static final Template templ = new Template(VerificationForm.class.getResource("VerificationForm.templ"));
74 private CountrySelector cs;
76 public VerificationForm(HttpServletRequest hsr, User applicant) throws GigiApiException {
78 agent = Page.getUser(hsr);
79 this.applicant = applicant;
81 if (agent.getId() == applicant.getId()) {
82 throw new GigiApiException("You cannot verify yourself.");
84 if ( !agent.canVerify()) {
85 throw new GigiApiException("You are not a RA-Agent.");
88 Name[] initialNames = this.applicant.getNonDeprecatedNames();
89 LinkedList<Name> names = new LinkedList<>();
90 for (Name name : initialNames) {
91 if (Notary.checkVerificationIsPossible(agent, name)) {
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));
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);
104 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
106 SimpleDateFormat sdf2 = new SimpleDateFormat("dd. MMM yyyy");
109 public void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
110 HashMap<String, Object> res = new HashMap<String, Object>(vars);
112 res.put("names", new ArrayIterable<Name>(applicantNames) {
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" : "");
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);
131 final LinkedList<VerificationType> ats = new LinkedList<>();
132 for (VerificationType at : VerificationType.values()) {
134 Notary.may(agent, applicant, at);
136 } catch (GigiApiException e) {
139 res.put("ats", new IterableDataset() {
141 Iterator<VerificationType> t = ats.iterator();
144 public boolean next(Language l, Map<String, Object> vars) {
148 VerificationType t1 = t.next();
149 vars.put("type", t1.getDescription());
150 vars.put("id", t1.toString());
151 vars.put("sel", t1 == type ? " selected" : "");
155 templ.output(out, l, res);
159 public SubmissionResult submit(HttpServletRequest req) throws GigiApiException {
160 location = req.getParameter("location");
161 date = req.getParameter("date");
163 GigiApiException gae = new GigiApiException();
164 if (date == null || location == null) {
165 gae.mergeInto(new GigiApiException("You need to enter location and date!"));
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())));
171 if ("1".equals(req.getParameter("passwordReset"))) {
172 aword = req.getParameter("passwordResetValue");
173 if ("".equals(aword)) {
179 String val = req.getParameter("verificationType");
182 type = VerificationType.valueOf(val);
183 } catch (IllegalArgumentException e) {
184 gae.mergeInto(new GigiApiException("Verification Type wrong."));
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."));
194 pointsI = Integer.parseInt(points);
195 } catch (NumberFormatException e) {
196 gae.mergeInto(new GigiApiException("The points entered were not a number."));
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()));
205 if ( !gae.isEmpty()) {
209 LinkedList<Name> toVerify = new LinkedList<Name>();
210 for (int i = 0; i < selected.length; i++) {
212 toVerify.add(applicantNames[i]);
215 if (toVerify.size() == 0) {
216 throw new GigiApiException("You must confirm at least one name to verify an account.");
219 Notary.verifyAll(agent, applicant, dob, pointsI, location, req.getParameter("date"), type, toVerify.toArray(new Name[toVerify.size()]), cs.getCountry());
221 Outputable result = SprintfCommand.createSimple("Verification of user with email address {0} and {1} verification points complete.", applicant.getEmail(), points);
222 if (isWithPasswordReset()) {
223 Language langApplicant = Language.getInstance(applicant.getPreferredLocale());
224 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:");
225 String subject = langApplicant.getTranslation("Password reset by verification");
226 PasswordResetPage.initPasswordResetProcess(applicant, req, aword, langApplicant, method, subject);
227 result = new ConcatOutputable(result, new TranslateCommand("Password reset successful."));
229 return new SuccessMessageResult(result);
232 public boolean isWithPasswordReset() {
233 return aword != null && !aword.equals("");
236 public User getApplicant() {