]> WPIA git - gigi.git/blob - util-testing/org/cacert/gigi/pages/Manager.java
upd: cleanup Test Manager and add an "add verified email"-form
[gigi.git] / util-testing / org / cacert / gigi / pages / Manager.java
1 package org.cacert.gigi.pages;
2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.lang.reflect.Field;
6 import java.sql.Date;
7 import java.util.Calendar;
8 import java.util.GregorianCalendar;
9 import java.util.HashMap;
10 import java.util.Iterator;
11 import java.util.LinkedList;
12 import java.util.Locale;
13 import java.util.Map;
14 import java.util.Properties;
15
16 import javax.servlet.http.HttpServletRequest;
17 import javax.servlet.http.HttpServletResponse;
18
19 import org.cacert.gigi.GigiApiException;
20 import org.cacert.gigi.database.DatabaseConnection;
21 import org.cacert.gigi.database.GigiPreparedStatement;
22 import org.cacert.gigi.dbObjects.EmailAddress;
23 import org.cacert.gigi.dbObjects.Group;
24 import org.cacert.gigi.dbObjects.Name;
25 import org.cacert.gigi.dbObjects.User;
26 import org.cacert.gigi.email.EmailProvider;
27 import org.cacert.gigi.localisation.Language;
28 import org.cacert.gigi.output.template.IterableDataset;
29 import org.cacert.gigi.output.template.Template;
30 import org.cacert.gigi.util.Notary;
31
32 public class Manager extends Page {
33
34     public static final String PATH = "/manager";
35
36     Field f;
37
38     private Manager() {
39         super("Test Manager");
40         try {
41             f = EmailAddress.class.getDeclaredField("hash");
42             f.setAccessible(true);
43         } catch (ReflectiveOperationException e) {
44             throw new Error(e);
45         }
46     }
47
48     public User[] getAssurers() {
49         if (assurers != null) {
50             return assurers;
51         }
52         assurers = new User[10];
53         try {
54             GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?");
55             for (int i = 0; i < assurers.length; i++) {
56                 String mail = "test-assurer" + i + "@example.com";
57                 User u = User.getByEmail(mail);
58                 if (u == null) {
59                     System.out.println("Creating assurer");
60                     createUser(mail);
61                     u = User.getByEmail(mail);
62                     passCATS(u);
63                     ps.setInt(1, u.getId());
64                     ps.setInt(2, u.getId());
65                     ps.setInt(3, 100);
66                     ps.setString(4, "Manager init code");
67                     ps.setString(5, "1990-01-01");
68                     ps.execute();
69                 }
70                 assurers[i] = u;
71
72             }
73         } catch (ReflectiveOperationException | GigiApiException e) {
74             e.printStackTrace();
75         }
76         return assurers;
77     }
78
79     private void passCATS(User u) {
80         GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO cats_passed SET user_id=?, variant_id=3");
81         ps.setInt(1, u.getId());
82         ps.execute();
83     }
84
85     private static Manager instance;
86
87     Template t = new Template(Manager.class.getResource("ManagerMails.templ"));
88
89     HashMap<String, LinkedList<String>> emails = new HashMap<>();
90
91     public static Manager getInstance() {
92         if (instance == null) {
93             instance = new Manager();
94         }
95         return instance;
96     }
97
98     public static class MailFetcher extends EmailProvider {
99
100         public MailFetcher(Properties p) {}
101
102         @Override
103         public String checkEmailServer(int forUid, String address) throws IOException {
104             return OK;
105         }
106
107         @Override
108         public synchronized void sendmail(String to, String subject, String message, String from, String replyto, String toname, String fromname, String errorsto, boolean extra) throws IOException {
109             HashMap<String, LinkedList<String>> mails = Manager.getInstance().emails;
110             LinkedList<String> hismails = mails.get(to);
111             if (hismails == null) {
112                 mails.put(to, hismails = new LinkedList<>());
113             }
114             hismails.addFirst(subject + "\n" + message);
115         }
116
117     }
118
119     public void batchCreateUsers(String mailPrefix, String domain, int amount, PrintWriter out) {
120
121         try {
122             if (amount > 100) {
123                 out.print("100 at most, please.");
124                 return;
125             }
126             for (int i = 0; i < amount; i++) {
127                 String email = mailPrefix + i + "@" + domain;
128                 createUser(email);
129             }
130         } catch (ReflectiveOperationException e) {
131             out.println("failed");
132             e.printStackTrace();
133         } catch (GigiApiException e) {
134             out.println("failed: " + e.getMessage());
135             e.printStackTrace();
136         }
137     }
138
139     private void createUser(String email) throws GigiApiException, IllegalAccessException {
140         User u = new User();
141         u.setName(new Name("Först", "Läst", "Müddle", "Süffix"));
142         u.setEmail(email);
143         Calendar gc = GregorianCalendar.getInstance();
144         gc.set(1990, 0, 1);
145         u.setDoB(new Date(gc.getTime().getTime()));
146         u.setPreferredLocale(Locale.ENGLISH);
147         u.insert("xvXV12°§");
148         EmailAddress ea = new EmailAddress(u, email);
149         ea.insert(Language.getInstance(Locale.ENGLISH));
150         String hash = (String) f.get(ea);
151
152         ea.verify(hash);
153     }
154
155     User[] assurers;
156
157     @Override
158     public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
159         if (req.getParameter("create") != null) {
160             batchCreateUsers(req.getParameter("prefix"), req.getParameter("suffix"), Integer.parseInt(req.getParameter("amount")), resp.getWriter());
161             resp.getWriter().println("User batch created.");
162         } else if (req.getParameter("addpriv") != null || req.getParameter("delpriv") != null) {
163             User u = User.getByEmail(req.getParameter("email"));
164             if (u == null) {
165                 resp.getWriter().println("User not found.");
166                 return;
167             }
168             if (req.getParameter("addpriv") != null) {
169                 u.grantGroup(u, Group.getByString(req.getParameter("priv")));
170                 resp.getWriter().println("Privilege granted");
171             } else {
172                 u.revokeGroup(u, Group.getByString(req.getParameter("priv")));
173                 resp.getWriter().println("Privilege revoked");
174             }
175         } else if (req.getParameter("fetch") != null) {
176             String mail = req.getParameter("femail");
177             fetchMails(req, resp, mail);
178         } else if (req.getParameter("cats") != null) {
179             String mail = req.getParameter("catsEmail");
180             User byEmail = User.getByEmail(mail);
181             if (byEmail == null) {
182                 resp.getWriter().println("User not found.");
183                 return;
184             }
185             passCATS(byEmail);
186             resp.getWriter().println("User has been passed CATS");
187         } else if (req.getParameter("assure") != null) {
188             String mail = req.getParameter("assureEmail");
189             User byEmail = User.getByEmail(mail);
190             if (byEmail == null) {
191                 resp.getWriter().println("User not found.");
192                 return;
193             }
194             try {
195                 for (int i = 0; i < getAssurers().length; i++) {
196                     Notary.assure(getAssurers()[i], byEmail, byEmail.getName(), byEmail.getDoB(), 10, "Testmanager Assure up code", "2014-11-06");
197                 }
198             } catch (GigiApiException e) {
199                 throw new Error(e);
200             }
201             resp.getWriter().println("User has been assured.");
202         } else if (req.getParameter("addEmail") != null) {
203             User u = User.getByEmail(req.getParameter("addEmailEmail"));
204             EmailAddress ea = new EmailAddress(u, req.getParameter("addEmailNew"));
205             try {
206                 ea.insert(Language.getInstance(Locale.ENGLISH));
207                 String hash = (String) f.get(ea);
208                 ea.verify(hash);
209                 resp.getWriter().println("Email added and verified");
210             } catch (IllegalArgumentException e) {
211                 e.printStackTrace();
212                 resp.getWriter().println("An internal error occured.");
213             } catch (IllegalAccessException e) {
214                 e.printStackTrace();
215                 resp.getWriter().println("An internal error occured.");
216             } catch (GigiApiException e) {
217                 e.format(resp.getWriter(), Language.getInstance(Locale.ENGLISH));
218             }
219
220         }
221     }
222
223     private void fetchMails(HttpServletRequest req, HttpServletResponse resp, String mail) throws IOException {
224         final LinkedList<String> mails = emails.get(mail);
225         HashMap<String, Object> vars = new HashMap<>();
226         vars.put("mail", mail);
227         if (mails != null) {
228             vars.put("mails", new IterableDataset() {
229
230                 Iterator<String> s = mails.iterator();
231
232                 @Override
233                 public boolean next(Language l, Map<String, Object> vars) {
234                     if ( !s.hasNext()) {
235                         return false;
236                     }
237                     vars.put("body", s.next().replaceAll("(https?://\\S+)", "<a href=\"$1\">$1</a>"));
238                     return true;
239                 }
240             });
241         }
242         t.output(resp.getWriter(), getLanguage(req), vars);
243         if (mails == null) {
244             resp.getWriter().println("No mails");
245
246         }
247     }
248
249     private Template form = new Template(Manager.class.getResource("Manager.templ"));
250
251     @Override
252     public boolean needsLogin() {
253         return false;
254     }
255
256     @Override
257     public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
258         getAssurers();
259         String pi = req.getPathInfo().substring(PATH.length());
260         if (pi.length() > 1 && pi.startsWith("/fetch-")) {
261             String mail = pi.substring(pi.indexOf('-', 2) + 1);
262             fetchMails(req, resp, mail);
263             return;
264         }
265
266         form.output(resp.getWriter(), getLanguage(req), new HashMap<String, Object>());
267     }
268 }