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