Merge "add: ensure that for TTP Agent actions certificate login is used"
[gigi.git] / src / club / wpia / gigi / pages / admin / TTPAdminPage.java
1 package club.wpia.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 club.wpia.gigi.dbObjects.Group;
12 import club.wpia.gigi.dbObjects.User;
13 import club.wpia.gigi.localisation.Language;
14 import club.wpia.gigi.output.template.Form;
15 import club.wpia.gigi.output.template.IterableDataset;
16 import club.wpia.gigi.output.template.SprintfCommand;
17 import club.wpia.gigi.pages.Page;
18 import club.wpia.gigi.pages.error.PageNotFound;
19 import club.wpia.gigi.util.AuthorizationContext;
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.TTP_APPLICANT;
26
27     public TTPAdminPage() {
28         super("TTP-Admin");
29     }
30
31     @Override
32     public boolean beforePost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
33         return Form.getForm(req, TTPAdminForm.class).submitExceptionProtected(req, resp);
34     }
35
36     @Override
37     public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
38         if (Form.printFormErrors(req, resp.getWriter())) {
39             Form.getForm(req, TTPAdminForm.class).output(resp.getWriter(), getLanguage(req), new HashMap<String, Object>());
40         }
41     }
42
43     private static final int PAGE_LEN = 30;
44
45     @Override
46     public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
47         String path = req.getPathInfo();
48         if (path != null && path.length() > PATH.length() + 1) {
49             int id = Integer.parseInt(path.substring(1 + PATH.length()));
50             User u = User.getById(id);
51             if (u == null || !u.isInGroup(TTP_APPLICANT)) {
52                 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>"));
53                 req.setAttribute(PageNotFound.MESSAGE_ATTRIBUTE, command);
54                 resp.sendError(404);
55                 return;
56             }
57             new TTPAdminForm(req, u).output(resp.getWriter(), getLanguage(req), new HashMap<String, Object>());
58             return;
59         }
60         int offset = 0;
61         String offsetS = req.getParameter("offset");
62         if (offsetS != null) {
63             offset = Integer.parseInt(offsetS);
64         }
65
66         final User[] users = TTP_APPLICANT.getMembers(offset, PAGE_LEN + 1);
67         HashMap<String, Object> vars = new HashMap<>();
68         vars.put("users", new IterableDataset() {
69
70             int i = 0;
71
72             @Override
73             public boolean next(Language l, Map<String, Object> vars) {
74                 if (i >= Math.min(PAGE_LEN, users.length)) {
75                     return false;
76                 }
77                 vars.put("id", Integer.toString(users[i].getId()));
78                 vars.put("name", users[i].getPreferredName().toString());
79                 vars.put("email", users[i].getEmail());
80
81                 i++;
82                 return true;
83             }
84         });
85         if (users.length == PAGE_LEN + 1) {
86             vars.put("next", Integer.toString(offset + 30));
87         }
88         getDefaultTemplate().output(resp.getWriter(), getLanguage(req), vars);
89     }
90
91     @Override
92     public boolean isPermitted(AuthorizationContext ac) {
93         return ac != null && ac.isInGroup(Group.TTP_AGENT) && ac.isStronglyAuthenticated();
94     }
95 }