]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/wot/AssuranceForm.java
upd: remove ToS check from assurance form
[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.text.SimpleDateFormat;
5 import java.util.HashMap;
6 import java.util.Iterator;
7 import java.util.LinkedList;
8 import java.util.Map;
9
10 import javax.servlet.http.HttpServletRequest;
11
12 import org.cacert.gigi.GigiApiException;
13 import org.cacert.gigi.dbObjects.Assurance.AssuranceType;
14 import org.cacert.gigi.dbObjects.Name;
15 import org.cacert.gigi.dbObjects.User;
16 import org.cacert.gigi.localisation.Language;
17 import org.cacert.gigi.output.template.Form;
18 import org.cacert.gigi.output.template.IterableDataset;
19 import org.cacert.gigi.output.template.Template;
20 import org.cacert.gigi.pages.Page;
21 import org.cacert.gigi.pages.PasswordResetPage;
22 import org.cacert.gigi.util.DayDate;
23 import org.cacert.gigi.util.Notary;
24
25 public class AssuranceForm extends Form {
26
27     private User assuree;
28
29     private Name assureeName;
30
31     private DayDate dob;
32
33     private String location = "";
34
35     private String date = "";
36
37     private String aword;
38
39     private User assurer;
40
41     private AssuranceType type = AssuranceType.FACE_TO_FACE;
42
43     private static final Template templ;
44     static {
45         templ = new Template(AssuranceForm.class.getResource("AssuranceForm.templ"));
46     }
47
48     public AssuranceForm(HttpServletRequest hsr, User assuree) {
49         super(hsr);
50         assurer = Page.getUser(hsr);
51         this.assuree = assuree;
52         assureeName = this.assuree.getName();
53         dob = this.assuree.getDoB();
54     }
55
56     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
57
58     SimpleDateFormat sdf2 = new SimpleDateFormat("dd. MMM yyyy");
59
60     @Override
61     public void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
62         HashMap<String, Object> res = new HashMap<String, Object>();
63         res.putAll(vars);
64         res.put("nameExplicit", assuree.getName());
65         res.put("name", assuree.getName().toString());
66         res.put("maxpoints", assurer.getMaxAssurePoints());
67         res.put("dob", sdf.format(assuree.getDoB().toDate()));
68         res.put("dobFmt2", sdf2.format(assuree.getDoB().toDate()));
69         res.put("location", location);
70         res.put("date", date);
71         res.put("aword", aword);
72         final LinkedList<AssuranceType> ats = new LinkedList<>();
73         for (AssuranceType at : AssuranceType.values()) {
74             try {
75                 Notary.may(assurer, assuree, at);
76                 ats.add(at);
77             } catch (GigiApiException e) {
78             }
79         }
80         res.put("ats", new IterableDataset() {
81
82             Iterator<AssuranceType> t = ats.iterator();
83
84             @Override
85             public boolean next(Language l, Map<String, Object> vars) {
86                 if ( !t.hasNext()) {
87                     return false;
88                 }
89                 AssuranceType t1 = t.next();
90                 vars.put("type", t1.getDescription());
91                 vars.put("id", t1.toString());
92                 vars.put("sel", t1 == type ? " selected" : "");
93                 return true;
94             }
95         });
96         templ.output(out, l, res);
97     }
98
99     @Override
100     public boolean submit(PrintWriter out, HttpServletRequest req) throws GigiApiException {
101         location = req.getParameter("location");
102         date = req.getParameter("date");
103         GigiApiException gae = new GigiApiException();
104         if (date == null || location == null) {
105             gae.mergeInto(new GigiApiException("You need to enter location and date!"));
106         }
107
108         if ( !"1".equals(req.getParameter("certify")) || !"1".equals(req.getParameter("rules")) || !"1".equals(req.getParameter("assertion"))) {
109             gae.mergeInto(new GigiApiException("You failed to check all boxes to validate" + " your adherence to the rules and policies of SomeCA"));
110         }
111         if ("1".equals(req.getParameter("passwordReset"))) {
112             aword = req.getParameter("passwordResetValue");
113             if ("".equals(aword)) {
114                 aword = null;
115             }
116         } else {
117             aword = null;
118         }
119         String val = req.getParameter("assuranceType");
120         if (val != null) {
121             try {
122                 type = AssuranceType.valueOf(val);
123             } catch (IllegalArgumentException e) {
124                 gae.mergeInto(new GigiApiException("Assurance Type wrong."));
125             }
126         }
127
128         int pointsI = 0;
129         String points = req.getParameter("points");
130         if (points == null || "".equals(points)) {
131             gae.mergeInto(new GigiApiException("For an assurance, you need to enter points."));
132         } else {
133             try {
134                 pointsI = Integer.parseInt(points);
135             } catch (NumberFormatException e) {
136                 gae.mergeInto(new GigiApiException("The points entered were not a number."));
137             }
138         }
139
140         if ( !gae.isEmpty()) {
141             throw gae;
142         }
143         Notary.assure(assurer, assuree, assureeName, dob, pointsI, location, req.getParameter("date"), type);
144         if (aword != null && !aword.equals("")) {
145             Language l = Language.getInstance(assuree.getPreferredLocale());
146             String method = l.getTranslation("A password reset was triggered. If you did a password reset by assurance, please enter your secret password using this form:");
147             String subject = l.getTranslation("Password reset by assurance");
148             PasswordResetPage.initPasswordResetProcess(out, assuree, req, aword, l, method, subject);
149         }
150         return true;
151     }
152
153     public User getAssuree() {
154         return assuree;
155     }
156
157 }