]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/admin/TTPAdminPage.java
Merge branch 'libs/jetty/local'
[gigi.git] / src / org / cacert / gigi / pages / admin / TTPAdminPage.java
1 package org.cacert.gigi.pages.admin;
2
3 import java.io.IOException;
4 import java.util.Arrays;
5 import java.util.HashMap;
6 import java.util.Map;
7
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpServletResponse;
10
11 import org.cacert.gigi.GigiApiException;
12 import org.cacert.gigi.dbObjects.Group;
13 import org.cacert.gigi.dbObjects.User;
14 import org.cacert.gigi.localisation.Language;
15 import org.cacert.gigi.output.template.Form;
16 import org.cacert.gigi.output.template.IterableDataset;
17 import org.cacert.gigi.output.template.SprintfCommand;
18 import org.cacert.gigi.pages.Page;
19 import org.cacert.gigi.pages.error.PageNotFound;
20 import org.cacert.gigi.util.AuthorizationContext;
21
22 public class TTPAdminPage extends Page {
23
24     public static final String PATH = "/admin/ttp";
25
26     public static final Group TTP_APPLICANT = Group.getByString("ttp-applicant");
27
28     public TTPAdminPage() {
29         super("TTP-Admin");
30     }
31
32     @Override
33     public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
34         try {
35             Form.getForm(req, TTPAdminForm.class).submit(resp.getWriter(), req);
36         } catch (GigiApiException e) {
37             e.format(resp.getWriter(), getLanguage(req));
38         }
39         resp.sendRedirect(PATH);
40     }
41
42     private static final int PAGE_LEN = 30;
43
44     @Override
45     public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
46         String path = req.getPathInfo();
47         if (path != null && path.length() > PATH.length() + 1) {
48             int id = Integer.parseInt(path.substring(1 + PATH.length()));
49             User u = User.getById(id);
50             if (u == null || !u.isInGroup(TTP_APPLICANT)) {
51                 SprintfCommand command = new SprintfCommand("The TTP-request is not available anymore. You might want to go {0}back{1}.", Arrays.asList("!'<a href=\"" + PATH + "\">", "!'</a>"));
52                 req.setAttribute(PageNotFound.MESSAGE_ATTRIBUTE, command);
53                 resp.sendError(404);
54                 return;
55             }
56             new TTPAdminForm(req, u).output(resp.getWriter(), getLanguage(req), new HashMap<String, Object>());
57             return;
58         }
59         int offset = 0;
60         String offsetS = req.getParameter("offset");
61         if (offsetS != null) {
62             offset = Integer.parseInt(offsetS);
63         }
64
65         final User[] users = TTP_APPLICANT.getMembers(offset, PAGE_LEN + 1);
66         HashMap<String, Object> vars = new HashMap<>();
67         vars.put("users", new IterableDataset() {
68
69             int i = 0;
70
71             @Override
72             public boolean next(Language l, Map<String, Object> vars) {
73                 if (i >= Math.min(PAGE_LEN, users.length)) {
74                     return false;
75                 }
76                 vars.put("id", Integer.toString(users[i].getId()));
77                 vars.put("name", users[i].getName().toString());
78                 vars.put("email", users[i].getEmail());
79
80                 i++;
81                 return true;
82             }
83         });
84         if (users.length == PAGE_LEN + 1) {
85             vars.put("next", Integer.toString(offset + 30));
86         }
87         getDefaultTemplate().output(resp.getWriter(), getLanguage(req), vars);
88     }
89
90     @Override
91     public boolean isPermitted(AuthorizationContext ac) {
92         return ac != null && ac.isInGroup(Group.getByString("ttp-assurer"));
93     }
94 }