]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/wot/AssuranceForm.java
Convert CSRF-Problems to Exceptions.
[gigi.git] / src / org / cacert / gigi / pages / wot / AssuranceForm.java
1 package org.cacert.gigi.pages.wot;
2
3 import java.io.PrintWriter;
4 import java.sql.SQLException;
5 import java.text.ParseException;
6 import java.text.SimpleDateFormat;
7 import java.util.Date;
8 import java.util.HashMap;
9 import java.util.Map;
10
11 import javax.servlet.http.HttpServletRequest;
12
13 import org.cacert.gigi.Language;
14 import org.cacert.gigi.User;
15 import org.cacert.gigi.output.Form;
16 import org.cacert.gigi.output.template.Template;
17 import org.cacert.gigi.pages.LoginPage;
18 import org.cacert.gigi.util.Notary;
19 import org.cacert.gigi.util.Notary.AssuranceResult;
20
21 public class AssuranceForm extends Form {
22         User assuree;
23         static final Template templ;
24         static {
25                 templ = new Template(AssuranceForm.class.getResource("AssuranceForm.templ"));
26         }
27
28         public AssuranceForm(HttpServletRequest hsr, int assuree) {
29                 super(hsr);
30                 this.assuree = new User(assuree);
31         }
32
33         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
34
35         @Override
36         public void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
37                 HashMap<String, Object> res = new HashMap<String, Object>();
38                 res.putAll(vars);
39                 res.put("name", assuree.getName());
40                 try {
41                         res.put("maxpoints", assuree.getMaxAssurePoints());
42                 } catch (SQLException e) {
43                         e.printStackTrace();
44                 }
45                 res.put("dob", sdf.format(assuree.getDob()));
46                 templ.output(out, l, res);
47         }
48
49         @Override
50         public boolean submit(PrintWriter out, HttpServletRequest req) {
51                 out.println("<div class='formError'>");
52                 boolean failed = false;
53
54                 if (!"1".equals(req.getParameter("certify")) || !"1".equals(req.getParameter("rules"))
55                         || !"1".equals(req.getParameter("CCAAgreed")) || !"1".equals(req.getParameter("assertion"))) {
56                         outputError(out, req, "You failed to check all boxes to validate"
57                                 + " your adherence to the rules and policies of CAcert");
58                         failed = true;
59
60                 }
61                 if (req.getParameter("date") == null || req.getParameter("date").equals("")) {
62                         outputError(out, req, "You must enter the date when you met the assuree.");
63                         failed = true;
64                 } else {
65                         try {
66                                 Date d = sdf.parse(req.getParameter("date"));
67                                 if (d.getTime() > System.currentTimeMillis()) {
68                                         outputError(out, req, "You must not enter a date in the future.");
69                                         failed = true;
70                                 }
71                         } catch (ParseException e) {
72                                 outputError(out, req, "You must enter the date in this format: YYYY-MM-DD.");
73                                 failed = true;
74                         }
75                 }
76                 // check location, min 3 characters
77                 if (req.getParameter("location") == null || req.getParameter("location").equals("")) {
78                         outputError(out, req, "You failed to enter a location of your meeting.");
79                         failed = true;
80                 } else if (req.getParameter("location").length() <= 2) {
81                         outputError(out, req, "You must enter a location with at least 3 characters eg town and country.");
82                         failed = true;
83                 }
84                 // TODO checkPoints
85                 String points = req.getParameter("points");
86                 if (points == null || "".equals(points)) {
87                         // TODO message
88                         failed = true;
89                 }
90                 if (failed) {
91                         out.println("</div>");
92                         return false;
93                 }
94                 try {
95                         AssuranceResult success = Notary.assure(LoginPage.getUser(req), assuree,
96                                 Integer.parseInt(req.getParameter("points")), req.getParameter("location"), req.getParameter("date"));
97                         if (success != AssuranceResult.ASSURANCE_SUCCEDED) {
98                                 outputError(out, req, success.getMessage());
99                         }
100                         out.println("</div>");
101                         return success == AssuranceResult.ASSURANCE_SUCCEDED;
102                 } catch (SQLException e) {
103                         e.printStackTrace();
104                 }
105
106                 out.println("</div>");
107                 return false;
108         }
109
110 }