]> WPIA git - gigi.git/blob - util-testing/org/cacert/gigi/pages/Manager.java
ADD: TestManager CATs-passing und 100-points-assuring
[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.User;
25 import org.cacert.gigi.email.EmailProvider;
26 import org.cacert.gigi.localisation.Language;
27 import org.cacert.gigi.output.Form;
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                     createUser(mail);
60                     u = User.getByEmail(mail);
61                     passCATS(u);
62                     ps.setInt(1, u.getId());
63                     ps.setInt(2, u.getId());
64                     ps.setInt(3, 100);
65                     ps.setString(4, "Manager init code");
66                     ps.setString(5, "1990-01-01");
67                     ps.execute();
68                 }
69                 assurers[i] = u;
70
71             }
72         } catch (ReflectiveOperationException | GigiApiException e) {
73             e.printStackTrace();
74         }
75         return assurers;
76     }
77
78     private void passCATS(User u) {
79         GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO cats_passed SET user_id=?, variant_id=3");
80         ps.setInt(1, u.getId());
81         ps.execute();
82     }
83
84     private static Manager instance;
85
86     Template t = new Template(Manager.class.getResource("ManagerMails.templ"));
87
88     HashMap<String, LinkedList<String>> emails = new HashMap<>();
89
90     public static Manager getInstance() {
91         if (instance == null) {
92             instance = new Manager();
93         }
94         return instance;
95     }
96
97     public static class MailFetcher extends EmailProvider {
98
99         public MailFetcher(Properties p) {}
100
101         @Override
102         public String checkEmailServer(int forUid, String address) throws IOException {
103             return OK;
104         }
105
106         @Override
107         public synchronized void sendmail(String to, String subject, String message, String from, String replyto, String toname, String fromname, String errorsto, boolean extra) throws IOException {
108             HashMap<String, LinkedList<String>> mails = Manager.getInstance().emails;
109             LinkedList<String> hismails = mails.get(to);
110             if (hismails == null) {
111                 mails.put(to, hismails = new LinkedList<>());
112             }
113             hismails.addFirst(subject + "\n" + message);
114         }
115
116     }
117
118     public class ManagementForm extends Form {
119
120         public ManagementForm(HttpServletRequest hsr) {
121             super(hsr);
122         }
123
124         @Override
125         public boolean submit(PrintWriter out, HttpServletRequest req) throws GigiApiException {
126             return false;
127         }
128
129         @Override
130         protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
131             getDefaultTemplate().output(out, l, vars);
132         }
133
134     }
135
136     public void batchCreateUsers(String mailPrefix, String domain, int amount, PrintWriter out) {
137
138         try {
139             if (amount > 100) {
140                 out.print("100 at most, please.");
141                 return;
142             }
143             for (int i = 0; i < amount; i++) {
144                 String email = mailPrefix + i + "@" + domain;
145                 createUser(email);
146             }
147         } catch (ReflectiveOperationException e) {
148             out.println("failed");
149             e.printStackTrace();
150         } catch (GigiApiException e) {
151             out.println("failed: " + e.getMessage());
152             e.printStackTrace();
153         }
154     }
155
156     private void createUser(String email) throws GigiApiException, IllegalAccessException {
157         User u = new User();
158         u.setFname("Först");
159         u.setMname("Müddle");
160         u.setLname("Läst");
161         u.setSuffix("Süffix");
162         u.setEmail(email);
163         Calendar gc = GregorianCalendar.getInstance();
164         gc.set(1990, 0, 1);
165         u.setDob(new Date(gc.getTime().getTime()));
166         u.setPreferredLocale(Locale.ENGLISH);
167         u.insert("xvXV12°§");
168         EmailAddress ea = new EmailAddress(u, email);
169         ea.insert(Language.getInstance(Locale.ENGLISH));
170         String hash = (String) f.get(ea);
171
172         ea.verify(hash);
173     }
174
175     User[] assurers;
176
177     @Override
178     public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
179         if (req.getParameter("create") != null) {
180             batchCreateUsers(req.getParameter("prefix"), req.getParameter("suffix"), Integer.parseInt(req.getParameter("amount")), resp.getWriter());
181         } else if (req.getParameter("addpriv") != null || req.getParameter("delpriv") != null) {
182             User u = User.getByEmail(req.getParameter("email"));
183             if (u == null) {
184                 resp.getWriter().println("User not found.");
185                 return;
186             }
187             if (req.getParameter("addpriv") != null) {
188                 u.grantGroup(u, Group.getByString(req.getParameter("priv")));
189             } else {
190                 u.revokeGroup(u, Group.getByString(req.getParameter("priv")));
191             }
192
193         } else if (req.getParameter("fetch") != null) {
194             String mail = req.getParameter("femail");
195             fetchMails(req, resp, mail);
196         } else if (req.getParameter("cats") != null) {
197             String mail = req.getParameter("catsEmail");
198             User byEmail = User.getByEmail(mail);
199             if (byEmail == null) {
200                 resp.getWriter().println("User not found.");
201                 return;
202             }
203             passCATS(byEmail);
204         } else if (req.getParameter("assure") != null) {
205             String mail = req.getParameter("assureEmail");
206             User byEmail = User.getByEmail(mail);
207             if (byEmail == null) {
208                 resp.getWriter().println("User not found.");
209                 return;
210             }
211             try {
212                 for (int i = 0; i < getAssurers().length; i++) {
213                     Notary.assure(getAssurers()[i], byEmail, byEmail.getName(), byEmail.getDob(), 10, "Testmanager Assure up code", "2014-11-06");
214                 }
215             } catch (GigiApiException e) {
216                 throw new Error(e);
217             }
218         }
219     }
220
221     private void fetchMails(HttpServletRequest req, HttpServletResponse resp, String mail) throws IOException {
222         final LinkedList<String> mails = emails.get(mail);
223         HashMap<String, Object> vars = new HashMap<>();
224         vars.put("mail", mail);
225         if (mails != null) {
226             vars.put("mails", new IterableDataset() {
227
228                 Iterator<String> s = mails.iterator();
229
230                 @Override
231                 public boolean next(Language l, Map<String, Object> vars) {
232                     if ( !s.hasNext())
233                         return false;
234                     vars.put("body", s.next().replaceAll("(https?://\\S+)", "<a href=\"$1\">$1</a>"));
235                     return true;
236                 }
237             });
238         }
239         t.output(resp.getWriter(), getLanguage(req), vars);
240         if (mails == null) {
241             resp.getWriter().println("No mails");
242
243         }
244     }
245
246     @Override
247     public boolean needsLogin() {
248         return false;
249     }
250
251     @Override
252     public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
253         getAssurers();
254         String pi = req.getPathInfo().substring(PATH.length());
255         if (pi.length() > 1 && pi.startsWith("/fetch-")) {
256             String mail = pi.substring(pi.indexOf('-', 2) + 1);
257             fetchMails(req, resp, mail);
258             return;
259         }
260
261         new ManagementForm(req).output(resp.getWriter(), getLanguage(req), new HashMap<String, Object>());
262     }
263 }