1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.core.api.config.property.ConfigContext;
20 import org.kuali.rice.kim.api.identity.Person;
21 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
22 import org.kuali.rice.krad.util.SessionTicket;
23
24 import java.io.Serializable;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.concurrent.ConcurrentHashMap;
30 import java.util.concurrent.atomic.AtomicInteger;
31
32
33
34
35
36
37 public class UserSession implements Serializable {
38 private static final long serialVersionUID = 4532616762540067557L;
39
40 private static final Object NULL_VALUE = new Object();
41
42 private Person person;
43 private Person backdoorUser;
44 private AtomicInteger nextObjectKey;
45 private ConcurrentHashMap<String, Object> objectMap;
46 private String kualiSessionId;
47
48
49
50
51
52 public String getKualiSessionId() {
53 return this.kualiSessionId;
54 }
55
56
57
58
59
60 public void setKualiSessionId(String kualiSessionId) {
61 this.kualiSessionId = kualiSessionId;
62 }
63
64
65
66
67
68
69
70 public UserSession(String principalName) {
71 initPerson(principalName);
72 this.nextObjectKey = new AtomicInteger(0);
73 this.objectMap = new ConcurrentHashMap<String, Object>();
74 }
75
76
77
78
79
80 protected void initPerson(String principalName) {
81 this.person = KimApiServiceLocator.getPersonService().getPersonByPrincipalName(principalName);
82 if (this.person == null) {
83 throw new IllegalArgumentException(
84 "Failed to locate a principal with principal name '" + principalName + "'");
85 }
86 }
87
88
89
90
91
92 public String getPrincipalId() {
93 if (backdoorUser != null) {
94 return backdoorUser.getPrincipalId();
95 }
96 return person.getPrincipalId();
97 }
98
99
100
101
102
103 public String getPrincipalName() {
104 if (backdoorUser != null) {
105 return backdoorUser.getPrincipalName();
106 }
107 return person.getPrincipalName();
108 }
109
110
111
112
113
114
115
116 public String getLoggedInUserPrincipalName() {
117 if (person != null) {
118 return person.getPrincipalName();
119 }
120 return "";
121 }
122
123
124
125
126
127
128
129 public String getLoggedInUserPrincipalId() {
130 if (person != null) {
131 return person.getPrincipalId();
132 }
133 return "";
134 }
135
136
137
138
139
140 public Person getPerson() {
141 if (backdoorUser != null) {
142 return backdoorUser;
143 }
144 return person;
145 }
146
147
148
149
150
151 public Person getActualPerson() {
152 return person;
153 }
154
155
156
157
158
159
160
161 public void setBackdoorUser(String principalName) {
162
163 if (!isProductionEnvironment()) {
164 this.backdoorUser = KimApiServiceLocator.getPersonService().getPersonByPrincipalName(principalName);
165 }
166 }
167
168
169
170
171
172
173 public boolean isProductionEnvironment() {
174 return ConfigContext.getCurrentContextConfig().isProductionEnvironment();
175 }
176
177 public String getCurrentEnvironment() {
178 return ConfigContext.getCurrentContextConfig().getEnvironment();
179 }
180
181 public boolean isDisplayTestBanner() {
182 boolean isProd = this.isProductionEnvironment();
183 boolean isBannerEnabled = ConfigContext.getCurrentContextConfig().getBooleanProperty("test.banner.enabled", false);
184 return !isProd && isBannerEnabled;
185 }
186
187
188
189
190 public void clearBackdoorUser() {
191 this.backdoorUser = null;
192 }
193
194
195
196
197
198
199
200
201
202
203
204 public String addObjectWithGeneratedKey(Serializable object, String keyPrefix) {
205 String objectKey = keyPrefix + nextObjectKey.incrementAndGet();
206 addObject(objectKey, object);
207 return objectKey;
208 }
209
210
211
212
213
214
215
216
217
218
219 public String addObjectWithGeneratedKey(Object object) {
220 String objectKey = nextObjectKey.incrementAndGet() + "";
221 addObject(objectKey, object);
222 return objectKey;
223 }
224
225
226
227
228
229
230
231
232 public void addObject(String key, Object object) {
233 if (object != null) {
234 objectMap.put(key, object);
235 } else {
236 objectMap.put(key, NULL_VALUE);
237 }
238 }
239
240
241
242
243
244
245
246
247
248
249 public void addObjectIfAbsent(String key, Object object) {
250 if (object != null) {
251 objectMap.putIfAbsent(key, object);
252 } else {
253 objectMap.putIfAbsent(key, NULL_VALUE);
254 }
255 }
256
257
258
259
260
261
262
263
264
265 public Object retrieveObject(String objectKey) {
266 if (objectKey == null) {
267 return null;
268 }
269 Object object = objectMap.get(objectKey);
270
271 if (!NULL_VALUE.equals(object)) {
272 return object;
273 } else {
274 return null;
275 }
276 }
277
278
279
280
281
282
283
284
285 public void removeObject(String objectKey) {
286 if (objectKey != null) {
287 this.objectMap.remove(objectKey);
288 }
289 }
290
291
292
293
294
295
296 public void removeObjectsByPrefix(String objectKeyPrefix) {
297 synchronized (objectMap) {
298 List<String> removeKeys = new ArrayList<String>();
299 for (String key : objectMap.keySet()) {
300 if (key.startsWith(objectKeyPrefix)) {
301 removeKeys.add(key);
302 }
303 }
304
305 for (String key : removeKeys) {
306 this.objectMap.remove(key);
307 }
308 }
309 }
310
311
312
313
314 public boolean isBackdoorInUse() {
315 return backdoorUser != null;
316 }
317
318
319
320
321
322
323
324 public String putSessionTicket(SessionTicket ticket) {
325 return addObjectWithGeneratedKey(ticket);
326 }
327
328
329
330
331
332
333 public List<SessionTicket> getAllSessionTickets() {
334 List<SessionTicket> sessionTickets = new ArrayList<SessionTicket>();
335
336 synchronized (objectMap) {
337 for (Object object : objectMap.values()) {
338 if (object instanceof SessionTicket) {
339 sessionTickets.add((SessionTicket) object);
340 }
341 }
342 }
343
344 return sessionTickets;
345 }
346
347
348
349
350
351
352 public List<SessionTicket> getAllSessionTicketsByType(String ticketTypeName) {
353 List<SessionTicket> sessionTickets = new ArrayList<SessionTicket>();
354
355 for (SessionTicket ticket : getAllSessionTickets()) {
356 if (StringUtils.equalsIgnoreCase(ticket.getTicketTypeName(), ticketTypeName)) {
357 sessionTickets.add(ticket);
358 }
359 }
360
361 return sessionTickets;
362 }
363
364
365
366
367
368
369
370
371
372
373 public boolean hasMatchingSessionTicket(String ticketTypeName, Map<String, String> matchContext) {
374 boolean hasTicket = false;
375
376 for (SessionTicket ticket : getAllSessionTicketsByType(ticketTypeName)) {
377 Map<String, String> ticketContext = ticket.getTicketContext();
378
379 boolean keySetMatch = ticketContext.keySet().equals(matchContext.keySet());
380 if (keySetMatch) {
381 boolean valuesMatch = true;
382 for (String contextKey : ticketContext.keySet()) {
383 String ticketValue = ticketContext.get(contextKey);
384 String matchValue = matchContext.get(contextKey);
385 if (!StringUtils.equalsIgnoreCase(ticketValue, matchValue)) {
386 valuesMatch = false;
387 }
388 }
389
390 if (valuesMatch) {
391 hasTicket = true;
392 break;
393 }
394 }
395 }
396
397 return hasTicket;
398 }
399
400
401
402
403 public Map<String, Object> getObjectMap() {
404 return Collections.unmodifiableMap(this.objectMap);
405 }
406
407
408
409
410 public void clearObjectMap() {
411 this.objectMap = new ConcurrentHashMap<String, Object>();
412 }
413 }