1 17 18 19 20 package org.apache.lenya.cms.cocoon.transformation; 21 22 import java.io.IOException ; 23 import java.util.Map ; 24 25 import org.apache.avalon.framework.activity.Disposable; 26 import org.apache.avalon.framework.parameters.Parameters; 27 import org.apache.avalon.framework.service.ServiceSelector; 28 import org.apache.cocoon.ProcessingException; 29 import org.apache.cocoon.environment.ObjectModelHelper; 30 import org.apache.cocoon.environment.Request; 31 import org.apache.cocoon.environment.SourceResolver; 32 import org.apache.cocoon.transformation.AbstractSAXTransformer; 33 import org.apache.lenya.ac.AccessControlException; 34 import org.apache.lenya.ac.AccessController; 35 import org.apache.lenya.ac.AccessControllerResolver; 36 import org.apache.lenya.ac.AccreditableManager; 37 import org.apache.lenya.ac.Authorizer; 38 import org.apache.lenya.ac.Identity; 39 import org.apache.lenya.ac.Policy; 40 import org.apache.lenya.ac.PolicyManager; 41 import org.apache.lenya.ac.Role; 42 import org.apache.lenya.ac.impl.DefaultAccessController; 43 import org.apache.lenya.ac.impl.PolicyAuthorizer; 44 import org.apache.lenya.cms.publication.SiteTree; 45 import org.apache.lenya.cms.publication.SiteTreeNodeImpl; 46 import org.apache.lenya.util.ServletHelper; 47 import org.xml.sax.Attributes ; 48 import org.xml.sax.SAXException ; 49 import org.xml.sax.helpers.AttributesImpl ; 50 51 57 public class AccessControlSitetreeTransformer 58 extends AbstractSAXTransformer 59 implements Disposable { 60 61 public static final String ATTRIBUTE_PROTECTED = "protected"; 62 public static final String PARAMETER_PUBLICATION_ID = "publication-id"; 63 public static final String PARAMETER_AREA = "area"; 64 65 private String documentId; 66 private ServiceSelector serviceSelector; 67 private PolicyManager policyManager; 68 private AccessControllerResolver acResolver; 69 private AccreditableManager accreditableManager; 70 private Identity identity; 71 private String urlPrefix; 72 73 76 public void setup(SourceResolver resolver, Map objectModel, String src, Parameters par) 77 throws ProcessingException, SAXException , IOException { 78 super.setup(resolver, objectModel, src, par); 79 80 serviceSelector = null; 81 acResolver = null; 82 policyManager = null; 83 84 identity = Identity.getIdentity(request.getSession(false)); 85 86 try { 87 String publicationId = par.getParameter(PARAMETER_PUBLICATION_ID); 88 String area = par.getParameter(PARAMETER_AREA); 89 90 if (getLogger().isDebugEnabled()) { 91 getLogger().debug("Setting up transformer"); 92 getLogger().debug(" Identity: [" + identity + "]"); 93 getLogger().debug(" Publication ID: [" + publicationId + "]"); 94 getLogger().debug(" Area: [" + area + "]"); 95 } 96 97 urlPrefix = "/" + publicationId + "/" + area; 98 99 Request request = ObjectModelHelper.getRequest(objectModel); 100 101 serviceSelector = 102 (ServiceSelector) manager.lookup(AccessControllerResolver.ROLE + "Selector"); 103 104 acResolver = 105 (AccessControllerResolver) serviceSelector.select( 106 AccessControllerResolver.DEFAULT_RESOLVER); 107 108 if (getLogger().isDebugEnabled()) { 109 getLogger().debug(" Resolved AC resolver [" + acResolver + "]"); 110 } 111 112 String webappUrl = ServletHelper.getWebappURI(request); 113 AccessController accessController = acResolver.resolveAccessController(webappUrl); 114 115 if (accessController instanceof DefaultAccessController) { 116 DefaultAccessController defaultAccessController = 117 (DefaultAccessController) accessController; 118 119 accreditableManager = defaultAccessController.getAccreditableManager(); 120 121 Authorizer[] authorizers = defaultAccessController.getAuthorizers(); 122 for (int i = 0; i < authorizers.length; i++) { 123 if (authorizers[i] instanceof PolicyAuthorizer) { 124 PolicyAuthorizer policyAuthorizer = (PolicyAuthorizer) authorizers[i]; 125 policyManager = policyAuthorizer.getPolicyManager(); 126 } 127 } 128 } 129 130 if (getLogger().isDebugEnabled()) { 131 getLogger().debug(" Using policy manager [" + policyManager + "]"); 132 } 133 } catch (Exception e) { 134 throw new ProcessingException(e); 135 } 136 137 } 138 139 142 public void dispose() { 143 if (getLogger().isDebugEnabled()) { 144 getLogger().debug("Disposing transformer"); 145 } 146 if (serviceSelector != null) { 147 if (acResolver != null) { 148 serviceSelector.release(acResolver); 149 } 150 manager.release(serviceSelector); 151 } 152 } 153 154 157 public void startDocument() throws SAXException { 158 super.startDocument(); 159 documentId = ""; 160 } 161 162 165 public void startElement(String uri, String localName, String raw, Attributes attr) 166 throws SAXException { 167 168 Attributes attributes = attr; 169 170 if (isFragmentNode(uri, localName)) { 171 String area = attr.getValue("area"); String base = attr.getValue("base"); 173 if (area!=null && base!=null) { 174 documentId = "/"+area+base; 175 } 176 } 177 if (isNode(uri, localName)) { 178 String id = attr.getValue(SiteTreeNodeImpl.ID_ATTRIBUTE_NAME); 179 if (id != null) { 180 documentId += "/" + id; 181 } 182 183 if (getLogger().isDebugEnabled()) { 184 getLogger().debug("Checking node"); 185 getLogger().debug(" Document ID: [" + documentId + "]"); 186 getLogger().debug(" URL: [" + urlPrefix + documentId + "]"); 187 } 188 189 try { 190 String url = urlPrefix + documentId; 191 Policy policy = policyManager.getPolicy(accreditableManager, url); 192 Role[] roles = policy.getRoles(identity); 193 194 getLogger().debug(" Roles: [" + roles.length + "]"); 195 196 if (roles.length == 0) { 197 getLogger().debug(" Adding attribute [protected='true']"); 198 199 AttributesImpl attributesImpl = new AttributesImpl (attributes); 200 attributesImpl.addAttribute( 201 "", 202 ATTRIBUTE_PROTECTED, 203 ATTRIBUTE_PROTECTED, 204 "", 205 Boolean.toString(true)); 206 attributes = attributesImpl; 207 } 208 } catch (AccessControlException e) { 209 throw new SAXException (e); 210 } 211 } 212 213 super.startElement(uri, localName, raw, attributes); 214 } 215 216 219 public void endElement(String uri, String localName, String raw) throws SAXException { 220 super.endElement(uri, localName, raw); 221 if (isNode(uri, localName) && documentId.length() > 0) { 222 documentId = documentId.substring(0, documentId.lastIndexOf("/")); 223 } 224 } 225 226 232 protected boolean isNode(String uri, String localName) { 233 return uri.equals(SiteTree.NAMESPACE_URI) 234 && (localName.equals(SiteTreeNodeImpl.NODE_NAME) || localName.equals("site")); 235 } 236 237 243 protected boolean isFragmentNode(String uri, String localName) { 244 return uri.equals(SiteTree.NAMESPACE_URI) 245 && (localName.equals("fragment")); 246 } 247 248 } 249 | Popular Tags |