diff --git a/core/src/main/java/org/acegisecurity/intercept/method/MethodDefinitionMap.java b/core/src/main/java/org/acegisecurity/intercept/method/MethodDefinitionMap.java index ac667ec003..93802b0cd2 100644 --- a/core/src/main/java/org/acegisecurity/intercept/method/MethodDefinitionMap.java +++ b/core/src/main/java/org/acegisecurity/intercept/method/MethodDefinitionMap.java @@ -17,6 +17,7 @@ package org.acegisecurity.intercept.method; import org.acegisecurity.ConfigAttribute; import org.acegisecurity.ConfigAttributeDefinition; +import org.acegisecurity.SecurityConfig; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -232,4 +233,25 @@ public class MethodDefinitionMap extends AbstractMethodDefinitionSource { definition.addConfigAttribute((ConfigAttribute) attribs.next()); } } + + /** + * Easier configuration of the instance, using {@link MethodDefinitionSourceMapping}. + * + * @param mappings {@link List} of {@link MethodDefinitionSourceMapping} objects. + */ + public void setMappings(List mappings) { + Iterator it = mappings.iterator(); + while (it.hasNext()) { + MethodDefinitionSourceMapping mapping = (MethodDefinitionSourceMapping) it.next(); + ConfigAttributeDefinition configDefinition = new ConfigAttributeDefinition(); + + Iterator configAttributesIt = mapping.getConfigAttributes().iterator(); + while (configAttributesIt.hasNext()) { + String s = (String) configAttributesIt.next(); + configDefinition.addConfigAttribute(new SecurityConfig(s)); + } + + addSecureMethod(mapping.getMethodName(), configDefinition); + } + } } diff --git a/core/src/main/java/org/acegisecurity/intercept/method/MethodDefinitionSourceEditor.java b/core/src/main/java/org/acegisecurity/intercept/method/MethodDefinitionSourceEditor.java index 090a064f52..b698eea553 100644 --- a/core/src/main/java/org/acegisecurity/intercept/method/MethodDefinitionSourceEditor.java +++ b/core/src/main/java/org/acegisecurity/intercept/method/MethodDefinitionSourceEditor.java @@ -15,17 +15,17 @@ package org.acegisecurity.intercept.method; -import org.acegisecurity.ConfigAttributeDefinition; -import org.acegisecurity.ConfigAttributeEditor; - import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.propertyeditors.PropertiesEditor; +import org.springframework.util.StringUtils; import java.beans.PropertyEditorSupport; +import java.util.ArrayList; import java.util.Iterator; +import java.util.List; import java.util.Properties; @@ -56,20 +56,24 @@ public class MethodDefinitionSourceEditor extends PropertyEditorSupport { Properties props = (Properties) propertiesEditor.getValue(); // Now we have properties, process each one individually - ConfigAttributeEditor configAttribEd = new ConfigAttributeEditor(); + List mappings = new ArrayList(); for (Iterator iter = props.keySet().iterator(); iter.hasNext();) { String name = (String) iter.next(); String value = props.getProperty(name); - // Convert value to series of security configuration attributes - configAttribEd.setAsText(value); + MethodDefinitionSourceMapping mapping = new MethodDefinitionSourceMapping(); + mapping.setMethodName(name); - ConfigAttributeDefinition attr = (ConfigAttributeDefinition) configAttribEd.getValue(); + String[] tokens = StringUtils.commaDelimitedListToStringArray(value); - // Register name and attribute - source.addSecureMethod(name, attr); + for (int i = 0; i < tokens.length; i++) { + mapping.addConfigAttribute(tokens[i].trim()); + } + + mappings.add(mapping); } + source.setMappings(mappings); } setValue(source); diff --git a/core/src/main/java/org/acegisecurity/intercept/method/MethodDefinitionSourceMapping.java b/core/src/main/java/org/acegisecurity/intercept/method/MethodDefinitionSourceMapping.java new file mode 100644 index 0000000000..d1156c4562 --- /dev/null +++ b/core/src/main/java/org/acegisecurity/intercept/method/MethodDefinitionSourceMapping.java @@ -0,0 +1,82 @@ +/* Copyright 2006 Acegi Technology Pty Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.acegisecurity.intercept.method; + +import java.util.ArrayList; +import java.util.List; + +import org.acegisecurity.ConfigAttribute; + +/** + * Configuration entry for {@link MethodDefinitionSource}, that holds + * the method to be protected and the {@link ConfigAttribute}s as {@link String} + * that apply to that url. + * + * @author Carlos Sanchez + * @version $Id$ + * @since 1.1 + */ +public class MethodDefinitionSourceMapping { + + private String methodName; + + private List configAttributes = new ArrayList(); + + /** + * Name of the method to be secured, including package and class name. + * eg. org.mydomain.MyClass.myMethod + * + * @param methodName + */ + public void setMethodName(String methodName) { + this.methodName = methodName; + } + + /** + * Name of the method to be secured. + * + * @return the name of the method + */ + public String getMethodName() { + return methodName; + } + + /** + * + * @param roles {@link List}<{@link String}> + */ + public void setConfigAttributes(List roles) { + this.configAttributes = roles; + } + + /** + * + * @return {@link List}<{@link String}> + */ + public List getConfigAttributes() { + return configAttributes; + } + + /** + * Add a {@link ConfigAttribute} as {@link String} + * + * @param configAttribute + */ + public void addConfigAttribute(String configAttribute) { + configAttributes.add(configAttribute); + } + +}