You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shiro.apache.org by ka...@apache.org on 2010/02/14 02:42:13 UTC
svn commit: r909947 - in /incubator/shiro/trunk/core/src:
main/java/org/apache/shiro/authz/
main/java/org/apache/shiro/authz/permission/
main/java/org/apache/shiro/realm/ test/java/org/apache/shiro/authz/
test/java/org/apache/shiro/realm/
Author: kaosko
Date: Sun Feb 14 01:42:12 2010
New Revision: 909947
URL: http://svn.apache.org/viewvc?rev=909947&view=rev
Log:
Complete - issue SHIRO-59: Refactor Realm implementations to favor delegation over inheritance
http://issues.apache.org/jira/browse/SHIRO-59
- Applying high-quality patch from Brian Demers that introduces new RolePermissionResolver, modeled after WildCardPermissionResolver. Tests included and passing
- Checked and added Apache license header to a few files, otherwise applied as is
Added:
incubator/shiro/trunk/core/src/main/java/org/apache/shiro/authz/permission/RolePermissionResolver.java
incubator/shiro/trunk/core/src/main/java/org/apache/shiro/authz/permission/RolePermissionResolverAware.java
incubator/shiro/trunk/core/src/test/java/org/apache/shiro/authz/ModularRealmAuthorizerTest.java
Modified:
incubator/shiro/trunk/core/src/main/java/org/apache/shiro/authz/ModularRealmAuthorizer.java
incubator/shiro/trunk/core/src/main/java/org/apache/shiro/realm/AuthorizingRealm.java
incubator/shiro/trunk/core/src/test/java/org/apache/shiro/realm/AuthorizingRealmTest.java
Modified: incubator/shiro/trunk/core/src/main/java/org/apache/shiro/authz/ModularRealmAuthorizer.java
URL: http://svn.apache.org/viewvc/incubator/shiro/trunk/core/src/main/java/org/apache/shiro/authz/ModularRealmAuthorizer.java?rev=909947&r1=909946&r2=909947&view=diff
==============================================================================
--- incubator/shiro/trunk/core/src/main/java/org/apache/shiro/authz/ModularRealmAuthorizer.java (original)
+++ incubator/shiro/trunk/core/src/main/java/org/apache/shiro/authz/ModularRealmAuthorizer.java Sun Feb 14 01:42:12 2010
@@ -23,6 +23,8 @@
import org.apache.shiro.authz.permission.PermissionResolver;
import org.apache.shiro.authz.permission.PermissionResolverAware;
+import org.apache.shiro.authz.permission.RolePermissionResolver;
+import org.apache.shiro.authz.permission.RolePermissionResolverAware;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.subject.PrincipalCollection;
@@ -34,7 +36,7 @@
* @author Les Hazlewood
* @since 0.2
*/
-public class ModularRealmAuthorizer implements Authorizer, PermissionResolverAware {
+public class ModularRealmAuthorizer implements Authorizer, PermissionResolverAware, RolePermissionResolverAware {
/**
* The realms to consult during any authorization check.
@@ -46,6 +48,12 @@
* to configure different resolvers for different realms.
*/
protected PermissionResolver permissionResolver;
+
+ /**
+ * A RolePermissionResolver to be used by <em>all</em> configured realms. Leave <code>null</code> if you wish
+ * to configure different resolvers for different realms.
+ */
+ protected RolePermissionResolver rolePermissionResolver;
/**
* Default no-argument constructor, does nothing.
@@ -109,7 +117,7 @@
this.permissionResolver = permissionResolver;
applyPermissionResolverToRealms();
}
-
+
/**
* Sets the internal {@link #getPermissionResolver} on any internal configured
* {@link #getRealms Realms} that implement the {@link org.apache.shiro.authz.permission.PermissionResolverAware PermissionResolverAware} interface.
@@ -133,6 +141,59 @@
}
}
}
+
+ /**
+ * Returns the RolePermissionResolver to be used on <em>all</em> configured realms, or <code>null</code (the default)
+ * if all realm instances will each configure their own permission resolver.
+ *
+ * @return the RolePermissionResolver to be used on <em>all</em> configured realms, or <code>null</code (the default)
+ * if realm instances will each configure their own role permission resolver.
+ * @since 1.0
+ */
+ public RolePermissionResolver getRolePermissionResolver() {
+ return this.rolePermissionResolver;
+ }
+
+ /**
+ * Sets the specified {@link RolePermissionResolver RolePermissionResolver} on <em>all</em> of the wrapped realms that
+ * implement the {@link org.apache.shiro.authz.permission.RolePermissionResolverAware PermissionResolverAware} interface.
+ * <p/>
+ * Only call this method if you want the permission resolver to be passed to all realms that implement the
+ * <code>RolePermissionResolver</code> interface. If you do not want this to occur, the realms must
+ * configure themselves individually (or be configured individually).
+ *
+ * @param rolePermissionResolver the rolePermissionResolver to set on all of the wrapped realms that implement the
+ * {@link org.apache.shiro.authz.permission.RolePermissionResolverAware RolePermissionResolverAware} interface.
+ */
+ public void setRolePermissionResolver( RolePermissionResolver rolePermissionResolver ) {
+ this.rolePermissionResolver = rolePermissionResolver;
+ applyRolePermissionResolverToRealms();
+ }
+
+
+ /**
+ * Sets the internal {@link #getRolePermissionResolver} on any internal configured
+ * {@link #getRealms Realms} that implement the {@link org.apache.shiro.authz.permission.RolePermissionResolverAware RolePermissionResolverAware} interface.
+ * <p/>
+ * This method is called after setting a rolePermissionResolver on this ModularRealmAuthorizer via the
+ * {@link #setRolePermissionResolver(org.apache.shiro.authz.permission.RolePermissionResolver) setRolePermissionResolver} method.
+ * <p/>
+ * It is also called after setting one or more realms via the {@link #setRealms setRealms} method to allow these
+ * newly available realms to be given the <code>RolePermissionResolver</code> already in use.
+ *
+ * @since 1.0
+ */
+ protected void applyRolePermissionResolverToRealms() {
+ RolePermissionResolver resolver = getRolePermissionResolver();
+ Collection<Realm> realms = getRealms();
+ if (resolver != null && realms != null && !realms.isEmpty()) {
+ for (Realm realm : realms) {
+ if (realm instanceof RolePermissionResolverAware) {
+ ((RolePermissionResolverAware) realm).setRolePermissionResolver(resolver);
+ }
+ }
+ }
+ }
/**
@@ -369,5 +430,4 @@
}
}
}
-
}
Added: incubator/shiro/trunk/core/src/main/java/org/apache/shiro/authz/permission/RolePermissionResolver.java
URL: http://svn.apache.org/viewvc/incubator/shiro/trunk/core/src/main/java/org/apache/shiro/authz/permission/RolePermissionResolver.java?rev=909947&view=auto
==============================================================================
--- incubator/shiro/trunk/core/src/main/java/org/apache/shiro/authz/permission/RolePermissionResolver.java (added)
+++ incubator/shiro/trunk/core/src/main/java/org/apache/shiro/authz/permission/RolePermissionResolver.java Sun Feb 14 01:42:12 2010
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.shiro.authz.permission;
+
+import java.util.Collection;
+
+import org.apache.shiro.authz.Permission;
+
+/**
+ * <p>A RolePermissionResolver resolves a String value and converts it into a Collection of
+ * {@link org.apache.shiro.authz.Permission} instances.
+ * <BR/>
+ *
+ * In some cases a {@link org.apache.shiro.realm.Realm} my only be able to return a list of roles. This
+ * component allows an application to resolve the roles into permissions.
+ * <BR/>
+ *
+ * @author Brian Demers
+ *
+ */
+public interface RolePermissionResolver
+{
+ /**
+ * Resolves a Collection of Permissions based on the given String representation.
+ *
+ * @param roleString the String representation of a role name to resolve.
+ * @return
+ */
+ Collection<Permission> resolvePermissionsInRole(String roleString);
+
+}
Added: incubator/shiro/trunk/core/src/main/java/org/apache/shiro/authz/permission/RolePermissionResolverAware.java
URL: http://svn.apache.org/viewvc/incubator/shiro/trunk/core/src/main/java/org/apache/shiro/authz/permission/RolePermissionResolverAware.java?rev=909947&view=auto
==============================================================================
--- incubator/shiro/trunk/core/src/main/java/org/apache/shiro/authz/permission/RolePermissionResolverAware.java (added)
+++ incubator/shiro/trunk/core/src/main/java/org/apache/shiro/authz/permission/RolePermissionResolverAware.java Sun Feb 14 01:42:12 2010
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.shiro.authz.permission;
+
+/**
+ * Interface implemented by a component that wishes to use any application-configured <tt>RolePermissionResolver</tt> that
+ * might already exist instead of potentially creating one itself.
+ *
+ * <p>This is mostly implemented by {@link org.apache.shiro.authz.Authorizer Authorizer} and
+ * {@link org.apache.shiro.realm.Realm Realm} implementations since they
+ * are the ones performing permission checks and need to know how to resolve Strings into
+ * {@link org.apache.shiro.authz.Permission Permission} instances.
+ *
+ * @author Brian Demers
+ * @since 1.0
+ */
+public interface RolePermissionResolverAware {
+
+ /**
+ * Sets the specified <tt>RolePermissionResolver</tt> on this instance.
+ *
+ * @param pr the <tt>RolePermissionResolver</tt> being set.
+ */
+ public void setRolePermissionResolver(RolePermissionResolver rpr);
+}
Modified: incubator/shiro/trunk/core/src/main/java/org/apache/shiro/realm/AuthorizingRealm.java
URL: http://svn.apache.org/viewvc/incubator/shiro/trunk/core/src/main/java/org/apache/shiro/realm/AuthorizingRealm.java?rev=909947&r1=909946&r2=909947&view=diff
==============================================================================
--- incubator/shiro/trunk/core/src/main/java/org/apache/shiro/realm/AuthorizingRealm.java (original)
+++ incubator/shiro/trunk/core/src/main/java/org/apache/shiro/realm/AuthorizingRealm.java Sun Feb 14 01:42:12 2010
@@ -18,10 +18,24 @@
*/
package org.apache.shiro.realm;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicInteger;
+
import org.apache.shiro.authc.credential.CredentialsMatcher;
-import org.apache.shiro.authz.*;
+import org.apache.shiro.authz.AuthorizationException;
+import org.apache.shiro.authz.AuthorizationInfo;
+import org.apache.shiro.authz.AuthorizingAccount;
+import org.apache.shiro.authz.Permission;
+import org.apache.shiro.authz.UnauthorizedException;
import org.apache.shiro.authz.permission.PermissionResolver;
import org.apache.shiro.authz.permission.PermissionResolverAware;
+import org.apache.shiro.authz.permission.RolePermissionResolver;
+import org.apache.shiro.authz.permission.RolePermissionResolverAware;
import org.apache.shiro.authz.permission.WildcardPermissionResolver;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheManager;
@@ -30,9 +44,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import java.util.*;
-import java.util.concurrent.atomic.AtomicInteger;
-
/**
* An {@code AuthorizingRealm} extends the {@code AuthenticatingRealm}'s capabilities by adding Authorization
@@ -55,7 +66,7 @@
* @see org.apache.shiro.authz.SimpleAuthorizationInfo
* @since 0.2
*/
-public abstract class AuthorizingRealm extends AuthenticatingRealm implements Initializable, PermissionResolverAware {
+public abstract class AuthorizingRealm extends AuthenticatingRealm implements Initializable, PermissionResolverAware, RolePermissionResolverAware {
//TODO - complete JavaDoc
@@ -82,6 +93,8 @@
private String authorizationCacheName = null;
private PermissionResolver permissionResolver = new WildcardPermissionResolver();
+
+ private RolePermissionResolver permissionRoleResolver = null;
/*--------------------------------------------
| C O N S T R U C T O R S |
@@ -157,7 +170,15 @@
public void setPermissionResolver(PermissionResolver permissionResolver) {
this.permissionResolver = permissionResolver;
}
+
+ public RolePermissionResolver getRolePermissionResolver() {
+ return permissionRoleResolver;
+ }
+ public void setRolePermissionResolver(RolePermissionResolver permissionRoleResolver) {
+ this.permissionRoleResolver = permissionRoleResolver;
+ }
+
/*--------------------------------------------
| M E T H O D S |
============================================*/
@@ -396,6 +417,12 @@
permissions.add(permission);
}
}
+ if(info.getRoles() != null && getRolePermissionResolver() != null ) {
+ for (String role : info.getRoles()) {
+ Collection<Permission> rolesPermissions = getRolePermissionResolver().resolvePermissionsInRole( role );
+ permissions.addAll( rolesPermissions );
+ }
+ }
}
if (permissions.isEmpty()) {
Added: incubator/shiro/trunk/core/src/test/java/org/apache/shiro/authz/ModularRealmAuthorizerTest.java
URL: http://svn.apache.org/viewvc/incubator/shiro/trunk/core/src/test/java/org/apache/shiro/authz/ModularRealmAuthorizerTest.java?rev=909947&view=auto
==============================================================================
--- incubator/shiro/trunk/core/src/test/java/org/apache/shiro/authz/ModularRealmAuthorizerTest.java (added)
+++ incubator/shiro/trunk/core/src/test/java/org/apache/shiro/authz/ModularRealmAuthorizerTest.java Sun Feb 14 01:42:12 2010
@@ -0,0 +1,104 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.shiro.authz;
+
+import java.util.ArrayList;
+import java.util.Collection;
+
+import junit.framework.Assert;
+
+import org.apache.shiro.authc.AuthenticationException;
+import org.apache.shiro.authc.AuthenticationInfo;
+import org.apache.shiro.authc.AuthenticationToken;
+import org.apache.shiro.authz.permission.RolePermissionResolver;
+import org.apache.shiro.realm.AuthorizingRealm;
+import org.apache.shiro.realm.Realm;
+import org.apache.shiro.subject.PrincipalCollection;
+import org.junit.Test;
+
+public class ModularRealmAuthorizerTest
+{
+
+ @Test
+ public void testSettingOfRolePermissionResolver()
+ {
+ Collection<Realm> realms = new ArrayList<Realm>();
+
+ realms.add( new MockAuthorizingRealm() );
+ realms.add( new MockAuthorizingRealm() );
+
+ // its null to start with
+ for ( Realm realm : realms )
+ {
+ Assert.assertNull( ((AuthorizingRealm)realm).getRolePermissionResolver() );
+ }
+
+ ModularRealmAuthorizer modRealmAuthz = new ModularRealmAuthorizer();
+ modRealmAuthz.setRealms( realms );
+
+ // make sure they are still null
+ for ( Realm realm : realms )
+ {
+ Assert.assertNull( ((AuthorizingRealm)realm).getRolePermissionResolver() );
+ }
+
+ // now set the RolePermissionResolver
+ RolePermissionResolver rolePermissionResolver = new RolePermissionResolver()
+ {
+ public Collection<Permission> resolvePermissionsInRole( String roleString )
+ {
+ return null;
+ }
+ };
+ modRealmAuthz.setRolePermissionResolver( rolePermissionResolver );
+
+ // make sure they are set
+ for ( Realm realm : realms )
+ {
+ // check for same instance
+ Assert.assertTrue( ((AuthorizingRealm)realm).getRolePermissionResolver() == rolePermissionResolver );
+ }
+
+ // TODO: no way to unset them, not sure if that is a valid use case, but this is conistent with the PermissionResolver logic
+// // now just to be sure, unset them
+// modRealmAuthz.setRolePermissionResolver( null );
+// for ( Realm realm : realms )
+// {
+// Assert.assertNull( ((AuthorizingRealm)realm).getRolePermissionResolver() );
+// }
+ }
+
+ class MockAuthorizingRealm extends AuthorizingRealm
+ {
+
+ @Override
+ protected AuthorizationInfo doGetAuthorizationInfo( PrincipalCollection principals )
+ {
+ return null;
+ }
+
+ @Override
+ protected AuthenticationInfo doGetAuthenticationInfo( AuthenticationToken token )
+ throws AuthenticationException
+ {
+ return null;
+ }
+
+ }
+}
Modified: incubator/shiro/trunk/core/src/test/java/org/apache/shiro/realm/AuthorizingRealmTest.java
URL: http://svn.apache.org/viewvc/incubator/shiro/trunk/core/src/test/java/org/apache/shiro/realm/AuthorizingRealmTest.java?rev=909947&r1=909946&r2=909947&view=diff
==============================================================================
--- incubator/shiro/trunk/core/src/test/java/org/apache/shiro/realm/AuthorizingRealmTest.java (original)
+++ incubator/shiro/trunk/core/src/test/java/org/apache/shiro/realm/AuthorizingRealmTest.java Sun Feb 14 01:42:12 2010
@@ -24,6 +24,7 @@
import org.apache.shiro.authz.Permission;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.authz.UnauthorizedException;
+import org.apache.shiro.authz.permission.RolePermissionResolver;
import org.apache.shiro.authz.permission.WildcardPermission;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
@@ -183,6 +184,35 @@
assertFalse(realm.isPermittedAll(pCollection, "perm1", "perm2"));
assertFalse(realm.isPermittedAll(pCollection, permList));
}
+
+ @Test
+ public void testRealmWithRolePermissionResolver()
+ {
+ Principal principal = new UsernamePrincipal("rolePermResolver");
+ PrincipalCollection pCollection = new SimplePrincipalCollection(principal, "testRealmWithRolePermissionResolver");
+
+ AuthorizingRealm realm = new AllowAllRealm();
+ realm.setRolePermissionResolver( new RolePermissionResolver()
+ {
+ public Collection<Permission> resolvePermissionsInRole( String roleString )
+ {
+ Collection<Permission> permissions = new HashSet<Permission>();
+ if( roleString.equals( ROLE ))
+ {
+ permissions.add( new WildcardPermission( ROLE + ":perm1" ) );
+ permissions.add( new WildcardPermission( ROLE + ":perm2" ) );
+ permissions.add( new WildcardPermission( "other:*:foo" ) );
+ }
+ return permissions;
+ }
+ });
+
+ assertTrue( realm.hasRole( pCollection, ROLE ) );
+ assertTrue( realm.isPermitted( pCollection, ROLE + ":perm1" ) );
+ assertTrue( realm.isPermitted( pCollection, ROLE + ":perm2" ) );
+ assertFalse( realm.isPermitted( pCollection, ROLE + ":perm3" ) );
+ assertTrue( realm.isPermitted( pCollection, "other:bar:foo" ) );
+ }
private void assertArrayEquals(boolean[] expected, boolean[] actual) {
if (expected.length != actual.length) {