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