1 17 18 package org.apache.lenya.cms.ac.usecase; 19 20 import java.util.Arrays ; 21 import java.util.List ; 22 23 import org.apache.avalon.framework.activity.Disposable; 24 import org.apache.avalon.framework.logger.AbstractLogEnabled; 25 import org.apache.avalon.framework.parameters.ParameterException; 26 import org.apache.avalon.framework.parameters.Parameterizable; 27 import org.apache.avalon.framework.parameters.Parameters; 28 import org.apache.avalon.framework.service.ServiceException; 29 import org.apache.avalon.framework.service.ServiceManager; 30 import org.apache.avalon.framework.service.Serviceable; 31 import org.apache.cocoon.environment.Request; 32 import org.apache.excalibur.source.SourceResolver; 33 import org.apache.lenya.ac.AccessControlException; 34 import org.apache.lenya.ac.Authorizer; 35 import org.apache.lenya.ac.Role; 36 import org.apache.lenya.ac.cache.CachingException; 37 import org.apache.lenya.ac.cache.SourceCache; 38 import org.apache.lenya.ac.impl.PolicyAuthorizer; 39 import org.apache.lenya.cms.publication.Publication; 40 import org.apache.lenya.cms.publication.PublicationFactory; 41 42 46 public class UsecaseAuthorizer 47 extends AbstractLogEnabled 48 implements Authorizer, Serviceable, Disposable, Parameterizable { 49 50 public static final String TYPE = "usecase"; 51 public static final String USECASE_PARAMETER = "lenya.usecase"; 52 53 private SourceCache cache; 54 private String configurationUri; 55 56 61 public SourceCache getCache() { 62 return cache; 63 } 64 65 71 protected String getConfigurationURI(Publication publication) { 72 return "context:///" 73 + Publication.PUBLICATION_PREFIX_URI 74 + "/" 75 + publication.getId() 76 + CONFIGURATION_FILE; 77 } 78 79 82 public boolean authorize(Request request) throws AccessControlException { 83 84 String usecase = request.getParameter(USECASE_PARAMETER); 85 boolean authorized = true; 86 87 SourceResolver resolver = null; 88 try { 89 resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE); 90 if (usecase != null) { 91 92 String configurationUri; 93 if (getConfigurationURI() != null) { 94 configurationUri = getConfigurationURI(); 95 } else { 96 Publication publication = PublicationFactory.getPublication(resolver, request); 97 configurationUri = getConfigurationURI(publication); 98 } 99 100 Role[] roles = PolicyAuthorizer.getRoles(request); 101 authorized = authorizeUsecase(usecase, roles, configurationUri); 102 } else { 103 getLogger().debug("No usecase to authorize. Granting access."); 104 } 105 } catch (Exception e) { 106 throw new AccessControlException(e); 107 } finally { 108 if (resolver != null) { 109 manager.release(resolver); 110 } 111 } 112 113 return authorized; 114 } 115 116 125 public boolean authorizeUsecase(String usecase, Role[] roles, String configurationUri) 126 throws AccessControlException { 127 getLogger().debug("Authorizing usecase [" + usecase + "]"); 128 boolean authorized = true; 129 130 UsecaseRolesBuilder builder = new UsecaseRolesBuilder(); 131 UsecaseRoles usecaseRoles; 132 try { 133 usecaseRoles = (UsecaseRoles) getCache().get(configurationUri, builder); 134 } catch (CachingException e) { 135 throw new AccessControlException(e); 136 } 137 138 if (usecaseRoles == null) { 139 throw new AccessControlException("Usecase policies configuration not found at [" + configurationUri + "]"); 140 } 141 142 if (usecaseRoles.hasRoles(usecase)) { 143 144 getLogger().debug("Roles for usecase found."); 145 146 List usecaseRoleIds = Arrays.asList(usecaseRoles.getRoles(usecase)); 147 148 int i = 0; 149 authorized = false; 150 while (!authorized && i < roles.length) { 151 authorized = usecaseRoleIds.contains(roles[i].getId()); 152 getLogger().debug( 153 "Authorization for role [" + roles[i].getId() + "] is [" + authorized + "]"); 154 i++; 155 } 156 } else { 157 getLogger().debug("No roles for usecase found. Granting access."); 158 } 159 return authorized; 160 } 161 162 private ServiceManager manager; 163 164 167 public void service(ServiceManager manager) throws ServiceException { 168 getLogger().debug("Servicing [" + getClass().getName() + "]"); 169 this.manager = manager; 170 this.cache = (SourceCache) manager.lookup(SourceCache.ROLE); 171 } 172 173 176 public void dispose() { 177 if (getCache() != null) { 178 manager.release(getCache()); 179 } 180 } 181 182 public static final String CONFIGURATION_FILE = "/config/ac/usecase-policies.xml"; 183 public static final String PARAMETER_CONFIGURATION = "configuration"; 184 185 188 public void parameterize(Parameters parameters) throws ParameterException { 189 if (parameters.isParameter(PARAMETER_CONFIGURATION)) { 190 this.configurationUri = parameters.getParameter(PARAMETER_CONFIGURATION); 191 } 192 } 193 194 199 public String getConfigurationURI() { 200 return configurationUri; 201 } 202 203 212 public boolean authorizeUsecase(String usecase, Role[] roles, Publication publication) 213 throws AccessControlException { 214 return authorizeUsecase(usecase, roles, getConfigurationURI(publication)); 215 } 216 217 } 218 | Popular Tags |