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.Authorizer; 37 import org.apache.lenya.ac.Role; 38 import org.apache.lenya.ac.impl.DefaultAccessController; 39 import org.apache.lenya.ac.impl.PolicyAuthorizer; 40 import org.apache.lenya.cms.ac.usecase.UsecaseAuthorizer; 41 import org.apache.lenya.cms.publication.Publication; 42 import org.apache.lenya.cms.publication.PublicationFactory; 43 import org.apache.lenya.util.ServletHelper; 44 import org.xml.sax.Attributes ; 45 import org.xml.sax.SAXException ; 46 import org.xml.sax.helpers.AttributesImpl ; 47 48 52 public class UsecaseMenuTransformer extends AbstractSAXTransformer implements Disposable { 53 54 public static final String MENU_ELEMENT = "menu"; 55 public static final String ITEM_ELEMENT = "item"; 56 public static final String USECASE_ATTRIBUTE = "usecase"; 57 public static final String NAMESPACE = "http://apache.org/cocoon/lenya/usecase/1.0"; 58 59 62 public void startElement(String uri, String localName, String raw, Attributes attr) 63 throws SAXException { 64 65 Attributes attributes = attr; 66 67 if (authorizer != null && localName.equals(ITEM_ELEMENT)) { 68 String usecase = attr.getValue(NAMESPACE, USECASE_ATTRIBUTE); 69 70 if (usecase != null) { 72 getLogger().debug("Found usecase [" + usecase + "]"); 73 74 try { 75 if (!authorizer.authorizeUsecase(usecase, roles, publication)) { 76 getLogger().debug("Usecase not authorized"); 77 int hrefIndex = attributes.getIndex("href"); 78 if (hrefIndex > -1) { 79 attributes = new AttributesImpl (attr); 80 ((AttributesImpl ) attributes).removeAttribute(hrefIndex); 81 } 82 } 83 } catch (AccessControlException e) { 84 throw new SAXException (e); 85 } 86 } 87 } 88 89 super.startElement(uri, localName, raw, attributes); 90 91 } 92 93 private UsecaseAuthorizer authorizer; 94 private ServiceSelector serviceSelector = null; 95 private Role[] roles; 96 private Publication publication; 97 private AccessControllerResolver acResolver; 98 99 102 public void setup(SourceResolver resolver, Map objectModel, String src, Parameters parameters) 103 throws ProcessingException, SAXException , IOException { 104 105 super.setup(resolver, objectModel, src, parameters); 106 107 getLogger().debug("Setting up transformer"); 108 109 serviceSelector = null; 110 acResolver = null; 111 authorizer = null; 112 113 Request request = ObjectModelHelper.getRequest(objectModel); 114 115 try { 116 roles = PolicyAuthorizer.getRoles(request); 117 118 publication = PublicationFactory.getPublication(objectModel); 119 120 serviceSelector = 121 (ServiceSelector) manager.lookup(AccessControllerResolver.ROLE + "Selector"); 122 acResolver = 123 (AccessControllerResolver) serviceSelector.select( 124 AccessControllerResolver.DEFAULT_RESOLVER); 125 getLogger().debug("Resolved AC resolver [" + acResolver + "]"); 126 127 String webappUrl = ServletHelper.getWebappURI(request); 128 AccessController accessController = acResolver.resolveAccessController(webappUrl); 129 130 if (accessController instanceof DefaultAccessController) { 131 DefaultAccessController defaultAccessController = 132 (DefaultAccessController) accessController; 133 Authorizer[] authorizers = defaultAccessController.getAuthorizers(); 134 for (int i = 0; i < authorizers.length; i++) { 135 if (authorizers[i] instanceof UsecaseAuthorizer) { 136 authorizer = (UsecaseAuthorizer) authorizers[i]; 137 } 138 } 139 } 140 141 getLogger().debug("Using authorizer [" + authorizer + "]"); 142 } catch (Exception e) { 143 throw new ProcessingException(e); 144 } 145 } 146 147 150 public void dispose() { 151 getLogger().debug("Disposing transformer"); 152 if (serviceSelector != null) { 153 if (acResolver != null) { 154 serviceSelector.release(acResolver); 155 } 156 manager.release(serviceSelector); 157 } 158 } 159 160 } 161 | Popular Tags |