1 19 20 package com.sslexplorer.policyframework; 21 22 import java.util.HashMap ; 23 import java.util.Map ; 24 25 import com.sslexplorer.security.SessionInfo; 26 import com.sslexplorer.security.User; 27 28 34 public class LaunchSession { 35 36 40 public final static AccessRight USER_ACCESS = new AccessRight(); 41 42 46 public final static AccessRight MANAGEMENT_ACCESS = new AccessRight(); 47 48 51 public static final String LAUNCH_ID = "launchId"; 52 53 56 public static final String LONG_LAUNCH_ID = "sslx_launchId"; 57 58 60 private SessionInfo session; 61 private Resource resource; 62 private Policy policy; 63 private String id; 64 private Map <String , Object > attributes = new HashMap <String , Object >(); 65 66 72 public LaunchSession(Resource resource) { 73 this(null, null, resource, null); 74 } 75 76 82 public LaunchSession(SessionInfo session) { 83 this(null, session, null, null); 84 } 85 86 94 public LaunchSession(String id, SessionInfo session, Resource resource, Policy policy) { 95 super(); 96 if(session == null) { 97 throw new IllegalArgumentException ("Session must be provided."); 98 } 99 this.id = id; 100 this.session = session; 101 this.resource = resource; 102 this.policy = policy; 103 if( ( resource == null && policy != null ) || ( policy == null && resource != null ) ) { 104 throw new IllegalArgumentException ("If either resource or policy is provided, resource and policy must be provided."); 105 } 106 } 107 108 114 public boolean hasPolicy() { 115 return resource != null && policy != null; 116 } 117 118 129 public AccessRight checkAccessRights(User user, SessionInfo sessionInfo) throws NoPermissionException, PolicyException { 130 if (resource == null) { 131 throw new PolicyException(PolicyException.INTERNAL_ERROR, "This resource session is not attached to a resource."); 132 } 133 ResourceType resourceType = resource.getResourceType(); 134 if(sessionInfo != null && this.session != null && !sessionInfo.equals(this.session)) { 135 throw new NoPermissionException("You do not own this session.", session.getUser(), resourceType); 136 } 137 if(user != null && this.session != null && !user.equals(this.session.getUser())) { 138 throw new NoPermissionException("Your user does not own this session.", user, resourceType); 139 } 140 try { 141 if (!(resource instanceof OwnedResource) || (resource instanceof OwnedResource && ((OwnedResource) resource).getOwnerUsername() == null)) { 142 try { 143 if (!PolicyDatabaseFactory.getInstance().isPrincipalAllowed(session.getUser(), 145 resource, 146 false)) { 147 throw new NoPermissionException("You may not access this resource here.", session.getUser(), resourceType); 148 } 149 } catch (NoPermissionException npe2) { 150 throw npe2; 151 } catch (Exception e) { 152 throw new NoPermissionException("Failed to determine if resource is accessable.", 153 session.getUser(), 154 resourceType); 155 } 156 } else { 157 if (!(session.getUser().getPrincipalName().equals(((OwnedResource) resource).getOwnerUsername()))) { 159 throw new NoPermissionException("You do not have permission to access this resource.", 160 session.getUser(), 161 resourceType); 162 } 163 } 164 return USER_ACCESS; 165 } catch (NoPermissionException npe) { 166 ResourceUtil.checkResourceManagementRights(resource, session, new Permission[] { PolicyConstants.PERM_CREATE_EDIT_AND_ASSIGN, PolicyConstants.PERM_EDIT_AND_ASSIGN }); 167 return MANAGEMENT_ACCESS; 168 } catch (Exception e) { 169 throw new NoPermissionException("Failed to determine if resource is accessable.", session.getUser(), resourceType); 170 } 171 } 172 173 178 public Policy getPolicy() { 179 return policy; 180 } 181 182 187 public Resource getResource() { 188 return resource; 189 } 190 191 196 public void setResource(Resource resource) { 197 this.resource = resource; 198 199 } 200 201 206 public SessionInfo getSession() { 207 return session; 208 } 209 210 215 public String getId() { 216 return id; 217 } 218 219 222 public void takePolicy() { 223 policy = null; 224 } 225 226 231 public void givePolicy(Policy policy) { 232 this.policy = policy; 233 } 234 235 241 public boolean isTracked() { 242 return id != null; 243 } 244 245 252 public Object getAttribute(String name) { 253 return attributes.get(name); 254 } 255 256 262 public void setAttribute(String name, Object value) { 263 attributes.put(name, value); 264 } 265 266 271 public boolean equals(Object o) { 272 return o instanceof LaunchSession && getId() != null 273 && ((LaunchSession) o).getId() != null 274 && ((LaunchSession) o).getId().equals(getId()) 275 && ((LaunchSession) o).getSession().getHttpSession().equals(getSession().getHttpSession()); 276 } 277 278 283 @Override 284 public int hashCode() { 285 return (getSession().getHttpSession().getId() + "_" + getId()).hashCode(); 286 } 287 288 293 @Override 294 public String toString() { 295 return (hasPolicy() ? ("PolicyProtected [policy=" + policy.getResourceId() 296 + ",resource=" 297 + resource.getResourceName() 298 + "/" 299 + resource.getResourceType().getResourceTypeId() + ",") : "Unprotected [") + "session=" 300 + session.getId() 301 + ",httpSession=" 302 + session.getHttpSession().getId() 303 + "]"; 304 } 305 306 309 public static class AccessRight { 310 } 311 } 312 | Popular Tags |