]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/wot/AssurePage.java
UPD: Only show "assure someone" if the user actually can assure
[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.PreparedStatement;
6 import java.sql.ResultSet;
7 import java.sql.SQLException;
8 import java.util.HashMap;
9
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12
13 import org.cacert.gigi.User;
14 import org.cacert.gigi.database.DatabaseConnection;
15 import org.cacert.gigi.output.DateSelector;
16 import org.cacert.gigi.output.Form;
17 import org.cacert.gigi.output.template.Template;
18 import org.cacert.gigi.pages.LoginPage;
19 import org.cacert.gigi.pages.Page;
20 import org.cacert.gigi.util.Notary;
21 import org.cacert.gigi.util.Notary.AssuranceResult;
22
23 public class AssurePage extends Page {
24
25     public static final String PATH = "/wot/assure";
26
27     DateSelector ds = new DateSelector("day", "month", "year");
28
29     Template t;
30
31     public AssurePage() {
32         super("Assure someone");
33         t = new Template(AssuranceForm.class.getResource("AssureeSearch.templ"));
34
35     }
36
37     @Override
38     public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
39
40         PrintWriter out = resp.getWriter();
41         String pi = req.getPathInfo().substring(PATH.length());
42         if (pi.length() > 1) {
43             int mid = Integer.parseInt(pi.substring(1));
44             AssuranceForm form = new AssuranceForm(req, mid);
45             outputForm(req, out, mid, form);
46
47         } else {
48             HashMap<String, Object> vars = new HashMap<String, Object>();
49             vars.put("DoB", ds);
50             t.output(out, getLanguage(req), vars);
51         }
52     }
53
54     @Override
55     public boolean isPermitted(User u) {
56         try {
57             return u != null && u.canAssure();
58         } catch (SQLException e) {
59             e.printStackTrace();
60             return false;
61         }
62     }
63
64     private void outputForm(HttpServletRequest req, PrintWriter out, int mid, AssuranceForm form) {
65         User myself = LoginPage.getUser(req);
66         AssuranceResult check = Notary.checkAssuranceIsPossible(myself, new User(mid));
67         if (check != AssuranceResult.ASSURANCE_SUCCEDED) {
68             out.println(translate(req, check.getMessage()));
69             return;
70         }
71         if (form == null || form.getAssuree().getId() != mid) {
72             form = new AssuranceForm(req, mid);
73         }
74
75         form.output(out, getLanguage(req), new HashMap<String, Object>());
76     }
77
78     @Override
79     public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
80         PrintWriter out = resp.getWriter();
81         String pi = req.getPathInfo().substring(PATH.length());
82         if (pi.length() > 1) {
83             User myself = getUser(req);
84             int mid = Integer.parseInt(pi.substring(1));
85             if (mid == myself.getId()) {
86                 out.println(translate(req, "Cannot assure myself."));
87                 return;
88             }
89
90             AssuranceForm form = Form.getForm(req, AssuranceForm.class);
91             if (mid != form.getAssuree().getId()) {
92                 return;
93             }
94             if (form.submit(out, req)) {
95                 out.println(translate(req, "Assurance complete."));
96             } else {
97                 outputForm(req, resp.getWriter(), mid, form);
98             }
99
100             return;
101         }
102
103         ResultSet rs = null;
104         try {
105             PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id, verified FROM users WHERE email=? AND dob=? AND deleted=0");
106             ps.setString(1, req.getParameter("email"));
107             String day = req.getParameter("year") + "-" + req.getParameter("month") + "-" + req.getParameter("day");
108             ps.setString(2, day);
109             rs = ps.executeQuery();
110             int id = 0;
111             if (rs.next()) {
112                 id = rs.getInt(1);
113                 int verified = rs.getInt(2);
114                 if (rs.next()) {
115                     out.println("Error, ambigous user. Please contact support@cacert.org.");
116                 } else {
117                     if (verified == 0) {
118                         out.println(translate(req, "User is not yet verified. Please try again in 24 hours!"));
119                     }
120                     resp.sendRedirect(PATH + "/" + id);
121                 }
122             } else {
123                 out.print("<div class='formError'>");
124
125                 out.println(translate(req, "I'm sorry, there was no email and date of birth matching" + " what you entered in the system. Please double check" + " your information."));
126                 out.print("</div>");
127             }
128
129             rs.close();
130         } catch (SQLException e) {
131             e.printStackTrace();
132         } finally {
133             try {
134                 if (rs != null) {
135                     rs.close();
136                 }
137             } catch (SQLException e) {
138                 e.printStackTrace();
139             }
140         }
141     }
142 }