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