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