瀏覽代碼

Add MethodDefinitionSourceMapping for easier configuration

Carlos Sanchez 19 年之前
父節點
當前提交
69ec903088

+ 22 - 0
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);
+        }
+    }
 }

+ 13 - 9
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);
+
+                String[] tokens = StringUtils.commaDelimitedListToStringArray(value);
 
-                ConfigAttributeDefinition attr = (ConfigAttributeDefinition) configAttribEd.getValue();
+                for (int i = 0; i < tokens.length; i++) {
+                    mapping.addConfigAttribute(tokens[i].trim());
+                }
 
-                // Register name and attribute
-                source.addSecureMethod(name, attr);
+                mappings.add(mapping);
             }
+            source.setMappings(mappings);
         }
 
         setValue(source);

+ 82 - 0
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 <a href="mailto:carlos@apache.org">Carlos Sanchez</a>
+ * @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. <code>org.mydomain.MyClass.myMethod</code>
+     * 
+     * @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}&lt;{@link String}>
+     */
+    public void setConfigAttributes(List roles) {
+        this.configAttributes = roles;
+    }
+
+    /**
+     * 
+     * @return {@link List}&lt;{@link String}>
+     */
+    public List getConfigAttributes() {
+        return configAttributes;
+    }
+
+    /**
+     * Add a {@link ConfigAttribute} as {@link String}
+     * 
+     * @param configAttribute
+     */
+    public void addConfigAttribute(String configAttribute) {
+        configAttributes.add(configAttribute);
+    }
+
+}