40a63335fe59929f15f3b7c5f8adabb7a844d472
[gigi.git] / src / club / wpia / gigi / util / AuthorizationContext.java
1 package club.wpia.gigi.util;
2
3 import java.io.PrintWriter;
4 import java.io.Serializable;
5 import java.util.Arrays;
6 import java.util.Map;
7
8 import club.wpia.gigi.GigiApiException;
9 import club.wpia.gigi.dbObjects.CertificateOwner;
10 import club.wpia.gigi.dbObjects.Group;
11 import club.wpia.gigi.dbObjects.Organisation;
12 import club.wpia.gigi.dbObjects.User;
13 import club.wpia.gigi.localisation.Language;
14 import club.wpia.gigi.output.template.Outputable;
15 import club.wpia.gigi.output.template.SprintfCommand;
16
17 public class AuthorizationContext implements Outputable, Serializable {
18
19     private static final long serialVersionUID = -2596733469159940154L;
20
21     private CertificateOwner target;
22
23     private User actor;
24
25     private String supporterTicketId;
26
27     public AuthorizationContext(CertificateOwner target, User actor) {
28         this.target = target;
29         this.actor = actor;
30     }
31
32     public AuthorizationContext(User actor, String supporterTicket) throws GigiApiException {
33         this.target = actor;
34         this.actor = actor;
35         if ( !isInGroup(Group.SUPPORTER)) {
36             throw new GigiApiException("requires a supporter");
37         }
38         supporterTicketId = supporterTicket;
39     }
40
41     public CertificateOwner getTarget() {
42         return target;
43     }
44
45     public User getActor() {
46         return actor;
47     }
48
49     public boolean isInGroup(Group g) {
50         return actor.isInGroup(g);
51     }
52
53     public User getActor(AuthorizationContext ac) {
54         if (ac == null) {
55             return null;
56         }
57         return ac.getActor();
58     }
59
60     public String getSupporterTicketId() {
61         return supporterTicketId;
62     }
63
64     public boolean canSupport() {
65         return getSupporterTicketId() != null && isInGroup(Group.SUPPORTER);
66     }
67
68     private static final SprintfCommand sp = new SprintfCommand("Logged in as {0} via {1}.", Arrays.asList("${username", "${loginMethod"));
69
70     private static final SprintfCommand inner = new SprintfCommand("{0} (on behalf of {1})", Arrays.asList("${user", "${target"));
71
72     @Override
73     public void output(PrintWriter out, Language l, Map<String, Object> vars) {
74         out.println("<div>");
75         vars.put("username", new Outputable() {
76
77             @Override
78             public void output(PrintWriter out, Language l, Map<String, Object> vars) {
79                 if (target != actor) {
80                     vars.put("user", ((Organisation) target).getName().toString());
81                     vars.put("target", actor.getPreferredName().toString());
82                     inner.output(out, l, vars);
83                 } else {
84                     out.println(actor.getPreferredName().toString());
85                 }
86             }
87         });
88         sp.output(out, l, vars);
89         out.println("</div>");
90         if (supporterTicketId != null) {
91             out.println("<div>");
92             out.println(l.getTranslation("SupportTicket: "));
93             out.println(HTMLEncoder.encodeHTML(supporterTicketId));
94             out.println("</div>");
95         }
96     }
97
98     public boolean canVerify() {
99         return target instanceof User && ((User) target).canVerify();
100     }
101 }