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