SessionHandler.java
4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package fi.insomnia.bortal.handler;
import java.util.TimeZone;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fi.insomnia.bortal.beans.EventBeanLocal;
import fi.insomnia.bortal.beans.RoleBeanLocal;
import fi.insomnia.bortal.beans.UserBeanLocal;
import fi.insomnia.bortal.enums.Permission;
import fi.insomnia.bortal.enums.RolePermission;
import fi.insomnia.bortal.model.User;
import fi.insomnia.bortal.web.converter.RoleConverter;
/**
*
* @author tuukka
*/
@ManagedBean(name = "sessionHandler")
@SessionScoped
public class SessionHandler {
private static final Logger logger = LoggerFactory.getLogger(SessionHandler.class);
@EJB
private RoleBeanLocal rolebean;
@EJB
private EventBeanLocal eventbean;
@EJB
private UserBeanLocal userbean;
/** Creates a new instance of SessionHandler */
public SessionHandler() {
}
public TimeZone getTimezone() {
return TimeZone.getTimeZone("Europe/Helsinki");
}
public String getLocale() {
// TODO: Locale selection code missing
// return "en_ST_v7";
return "en_ST_v7";
}
public String getLayout() {
// TODO: layout selection code missing!!
return "stream1";
}
public boolean hasPermission(String target, String permission) {
RolePermission perm = RolePermission.valueOf(permission.toUpperCase());
// RolePermission perm = null;
// if (permission.equalsIgnoreCase("read")) {
// perm = RolePermission.READ;
// } else if (permission.equals("write")) {
// perm = RolePermission.WRITE;
// } else if (permission.equals("execute")) {
// perm = RolePermission.EXECUTE;
// }else {
// throw new RuntimeException("permission " + permission +
// " does not match any")
// }
if (perm == null) {
logger.warn("Permission {} does not have matching value in RolePermission enum!");
throw new RuntimeException("Matching role permission could not be found!");
}
return hasPermission(target, perm);
}
public void fatalPermission(String target, String permission) {
fatalPermission(Permission.getPermission(target), RolePermission.valueOf(permission));
}
public void fatalPermission(Permission target, RolePermission permission) {
userbean.fatalPermission(target, permission, new Object[] {});
}
//
// private HttpSession getHttpSession() {
// FacesContext ctx = FacesContext.getCurrentInstance();
// HttpSession sess = (HttpSession)
// ctx.getExternalContext().getSession(false);
// return sess;
// }
public boolean hasPermission(Permission target, RolePermission permission) {
if (target == null || permission == null) {
logger.warn("Target {} or permission {} is null", target, permission);
throw new RuntimeException("Empty target or permission!");
}
boolean ret = userbean.hasPermission(target, permission);
return ret;
}
public boolean hasPermission(String target, RolePermission permission) {
return hasPermission(Permission.getPermission(target), permission);
}
public boolean canWrite(String target) {
return hasPermission(target, RolePermission.WRITE);
}
public boolean canRead(String target) {
return hasPermission(target, RolePermission.READ);
}
public boolean canExecute(String target) {
return hasPermission(target, RolePermission.EXECUTE);
}
public String logout() {
FacesContext ctx = FacesContext.getCurrentInstance();
HttpSession sess = (HttpSession) ctx.getExternalContext().getSession(false);
if (sess != null) {
sess.invalidate();
}
return "logout";
}
public boolean isLoggedIn() {
boolean ret = userbean.isLoggedIn();
return ret;
}
public boolean isSuperadmin() {
return userbean.getCurrentUser().isSuperadmin();
}
public String getLoginname() {
return userbean.getCurrentUser().getLogin();
}
public User getCurrentUser() {
return userbean.getCurrentUser();
}
public RoleConverter getRoleConverter() {
return new RoleConverter(rolebean, eventbean.getCurrentEvent());
}
public String flushCache() {
return eventbean.flushCache();
}
}