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.Group;
16 import club.wpia.gigi.dbObjects.Name;
17 import club.wpia.gigi.dbObjects.User;
18 import club.wpia.gigi.dbObjects.Verification.VerificationType;
19 import club.wpia.gigi.localisation.Language;
20 import club.wpia.gigi.output.ArrayIterable;
21 import club.wpia.gigi.output.CountrySelector;
22 import club.wpia.gigi.output.template.Form;
23 import club.wpia.gigi.output.template.IterableDataset;
24 import club.wpia.gigi.output.template.Outputable;
25 import club.wpia.gigi.output.template.SprintfCommand;
26 import club.wpia.gigi.output.template.Template;
27 import club.wpia.gigi.output.template.TranslateCommand;
28 import club.wpia.gigi.pages.Page;
29 import club.wpia.gigi.pages.PasswordResetPage;
30 import club.wpia.gigi.util.DayDate;
31 import club.wpia.gigi.util.Notary;
32 import club.wpia.gigi.util.ServerConstants;
34 public class VerificationForm extends Form {
36 public static class ConcatOutputable implements Outputable {
38 private Outputable[] outputables;
40 public ConcatOutputable(Outputable... outputables) {
41 this.outputables = outputables;
45 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
46 for (int i = 0; i < outputables.length; i++) {
50 outputables[i].output(out, l, vars);
55 private User applicant;
57 private Name[] applicantNames;
59 private boolean[] selected;
63 private String location = "";
65 private String date = "";
71 private VerificationType type = VerificationType.FACE_TO_FACE;
73 private static final Template templ = new Template(VerificationForm.class.getResource("VerificationForm.templ"));
75 private CountrySelector cs;
77 public VerificationForm(HttpServletRequest hsr, User applicant) throws GigiApiException {
79 agent = Page.getUser(hsr);
80 this.applicant = applicant;
82 if (agent.getId() == applicant.getId()) {
83 throw new GigiApiException("You cannot verify yourself.");
85 if ( !agent.canVerify()) {
86 throw new GigiApiException("You are not a RA-Agent.");
89 Name[] initialNames = this.applicant.getNonDeprecatedNames();
90 LinkedList<Name> names = new LinkedList<>();
91 for (Name name : initialNames) {
92 if (Notary.checkVerificationIsPossible(agent, name)) {
96 if (names.size() == 0) {
97 throw new GigiApiException(SprintfCommand.createSimple("You have already verified all names of this applicant within the last {0} days.", Notary.LIMIT_DAYS_VERIFICATION));
99 applicantNames = names.toArray(new Name[names.size()]);
100 dob = this.applicant.getDoB();
101 selected = new boolean[applicantNames.length];
102 cs = new CountrySelector("countryCode", false);
105 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
107 SimpleDateFormat sdf2 = new SimpleDateFormat("dd. MMM yyyy");
110 public void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
111 HashMap<String, Object> res = new HashMap<String, Object>(vars);
113 res.put("names", new ArrayIterable<Name>(applicantNames) {
116 public void apply(Name t, Language l, Map<String, Object> vars) {
117 vars.put("nameExplicit", t);
118 vars.put("nameId", t.getId());
119 vars.put("checked", selected[i] ? " checked" : "");
123 res.put("name", applicant.getPreferredName().toString());
124 res.put("maxpoints", agent.getMaxVerifyPoints());
125 res.put("dob", sdf.format(applicant.getDoB().toDate()));
126 res.put("dobFmt2", sdf2.format(applicant.getDoB().toDate()));
127 res.put("location", location);
128 res.put("date", date);
129 res.put("aword", aword);
130 res.put("countryCode", cs);
132 final LinkedList<VerificationType> ats = new LinkedList<>();
133 for (VerificationType at : VerificationType.values()) {
135 Notary.may(agent, applicant, at);
137 } catch (GigiApiException e) {
140 res.put("ats", new IterableDataset() {
142 Iterator<VerificationType> t = ats.iterator();
145 public boolean next(Language l, Map<String, Object> vars) {
149 VerificationType t1 = t.next();
150 vars.put("type", t1.getDescription());
151 vars.put("id", t1.toString());
152 vars.put("sel", t1 == type ? " selected" : "");
156 res.put("ttpinfo", agent.isInGroup(Group.TTP_AGENT) && !agent.hasValidTTPAgentChallenge() && applicant.isInGroup(Group.TTP_APPLICANT));
158 templ.output(out, l, res);
162 public SubmissionResult submit(HttpServletRequest req) throws GigiApiException {
163 location = req.getParameter("location");
164 date = req.getParameter("date");
166 GigiApiException gae = new GigiApiException();
167 if (date == null || location == null) {
168 gae.mergeInto(new GigiApiException("You need to enter location and date!"));
171 if ( !"1".equals(req.getParameter("certify")) || !"1".equals(req.getParameter("rules")) || !"1".equals(req.getParameter("assertion"))) {
172 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())));
174 if ("1".equals(req.getParameter("passwordReset"))) {
175 aword = req.getParameter("passwordResetValue");
176 if ("".equals(aword)) {
182 String val = req.getParameter("verificationType");
185 type = VerificationType.valueOf(val);
186 } catch (IllegalArgumentException e) {
187 gae.mergeInto(new GigiApiException("Verification Type wrong."));
192 String points = req.getParameter("points");
193 if (points == null || "".equals(points)) {
194 gae.mergeInto(new GigiApiException("For a verification, you need to enter points."));
197 pointsI = Integer.parseInt(points);
198 } catch (NumberFormatException e) {
199 gae.mergeInto(new GigiApiException("The points entered were not a number."));
202 String[] parameterValues = req.getParameterValues("verifiedName");
203 HashSet<String> data = new HashSet<>(Arrays.asList(parameterValues == null ? new String[0] : parameterValues));
204 for (int i = 0; i < applicantNames.length; i++) {
205 selected[i] = data.contains(Integer.toString(applicantNames[i].getId()));
208 if ( !gae.isEmpty()) {
212 LinkedList<Name> toVerify = new LinkedList<Name>();
213 for (int i = 0; i < selected.length; i++) {
215 toVerify.add(applicantNames[i]);
218 if (toVerify.size() == 0) {
219 throw new GigiApiException("You must confirm at least one name to verify an account.");
222 Notary.verifyAll(agent, applicant, dob, pointsI, location, req.getParameter("date"), type, toVerify.toArray(new Name[toVerify.size()]), cs.getCountry());
224 Outputable result = SprintfCommand.createSimple("Verification of user with email address {0} and {1} verification points complete.", applicant.getEmail(), points);
225 if (isWithPasswordReset()) {
226 Language langApplicant = Language.getInstance(applicant.getPreferredLocale());
227 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:");
228 String subject = langApplicant.getTranslation("Password reset by verification");
229 PasswordResetPage.initPasswordResetProcess(applicant, req, aword, langApplicant, method, subject);
230 result = new ConcatOutputable(result, new TranslateCommand("Password reset successful."));
231 agent.writeUserLog(applicant, "RA Agent triggered password reset");
233 return new SuccessMessageResult(result);
236 public boolean isWithPasswordReset() {
237 return aword != null && !aword.equals("");
240 public User getApplicant() {