001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali.core;
017:
018: import java.io.Serializable;
019: import java.util.ArrayList;
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Map;
024:
025: import org.apache.log4j.Logger;
026: import org.kuali.core.bo.user.AuthenticationUserId;
027: import org.kuali.core.bo.user.UniversalUser;
028: import org.kuali.core.exceptions.UserNotFoundException;
029: import org.kuali.core.workflow.service.KualiWorkflowDocument;
030: import org.kuali.rice.KNSServiceLocator;
031:
032: import edu.iu.uis.eden.clientapp.vo.NetworkIdVO;
033: import edu.iu.uis.eden.clientapp.vo.UserVO;
034: import edu.iu.uis.eden.exception.EdenUserNotFoundException;
035: import edu.iu.uis.eden.exception.ResourceUnavailableException;
036: import edu.iu.uis.eden.exception.WorkflowException;
037:
038: /**
039: * This class represents a User Session
040: *
041: *
042: */
043: public class UserSession implements Serializable {
044:
045: private static final long serialVersionUID = 4532616762540067557L;
046:
047: private static final Logger LOG = Logger
048: .getLogger(UserSession.class);
049:
050: private UniversalUser universalUser;
051: private UniversalUser backdoorUser;
052: private UserVO workflowUser;
053: private UserVO backdoorWorkflowUser;
054: private int nextObjectKey;
055: private Map objectMap;
056:
057: private transient Map workflowDocMap = new HashMap();
058:
059: /**
060: * Take in a netid, and construct the user from that.
061: *
062: * @param networkId
063: * @throws UserNotFoundException
064: * @throws EdenUserNotFoundException
065: * @throws ResourceUnavailableException
066: */
067: public UserSession(String networkId) throws UserNotFoundException,
068: WorkflowException {
069: this .universalUser = KNSServiceLocator
070: .getUniversalUserService().getUniversalUser(
071: new AuthenticationUserId(networkId));
072: this .workflowUser = KNSServiceLocator.getWorkflowInfoService()
073: .getWorkflowUser(new NetworkIdVO(networkId));
074: this .nextObjectKey = 0;
075: this .objectMap = new HashMap();
076: }
077:
078: /**
079: * @return the networkId of the current user in the system, backdoor network id if backdoor is set
080: */
081: public String getNetworkId() {
082: if (backdoorUser != null) {
083: return backdoorUser.getPersonUserIdentifier();
084: } else {
085: return universalUser.getPersonUserIdentifier();
086: }
087: }
088:
089: /**
090: * 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
091: * as the backdoor user.
092: *
093: * @return String
094: */
095: public String getLoggedInUserNetworkId() {
096: return universalUser.getPersonUserIdentifier();
097: }
098:
099: /**
100: * @return the KualiUser which is the current user in the system, backdoor if backdoor is set
101: */
102: public UniversalUser getUniversalUser() {
103: if (backdoorUser != null) {
104: return backdoorUser;
105: } else {
106: return universalUser;
107: }
108: }
109:
110: /**
111: * @return the workflowUser which is the current user in the system, backdoor if backdoor is set
112: */
113: public UserVO getWorkflowUser() {
114: if (backdoorUser != null) {
115: return backdoorWorkflowUser;
116: } else {
117: return workflowUser;
118: }
119: }
120:
121: /**
122: * override the current user in the system by setting the backdoor networkId, which is useful when dealing with routing or other
123: * reasons why you would need to assume an identity in the system
124: *
125: * @param networkId
126: * @throws UserNotFoundException
127: * @throws ResourceUnavailableException
128: * @throws EdenUserNotFoundException
129: */
130: public void setBackdoorUser(String networkId)
131: throws UserNotFoundException, WorkflowException {
132: // only allow backdoor in non-production environments
133: if (!KNSServiceLocator.getKualiConfigurationService()
134: .isProductionEnvironment()) {
135: this .backdoorUser = KNSServiceLocator
136: .getUniversalUserService().getUniversalUser(
137: new AuthenticationUserId(networkId));
138: this .backdoorWorkflowUser = KNSServiceLocator
139: .getWorkflowInfoService().getWorkflowUser(
140: new NetworkIdVO(networkId));
141: this .workflowDocMap = new HashMap();
142: }
143: }
144:
145: /**
146: * clear the backdoor user
147: *
148: */
149: public void clearBackdoorUser() {
150: this .backdoorUser = null;
151: this .backdoorWorkflowUser = null;
152: this .workflowDocMap = new HashMap();
153: }
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
158: *
159: * @param object
160: * @return
161: */
162: public String addObject(Object object, String keyPrefix) {
163: String objectKey = keyPrefix + nextObjectKey++;
164: objectMap.put(objectKey, object);
165: return objectKey;
166: }
167:
168: /**
169: * allows adding an arbitrary object to the session with static a string key that can be used to later access this object from
170: * the session using the retrieveObject method in this class
171: *
172: * @param object
173: *
174: */
175: public void addObject(String key, Object object) {
176:
177: objectMap.put(key, object);
178:
179: }
180:
181: /**
182: * allows adding an arbitrary object to the session and returns a string key that can be used to later access this object from
183: * the session using the retrieveObject method in this class
184: *
185: * @param object
186: * @return
187: */
188: public String addObject(Object object) {
189: String objectKey = nextObjectKey++ + "";
190: objectMap.put(objectKey, object);
191: return objectKey;
192: }
193:
194: /**
195: * allows for fetching an object that has been put into the userSession based on the key that would have been returned when
196: * adding the object
197: *
198: * @param objectKey
199: * @return
200: */
201: public Object retrieveObject(String objectKey) {
202: return this .objectMap.get(objectKey);
203: }
204:
205: /**
206: * allows for removal of an object from session that has been put into the userSession based on the key that would have been
207: * assigned
208: *
209: * @param objectKey
210: */
211: public void removeObject(String objectKey) {
212: this .objectMap.remove(objectKey);
213: }
214:
215: /**
216: * allows for removal of an object from session that has been put into the userSession based on a key that starts with the given
217: * prefix
218: *
219: * @param objectKey
220: */
221: public void removeObjectsByPrefix(String objectKeyPrefix) {
222: List removeKeys = new ArrayList();
223: for (Iterator iter = objectMap.keySet().iterator(); iter
224: .hasNext();) {
225: String key = (String) iter.next();
226: if (key.startsWith(objectKeyPrefix)) {
227: removeKeys.add(key);
228: }
229: }
230:
231: for (Iterator iter = removeKeys.iterator(); iter.hasNext();) {
232: String key = (String) iter.next();
233: this .objectMap.remove(key);
234: }
235: }
236:
237: /**
238: * @return boolean indicating if the backdoor is in use
239: */
240: public boolean isBackdoorInUse() {
241: return backdoorUser != null;
242: }
243:
244: /**
245: * retrieve a flexdoc from the userSession based on the document id
246: *
247: * @param docId
248: * @return
249: */
250: public KualiWorkflowDocument getWorkflowDocument(String docId) {
251: if (workflowDocMap == null) {
252: workflowDocMap = new HashMap();
253: }
254: if (workflowDocMap.containsKey(docId)) {
255: return (KualiWorkflowDocument) workflowDocMap.get(docId);
256: } else {
257: return null;
258: }
259: }
260:
261: /**
262: * set a flexDoc into the userSession which will be stored under the document id
263: *
264: * @param flexDoc
265: */
266: public void setWorkflowDocument(
267: KualiWorkflowDocument workflowDocument) {
268: try {
269: if (workflowDocMap == null) {
270: workflowDocMap = new HashMap();
271: }
272: workflowDocMap.put(workflowDocument.getRouteHeaderId()
273: .toString(), workflowDocument);
274: } catch (WorkflowException e) {
275: throw new IllegalStateException(
276: "could not save the document in the session msg: "
277: + e.getMessage());
278: }
279: }
280:
281: private void refreshUserGroups(UniversalUser universalUser) {
282: universalUser.refreshUserGroups();
283: }
284: }
|