1 package org.cacert.gigi.pages;
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.lang.reflect.Field;
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;
14 import java.util.Properties;
16 import javax.servlet.http.HttpServletRequest;
17 import javax.servlet.http.HttpServletResponse;
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.template.Form;
28 import org.cacert.gigi.output.template.IterableDataset;
29 import org.cacert.gigi.output.template.Template;
30 import org.cacert.gigi.util.Notary;
32 public class Manager extends Page {
34 public static final String PATH = "/manager";
39 super("Test Manager");
41 f = EmailAddress.class.getDeclaredField("hash");
42 f.setAccessible(true);
43 } catch (ReflectiveOperationException e) {
48 public User[] getAssurers() {
49 if (assurers != null) {
52 assurers = new User[10];
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);
60 u = User.getByEmail(mail);
62 ps.setInt(1, u.getId());
63 ps.setInt(2, u.getId());
65 ps.setString(4, "Manager init code");
66 ps.setString(5, "1990-01-01");
72 } catch (ReflectiveOperationException | GigiApiException e) {
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());
84 private static Manager instance;
86 Template t = new Template(Manager.class.getResource("ManagerMails.templ"));
88 HashMap<String, LinkedList<String>> emails = new HashMap<>();
90 public static Manager getInstance() {
91 if (instance == null) {
92 instance = new Manager();
97 public static class MailFetcher extends EmailProvider {
99 public MailFetcher(Properties p) {}
102 public String checkEmailServer(int forUid, String address) throws IOException {
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<>());
113 hismails.addFirst(subject + "\n" + message);
118 public class ManagementForm extends Form {
120 public ManagementForm(HttpServletRequest hsr) {
125 public boolean submit(PrintWriter out, HttpServletRequest req) throws GigiApiException {
130 protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
131 getDefaultTemplate().output(out, l, vars);
136 public void batchCreateUsers(String mailPrefix, String domain, int amount, PrintWriter out) {
140 out.print("100 at most, please.");
143 for (int i = 0; i < amount; i++) {
144 String email = mailPrefix + i + "@" + domain;
147 } catch (ReflectiveOperationException e) {
148 out.println("failed");
150 } catch (GigiApiException e) {
151 out.println("failed: " + e.getMessage());
156 private void createUser(String email) throws GigiApiException, IllegalAccessException {
159 u.setMname("Müddle");
161 u.setSuffix("Süffix");
163 Calendar gc = GregorianCalendar.getInstance();
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);
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"));
184 resp.getWriter().println("User not found.");
187 if (req.getParameter("addpriv") != null) {
188 u.grantGroup(u, Group.getByString(req.getParameter("priv")));
190 u.revokeGroup(u, Group.getByString(req.getParameter("priv")));
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.");
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.");
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");
215 } catch (GigiApiException e) {
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);
226 vars.put("mails", new IterableDataset() {
228 Iterator<String> s = mails.iterator();
231 public boolean next(Language l, Map<String, Object> vars) {
234 vars.put("body", s.next().replaceAll("(https?://\\S+)", "<a href=\"$1\">$1</a>"));
239 t.output(resp.getWriter(), getLanguage(req), vars);
241 resp.getWriter().println("No mails");
247 public boolean needsLogin() {
252 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
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);
261 new ManagementForm(req).output(resp.getWriter(), getLanguage(req), new HashMap<String, Object>());