Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
UserSession |
|
| 2.260869565217391;2.261 |
1 | /* | |
2 | * Copyright 2005-2007 The Kuali Foundation | |
3 | * | |
4 | * Licensed under the Educational Community License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.opensource.org/licenses/ecl2.php | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | package org.kuali.rice.kns; | |
17 | ||
18 | import java.io.Serializable; | |
19 | import java.util.ArrayList; | |
20 | import java.util.HashMap; | |
21 | import java.util.Iterator; | |
22 | import java.util.List; | |
23 | import java.util.Map; | |
24 | ||
25 | import org.apache.commons.lang.StringUtils; | |
26 | import org.kuali.rice.kew.exception.WorkflowException; | |
27 | import org.kuali.rice.kim.bo.Person; | |
28 | import org.kuali.rice.kns.service.KNSServiceLocator; | |
29 | import org.kuali.rice.kns.util.SessionTicket; | |
30 | import org.kuali.rice.kns.web.EditablePropertiesHistoryHolder; | |
31 | import org.kuali.rice.kns.workflow.service.KualiWorkflowDocument; | |
32 | ||
33 | ||
34 | /** | |
35 | * Holds info about the User Session | |
36 | */ | |
37 | public class UserSession implements Serializable { | |
38 | ||
39 | private static final long serialVersionUID = 4532616762540067557L; | |
40 | ||
41 | private Person person; | |
42 | private Person backdoorUser; | |
43 | private int nextObjectKey; | |
44 | private Map<String,Object> objectMap; | |
45 | private String kualiSessionId; | |
46 | private EditablePropertiesHistoryHolder editablePropertiesHistoryHolder; | |
47 | ||
48 | /** | |
49 | * @return the kualiSessionId | |
50 | */ | |
51 | public String getKualiSessionId() { | |
52 | 0 | return this.kualiSessionId; |
53 | } | |
54 | ||
55 | /** | |
56 | * @param kualiSessionId the kualiSessionId to set | |
57 | */ | |
58 | public void setKualiSessionId(String kualiSessionId) { | |
59 | 0 | this.kualiSessionId = kualiSessionId; |
60 | 0 | } |
61 | ||
62 | // KULRICE-4443 Allow workflow sessions to fail over between nodes to prevent document loss by removing transient modifier | |
63 | 0 | private Map<String,KualiWorkflowDocument> workflowDocMap = new HashMap<String,KualiWorkflowDocument>(); |
64 | ||
65 | /** | |
66 | * Take in a netid, and construct the user from that. | |
67 | * | |
68 | * @param principalName | |
69 | */ | |
70 | 0 | public UserSession(String principalName) { |
71 | 0 | this.person = org.kuali.rice.kim.service.KIMServiceLocator.getPersonService().getPersonByPrincipalName(principalName); |
72 | 0 | if (this.person == null) { |
73 | 0 | throw new IllegalArgumentException("Failed to locate a principal with principal name '" + principalName + "'"); |
74 | } | |
75 | 0 | this.nextObjectKey = 0; |
76 | 0 | this.objectMap = new HashMap<String,Object>(); |
77 | 0 | this.editablePropertiesHistoryHolder = new EditablePropertiesHistoryHolder(); |
78 | 0 | } |
79 | ||
80 | ||
81 | /** | |
82 | * @return the principalId of the current user in the system, backdoor principalId if backdoor is set | |
83 | */ | |
84 | public String getPrincipalId() { | |
85 | 0 | if (backdoorUser != null) { |
86 | 0 | return backdoorUser.getPrincipalId(); |
87 | } | |
88 | else { | |
89 | 0 | return person.getPrincipalId(); |
90 | } | |
91 | } | |
92 | ||
93 | /** | |
94 | * @return the principalName of the current user in the system, backdoor principalName if backdoor is set | |
95 | */ | |
96 | public String getPrincipalName() { | |
97 | 0 | if (backdoorUser != null) { |
98 | 0 | return backdoorUser.getPrincipalName(); |
99 | } | |
100 | else { | |
101 | 0 | return person.getPrincipalName(); |
102 | } | |
103 | } | |
104 | ||
105 | ||
106 | /** | |
107 | * This returns who is logged in. If the backdoor is in use, this will return the network id of the person that is standing in | |
108 | * as the backdoor user. | |
109 | * | |
110 | * @return String | |
111 | */ | |
112 | public String getLoggedInUserPrincipalName() { | |
113 | 0 | if ( person != null ) { |
114 | 0 | return person.getPrincipalName(); |
115 | } else { | |
116 | 0 | return ""; |
117 | } | |
118 | } | |
119 | ||
120 | /** | |
121 | * @return the KualiUser which is the current user in the system, backdoor if backdoor is set | |
122 | */ | |
123 | public Person getPerson() { | |
124 | 0 | if (backdoorUser != null) { |
125 | 0 | return backdoorUser; |
126 | } | |
127 | else { | |
128 | 0 | return person; |
129 | } | |
130 | } | |
131 | ||
132 | /** | |
133 | * override the current user in the system by setting the backdoor networkId, which is useful when dealing with routing or other | |
134 | * reasons why you would need to assume an identity in the system | |
135 | * | |
136 | * @param principalName | |
137 | */ | |
138 | public void setBackdoorUser(String principalName) { | |
139 | // only allow backdoor in non-production environments | |
140 | 0 | if ( !KNSServiceLocator.getKualiConfigurationService().isProductionEnvironment() ) { |
141 | 0 | this.backdoorUser = org.kuali.rice.kim.service.KIMServiceLocator.getPersonService().getPersonByPrincipalName(principalName); |
142 | 0 | this.workflowDocMap = new HashMap<String,KualiWorkflowDocument>(); |
143 | } | |
144 | 0 | } |
145 | ||
146 | /** | |
147 | * clear the backdoor user | |
148 | * | |
149 | */ | |
150 | public void clearBackdoorUser() { | |
151 | 0 | this.backdoorUser = null; |
152 | 0 | this.workflowDocMap = new HashMap<String,KualiWorkflowDocument>(); |
153 | 0 | } |
154 | ||
155 | /** | |
156 | * allows adding an arbitrary object to the session and returns a string key that can be used to later access this object from | |
157 | * the session using the retrieveObject method in this class. This allows for a prefix to be placed in front of the | |
158 | * incremented key. So if the prefix is "searchResults" and the nextObjectKey (local int that holds the key value) is 2 then | |
159 | * the new key will be "searchResults3". "searchResults3" will be returned from the method. | |
160 | * | |
161 | * @param object | |
162 | */ | |
163 | public String addObject(Object object, String keyPrefix) { | |
164 | 0 | String objectKey = keyPrefix + nextObjectKey++; |
165 | 0 | objectMap.put(objectKey, object); |
166 | 0 | return objectKey; |
167 | } | |
168 | ||
169 | /** | |
170 | * allows adding an arbitrary object to the session with static a string key that can be used to later access this object from | |
171 | * the session using the retrieveObject method in this class | |
172 | * | |
173 | * @param object | |
174 | * | |
175 | */ | |
176 | public void addObject(String key, Object object) { | |
177 | ||
178 | 0 | objectMap.put(key, object); |
179 | ||
180 | 0 | } |
181 | ||
182 | ||
183 | /** | |
184 | * allows adding an arbitrary object to the session and returns a string key that can be used to later access this object from | |
185 | * the session using the retrieveObject method in this class. The key is generated from an integer and incremented for every | |
186 | * object added. So the first object added with have a key of "1". This key will be returned from the method. | |
187 | * | |
188 | * @param object | |
189 | */ | |
190 | public String addObject(Object object) { | |
191 | 0 | String objectKey = nextObjectKey++ + ""; |
192 | 0 | objectMap.put(objectKey, object); |
193 | 0 | return objectKey; |
194 | } | |
195 | ||
196 | /** | |
197 | * allows for fetching an object that has been put into the userSession based on the key that would have been returned when | |
198 | * adding the object | |
199 | * | |
200 | * @param objectKey | |
201 | */ | |
202 | public Object retrieveObject(String objectKey) { | |
203 | 0 | return this.objectMap.get(objectKey); |
204 | } | |
205 | ||
206 | /** | |
207 | * allows for removal of an object from session that has been put into the userSession based on the key that would have been | |
208 | * assigned | |
209 | * | |
210 | * @param objectKey | |
211 | */ | |
212 | public void removeObject(String objectKey) { | |
213 | 0 | this.objectMap.remove(objectKey); |
214 | 0 | } |
215 | ||
216 | /** | |
217 | * allows for removal of an object from session that has been put into the userSession based on a key that starts with the given | |
218 | * prefix | |
219 | */ | |
220 | public void removeObjectsByPrefix(String objectKeyPrefix) { | |
221 | 0 | List<String> removeKeys = new ArrayList<String>(); |
222 | 0 | for (Iterator<String> iter = objectMap.keySet().iterator(); iter.hasNext();) { |
223 | 0 | String key = iter.next(); |
224 | 0 | if (key.startsWith(objectKeyPrefix)) { |
225 | 0 | removeKeys.add(key); |
226 | } | |
227 | 0 | } |
228 | ||
229 | 0 | for (Iterator<String> iter = removeKeys.iterator(); iter.hasNext();) { |
230 | 0 | String key = iter.next(); |
231 | 0 | this.objectMap.remove(key); |
232 | 0 | } |
233 | 0 | } |
234 | ||
235 | /** | |
236 | * @return boolean indicating if the backdoor is in use | |
237 | */ | |
238 | public boolean isBackdoorInUse() { | |
239 | 0 | return backdoorUser != null; |
240 | } | |
241 | ||
242 | /** | |
243 | * retrieve a flexdoc from the userSession based on the document id | |
244 | * | |
245 | * @param docId | |
246 | */ | |
247 | public KualiWorkflowDocument getWorkflowDocument(String docId) { | |
248 | 0 | if (workflowDocMap == null) { |
249 | 0 | workflowDocMap = new HashMap<String,KualiWorkflowDocument>(); |
250 | } | |
251 | 0 | if (workflowDocMap.containsKey(docId)) { |
252 | 0 | return workflowDocMap.get(docId); |
253 | } | |
254 | else { | |
255 | 0 | return null; |
256 | } | |
257 | } | |
258 | ||
259 | /** | |
260 | * set a flexDoc into the userSession which will be stored under the document id | |
261 | */ | |
262 | public void setWorkflowDocument(KualiWorkflowDocument workflowDocument) { | |
263 | try { | |
264 | 0 | if (workflowDocMap == null) { |
265 | 0 | workflowDocMap = new HashMap<String,KualiWorkflowDocument>(); |
266 | } | |
267 | 0 | workflowDocMap.put(workflowDocument.getRouteHeaderId().toString(), workflowDocument); |
268 | } | |
269 | 0 | catch (WorkflowException e) { |
270 | 0 | throw new IllegalStateException("could not save the document in the session msg: " + e.getMessage()); |
271 | 0 | } |
272 | 0 | } |
273 | ||
274 | /** | |
275 | * @return this session's editable properties history holder | |
276 | */ | |
277 | public EditablePropertiesHistoryHolder getEditablePropertiesHistoryHolder() { | |
278 | 0 | return editablePropertiesHistoryHolder; |
279 | } | |
280 | ||
281 | /** | |
282 | * Adds the given SessionTicket to the objectMap and returns the associated key | |
283 | * | |
284 | * @param ticket | |
285 | * - SessionTicket to add | |
286 | * @return the objectMap key for the ticket as a String | |
287 | */ | |
288 | public String putSessionTicket(SessionTicket ticket) { | |
289 | 0 | return addObject(ticket); |
290 | } | |
291 | ||
292 | /** | |
293 | * Retrieves all SessionTicket instances currently in the UserSession#objectMap | |
294 | * | |
295 | * @return List<SessionTicket> contained in user session | |
296 | */ | |
297 | public List<SessionTicket> getAllSessionTickets() { | |
298 | 0 | List<SessionTicket> sessionTickets = new ArrayList<SessionTicket>(); |
299 | ||
300 | 0 | for (Object object : objectMap.values()) { |
301 | 0 | if (object instanceof SessionTicket) { |
302 | 0 | sessionTickets.add((SessionTicket) object); |
303 | } | |
304 | } | |
305 | ||
306 | 0 | return sessionTickets; |
307 | } | |
308 | ||
309 | /** | |
310 | * Retrieves all SessionTicket instances currently in the UserSession#objectMap that are of a given ticket type | |
311 | * | |
312 | * @return List<SessionTicket> contained in user session | |
313 | */ | |
314 | public List<SessionTicket> getAllSessionTicketsByType(String ticketTypeName) { | |
315 | 0 | List<SessionTicket> sessionTickets = new ArrayList<SessionTicket>(); |
316 | ||
317 | 0 | for (SessionTicket ticket : getAllSessionTickets()) { |
318 | 0 | if (StringUtils.equalsIgnoreCase(ticket.getTicketTypeName(), ticketTypeName)) { |
319 | 0 | sessionTickets.add(ticket); |
320 | } | |
321 | } | |
322 | ||
323 | 0 | return sessionTickets; |
324 | } | |
325 | ||
326 | /** | |
327 | * Determines if the UserSession contains a ticket of the given type that matches the given context. To match context the ticket must | |
328 | * contain all the same keys at the given context and the values must be equal with the exception of case | |
329 | * | |
330 | * @param ticketTypeName | |
331 | * - Name of the ticket type to match | |
332 | * @param matchContext | |
333 | * - Map on context parameters to match on | |
334 | * @return true if a ticket was found in the UserSession that matches the request, false if one was not found | |
335 | */ | |
336 | public boolean hasMatchingSessionTicket(String ticketTypeName, Map<String, String> matchContext) { | |
337 | 0 | boolean hasTicket = false; |
338 | ||
339 | 0 | for (SessionTicket ticket : getAllSessionTicketsByType(ticketTypeName)) { |
340 | 0 | Map<String, String> ticketContext = ticket.getTicketContext(); |
341 | ||
342 | 0 | boolean keySetMatch = ticketContext.keySet().equals(matchContext.keySet()); |
343 | 0 | if (keySetMatch) { |
344 | 0 | boolean valuesMatch = true; |
345 | 0 | for (String contextKey : ticketContext.keySet()) { |
346 | 0 | String ticketValue = ticketContext.get(contextKey); |
347 | 0 | String matchValue = matchContext.get(contextKey); |
348 | 0 | if (!StringUtils.equalsIgnoreCase(ticketValue, matchValue)) { |
349 | 0 | valuesMatch = false; |
350 | } | |
351 | 0 | } |
352 | ||
353 | 0 | if (valuesMatch) { |
354 | 0 | hasTicket = true; |
355 | 0 | break; |
356 | } | |
357 | } | |
358 | 0 | } |
359 | ||
360 | 0 | return hasTicket; |
361 | } | |
362 | } |