1 package org.cacert.gigi.pages.admin;
3 import java.io.IOException;
4 import java.util.Arrays;
5 import java.util.HashMap;
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpServletResponse;
11 import org.cacert.gigi.dbObjects.Group;
12 import org.cacert.gigi.dbObjects.User;
13 import org.cacert.gigi.localisation.Language;
14 import org.cacert.gigi.output.template.Form;
15 import org.cacert.gigi.output.template.IterableDataset;
16 import org.cacert.gigi.output.template.SprintfCommand;
17 import org.cacert.gigi.pages.Page;
18 import org.cacert.gigi.pages.error.PageNotFound;
19 import org.cacert.gigi.util.AuthorizationContext;
21 public class TTPAdminPage extends Page {
23 public static final String PATH = "/admin/ttp";
25 public static final Group TTP_APPLICANT = Group.getByString("ttp-applicant");
27 public TTPAdminPage() {
32 public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
33 if (Form.getForm(req, TTPAdminForm.class).submitProtected(resp.getWriter(), req)) {
34 resp.sendRedirect(PATH);
38 private static final int PAGE_LEN = 30;
41 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
42 String path = req.getPathInfo();
43 if (path != null && path.length() > PATH.length() + 1) {
44 int id = Integer.parseInt(path.substring(1 + PATH.length()));
45 User u = User.getById(id);
46 if (u == null || !u.isInGroup(TTP_APPLICANT)) {
47 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>"));
48 req.setAttribute(PageNotFound.MESSAGE_ATTRIBUTE, command);
52 new TTPAdminForm(req, u).output(resp.getWriter(), getLanguage(req), new HashMap<String, Object>());
56 String offsetS = req.getParameter("offset");
57 if (offsetS != null) {
58 offset = Integer.parseInt(offsetS);
61 final User[] users = TTP_APPLICANT.getMembers(offset, PAGE_LEN + 1);
62 HashMap<String, Object> vars = new HashMap<>();
63 vars.put("users", new IterableDataset() {
68 public boolean next(Language l, Map<String, Object> vars) {
69 if (i >= Math.min(PAGE_LEN, users.length)) {
72 vars.put("id", Integer.toString(users[i].getId()));
73 vars.put("name", users[i].getPreferredName().toString());
74 vars.put("email", users[i].getEmail());
80 if (users.length == PAGE_LEN + 1) {
81 vars.put("next", Integer.toString(offset + 30));
83 getDefaultTemplate().output(resp.getWriter(), getLanguage(req), vars);
87 public boolean isPermitted(AuthorizationContext ac) {
88 return ac != null && ac.isInGroup(Group.getByString("ttp-assurer"));