| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| UserSession | 
  | 
  | 2.1363636363636362;2.136 | 
| 1 |  /* | |
| 2 |   * Copyright 2006-2011 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 | ||
| 17 |  package org.kuali.rice.kns; | |
| 18 | ||
| 19 |  import org.apache.commons.lang.StringUtils; | |
| 20 |  import org.kuali.rice.core.api.config.property.ConfigContext; | |
| 21 |  import org.kuali.rice.core.api.exception.RiceRuntimeException; | |
| 22 |  import org.kuali.rice.kim.api.services.KimApiServiceLocator; | |
| 23 |  import org.kuali.rice.kim.bo.Person; | |
| 24 |  import org.kuali.rice.kns.util.KNSConstants; | |
| 25 |  import org.kuali.rice.kns.util.SessionTicket; | |
| 26 | ||
| 27 |  import java.io.Serializable; | |
| 28 |  import java.util.ArrayList; | |
| 29 |  import java.util.Collections; | |
| 30 |  import java.util.HashMap; | |
| 31 |  import java.util.List; | |
| 32 |  import java.util.Map; | |
| 33 | ||
| 34 | ||
| 35 |  /** | |
| 36 |   * Holds info about the User Session | |
| 37 |   */ | |
| 38 | public class UserSession implements Serializable {  | |
| 39 | ||
| 40 | private static final long serialVersionUID = 4532616762540067557L;  | |
| 41 | ||
| 42 |      private Person person; | |
| 43 |      private Person backdoorUser; | |
| 44 | private int nextObjectKey;  | |
| 45 |      private Map<String,Object> objectMap; | |
| 46 |      private String kualiSessionId; | |
| 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 |      /** | |
| 63 |       * Take in a netid, and construct the user from that. | |
| 64 |       *  | |
| 65 |       * @param principalName | |
| 66 |       */ | |
| 67 | 0 |      public UserSession(String principalName) { | 
| 68 | 0 |          this.person = KimApiServiceLocator.getPersonService().getPersonByPrincipalName(principalName); | 
| 69 | 0 |          if (this.person == null) { | 
| 70 | 0 |                  throw new IllegalArgumentException("Failed to locate a principal with principal name '" + principalName + "'"); | 
| 71 | }  | |
| 72 | 0 |          this.nextObjectKey = 0; | 
| 73 | 0 |          this.objectMap = new HashMap<String,Object>(); | 
| 74 | 0 |      } | 
| 75 | ||
| 76 | ||
| 77 |      /** | |
| 78 |       * @return the principalId of the current user in the system, backdoor principalId if backdoor is set | |
| 79 |       */ | |
| 80 |      public String getPrincipalId() { | |
| 81 | 0 |          if (backdoorUser != null) { | 
| 82 | 0 |              return backdoorUser.getPrincipalId(); | 
| 83 | }  | |
| 84 | 0 |          return person.getPrincipalId(); | 
| 85 | }  | |
| 86 | ||
| 87 |      /** | |
| 88 |       * @return the principalName of the current user in the system, backdoor principalName if backdoor is set | |
| 89 |       */ | |
| 90 |      public String getPrincipalName() { | |
| 91 | 0 |          if (backdoorUser != null) { | 
| 92 | 0 |              return backdoorUser.getPrincipalName(); | 
| 93 | }  | |
| 94 | 0 |          return person.getPrincipalName(); | 
| 95 | }  | |
| 96 | ||
| 97 | ||
| 98 |      /** | |
| 99 |       * 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 | |
| 100 |       * as the backdoor user. | |
| 101 |       *  | |
| 102 |       * @return String | |
| 103 |       */ | |
| 104 |      public String getLoggedInUserPrincipalName() { | |
| 105 | 0 |              if ( person != null ) { | 
| 106 | 0 |                      return person.getPrincipalName(); | 
| 107 | }  | |
| 108 | 0 |              return ""; | 
| 109 | }  | |
| 110 | ||
| 111 |      /** | |
| 112 |       * @return the KualiUser which is the current user in the system, backdoor if backdoor is set | |
| 113 |       */ | |
| 114 |      public Person getPerson() { | |
| 115 | 0 |          if (backdoorUser != null) { | 
| 116 | 0 |              return backdoorUser; | 
| 117 | }  | |
| 118 | 0 |          return person; | 
| 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 principalName | |
| 126 |       */ | |
| 127 | public void setBackdoorUser(String principalName) {  | |
| 128 |         // only allow backdoor in non-production environments | |
| 129 | 0 |         if ( !isProductionEnvironment()) { | 
| 130 | 0 |          this.backdoorUser = KimApiServiceLocator.getPersonService().getPersonByPrincipalName(principalName); | 
| 131 | 0 |          if (backdoorUser == null) { | 
| 132 | 0 |                  throw new RiceRuntimeException(principalName + " is not a valid principalName"); | 
| 133 | }  | |
| 134 | }  | |
| 135 | 0 |      } | 
| 136 | ||
| 137 | private boolean isProductionEnvironment() {  | |
| 138 | 0 |              return ConfigContext.getCurrentContextConfig().getProperty(KNSConstants.PROD_ENVIRONMENT_CODE_KEY).equalsIgnoreCase( | 
| 139 | ConfigContext.getCurrentContextConfig().getProperty(KNSConstants.ENVIRONMENT_KEY));  | |
| 140 | }  | |
| 141 | ||
| 142 |      /** | |
| 143 |       * clear the backdoor user | |
| 144 |       *  | |
| 145 |       */ | |
| 146 | public void clearBackdoorUser() {  | |
| 147 | 0 |          this.backdoorUser = null; | 
| 148 | 0 |      } | 
| 149 | ||
| 150 |      /** | |
| 151 |       * allows adding an arbitrary object to the session and returns a string key that can be used to later access this object from | |
| 152 |       * the session using the retrieveObject method in this class. This allows for a prefix to be placed in front of the  | |
| 153 |       * incremented key. So if the prefix is "searchResults" and the nextObjectKey (local int that holds the key value) is 2 then | |
| 154 |       * the new key will be "searchResults3". "searchResults3" will be returned from the method. | |
| 155 |       *  | |
| 156 |       * @param object | |
| 157 |       */ | |
| 158 |      public String addObjectWithGeneratedKey(Serializable object, String keyPrefix) { | |
| 159 | 0 |          String objectKey = keyPrefix + nextObjectKey++; | 
| 160 | 0 |          objectMap.put(objectKey, object); | 
| 161 | 0 |          return objectKey; | 
| 162 | }  | |
| 163 | ||
| 164 |      /** | |
| 165 |       * allows adding an arbitrary object to the session and returns a string key that can be used to later access this object from | |
| 166 |       * the session using the retrieveObject method in this class. The key is generated from an integer and incremented for every  | |
| 167 |       * object added.  So the first object added with have a key of "1".  This key will be returned from the method. | |
| 168 |       *  | |
| 169 |       * @param object | |
| 170 |       */ | |
| 171 |      public String addObjectWithGeneratedKey(Object object) { | |
| 172 | 0 |          String objectKey = nextObjectKey++ + ""; | 
| 173 | 0 |          objectMap.put(objectKey, object); | 
| 174 | 0 |          return objectKey; | 
| 175 | }  | |
| 176 | ||
| 177 |      /** | |
| 178 |       * allows adding an arbitrary object to the session with static a string key that can be used to later access this object from | |
| 179 |       * the session using the retrieveObject method in this class | |
| 180 |       *  | |
| 181 |       * @param object | |
| 182 |       *  | |
| 183 |       */ | |
| 184 | public void addObject(String key, Object object) {  | |
| 185 | ||
| 186 | 0 |          objectMap.put(key, object); | 
| 187 | ||
| 188 | 0 |      } | 
| 189 | ||
| 190 |      /** | |
| 191 |       * allows for fetching an object that has been put into the userSession based on the key that would have been returned when | |
| 192 |       * adding the object | |
| 193 |       *  | |
| 194 |       * @param objectKey | |
| 195 |       */ | |
| 196 |      public Object retrieveObject(String objectKey) { | |
| 197 | 0 |          return this.objectMap.get(objectKey); | 
| 198 | }  | |
| 199 | ||
| 200 |      /** | |
| 201 |       * allows for removal of an object from session that has been put into the userSession based on the key that would have been | |
| 202 |       * assigned | |
| 203 |       *  | |
| 204 |       * @param objectKey | |
| 205 |       */ | |
| 206 | public void removeObject(String objectKey) {  | |
| 207 | 0 |          this.objectMap.remove(objectKey); | 
| 208 | 0 |      } | 
| 209 | ||
| 210 |      /** | |
| 211 |       * allows for removal of an object from session that has been put into the userSession based on a key that starts with the given | |
| 212 |       * prefix | |
| 213 |       */ | |
| 214 | public void removeObjectsByPrefix(String objectKeyPrefix) {  | |
| 215 | 0 |          List<String> removeKeys = new ArrayList<String>(); | 
| 216 | 0 |          for (String key : objectMap.keySet()) { | 
| 217 | 0 |              if (key.startsWith(objectKeyPrefix)) { | 
| 218 | 0 |                  removeKeys.add(key); | 
| 219 | }  | |
| 220 | }  | |
| 221 | ||
| 222 | 0 |          for (String key : removeKeys) { | 
| 223 | 0 |              this.objectMap.remove(key); | 
| 224 | }  | |
| 225 | 0 |      } | 
| 226 | ||
| 227 |      /** | |
| 228 |       * @return boolean indicating if the backdoor is in use | |
| 229 |       */ | |
| 230 | public boolean isBackdoorInUse() {  | |
| 231 | 0 |          return backdoorUser != null; | 
| 232 | }  | |
| 233 | ||
| 234 |          /** | |
| 235 |           * Adds the given SessionTicket to the objectMap and returns the associated key | |
| 236 |           *  | |
| 237 |           * @param ticket | |
| 238 |           *            - SessionTicket to add | |
| 239 |           * @return the objectMap key for the ticket as a String | |
| 240 |           */ | |
| 241 |          public String putSessionTicket(SessionTicket ticket) { | |
| 242 | 0 |                  return addObjectWithGeneratedKey(ticket); | 
| 243 | }  | |
| 244 | ||
| 245 |          /** | |
| 246 |           * Retrieves all SessionTicket instances currently in the UserSession#objectMap | |
| 247 |           *  | |
| 248 |           * @return List<SessionTicket> contained in user session | |
| 249 |           */ | |
| 250 |          public List<SessionTicket> getAllSessionTickets() { | |
| 251 | 0 |                  List<SessionTicket> sessionTickets = new ArrayList<SessionTicket>(); | 
| 252 | ||
| 253 | 0 |                  for (Object object : objectMap.values()) { | 
| 254 | 0 |                          if (object instanceof SessionTicket) { | 
| 255 | 0 |                                  sessionTickets.add((SessionTicket) object); | 
| 256 | }  | |
| 257 | }  | |
| 258 | ||
| 259 | 0 |                  return sessionTickets; | 
| 260 | }  | |
| 261 | ||
| 262 |          /** | |
| 263 |           * Retrieves all SessionTicket instances currently in the UserSession#objectMap that are of a given ticket type | |
| 264 |           *  | |
| 265 |           * @return List<SessionTicket> contained in user session | |
| 266 |           */ | |
| 267 |          public List<SessionTicket> getAllSessionTicketsByType(String ticketTypeName) { | |
| 268 | 0 |                  List<SessionTicket> sessionTickets = new ArrayList<SessionTicket>(); | 
| 269 | ||
| 270 | 0 |                  for (SessionTicket ticket : getAllSessionTickets()) { | 
| 271 | 0 |                          if (StringUtils.equalsIgnoreCase(ticket.getTicketTypeName(), ticketTypeName)) { | 
| 272 | 0 |                                  sessionTickets.add(ticket); | 
| 273 | }  | |
| 274 | }  | |
| 275 | ||
| 276 | 0 |                  return sessionTickets; | 
| 277 | }  | |
| 278 | ||
| 279 |          /** | |
| 280 |           * Determines if the UserSession contains a ticket of the given type that matches the given context. To match context the ticket must | |
| 281 |           * contain all the same keys at the given context and the values must be equal with the exception of case | |
| 282 |           *  | |
| 283 |           * @param ticketTypeName | |
| 284 |           *            - Name of the ticket type to match | |
| 285 |           * @param matchContext | |
| 286 |           *            - Map on context parameters to match on | |
| 287 |           * @return true if a ticket was found in the UserSession that matches the request, false if one was not found | |
| 288 |           */ | |
| 289 | public boolean hasMatchingSessionTicket(String ticketTypeName, Map<String, String> matchContext) {  | |
| 290 | 0 |                  boolean hasTicket = false; | 
| 291 | ||
| 292 | 0 |                  for (SessionTicket ticket : getAllSessionTicketsByType(ticketTypeName)) { | 
| 293 | 0 |                          Map<String, String> ticketContext = ticket.getTicketContext(); | 
| 294 | ||
| 295 | 0 |                          boolean keySetMatch = ticketContext.keySet().equals(matchContext.keySet()); | 
| 296 | 0 |                          if (keySetMatch) { | 
| 297 | 0 |                                  boolean valuesMatch = true; | 
| 298 | 0 |                                  for (String contextKey : ticketContext.keySet()) { | 
| 299 | 0 |                                          String ticketValue = ticketContext.get(contextKey); | 
| 300 | 0 |                                          String matchValue = matchContext.get(contextKey); | 
| 301 | 0 |                                          if (!StringUtils.equalsIgnoreCase(ticketValue, matchValue)) { | 
| 302 | 0 |                                                  valuesMatch = false; | 
| 303 | }  | |
| 304 | 0 |                                  } | 
| 305 | ||
| 306 | 0 |                                  if (valuesMatch) { | 
| 307 | 0 |                                          hasTicket = true; | 
| 308 | 0 |                                          break; | 
| 309 | }  | |
| 310 | }  | |
| 311 | 0 |                  } | 
| 312 | ||
| 313 | 0 |                  return hasTicket; | 
| 314 | }  | |
| 315 | ||
| 316 |          /** retrieves an unmodifiable view of the objectMap. */ | |
| 317 |          public Map<String, Object> getObjectMap() { | |
| 318 | 0 |                  return Collections.unmodifiableMap(this.objectMap); | 
| 319 | }  | |
| 320 | }  |