You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shiro.apache.org by bd...@apache.org on 2018/04/02 22:38:48 UTC

[3/5] shiro git commit: revision of overly complex changes.

revision of overly complex changes.

Reverted the [role_config] options from text configuration and
associated testing.

Added ability to set the case sensitivity on WildcardPermissionsResolver
so that it can be set from the Shiro.ini [main] section.

Project: http://git-wip-us.apache.org/repos/asf/shiro/repo
Commit: http://git-wip-us.apache.org/repos/asf/shiro/commit/2f949c77
Tree: http://git-wip-us.apache.org/repos/asf/shiro/tree/2f949c77
Diff: http://git-wip-us.apache.org/repos/asf/shiro/diff/2f949c77

Branch: refs/heads/master
Commit: 2f949c779b1c68cd8ce7cfadaeb62e3b45ac00d3
Parents: f66ce5a
Author: claude <cl...@claude-XPS-15-9560>
Authored: Thu Jan 11 11:10:21 2018 +0000
Committer: claude <cl...@claude-XPS-15-9560>
Committed: Thu Jan 11 11:10:21 2018 +0000

----------------------------------------------------------------------
 .../permission/WildcardPermissionResolver.java  |  7 ++
 .../org/apache/shiro/realm/text/IniRealm.java   | 13 +---
 .../realm/text/TextConfigurationRealm.java      | 82 +-------------------
 .../realm/text/TextConfigurationRealmTest.java  | 69 ----------------
 4 files changed, 9 insertions(+), 162 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/shiro/blob/2f949c77/core/src/main/java/org/apache/shiro/authz/permission/WildcardPermissionResolver.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/shiro/authz/permission/WildcardPermissionResolver.java b/core/src/main/java/org/apache/shiro/authz/permission/WildcardPermissionResolver.java
index 3d6fd6a..db0f8d2 100644
--- a/core/src/main/java/org/apache/shiro/authz/permission/WildcardPermissionResolver.java
+++ b/core/src/main/java/org/apache/shiro/authz/permission/WildcardPermissionResolver.java
@@ -49,6 +49,13 @@ public class WildcardPermissionResolver implements PermissionResolver {
     }
 
     /**
+     * Set the case sensitivity of the resolved Wildcard permissions.
+     * @param state the caseSensitive flag state for resolved permissions.
+     */
+    public void setCaseSensitive(boolean state) {
+        this.caseSensitive = state;
+    }
+    /**
      * Return true if this resolver produces case sensitive permissions.
      * @return true if this resolver produces case sensitive permissions.
      */

http://git-wip-us.apache.org/repos/asf/shiro/blob/2f949c77/core/src/main/java/org/apache/shiro/realm/text/IniRealm.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/shiro/realm/text/IniRealm.java b/core/src/main/java/org/apache/shiro/realm/text/IniRealm.java
index 15773e7..3a0540c 100644
--- a/core/src/main/java/org/apache/shiro/realm/text/IniRealm.java
+++ b/core/src/main/java/org/apache/shiro/realm/text/IniRealm.java
@@ -29,14 +29,11 @@ import org.slf4j.LoggerFactory;
  * {@link org.apache.shiro.authc.SimpleAccount SimpleAccount} instances based on
  * {@link Ini} configuration.
  * <p/>
- * This implementation looks for three {@link Ini.Section sections} in the {@code Ini} configuration:
+ * This implementation looks for two {@link Ini.Section sections} in the {@code Ini} configuration:
  * <pre>
  * [users]
  * # One or more {@link org.apache.shiro.realm.text.TextConfigurationRealm#setUserDefinitions(String) user definitions}
  * ...
- * [roles_config]
- * # One or more {@link org.apache.shiro.realm.text.TextConfigurationRealm#setRoleConfigDefinitions(String) role configuration definitions}
- * ...
  * [roles]
  * # One or more {@link org.apache.shiro.realm.text.TextConfigurationRealm#setRoleDefinitions(String) role definitions}</pre>
  * <p/>
@@ -49,7 +46,6 @@ public class IniRealm extends TextConfigurationRealm {
 
     public static final String USERS_SECTION_NAME = "users";
     public static final String ROLES_SECTION_NAME = "roles";
-    public static final String ROLES_CONFIG_SECTION_NAME = "roles_config";
 
     private static transient final Logger log = LoggerFactory.getLogger(IniRealm.class);
 
@@ -172,19 +168,12 @@ public class IniRealm extends TextConfigurationRealm {
         processDefinitions(ini);
     }
 
-    
     private void processDefinitions(Ini ini) {
         if (CollectionUtils.isEmpty(ini)) {
             log.warn("{} defined, but the ini instance is null or empty.", getClass().getSimpleName());
             return;
         }
 
-        Ini.Section rolesConfigSection = ini.getSection(ROLES_CONFIG_SECTION_NAME);
-        if (!CollectionUtils.isEmpty(rolesConfigSection)) {
-            log.debug("Discovered the [{}] section.  Processing...", ROLES_CONFIG_SECTION_NAME);
-            processRoleConfigDefinitions(rolesConfigSection);           
-        }
-        
         Ini.Section rolesSection = ini.getSection(ROLES_SECTION_NAME);
         if (!CollectionUtils.isEmpty(rolesSection)) {
             log.debug("Discovered the [{}] section.  Processing...", ROLES_SECTION_NAME);

http://git-wip-us.apache.org/repos/asf/shiro/blob/2f949c77/core/src/main/java/org/apache/shiro/realm/text/TextConfigurationRealm.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/shiro/realm/text/TextConfigurationRealm.java b/core/src/main/java/org/apache/shiro/realm/text/TextConfigurationRealm.java
index c2ef3fe..2b9344d 100644
--- a/core/src/main/java/org/apache/shiro/realm/text/TextConfigurationRealm.java
+++ b/core/src/main/java/org/apache/shiro/realm/text/TextConfigurationRealm.java
@@ -21,15 +21,11 @@ package org.apache.shiro.realm.text;
 import org.apache.shiro.authc.SimpleAccount;
 import org.apache.shiro.authz.Permission;
 import org.apache.shiro.authz.SimpleRole;
-import org.apache.shiro.authz.permission.PermissionResolver;
-import org.apache.shiro.authz.permission.WildcardPermissionResolver;
 import org.apache.shiro.config.ConfigurationException;
 import org.apache.shiro.realm.SimpleAccountRealm;
 import org.apache.shiro.util.PermissionUtils;
 import org.apache.shiro.util.StringUtils;
 
-import java.lang.reflect.Constructor;
-import java.lang.reflect.InvocationTargetException;
 import java.text.ParseException;
 import java.util.Collection;
 import java.util.HashMap;
@@ -58,7 +54,6 @@ public class TextConfigurationRealm extends SimpleAccountRealm {
 
     private volatile String userDefinitions;
     private volatile String roleDefinitions;
-    private volatile String roleConfigDefinitions;
 
     public TextConfigurationRealm() {
         super();
@@ -103,7 +98,7 @@ public class TextConfigurationRealm extends SimpleAccountRealm {
     public String getRoleDefinitions() {
         return roleDefinitions;
     }
-    
+
     /**
      * Sets a newline (\n) delimited String that defines role-to-permission definitions.
      * <p/>
@@ -129,38 +124,9 @@ public class TextConfigurationRealm extends SimpleAccountRealm {
     public void setRoleDefinitions(String roleDefinitions) {
         this.roleDefinitions = roleDefinitions;
     }
-    
-    public String getRoleConfigDefinitions() {
-        return roleConfigDefinitions;
-    }
-
-    /**
-     * Sets a newline (\n) delimited String that defines role configuration definitions.
-     * <p/>
-     * <p>Each line in the string must be one of the following name value pairs:</p>
-     * <p/>
-     * <p><code><em>permissionResolver</em> = <em>PermissionResolverClass</em></code></p>
-     * <p/>
-     * <p>where <em>permissionsResolverClass</em> is the name of a class implementing PermissionResolver interface
-     * and that has a no-argument constructor.  If not set the WildCardPermissionsResolver is used.</p>
-     * <p/>
-     * 
-     * <p><code><em>caseSensitiveWildCardPermissions</em> = <em>true</em></code></p>
-     * <p/>
-     * <p>Sets the cases sensitive flag on the default WildCardPermissionsResolver.  Using this paramter
-     * has the side effect of resetting the permissionsResolver to WildCardPermissionsResolver. 
-     * <p/>
-     *
-     * @param roleConfigDefinitions the role configuration options to be parsed at initialization
-     */
-    public void setRoleConfigDefinitions(String roleConfigDefinitions)
-    {
-        this.roleConfigDefinitions = roleConfigDefinitions;
-    }
 
     protected void processDefinitions() {
         try {
-            processRoleConfigDefinitions();
             processRoleDefinitions();
             processUserDefinitions();
         } catch (ParseException e) {
@@ -169,51 +135,6 @@ public class TextConfigurationRealm extends SimpleAccountRealm {
         }
     }
 
-    protected void processRoleConfigDefinitions() throws ParseException {
-        String roleConfigDefinitions = getRoleConfigDefinitions();
-        if (roleConfigDefinitions == null) {
-            return;
-        }
-        Map<String, String> roleConfigs = toMap(toLines(roleConfigDefinitions));
-        processRoleConfigDefinitions(roleConfigs);
-    }
-    
-    protected void processRoleConfigDefinitions(Map<String, String> roleDefs) {
-        if (roleDefs == null || roleDefs.isEmpty()) {
-            return;
-        }
-        for (String cfgOption : roleDefs.keySet()) {
-            // use a no-arg permission resolver.
-            if (cfgOption.equals( "permissionResolver" ))
-            {
-                try {
-                    Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass( roleDefs.get(cfgOption) );
-                    Constructor<?> c = clazz.getConstructor();
-                    setPermissionResolver(  (PermissionResolver) c.newInstance() );
-                } catch (ClassNotFoundException e) {
-                    throw new IllegalArgumentException( String.format( "Unable to construct %s",roleDefs.get(cfgOption) ), e );
-                } catch (NoSuchMethodException e) {
-                    throw new IllegalArgumentException( String.format( "Unable to construct %s",roleDefs.get(cfgOption) ), e );
-                } catch (SecurityException e) {
-                    throw new IllegalArgumentException( String.format( "Unable to construct %s",roleDefs.get(cfgOption) ), e );
-                } catch (InstantiationException e) {
-                    throw new IllegalArgumentException( String.format( "Unable to construct %s",roleDefs.get(cfgOption) ), e );
-                } catch (IllegalAccessException e) {
-                    throw new IllegalArgumentException( String.format( "Unable to construct %s",roleDefs.get(cfgOption) ), e );
-                } catch (IllegalArgumentException e) {
-                    throw new IllegalArgumentException( String.format( "Unable to construct %s",roleDefs.get(cfgOption) ), e );
-                } catch (InvocationTargetException e) {
-                    throw new IllegalArgumentException( String.format( "Unable to construct %s",roleDefs.get(cfgOption) ), e );
-                }
-            
-            }
-            if (cfgOption.equals(  "caseSensitiveWildCardPermissions" ))
-            {
-                boolean b = Boolean.valueOf( roleDefs.get(cfgOption).trim() );
-                setPermissionResolver( new WildcardPermissionResolver( b ));
-            }          
-        }
-    }
     protected void processRoleDefinitions() throws ParseException {
         String roleDefinitions = getRoleDefinitions();
         if (roleDefinitions == null) {
@@ -223,7 +144,6 @@ public class TextConfigurationRealm extends SimpleAccountRealm {
         processRoleDefinitions(roleDefs);
     }
 
-   
     protected void processRoleDefinitions(Map<String, String> roleDefs) {
         if (roleDefs == null || roleDefs.isEmpty()) {
             return;

http://git-wip-us.apache.org/repos/asf/shiro/blob/2f949c77/core/src/test/java/org/apache/shiro/realm/text/TextConfigurationRealmTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/shiro/realm/text/TextConfigurationRealmTest.java b/core/src/test/java/org/apache/shiro/realm/text/TextConfigurationRealmTest.java
index 2a0892a..4b0b8c2 100644
--- a/core/src/test/java/org/apache/shiro/realm/text/TextConfigurationRealmTest.java
+++ b/core/src/test/java/org/apache/shiro/realm/text/TextConfigurationRealmTest.java
@@ -19,9 +19,6 @@
 package org.apache.shiro.realm.text;
 
 import org.apache.shiro.authz.AuthorizationException;
-import org.apache.shiro.authz.Permission;
-import org.apache.shiro.authz.permission.PermissionResolver;
-import org.apache.shiro.authz.permission.WildcardPermissionResolver;
 import org.apache.shiro.subject.PrincipalCollection;
 import org.apache.shiro.subject.SimplePrincipalCollection;
 import org.junit.Test;
@@ -250,57 +247,6 @@ public class TextConfigurationRealmTest {
         assertTrue("account doesn't exist when it should", realm.accountExists("user1"));
         testThread.test();
     }
-    
-    @Test
-    public void testProcessRoleConfigurationDefinitions_caseSensitive() throws InterruptedException  {
-       realm = new TestRealm() {
-            public void test(Thread runnable) throws InterruptedException {
-                   runnable.start();
-            }
-        };
-        TestThread testThread = new TestThread(new Runnable() {
-            public void run() {
-                try {
-                    realm.processRoleConfigDefinitions();
-                } catch (ParseException e) {
-                    fail("Unable to parse user definitions");
-                }
-            }
-        });
-        realm.setRoleConfigDefinitions( "caseSensitiveWildCardPermissions=true" );
-        Thread testTask = new Thread(testThread);
-        realm.test(testTask);
-        testTask.join(500);
-        PermissionResolver resolver = realm.getPermissionResolver();
-        assertTrue("Resolver does not implement WildCardPermissionsResolver", resolver instanceof WildcardPermissionResolver);
-        assertTrue("WildcardPermissionsResolver is not case sensitive", ((WildcardPermissionResolver) resolver).isCaseSensitive());
-        testThread.test();
-    }
-
-    @Test
-    public void testProcessRoleConfigurationDefinitions_PermissionResolver() throws InterruptedException  {
-       realm = new TestRealm() {
-            public void test(Thread runnable) throws InterruptedException {
-                runnable.start();
-            }
-        };
-        TestThread testThread = new TestThread(new Runnable() {
-            public void run() {
-                try {
-                    realm.processRoleConfigDefinitions();
-                } catch (ParseException e) {
-                    fail("Unable to parse user definitions");
-                }
-            }
-        });
-        realm.setRoleConfigDefinitions( "permissionResolver="+TestPermissionResolver.class.getName() );
-        Thread testTask = new Thread(testThread);
-        realm.test(testTask);
-        testTask.join(500);
-        PermissionResolver resolver = realm.getPermissionResolver();
-        assertTrue("Resolver is not instance of TestPermissionResolver", resolver instanceof TestPermissionResolver);
-        testThread.test();
-    }
 
     /*
      * A Class that captures a thread's assertion error.
@@ -333,19 +279,4 @@ public class TextConfigurationRealmTest {
     private abstract class TestRealm extends TextConfigurationRealm {
         abstract public void test(Thread runnable) throws InterruptedException;
     }
-    
-    /*
-     * Provides a class for permisison resolver replacement testing.
-     */
-    private static class TestPermissionResolver implements PermissionResolver {
-        
-        public TestPermissionResolver() {}
-
-        @Override
-        public Permission resolvePermission(String permissionString) {
-            // TODO Auto-generated method stub
-            return null;
-        }
-        
-    }
 }