add: ensure that for Org Administrator actions certificate login is used
[gigi.git] / src / club / wpia / gigi / pages / MainPage.java
1 package club.wpia.gigi.pages;
2
3 import java.io.IOException;
4 import java.util.Iterator;
5 import java.util.List;
6 import java.util.Map;
7 import java.util.Set;
8
9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11
12 import club.wpia.gigi.dbObjects.Certificate;
13 import club.wpia.gigi.dbObjects.Domain;
14 import club.wpia.gigi.dbObjects.EmailAddress;
15 import club.wpia.gigi.dbObjects.Group;
16 import club.wpia.gigi.dbObjects.Organisation;
17 import club.wpia.gigi.dbObjects.User;
18 import club.wpia.gigi.localisation.Language;
19 import club.wpia.gigi.output.GroupList;
20 import club.wpia.gigi.output.template.IterableDataset;
21 import club.wpia.gigi.output.template.Template;
22
23 public class MainPage extends Page {
24
25     private static final Template notLog = new Template(MainPage.class.getResource("MainPageNotLogin.templ"));
26
27     public MainPage() {
28         super("Home");
29     }
30
31     @Override
32     public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
33         Map<String, Object> vars = getDefaultVars(req);
34         if (LoginPage.getUser(req) != null) {
35             User u = LoginPage.getUser(req);
36             vars.put("username", u.getPreferredName());
37             final Set<Group> gr = u.getGroups();
38             vars.put("support-groups", new GroupList(gr, true));
39             vars.put("groups", new GroupList(gr, false));
40             vars.put("ra-agent", u.canVerify());
41             vars.put("vp", u.getVerificationPoints());
42             vars.put("xp", u.getExperiencePoints());
43
44             Certificate[] c = u.getCertificates(false);
45             vars.put("c-no", c.length);
46
47             final EmailAddress[] emails = u.getEmails();
48             IterableDataset ds = new IterableDataset() {
49
50                 private int point = 0;
51
52                 @Override
53                 public boolean next(Language l, Map<String, Object> vars) {
54                     if (point >= emails.length) {
55                         return false;
56                     }
57                     EmailAddress emailAddress = emails[point];
58                     vars.put("verification", l.getTranslation(emailAddress.isVerified() ? "Verified" : "Unverified"));
59                     vars.put("last_verification", emailAddress.getLastPing(true));
60                     vars.put("address", emailAddress.getAddress());
61                     point++;
62                     return true;
63                 }
64             };
65             vars.put("emails", ds);
66
67             final Domain[] doms = u.getDomains();
68             IterableDataset dts = new IterableDataset() {
69
70                 private int point = 0;
71
72                 @Override
73                 public boolean next(Language l, Map<String, Object> vars) {
74                     if (point >= doms.length) {
75                         return false;
76                     }
77                     Domain domain = doms[point];
78                     vars.put("domain", domain.getSuffix());
79                     vars.put("status", l.getTranslation(domain.isVerified() ? "Verified" : "Unverified"));
80                     point++;
81                     return true;
82                 }
83             };
84             vars.put("domains", dts);
85             vars.put("nodomains", doms.length == 0);
86
87             final List<Organisation> o = u.getOrganisations();
88             vars.put("orgas", new IterableDataset() {
89
90                 Iterator<Organisation> it = o.iterator();
91
92                 @Override
93                 public boolean next(Language l, Map<String, Object> vars) {
94                     if ( !it.hasNext()) {
95                         return false;
96                     }
97                     Organisation o = it.next();
98                     vars.put("orgName", o.getName());
99                     vars.put("orgID", o.getId());
100                     return true;
101                 }
102             });
103             vars.put("hasorgs", !o.isEmpty());
104
105             if (u.isInGroup(Group.SUPPORTER) || u.isInGroup(Group.ORG_AGENT) || u.isInGroup(Group.TTP_AGENT) || u.canVerify() || !o.isEmpty()) {
106                 vars.put("certlogin", LoginPage.getAuthorizationContext(req).isStronglyAuthenticated());
107                 vars.put("certlogininfo", true);
108             } else {
109                 vars.put("certlogininfo", false);
110             }
111
112             getDefaultTemplate().output(resp.getWriter(), getLanguage(req), vars);
113
114         } else {
115             notLog.output(resp.getWriter(), getLanguage(req), vars);
116         }
117     }
118
119     @Override
120     public boolean needsLogin() {
121         return false;
122     }
123 }