]> WPIA git - gigi.git/blob - src/club/wpia/gigi/pages/wot/VerificationForm.java
add: implement password change log
[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.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;
33
34 public class VerificationForm extends Form {
35
36     public static class ConcatOutputable implements Outputable {
37
38         private Outputable[] outputables;
39
40         public ConcatOutputable(Outputable... outputables) {
41             this.outputables = outputables;
42         }
43
44         @Override
45         public void output(PrintWriter out, Language l, Map<String, Object> vars) {
46             for (int i = 0; i < outputables.length; i++) {
47                 if (i != 0) {
48                     out.println();
49                 }
50                 outputables[i].output(out, l, vars);
51             }
52         }
53     }
54
55     private User applicant;
56
57     private Name[] applicantNames;
58
59     private boolean[] selected;
60
61     private DayDate dob;
62
63     private String location = "";
64
65     private String date = "";
66
67     private String aword;
68
69     private User agent;
70
71     private VerificationType type = VerificationType.FACE_TO_FACE;
72
73     private static final Template templ = new Template(VerificationForm.class.getResource("VerificationForm.templ"));
74
75     private CountrySelector cs;
76
77     public VerificationForm(HttpServletRequest hsr, User applicant) throws GigiApiException {
78         super(hsr);
79         agent = Page.getUser(hsr);
80         this.applicant = applicant;
81
82         if (agent.getId() == applicant.getId()) {
83             throw new GigiApiException("You cannot verify yourself.");
84         }
85         if ( !agent.canVerify()) {
86             throw new GigiApiException("You are not a RA-Agent.");
87         }
88
89         Name[] initialNames = this.applicant.getNonDeprecatedNames();
90         LinkedList<Name> names = new LinkedList<>();
91         for (Name name : initialNames) {
92             if (Notary.checkVerificationIsPossible(agent, name)) {
93                 names.add(name);
94             }
95         }
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));
98         }
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);
103     }
104
105     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
106
107     SimpleDateFormat sdf2 = new SimpleDateFormat("dd. MMM yyyy");
108
109     @Override
110     public void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
111         HashMap<String, Object> res = new HashMap<String, Object>(vars);
112         res.putAll(vars);
113         res.put("names", new ArrayIterable<Name>(applicantNames) {
114
115             @Override
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" : "");
120             }
121
122         });
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);
131
132         final LinkedList<VerificationType> ats = new LinkedList<>();
133         for (VerificationType at : VerificationType.values()) {
134             try {
135                 Notary.may(agent, applicant, at);
136                 ats.add(at);
137             } catch (GigiApiException e) {
138             }
139         }
140         res.put("ats", new IterableDataset() {
141
142             Iterator<VerificationType> t = ats.iterator();
143
144             @Override
145             public boolean next(Language l, Map<String, Object> vars) {
146                 if ( !t.hasNext()) {
147                     return false;
148                 }
149                 VerificationType t1 = t.next();
150                 vars.put("type", t1.getDescription());
151                 vars.put("id", t1.toString());
152                 vars.put("sel", t1 == type ? " selected" : "");
153                 return true;
154             }
155         });
156         res.put("ttpinfo", agent.isInGroup(Group.TTP_AGENT) && !agent.hasValidTTPAgentChallenge() && applicant.isInGroup(Group.TTP_APPLICANT));
157
158         templ.output(out, l, res);
159     }
160
161     @Override
162     public SubmissionResult submit(HttpServletRequest req) throws GigiApiException {
163         location = req.getParameter("location");
164         date = req.getParameter("date");
165         cs.update(req);
166         GigiApiException gae = new GigiApiException();
167         if (date == null || location == null) {
168             gae.mergeInto(new GigiApiException("You need to enter location and date!"));
169         }
170
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())));
173         }
174         if ("1".equals(req.getParameter("passwordReset"))) {
175             aword = req.getParameter("passwordResetValue");
176             if ("".equals(aword)) {
177                 aword = null;
178             }
179         } else {
180             aword = null;
181         }
182         String val = req.getParameter("verificationType");
183         if (val != null) {
184             try {
185                 type = VerificationType.valueOf(val);
186             } catch (IllegalArgumentException e) {
187                 gae.mergeInto(new GigiApiException("Verification Type wrong."));
188             }
189         }
190
191         int pointsI = 0;
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."));
195         } else {
196             try {
197                 pointsI = Integer.parseInt(points);
198             } catch (NumberFormatException e) {
199                 gae.mergeInto(new GigiApiException("The points entered were not a number."));
200             }
201         }
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()));
206         }
207
208         if ( !gae.isEmpty()) {
209             throw gae;
210         }
211
212         LinkedList<Name> toVerify = new LinkedList<Name>();
213         for (int i = 0; i < selected.length; i++) {
214             if (selected[i]) {
215                 toVerify.add(applicantNames[i]);
216             }
217         }
218         if (toVerify.size() == 0) {
219             throw new GigiApiException("You must confirm at least one name to verify an account.");
220         }
221
222         Notary.verifyAll(agent, applicant, dob, pointsI, location, req.getParameter("date"), type, toVerify.toArray(new Name[toVerify.size()]), cs.getCountry());
223
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");
232         }
233         return new SuccessMessageResult(result);
234     }
235
236     public boolean isWithPasswordReset() {
237         return aword != null && !aword.equals("");
238     }
239
240     public User getApplicant() {
241         return applicant;
242     }
243
244 }