1 17 18 19 20 package org.apache.lenya.cms.ac.cocoon; 21 22 import java.util.ArrayList ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.Map ; 26 27 import org.apache.avalon.framework.component.ComponentManager; 28 import org.apache.avalon.framework.component.ComponentSelector; 29 import org.apache.cocoon.ProcessingException; 30 import org.apache.lenya.ac.AccessControlException; 31 import org.apache.lenya.ac.AccessControllerResolver; 32 import org.apache.lenya.ac.Accreditable; 33 import org.apache.lenya.ac.AccreditableManager; 34 import org.apache.lenya.ac.Identity; 35 import org.apache.lenya.ac.Item; 36 import org.apache.lenya.ac.Policy; 37 import org.apache.lenya.ac.Role; 38 import org.apache.lenya.ac.User; 39 import org.apache.lenya.ac.UserManager; 40 import org.apache.lenya.ac.impl.Credential; 41 import org.apache.lenya.ac.impl.DefaultAccessController; 42 import org.apache.lenya.ac.impl.DefaultPolicy; 43 import org.apache.lenya.ac.impl.InheritingPolicyManager; 44 import org.apache.lenya.cms.publication.PageEnvelope; 45 import org.apache.lenya.cms.publication.PageEnvelopeException; 46 import org.apache.lenya.cms.publication.PageEnvelopeFactory; 47 48 51 public class PolicyHelper { 52 53 56 public PolicyHelper() { 57 } 58 59 private DefaultAccessController accessController; 60 private ComponentSelector selector; 61 private AccessControllerResolver resolver; 62 private InheritingPolicyManager policyManager; 63 private ComponentManager manager; 64 private String url; 65 66 73 public void setup(Map objectModel, ComponentManager manager, String area) 74 throws ProcessingException { 75 76 this.manager = manager; 77 78 accessController = null; 79 selector = null; 80 resolver = null; 81 policyManager = null; 82 83 url = computeUrl(objectModel, area); 84 85 try { 86 selector = 87 (ComponentSelector) manager.lookup(AccessControllerResolver.ROLE + "Selector"); 88 resolver = 89 (AccessControllerResolver) selector.select( 90 AccessControllerResolver.DEFAULT_RESOLVER); 91 92 accessController = (DefaultAccessController) resolver.resolveAccessController(url); 93 94 policyManager = (InheritingPolicyManager) accessController.getPolicyManager(); 95 } catch (Exception e) { 96 throw new ProcessingException("Obtaining credentials failed: ", e); 97 } 98 } 99 100 103 public void tearDown() { 104 if (selector != null) { 105 if (resolver != null) { 106 if (accessController != null) { 107 resolver.release(accessController); 108 } 109 selector.release(resolver); 110 } 111 manager.release(selector); 112 } 113 } 114 115 120 public CredentialWrapper[] getURICredentials() throws ProcessingException { 121 return getCredentials(true); 122 } 123 124 130 public CredentialWrapper[] getParentCredentials() throws ProcessingException { 131 return getCredentials(false); 132 } 133 134 141 public CredentialWrapper[] getCredentials(boolean urlOnly) throws ProcessingException { 142 143 List credentials = new ArrayList (); 144 145 DefaultPolicy policies[] = getPolicies(urlOnly); 146 List policyCredentials = new ArrayList (); 147 for (int i = 0; i < policies.length; i++) { 148 Credential[] creds = policies[i].getCredentials(); 149 for (int j = 0; j < creds.length; j++) { 150 policyCredentials.add(creds[j]); 151 } 152 } 153 for (Iterator i = policyCredentials.iterator(); i.hasNext();) { 154 Credential credential = (Credential) i.next(); 155 Accreditable accreditable = credential.getAccreditable(); 156 Role[] roles = credential.getRoles(); 157 for (int j = 0; j < roles.length; j++) { 158 credentials.add(new CredentialWrapper(accreditable, roles[j])); 159 } 160 } 161 return (CredentialWrapper[]) credentials.toArray(new CredentialWrapper[credentials.size()]); 162 } 163 164 172 private static String computeUrl(Map objectModel, String area) throws ProcessingException { 173 PageEnvelope envelope; 174 try { 175 envelope = PageEnvelopeFactory.getInstance().getPageEnvelope(objectModel); 176 } catch (PageEnvelopeException e) { 177 throw new ProcessingException(e); 178 } 179 String url = 180 "/" + envelope.getPublication().getId() + "/" + area + envelope.getDocument().getId(); 181 return url; 182 } 183 184 191 protected DefaultPolicy[] getPolicies(boolean onlyUrl) throws ProcessingException { 192 193 DefaultPolicy[] policies; 194 195 try { 196 if (onlyUrl) { 197 policies = new DefaultPolicy[1]; 198 AccreditableManager manager = accessController.getAccreditableManager(); 199 policies[0] = policyManager.buildSubtreePolicy(manager, url); 200 } else { 201 String ancestorUrl = ""; 202 203 String currentUrl = url; 204 if (currentUrl.endsWith("/")) { 205 currentUrl = currentUrl.substring(0, currentUrl.length() - 1); 206 } 207 208 int lastSlashIndex = currentUrl.lastIndexOf("/"); 209 if (lastSlashIndex != -1) { 210 ancestorUrl = currentUrl.substring(0, lastSlashIndex); 211 } 212 policies = 213 policyManager.getPolicies( 214 accessController.getAccreditableManager(), 215 ancestorUrl); 216 } 217 } catch (AccessControlException e) { 218 throw new ProcessingException(e); 219 } 220 221 return policies; 222 } 223 224 public static final String ADD = "add"; 225 public static final String DELETE = "delete"; 226 227 234 public void manipulateCredential(Item item, Role role, String operation) 235 throws ProcessingException { 236 237 try { 238 DefaultPolicy policy = 239 policyManager.buildSubtreePolicy(accessController.getAccreditableManager(), url); 240 Accreditable accreditable = (Accreditable) item; 241 242 if (operation.equals(ADD)) { 243 policy.addRole(accreditable, role); 244 } else if (operation.equals(DELETE)) { 245 policy.removeRole(accreditable, role); 246 } 247 248 policyManager.saveSubtreePolicy(url, policy); 249 250 } catch (Exception e) { 251 throw new ProcessingException("Manipulating credential failed: ", e); 252 } 253 } 254 255 260 public boolean isAncestorSSLProtected() throws ProcessingException { 261 boolean ssl; 262 try { 263 String ancestorUrl = ""; 264 int lastSlashIndex = url.lastIndexOf("/"); 265 if (lastSlashIndex != -1) { 266 ancestorUrl = url.substring(0, lastSlashIndex); 267 } 268 Policy policy = 269 policyManager.getPolicy(accessController.getAccreditableManager(), ancestorUrl); 270 ssl = policy.isSSLProtected(); 271 } catch (AccessControlException e) { 272 throw new ProcessingException("Resolving policy failed: ", e); 273 } 274 return ssl; 275 } 276 277 282 public boolean isUrlSSLProtected() throws ProcessingException { 283 boolean ssl; 284 try { 285 DefaultPolicy policy = 286 policyManager.buildSubtreePolicy(accessController.getAccreditableManager(), url); 287 ssl = policy.isSSLProtected(); 288 } catch (AccessControlException e) { 289 throw new ProcessingException("Resolving policy failed: ", e); 290 } 291 return ssl; 292 } 293 294 299 public void setUrlSSLProtected(boolean ssl) throws ProcessingException { 300 try { 301 DefaultPolicy policy = 302 policyManager.buildSubtreePolicy(accessController.getAccreditableManager(), url); 303 policy.setSSL(ssl); 304 policyManager.saveSubtreePolicy(url, policy); 305 } catch (AccessControlException e) { 306 throw new ProcessingException("Resolving policy failed: ", e); 307 } 308 } 309 310 316 public User[] getUsersWithRole(String roleId) throws ProcessingException { 317 List users = new ArrayList (); 318 try { 319 Policy policy = 320 policyManager.getPolicy(accessController.getAccreditableManager(), getUrl()); 321 UserManager userManager = accessController.getAccreditableManager().getUserManager(); 322 User[] userArray = userManager.getUsers(); 323 for (int i = 0; i < userArray.length; i++) { 324 Identity identity = new Identity(); 325 identity.addIdentifiable(userArray[i]); 326 Role[] roles = policy.getRoles(identity); 327 for (int roleIndex = 0; roleIndex < roles.length; roleIndex++) { 328 if (roles[roleIndex].getId().equals(roleId)) { 329 users.add(userArray[i]); 330 } 331 } 332 } 333 } catch (AccessControlException e) { 334 throw new ProcessingException(e); 335 } 336 return (User[]) users.toArray(new User[users.size()]); 337 } 338 339 343 public String getUrl() { 344 return url; 345 } 346 347 } 348 | Popular Tags |