SessionHandler.java
6.21 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
* Copyright Codecrew Ry
*
* All rights reserved.
*
* This license applies to any software containing a notice placed by the
* copyright holder. Such software is herein referred to as the Software.
* This license covers modification, distribution and use of the Software.
*
* Any distribution and use in source and binary forms, with or without
* modification is not permitted without explicit written permission from the
* copyright owner.
*
* A non-exclusive royalty-free right is granted to the copyright owner of the
* Software to use, modify and distribute all modifications to the Software in
* future versions of the Software.
*
*/
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package fi.codecrew.moyamgmt.handler;
import java.util.EnumMap;
import java.util.Locale;
import java.util.TimeZone;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fi.codecrew.moya.beans.EventBeanLocal;
import fi.codecrew.moya.beans.PermissionBeanLocal;
import fi.codecrew.moya.beans.RoleBeanLocal;
import fi.codecrew.moya.clientutils.BortalLocalContextHolder;
import fi.codecrew.moya.enums.apps.IAppPermission;
import fi.codecrew.moya.model.EventUser;
import fi.codecrew.moya.model.LanEventProperty;
import fi.codecrew.moya.model.LanEventPropertyKey;
/**
*
* @author tuukka
*/
@Named("mgmtSessionHandler")
@RequestScoped
public class SessionHandler {
private static final Logger logger = LoggerFactory.getLogger(SessionHandler.class);
//
// @Inject
// private HttpServletRequest httprequest;
//
@EJB
private RoleBeanLocal rolebean;
@EJB
private EventBeanLocal eventbean;
@EJB
private PermissionBeanLocal permbean;
@Inject
private SessionStore sessionStore;
private String template;
public TimeZone getTimezone() {
return TimeZone.getTimeZone("Europe/Helsinki");
}
public String getLocale() {
Locale ret = sessionStore.getLocale();
String retStr = "fi_FI";
if (ret != null) {
retStr = ret.toLanguageTag();
logger.info("Got langtag {}", retStr);
}
return retStr;
}
public String getFullscreen() {
template = "blipview";
return template;
}
public String getAdduserfullscreen() {
template = "adduser";
return template;
}
public String getTemplateName()
{
if (template == null) {
template = eventbean.getPropertyString(LanEventPropertyKey.EVENT_LAYOUT);
}
if (template == null) {
template = "insomnia2";
}
return template;
}
public String getTemplatePath()
{
return "/resources/templates/" + getTemplateName();
}
public String getTemplate() {
return getTemplatePath() + "/template.xhtml";
}
private EnumMap<LanEventPropertyKey, Boolean> boolPropertyCache = new EnumMap<>(LanEventPropertyKey.class);
public boolean isEventBoolProperty(String property)
{
LanEventPropertyKey prop = LanEventPropertyKey.valueOf(property);
if (!prop.isBoolean()) {
throw new RuntimeException("Trying to fetch boolean value for non-boolean property!");
}
boolean value = false;
if (boolPropertyCache.containsKey(prop)) {
value = boolPropertyCache.get(prop);
} else {
LanEventProperty propertyValue = eventbean.getProperty(prop);
if (propertyValue == null) {
if (prop.getDefaultvalue() != null && prop.getDefaultvalue().equals("1"))
value = true;
} else {
value = propertyValue.isBooleanValue();
}
boolPropertyCache.put(prop, value);
}
return value;
}
// 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);
// }
//
// private HttpSession getHttpSession() {
// FacesContext ctx = FacesContext.getCurrentInstance();
// HttpSession sess = (HttpSession)
// ctx.getExternalContext().getSession(false);
// return sess;
// }
// public boolean hasPermission(String perm) {
// return permbean.hasPermission(perm);
// }
public String getDateFormat()
{
return "dd.MM.yyyy";
}
public String getDatetimeFormat()
{
return "dd.MM.yyyy HH:mm";
}
public String getShortDatetimeFormat()
{
return "dd.MM HH:MM:ss";
}
public boolean hasPermission(IAppPermission permission) {
if (permission == null) {
logger.warn("permission is null");
throw new RuntimeException("Empty target or permission!");
}
boolean ret = permbean.hasPermission(permission);
return ret;
}
public boolean isLoggedIn() {
boolean ret = permbean.isLoggedIn();
return ret;
}
public boolean isSuperadmin() {
return permbean.getCurrentUser().getUser().isSuperadmin();
}
public EventUser getCurrentUser() {
return permbean.getCurrentUser();
}
public String flushCache() {
return eventbean.flushCache();
}
public String navigateTo(String url) {
return url;
}
private String preurlString;
public String getRequestPreUrl()
{
if (preurlString == null)
{
Object ext = FacesContext.getCurrentInstance().getExternalContext().getRequest();
if (ext instanceof HttpServletRequest)
{
StringBuffer url = ((HttpServletRequest) ext).getRequestURL();
preurlString = url.substring(0, url.indexOf("/", 8));
} else {
preurlString = "";
}
}
return preurlString;
}
public boolean isInDevelopmentMode() {
return BortalLocalContextHolder.isInDevelopmentMode();
}
public SessionStore getSessionStore() {
return sessionStore;
}
public void setSessionStore(SessionStore sessionStore) {
this.sessionStore = sessionStore;
}
}