]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/wot/AssurePage.java
upd: use a more strict pattern for handling forms
[gigi.git] / src / org / cacert / gigi / pages / wot / AssurePage.java
1 package org.cacert.gigi.pages.wot;
2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.util.HashMap;
6
7 import javax.servlet.http.HttpServletRequest;
8 import javax.servlet.http.HttpServletResponse;
9
10 import org.cacert.gigi.GigiApiException;
11 import org.cacert.gigi.database.GigiPreparedStatement;
12 import org.cacert.gigi.database.GigiResultSet;
13 import org.cacert.gigi.dbObjects.User;
14 import org.cacert.gigi.output.DateSelector;
15 import org.cacert.gigi.output.template.Form;
16 import org.cacert.gigi.output.template.Template;
17 import org.cacert.gigi.pages.Page;
18 import org.cacert.gigi.util.AuthorizationContext;
19 import org.cacert.gigi.util.HTMLEncoder;
20
21 public class AssurePage extends Page {
22
23     public static final String PATH = "/wot/assure";
24
25     DateSelector ds = new DateSelector("day", "month", "year");
26
27     private static final Template t = new Template(AssuranceForm.class.getResource("AssureeSearch.templ"));
28
29     public AssurePage() {
30         super("Verify someone");
31
32     }
33
34     @Override
35     public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
36
37         PrintWriter out = resp.getWriter();
38         HashMap<String, Object> vars = new HashMap<String, Object>();
39         vars.put("DoB", ds);
40         t.output(out, getLanguage(req), vars);
41     }
42
43     @Override
44     public boolean isPermitted(AuthorizationContext ac) {
45         return ac != null && ac.canAssure();
46     }
47
48     @Override
49     public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
50         PrintWriter out = resp.getWriter();
51         if (req.getParameter("search") == null) {
52             AssuranceForm form = Form.getForm(req, AssuranceForm.class);
53             if (form.submitProtected(out, req)) {
54                 if (form.isWithPasswordReset()) {
55                     resp.getWriter().println(HTMLEncoder.encodeHTML(translate(req, "Password reset successful.")));
56                 }
57                 out.println(translate(req, "Verification complete."));
58                 return;
59             }
60             return;
61         }
62
63         try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `users`.`id`, `verified` FROM `users` INNER JOIN `certOwners` ON `certOwners`.`id`=`users`.`id` WHERE `email`=? AND `dob`=? AND `deleted` IS NULL")) {
64             ds.update(req);
65
66             ps.setString(1, req.getParameter("email"));
67             ps.setDate(2, ds.getDate().toSQLDate());
68             GigiResultSet rs = ps.executeQuery();
69             int id = 0;
70             if (rs.next()) {
71                 id = rs.getInt(1);
72                 boolean verified = rs.getBoolean(2);
73                 if (rs.next()) {
74                     out.println("Error, ambigous user. Please contact support@cacert.org.");
75                 } else {
76                     if ( !verified) {
77                         out.println(translate(req, "User is not yet verified. Please try again in 24 hours!"));
78                     } else if (getUser(req).getId() == id) {
79
80                     } else {
81                         User assuree = User.getById(id);
82                         try {
83                             new AssuranceForm(req, assuree).output(out, getLanguage(req), new HashMap<String, Object>());
84                         } catch (GigiApiException e) {
85                             e.format(out, Page.getLanguage(req));
86                         }
87                     }
88                 }
89             } else {
90                 throw new GigiApiException("I'm sorry, there was no email and date of birth matching" //
91                         + " what you entered in the system. Please double check your information.");
92             }
93
94         } catch (GigiApiException e) {
95             e.format(out, getLanguage(req));
96         }
97     }
98 }