| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.kuali.rice.kew.web.session; |
| 18 | |
|
| 19 | |
import java.io.Serializable; |
| 20 | |
import java.util.ArrayList; |
| 21 | |
import java.util.HashMap; |
| 22 | |
import java.util.Iterator; |
| 23 | |
import java.util.List; |
| 24 | |
import java.util.Map; |
| 25 | |
|
| 26 | |
import org.apache.commons.lang.StringUtils; |
| 27 | |
import org.kuali.rice.core.config.ConfigContext; |
| 28 | |
import org.kuali.rice.kew.actionlist.ActionListFilter; |
| 29 | |
import org.kuali.rice.kew.preferences.Preferences; |
| 30 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
| 31 | |
import org.kuali.rice.kew.user.UserUtils; |
| 32 | |
import org.kuali.rice.kew.util.KEWConstants; |
| 33 | |
import org.kuali.rice.kim.bo.Group; |
| 34 | |
import org.kuali.rice.kim.bo.Person; |
| 35 | |
import org.kuali.rice.kim.bo.entity.KimPrincipal; |
| 36 | |
import org.kuali.rice.kim.service.IdentityManagementService; |
| 37 | |
import org.kuali.rice.kim.service.KIMServiceLocator; |
| 38 | |
import org.kuali.rice.kim.service.PersonService; |
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
public class UserSession implements Serializable { |
| 50 | |
|
| 51 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger |
| 52 | |
.getLogger(UserSession.class); |
| 53 | |
|
| 54 | |
private static final long serialVersionUID = 1L; |
| 55 | |
|
| 56 | 0 | private static ThreadLocal currentUserSession = new ThreadLocal(); |
| 57 | |
|
| 58 | |
private int nextObjectKey; |
| 59 | 0 | private transient Map objectMap = new HashMap(); |
| 60 | |
private ActionListFilter actionListFilter; |
| 61 | |
private Preferences preferences; |
| 62 | 0 | private List authentications = new ArrayList(); |
| 63 | |
|
| 64 | |
private String sortOrder; |
| 65 | |
private String sortCriteria; |
| 66 | |
private int currentPage; |
| 67 | |
private boolean updateActionList; |
| 68 | |
|
| 69 | |
private KimPrincipal actualPrincipal; |
| 70 | |
private Person actualPerson; |
| 71 | 0 | private Map<String, Group> actualPrincipalGroups = new HashMap<String, Group>(); |
| 72 | |
|
| 73 | |
private KimPrincipal backdoorPrincipal; |
| 74 | |
private Person backdoorPerson; |
| 75 | 0 | private Map<String, Group> backdoorPrincipalGroups = new HashMap<String, Group>(); |
| 76 | |
|
| 77 | |
private KimPrincipal helpDeskActionListPrincipal; |
| 78 | |
private Person helpDeskActionListPerson; |
| 79 | |
|
| 80 | 0 | private static IdentityManagementService identityService = null; |
| 81 | 0 | private static PersonService<Person> personService = null; |
| 82 | |
|
| 83 | 0 | public UserSession (KimPrincipal actualPrincipal) { |
| 84 | 0 | init(actualPrincipal); |
| 85 | 0 | } |
| 86 | |
|
| 87 | 0 | public UserSession (String actualPrincipalId) { |
| 88 | 0 | init(getIdentityService().getPrincipal(actualPrincipalId)); |
| 89 | 0 | } |
| 90 | |
|
| 91 | |
private void init(KimPrincipal actualPrincipal) { |
| 92 | 0 | if (actualPrincipal == null) { |
| 93 | 0 | throw new IllegalArgumentException("UserSession was constructed with a null Principal"); |
| 94 | |
} |
| 95 | 0 | this.actualPrincipal = actualPrincipal; |
| 96 | 0 | actualPerson = getPersonService().getPerson(actualPrincipal.getPrincipalId()); |
| 97 | 0 | if ( actualPerson == null ) { |
| 98 | 0 | throw new RuntimeException( "Unable to create person object from the given principal ID: " + actualPrincipal.getPrincipalId() ); |
| 99 | |
} |
| 100 | 0 | establishPreferencesForPrincipal(actualPrincipal); |
| 101 | 0 | List<? extends Group> groups = getIdentityService().getGroupsForPrincipal(actualPrincipal.getPrincipalId()); |
| 102 | 0 | for (Group group : groups) { |
| 103 | 0 | actualPrincipalGroups.put(group.getGroupId(), group); |
| 104 | |
} |
| 105 | 0 | this.nextObjectKey = 0; |
| 106 | 0 | } |
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
public String getSortOrder() { |
| 112 | 0 | return this.sortOrder; |
| 113 | |
} |
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
public void setSortOrder(String sortOrder) { |
| 119 | 0 | this.sortOrder = sortOrder; |
| 120 | 0 | } |
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
public String getSortCriteria() { |
| 126 | 0 | return this.sortCriteria; |
| 127 | |
} |
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
public void setSortCriteria(String sortCriteria) { |
| 133 | 0 | this.sortCriteria = sortCriteria; |
| 134 | 0 | } |
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
public int getCurrentPage() { |
| 140 | 0 | return this.currentPage; |
| 141 | |
} |
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
public void setCurrentPage(int currentPage) { |
| 147 | 0 | this.currentPage = currentPage; |
| 148 | 0 | } |
| 149 | |
|
| 150 | |
public static UserSession getAuthenticatedUser() { |
| 151 | 0 | return (UserSession)currentUserSession.get(); |
| 152 | |
} |
| 153 | |
|
| 154 | |
public static void setAuthenticatedUser(UserSession currentUserSession) { |
| 155 | 0 | UserSession.currentUserSession.set(currentUserSession); |
| 156 | 0 | } |
| 157 | |
|
| 158 | |
public String getValue(String value) { |
| 159 | 0 | return value; |
| 160 | |
} |
| 161 | |
|
| 162 | |
public Preferences getPreferences() { |
| 163 | 0 | return preferences; |
| 164 | |
} |
| 165 | |
|
| 166 | |
public ActionListFilter getActionListFilter() { |
| 167 | 0 | return actionListFilter; |
| 168 | |
} |
| 169 | |
|
| 170 | |
public void setActionListFilter(ActionListFilter actionListFilter) { |
| 171 | 0 | this.actionListFilter = actionListFilter; |
| 172 | 0 | } |
| 173 | |
|
| 174 | |
public KimPrincipal getActualPrincipal() { |
| 175 | 0 | return actualPrincipal; |
| 176 | |
} |
| 177 | |
|
| 178 | |
public Person getActualPerson() { |
| 179 | 0 | return actualPerson; |
| 180 | |
} |
| 181 | |
|
| 182 | |
public KimPrincipal getBackdoorPrincipal() { |
| 183 | 0 | return this.backdoorPrincipal; |
| 184 | |
} |
| 185 | |
|
| 186 | |
public Person getBackdoorPerson() { |
| 187 | 0 | return this.backdoorPerson; |
| 188 | |
} |
| 189 | |
|
| 190 | |
public KimPrincipal getHelpDeskActionListPrincipal() { |
| 191 | 0 | return this.helpDeskActionListPrincipal; |
| 192 | |
} |
| 193 | |
|
| 194 | |
public Person getHelpDeskActionListPerson() { |
| 195 | 0 | return this.helpDeskActionListPerson; |
| 196 | |
} |
| 197 | |
|
| 198 | |
public String getPrincipalId() { |
| 199 | 0 | return getPrincipal().getPrincipalId(); |
| 200 | |
} |
| 201 | |
|
| 202 | |
public String getPrincipalName() { |
| 203 | 0 | return getPrincipal().getPrincipalName(); |
| 204 | |
} |
| 205 | |
|
| 206 | |
public KimPrincipal getPrincipal() { |
| 207 | 0 | if (getBackdoorPrincipal() != null) { |
| 208 | 0 | return getBackdoorPrincipal(); |
| 209 | 0 | } else if (getActualPrincipal() != null) { |
| 210 | 0 | return getActualPrincipal(); |
| 211 | |
} |
| 212 | 0 | throw new IllegalStateException("UserSession does not contain an established principal."); |
| 213 | |
} |
| 214 | |
|
| 215 | |
public Person getPerson() { |
| 216 | 0 | if (getBackdoorPerson() != null) { |
| 217 | 0 | return getBackdoorPerson(); |
| 218 | 0 | } else if (getActualPerson() != null) { |
| 219 | 0 | return getActualPerson(); |
| 220 | |
} |
| 221 | 0 | throw new IllegalStateException("UserSession does not contain an established person."); |
| 222 | |
} |
| 223 | |
|
| 224 | |
public boolean establishBackdoorWithPrincipalName(String principalName) { |
| 225 | 0 | if (!isProductionEnvironment()) { |
| 226 | 0 | this.backdoorPrincipal = KIMServiceLocator.getIdentityManagementService().getPrincipalByPrincipalName(principalName); |
| 227 | 0 | if (backdoorPrincipal == null) { |
| 228 | 0 | return false; |
| 229 | |
} |
| 230 | 0 | this.backdoorPerson = KEWServiceLocator.getIdentityHelperService().getPersonByPrincipalName(principalName); |
| 231 | 0 | List<? extends Group> groups = KIMServiceLocator.getIdentityManagementService().getGroupsForPrincipal(backdoorPrincipal.getPrincipalId()); |
| 232 | 0 | for (Group group : groups) { |
| 233 | 0 | backdoorPrincipalGroups.put(group.getGroupId(), group); |
| 234 | |
} |
| 235 | 0 | establishPreferencesForPrincipal(backdoorPrincipal); |
| 236 | 0 | return true; |
| 237 | |
} |
| 238 | 0 | return false; |
| 239 | |
} |
| 240 | |
|
| 241 | |
public void clearBackdoor() { |
| 242 | 0 | this.backdoorPrincipal = null; |
| 243 | 0 | this.backdoorPerson = null; |
| 244 | 0 | establishPreferencesForPrincipal(actualPrincipal); |
| 245 | 0 | } |
| 246 | |
|
| 247 | |
public void establishHelpDeskWithPrincipalName(String principalName) { |
| 248 | 0 | this.helpDeskActionListPrincipal = KEWServiceLocator.getIdentityHelperService().getPrincipalByPrincipalName(principalName); |
| 249 | 0 | this.helpDeskActionListPerson = KEWServiceLocator.getIdentityHelperService().getPersonByPrincipalName(principalName); |
| 250 | 0 | } |
| 251 | |
|
| 252 | |
public void clearHelpDesk() { |
| 253 | 0 | this.helpDeskActionListPrincipal = null; |
| 254 | 0 | this.helpDeskActionListPerson = null; |
| 255 | 0 | } |
| 256 | |
|
| 257 | |
public void refreshPreferences() { |
| 258 | 0 | establishPreferencesForPrincipal(getPrincipal()); |
| 259 | 0 | this.updateActionList = true; |
| 260 | |
|
| 261 | 0 | } |
| 262 | |
|
| 263 | |
protected void establishPreferencesForPrincipal(KimPrincipal principal) { |
| 264 | 0 | this.preferences = KEWServiceLocator.getPreferencesService().getPreferences(principal.getPrincipalId()); |
| 265 | 0 | if (this.preferences.isRequiresSave()) { |
| 266 | 0 | LOG.info("Detected that user preferences require saving."); |
| 267 | |
try { |
| 268 | 0 | KEWServiceLocator.getPreferencesService().savePreferences(principal.getPrincipalId(), this.preferences); |
| 269 | 0 | } catch (Exception e) { |
| 270 | 0 | LOG.warn("Failed to save preferences for user! Likely user tried to log in from more than one browser at the same time. Reloading preferences."); |
| 271 | 0 | } |
| 272 | 0 | this.preferences = KEWServiceLocator.getPreferencesService().getPreferences(principal.getPrincipalId()); |
| 273 | |
} |
| 274 | 0 | } |
| 275 | |
|
| 276 | |
protected boolean isProductionEnvironment() { |
| 277 | 0 | return ConfigContext.getCurrentContextConfig().getProperty(KEWConstants.PROD_DEPLOYMENT_CODE).equalsIgnoreCase( |
| 278 | |
ConfigContext.getCurrentContextConfig().getEnvironment()); |
| 279 | |
} |
| 280 | |
|
| 281 | |
public String addObject(Object object) { |
| 282 | 0 | String objectKey = nextObjectKey++ + ""; |
| 283 | 0 | getObjectMap().put(objectKey, object); |
| 284 | 0 | return objectKey; |
| 285 | |
} |
| 286 | |
|
| 287 | |
public Object retrieveObject(String objectKey) { |
| 288 | 0 | return getObjectMap().get(objectKey); |
| 289 | |
} |
| 290 | |
|
| 291 | |
public void removeObject(String objectKey) { |
| 292 | 0 | getObjectMap().remove(objectKey); |
| 293 | 0 | } |
| 294 | |
|
| 295 | |
public boolean isBackdoorInUse() { |
| 296 | 0 | return backdoorPrincipal != null; |
| 297 | |
} |
| 298 | |
|
| 299 | |
public String getEmailAddress() { |
| 300 | 0 | return getPerson().getEmailAddressUnmasked(); |
| 301 | |
} |
| 302 | |
|
| 303 | |
public int getNextObjectKey() { |
| 304 | 0 | return nextObjectKey; |
| 305 | |
} |
| 306 | |
public void setNextObjectKey(int nextObjectKey) { |
| 307 | 0 | this.nextObjectKey = nextObjectKey; |
| 308 | 0 | } |
| 309 | |
public Map getObjectMap() { |
| 310 | 0 | if (objectMap == null) { |
| 311 | 0 | objectMap = new HashMap(); |
| 312 | |
} |
| 313 | 0 | return objectMap; |
| 314 | |
} |
| 315 | |
public void setObjectMap(Map objectMap) { |
| 316 | 0 | this.objectMap = objectMap; |
| 317 | 0 | } |
| 318 | |
public String getDisplayName() { |
| 319 | 0 | return getPersonService().getPerson(getPrincipalId()).getNameUnmasked(); |
| 320 | |
} |
| 321 | |
|
| 322 | |
|
| 323 | |
|
| 324 | |
|
| 325 | |
|
| 326 | |
public List getAuthentications() { |
| 327 | 0 | return authentications; |
| 328 | |
} |
| 329 | |
|
| 330 | |
public void addAuthentication(Authentication authentication) { |
| 331 | 0 | getAuthentications().add(authentication); |
| 332 | 0 | } |
| 333 | |
|
| 334 | |
public void removeAuthentication(Authentication authentication) { |
| 335 | 0 | getAuthentications().remove(authentication); |
| 336 | 0 | } |
| 337 | |
|
| 338 | |
public boolean hasRole(String role) { |
| 339 | 0 | for (Iterator iterator = getAuthentications().iterator(); iterator.hasNext();) { |
| 340 | 0 | Authentication auth = (Authentication) iterator.next(); |
| 341 | 0 | if (auth.getAuthority().equalsIgnoreCase(role)) { |
| 342 | 0 | return true; |
| 343 | |
} |
| 344 | 0 | } |
| 345 | 0 | return false; |
| 346 | |
} |
| 347 | |
|
| 348 | |
public Map<String, Group> getGroups() { |
| 349 | 0 | if (getBackdoorPrincipal() != null) { |
| 350 | 0 | return backdoorPrincipalGroups; |
| 351 | |
} |
| 352 | 0 | return actualPrincipalGroups; |
| 353 | |
} |
| 354 | |
|
| 355 | |
public boolean isMemberOfGroupWithName(String namespace, String groupName) { |
| 356 | 0 | for (Group group : getGroups().values()) { |
| 357 | 0 | if (StringUtils.equals(namespace, group.getNamespaceCode()) && StringUtils.equals(groupName, group.getGroupName())) { |
| 358 | 0 | return true; |
| 359 | |
} |
| 360 | |
} |
| 361 | 0 | return false; |
| 362 | |
} |
| 363 | |
|
| 364 | |
public boolean isMemberOfGroupWithId(String groupId) { |
| 365 | 0 | return getGroups().containsKey(groupId); |
| 366 | |
} |
| 367 | |
|
| 368 | |
protected IdentityManagementService getIdentityService(){ |
| 369 | 0 | if(identityService == null){ |
| 370 | 0 | identityService = KIMServiceLocator.getIdentityManagementService(); |
| 371 | |
|
| 372 | |
} |
| 373 | 0 | return identityService; |
| 374 | |
} |
| 375 | |
|
| 376 | |
protected PersonService<Person> getPersonService(){ |
| 377 | 0 | if(personService == null){ |
| 378 | 0 | personService = KIMServiceLocator.getPersonService(); |
| 379 | |
} |
| 380 | 0 | return personService; |
| 381 | |
} |
| 382 | |
|
| 383 | |
public boolean isUpdateActionList() { |
| 384 | 0 | return this.updateActionList; |
| 385 | |
} |
| 386 | |
|
| 387 | |
public void actionListUpdated() { |
| 388 | 0 | this.updateActionList = false; |
| 389 | 0 | } |
| 390 | |
|
| 391 | |
} |