1 23 24 package org.apache.slide.security; 25 26 import java.util.Enumeration ; 27 28 import org.apache.slide.common.Namespace; 29 import org.apache.slide.common.NamespaceConfig; 30 import org.apache.slide.common.ServiceAccessException; 31 import org.apache.slide.common.Uri; 32 import org.apache.slide.structure.ActionNode; 33 import org.apache.slide.structure.LinkNode; 34 import org.apache.slide.structure.ObjectNode; 35 import org.apache.slide.structure.ObjectNotFoundException; 36 import org.apache.slide.structure.SubjectNode; 37 import org.apache.slide.util.logger.Logger; 38 39 44 public final class SecurityImplAllGrant extends SecurityImpl implements Security { 45 46 47 protected static final String LOG_CHANNEL = SecurityImplAllGrant.class.getName(); 48 49 51 54 public SecurityImplAllGrant() { 55 super(); 56 } 57 63 public SecurityImplAllGrant(Namespace namespace, NamespaceConfig namespaceConfig) { 64 super(namespace, namespaceConfig); 65 } 66 67 68 80 public boolean hasPermission(ObjectNode object, SubjectNode subject, 81 ActionNode action) 82 throws ServiceAccessException, ObjectNotFoundException { 83 84 if (action.equals(ActionNode.DEFAULT)) { 86 return true; 87 } 88 89 boolean granted = false; 90 boolean denied = false; 91 boolean rootObjectReached = false; 92 93 ObjectNode courObject = object; 94 95 Uri subjectUri = namespace.getUri(subject.getUri()); 96 Uri actionUri = namespace.getUri(action.getUri()); 97 98 100 while (!granted && !denied && !rootObjectReached) { 101 102 Uri courUri = namespace.getUri(courObject.getUri()); 103 Enumeration permissions = courUri.getStore() 104 .enumeratePermissions(courUri); 105 106 while (!granted && !denied && permissions.hasMoreElements()) { 107 108 boolean oldGranted = granted; 109 boolean oldDenied = denied; 110 111 NodePermission permission = 112 (NodePermission) permissions.nextElement(); 113 String permissionSubject = permission.getSubjectUri(); 114 115 if (permissionSubject.equals(SubjectNode.SELF_URI)) { 116 boolean check; 117 check = object.getUri().equals(subjectUri.toString()); 118 if (permission.isInheritable()) { 119 String subjectUriString = subjectUri.toString(); 120 if(!subjectUriString.endsWith("/")) 121 subjectUriString = subjectUriString + "/"; 122 123 check |= object.getUri().startsWith(subjectUriString); 124 } 125 126 granted = (!permission.isNegative()) 128 && (check) 129 && (actionUri.toString() 130 .startsWith(permission.getActionUri())); 131 denied = (permission.isNegative()) 132 && (check) 133 && (actionUri.toString() 134 .startsWith(permission.getActionUri())); 135 136 } else if (permission.isInheritable() 137 || permission.getObjectUri().equals(object.getUri())) { 138 139 if (permissionSubject.startsWith("/") || permissionSubject.equals(SubjectNode.ALL_URI)) { 140 141 String permSubj = permission.getSubjectUri(); 143 String permActn = permission.getActionUri(); 144 boolean match = false; 145 if (permSubj.equals(SubjectNode.ALL_URI)) { 146 match = true; 147 } 148 else { 149 if(!permSubj.endsWith("/")) 150 permSubj = permSubj + "/"; 151 match = subjectUri.toString(). 152 equals(permission.getSubjectUri()) || 153 subjectUri.toString().startsWith(permSubj); 154 } 155 if (permActn.equals(ActionNode.ALL_URI)) { 156 match &= true; 157 } 158 else { 159 match &= actionUri.toString(). 160 startsWith(permActn); 161 } 162 163 granted = (!permission.isNegative()) && match; 164 denied = permission.isNegative() && match; 165 166 } else if (permissionSubject.startsWith("+")) { 167 168 Uri permissionSubjectUri = 170 namespace.getUri(permissionSubject.substring(1)); 171 ObjectNode group; 172 try { 173 group = permissionSubjectUri.getStore().retrieveObject(permissionSubjectUri); 174 } catch (ObjectNotFoundException onfe) { 175 namespace.getLogger().log( 176 "Gracefully ignoring permission of dangling subject " + permissionSubjectUri, 177 onfe, 178 LOG_CHANNEL, 179 Logger.WARNING); 180 continue; 182 } 183 if (group instanceof 186 org.apache.slide.structure.GroupNode ) { 187 if (group.hasChildren()) { 188 Enumeration groupMembers = 189 group.enumerateChildren(); 190 while (groupMembers.hasMoreElements()) { 193 194 oldGranted = granted; 195 oldDenied = denied; 196 197 Uri childUri = 198 namespace.getUri 199 ((String ) groupMembers.nextElement()); 200 ObjectNode childNode; 201 try { 202 childNode = childUri.getStore().retrieveObject(childUri); 203 } catch (ObjectNotFoundException onfe) { 204 namespace.getLogger().log( 205 "Gracefully ignoring permission of dangling subject " 206 + childUri, 207 onfe, 208 LOG_CHANNEL, 209 Logger.WARNING); 210 continue; 212 } 213 String childSubjectUri = childNode 214 instanceof LinkNode ? 215 ((LinkNode) childNode) 216 .getLinkedUri() : 217 childNode.getUri() ; 218 219 String testUri; 220 if(!childSubjectUri.endsWith("/")) 221 testUri = childSubjectUri+"/"; 222 else 223 testUri = childSubjectUri; 224 225 boolean match = subjectUri.toString(). 226 equals(childSubjectUri) || 227 subjectUri.toString(). 228 startsWith(testUri); 229 match &= actionUri.toString(). 230 startsWith(permission.getActionUri()); 231 232 granted = (!permission.isNegative()) && 233 match; 234 denied = permission.isNegative() && match; 235 236 granted = granted | oldGranted; 237 denied = denied | oldDenied; 238 239 } 240 } 241 } 242 243 } else { 244 245 granted = (!permission.isNegative()) 247 && (hasRole(subject, permissionSubject)) 248 && (actionUri.toString() 249 .startsWith(permission.getActionUri())); 250 denied = (permission.isNegative()) 251 && (hasRole(subject, permissionSubject)) 252 && (actionUri.toString() 253 .startsWith(permission.getActionUri())); 254 255 } 256 257 } 258 259 granted = granted | oldGranted; 260 denied = denied | oldDenied; 261 262 } 263 264 Uri parentUri = courUri.getParentUri(); 265 266 if (parentUri != null) { 267 courObject = parentUri.getStore() 268 .retrieveObject(parentUri); 269 } else { 270 rootObjectReached = true; 271 } 272 } 273 274 if (denied) { 277 return false; 278 } 279 280 if (!granted) { 281 return false; 282 } 283 284 return true; 285 286 } 287 } 288 | Popular Tags |