e0cb4a211e57aed40125df653ee99a163283807e
[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.sql.Date;
6 import java.util.Calendar;
7 import java.util.HashMap;
8
9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11
12 import org.cacert.gigi.GigiApiException;
13 import org.cacert.gigi.database.GigiPreparedStatement;
14 import org.cacert.gigi.database.GigiResultSet;
15 import org.cacert.gigi.dbObjects.User;
16 import org.cacert.gigi.output.DateSelector;
17 import org.cacert.gigi.output.template.Form;
18 import org.cacert.gigi.output.template.Template;
19 import org.cacert.gigi.pages.LoginPage;
20 import org.cacert.gigi.pages.Page;
21 import org.cacert.gigi.util.AuthorizationContext;
22 import org.cacert.gigi.util.Notary;
23
24 public class AssurePage extends Page {
25
26     public static final String PATH = "/wot/assure";
27
28     DateSelector ds = new DateSelector("day", "month", "year");
29
30     Template t;
31
32     public AssurePage() {
33         super("Assure someone");
34         t = new Template(AssuranceForm.class.getResource("AssureeSearch.templ"));
35
36     }
37
38     @Override
39     public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
40
41         PrintWriter out = resp.getWriter();
42         HashMap<String, Object> vars = new HashMap<String, Object>();
43         vars.put("DoB", ds);
44         t.output(out, getLanguage(req), vars);
45     }
46
47     @Override
48     public boolean isPermitted(AuthorizationContext ac) {
49         return ac != null && ac.canAssure();
50     }
51
52     @Override
53     public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
54         PrintWriter out = resp.getWriter();
55         if (req.getParameter("search") == null) {
56             AssuranceForm form = Form.getForm(req, AssuranceForm.class);
57             try {
58                 if (form.submit(out, req)) {
59                     out.println(translate(req, "Assurance complete."));
60                     return;
61                 }
62             } catch (GigiApiException e) {
63                 e.format(out, Page.getLanguage(req));
64                 try {
65                     Notary.checkAssuranceIsPossible(LoginPage.getUser(req), form.getAssuree());
66                     form.output(out, getLanguage(req), new HashMap<String, Object>());
67                 } catch (GigiApiException e1) {
68                     e1.format(out, Page.getLanguage(req));
69                 }
70             }
71
72             return;
73         }
74
75         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")) {
76             ps.setString(1, req.getParameter("email"));
77             Calendar c = Calendar.getInstance();
78             c.set(Integer.parseInt(req.getParameter("year")), Integer.parseInt(req.getParameter("month")) - 1, Integer.parseInt(req.getParameter("day")));
79             ps.setDate(2, new Date(c.getTimeInMillis()));
80             GigiResultSet rs = ps.executeQuery();
81             int id = 0;
82             if (rs.next()) {
83                 id = rs.getInt(1);
84                 boolean verified = rs.getBoolean(2);
85                 if (rs.next()) {
86                     out.println("Error, ambigous user. Please contact support@cacert.org.");
87                 } else {
88                     if ( !verified) {
89                         out.println(translate(req, "User is not yet verified. Please try again in 24 hours!"));
90                     } else if (getUser(req).getId() == id) {
91
92                     } else {
93                         User assuree = User.getById(id);
94                         User myself = LoginPage.getUser(req);
95                         try {
96                             Notary.checkAssuranceIsPossible(myself, assuree);
97                             new AssuranceForm(req, assuree).output(out, getLanguage(req), new HashMap<String, Object>());
98                         } catch (GigiApiException e) {
99                             e.format(out, Page.getLanguage(req));
100                         }
101                     }
102                 }
103             } else {
104                 GigiApiException e = new GigiApiException("I'm sorry, there was no email and date of birth matching" //
105                         + " what you entered in the system. Please double check your information.");
106                 e.format(out, getLanguage(req));
107             }
108
109         }
110     }
111 }