]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/wot/AssuranceForm.java
b1cfbae950ae09afecb62ad6610d3a41676f5f1e
[gigi.git] / src / org / cacert / gigi / pages / wot / AssuranceForm.java
1 package org.cacert.gigi.pages.wot;
2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.net.URLEncoder;
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.GigiApiException;
14 import org.cacert.gigi.dbObjects.Name;
15 import org.cacert.gigi.dbObjects.User;
16 import org.cacert.gigi.email.Sendmail;
17 import org.cacert.gigi.localisation.Language;
18 import org.cacert.gigi.output.template.Form;
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.Notary;
23 import org.cacert.gigi.util.RandomToken;
24 import org.cacert.gigi.util.ServerConstants;
25
26 public class AssuranceForm extends Form {
27
28     private User assuree;
29
30     private Name assureeName;
31
32     private Date dob;
33
34     private String location = "";
35
36     private String date = "";
37
38     private String aword;
39
40     private static final Template templ;
41     static {
42         templ = new Template(AssuranceForm.class.getResource("AssuranceForm.templ"));
43     }
44
45     public AssuranceForm(HttpServletRequest hsr, User assuree) {
46         super(hsr);
47         this.assuree = assuree;
48         assureeName = this.assuree.getName();
49         dob = this.assuree.getDoB();
50     }
51
52     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
53
54     SimpleDateFormat sdf2 = new SimpleDateFormat("dd. MMM yyyy");
55
56     @Override
57     public void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
58         HashMap<String, Object> res = new HashMap<String, Object>();
59         res.putAll(vars);
60         res.put("nameExplicit", assuree.getName());
61         res.put("name", assuree.getName().toString());
62         res.put("maxpoints", assuree.getMaxAssurePoints());
63         res.put("dob", sdf.format(assuree.getDoB()));
64         res.put("dobFmt2", sdf2.format(assuree.getDoB()));
65         res.put("location", location);
66         res.put("date", date);
67         res.put("aword", aword);
68         templ.output(out, l, res);
69     }
70
71     @Override
72     public boolean submit(PrintWriter out, HttpServletRequest req) {
73         location = req.getParameter("location");
74         date = req.getParameter("date");
75         if (date == null || location == null) {
76             outputError(out, req, "You need to enter location and date!");
77         }
78
79         if ( !"1".equals(req.getParameter("certify")) || !"1".equals(req.getParameter("rules")) || !"1".equals(req.getParameter("CCAAgreed")) || !"1".equals(req.getParameter("assertion"))) {
80             outputError(out, req, "You failed to check all boxes to validate" + " your adherence to the rules and policies of CAcert");
81
82         }
83         if ("1".equals(req.getParameter("passwordReset"))) {
84             aword = req.getParameter("passwordResetValue");
85             if ("".equals(aword)) {
86                 aword = null;
87             }
88         } else {
89             aword = null;
90         }
91
92         int pointsI = 0;
93         String points = req.getParameter("points");
94         if (points == null || "".equals(points)) {
95             outputError(out, req, "For an assurance, you need to enter points.");
96         } else {
97             try {
98                 pointsI = Integer.parseInt(points);
99             } catch (NumberFormatException e) {
100                 outputError(out, req, "The points entered were not a number.");
101             }
102         }
103
104         if (isFailed(out)) {
105             return false;
106         }
107         try {
108             Notary.assure(Page.getUser(req), assuree, assureeName, dob, pointsI, location, req.getParameter("date"));
109             if (aword != null && !aword.equals("")) {
110                 String systemToken = RandomToken.generateToken(32);
111                 int id = assuree.generatePasswordResetTicket(Page.getUser(req), systemToken, aword);
112                 try {
113                     Language l = Language.getInstance(assuree.getPreferredLocale());
114                     StringBuffer body = new StringBuffer();
115                     body.append(l.getTranslation("Hi,") + "\n\n");
116                     body.append(l.getTranslation("A password reset was triggered. If you did a password reset by assurance, please enter your secret password using this form: \n"));
117                     body.append(ServerConstants.getWwwHostNamePortSecure() + PasswordResetPage.PATH);
118                     body.append("?id=");
119                     body.append(id);
120                     body.append("&token=");
121                     body.append(URLEncoder.encode(systemToken, "UTF-8"));
122                     body.append("\n");
123                     body.append("\n");
124                     body.append(l.getTranslation("Best regards"));
125                     body.append("\n");
126                     body.append(l.getTranslation("CAcert.org Support!"));
127                     Sendmail.getInstance().sendmail(assuree.getEmail(), "[CAcert.org] " + l.getTranslation("Password reset by assurance"), body.toString(), "support@cacert.org", null, null, null, null, false);
128                 } catch (IOException e) {
129                     e.printStackTrace();
130                 }
131             }
132             return true;
133         } catch (GigiApiException e) {
134             e.format(out, Page.getLanguage(req));
135         }
136
137         return false;
138     }
139
140     public User getAssuree() {
141         return assuree;
142     }
143
144 }