]> WPIA git - gigi.git/blob - src/club/wpia/gigi/api/FindAgent.java
Merge "upd: remove 'browser install'"
[gigi.git] / src / club / wpia / gigi / api / FindAgent.java
1 package club.wpia.gigi.api;
2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.math.BigInteger;
6 import java.util.HashMap;
7
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpServletResponse;
10
11 import org.json.JSONWriter;
12
13 import club.wpia.gigi.dbObjects.Certificate;
14 import club.wpia.gigi.dbObjects.CertificateOwner;
15 import club.wpia.gigi.dbObjects.Group;
16 import club.wpia.gigi.dbObjects.Organisation;
17 import club.wpia.gigi.dbObjects.User;
18 import club.wpia.gigi.email.EmailProvider;
19 import club.wpia.gigi.pages.account.FindAgentAccess;
20 import club.wpia.gigi.util.ServerConstants;
21 import club.wpia.gigi.util.ServerConstants.Host;
22
23 public class FindAgent extends APIPoint {
24
25     public static final String PATH_RESOLVE = "/find-agent/resolve";
26
27     public static final String PATH_INFO = "/find-agent/info";
28
29     public static final String PATH_MAIL = "/find-agent/email";
30
31     public FindAgent() {}
32
33     public static void register(HashMap<String, APIPoint> api) {
34         APIPoint p = new FindAgent();
35         api.put(PATH_RESOLVE, p);
36         api.put(PATH_INFO, p);
37         api.put(PATH_MAIL, p);
38     }
39
40     @Override
41     public void process(HttpServletRequest req, HttpServletResponse resp, CertificateOwner u) throws IOException {
42         if ( !(u instanceof Organisation)) {
43             resp.sendError(500, "Error, invalid cert");
44             return;
45         }
46         if ( !((Organisation) u).isSelfOrganisation()) {
47             resp.sendError(500, "Error, invalid cert");
48             return;
49         }
50         String pi = req.getPathInfo();
51         if (pi.equals(PATH_RESOLVE)) {
52             String serial = req.getParameter("serial");
53             if (serial == null || serial.isEmpty()) {
54                 resp.sendError(500, "Error, requires serial");
55                 return;
56             }
57             Certificate c = Certificate.getBySerial(new BigInteger(serial, 16));
58             if (c == null) {
59                 resp.sendError(500, "Error, requires serial");
60                 return;
61             }
62             CertificateOwner co = c.getOwner();
63             if ( !(co instanceof User)) {
64                 resp.sendError(500, "Error, requires serial");
65                 return;
66             }
67             User us = (User) co;
68             if ( !us.isInGroup(Group.LOCATE_AGENT)) {
69                 resp.setStatus(501);
70                 resp.setContentType("text/plain; charset=UTF-8");
71                 resp.getWriter().println("https://" + ServerConstants.getHostNamePortSecure(Host.SECURE) + FindAgentAccess.PATH);
72                 return;
73             }
74             resp.setContentType("text/plain; charset=UTF-8");
75             resp.getWriter().print(us.getId());
76         } else if (pi.equals(PATH_INFO)) {
77             resp.setContentType("application/json; charset=UTF-8");
78             PrintWriter out = resp.getWriter();
79             String[] uids = req.getParameterValues("id");
80             JSONWriter jw = new JSONWriter(out);
81             jw.array();
82             for (String i : uids) {
83                 User u1 = User.getById(Integer.parseInt(i));
84                 if ( !u1.isInGroup(Group.LOCATE_AGENT)) {
85                     continue;
86                 }
87                 // date, recheck(?), name
88                 jw.object();
89                 jw.key("id");
90                 jw.value(u1.getId());
91
92                 jw.key("canVerify");
93                 jw.value(u1.canVerify());
94
95                 jw.key("name");
96                 jw.value(u1.getPreferredName().toAbbreviatedString());
97                 jw.endObject();
98             }
99             jw.endArray();
100         } else if (pi.equals(PATH_MAIL)) {
101             String id = req.getParameter("from");
102             String rid = req.getParameter("to");
103             String subject = req.getParameter("subject");
104             String body = req.getParameter("body");
105             if (id == null || rid == null || subject == null || body == null) {
106                 resp.sendError(500, "Error, parameter missing");
107                 return;
108             }
109             User from = User.getById(Integer.parseInt(id));
110             User to = User.getById(Integer.parseInt(rid));
111             if (from == null || to == null) {
112                 resp.sendError(500, "Error, user not found");
113                 return;
114             }
115             if ( !from.isInGroup(Group.LOCATE_AGENT) || !to.isInGroup(Group.LOCATE_AGENT)) {
116                 resp.sendError(501, "Error, user needs to enable access");
117                 return;
118
119             }
120             EmailProvider.getInstance().sendMail(to.getEmail(), "[Find Agent] " + subject, body, null, null, null, null, false);
121         }
122     }
123 }