From directory-cvs-return-493-apmail-incubator-directory-cvs-archive=incubator.apache.org@incubator.apache.org Tue Mar 09 13:09:23 2004 Return-Path: Delivered-To: apmail-incubator-directory-cvs-archive@www.apache.org Received: (qmail 31814 invoked from network); 9 Mar 2004 13:09:23 -0000 Received: from daedalus.apache.org (HELO mail.apache.org) (208.185.179.12) by minotaur-2.apache.org with SMTP; 9 Mar 2004 13:09:23 -0000 Received: (qmail 22618 invoked by uid 500); 9 Mar 2004 13:09:20 -0000 Delivered-To: apmail-incubator-directory-cvs-archive@incubator.apache.org Received: (qmail 22584 invoked by uid 500); 9 Mar 2004 13:09:20 -0000 Mailing-List: contact directory-cvs-help@incubator.apache.org; run by ezmlm Precedence: bulk Reply-To: directory-dev@incubator.apache.org list-help: list-unsubscribe: list-post: Delivered-To: mailing list directory-cvs@incubator.apache.org Received: (qmail 22570 invoked from network); 9 Mar 2004 13:09:19 -0000 Received: from unknown (HELO minotaur.apache.org) (209.237.227.194) by daedalus.apache.org with SMTP; 9 Mar 2004 13:09:19 -0000 Received: (qmail 31753 invoked by uid 65534); 9 Mar 2004 13:09:21 -0000 Date: 9 Mar 2004 13:09:21 -0000 Message-ID: <20040309130921.31744.qmail@minotaur.apache.org> From: vtence@apache.org To: directory-cvs@incubator.apache.org Subject: svn commit: rev 9313 - in incubator/directory/janus/trunk: core/api/src/java/org/apache/janus/authorization/role core/impl/src/java/org/apache/janus/authorization/role core/impl/src/test/org/apache/janus/authorization core/impl/src/test/org/apache/janus/authorization/role script/src/java/org/apache/janus/script/xml script/src/test/org/apache/janus/script/xml X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N Author: vtence Date: Tue Mar 9 05:09:20 2004 New Revision: 9313 Added: incubator/directory/janus/trunk/core/api/src/java/org/apache/janus/authorization/role/MutableRoleManager.java (contents, props changed) incubator/directory/janus/trunk/script/src/java/org/apache/janus/script/xml/Dom4JRoleManagerBuilder.java incubator/directory/janus/trunk/script/src/test/org/apache/janus/script/xml/Dom4JRoleManagerBuilderTest.java Modified: incubator/directory/janus/trunk/core/impl/src/java/org/apache/janus/authorization/role/DefaultRoleManager.java incubator/directory/janus/trunk/core/impl/src/java/org/apache/janus/authorization/role/RoleMapping.java incubator/directory/janus/trunk/core/impl/src/test/org/apache/janus/authorization/DefaultAuthorizerTest.java incubator/directory/janus/trunk/core/impl/src/test/org/apache/janus/authorization/role/DefaultRoleManagerTest.java incubator/directory/janus/trunk/script/src/test/org/apache/janus/script/xml/Dom4JRealmBuilderTest.java Log: o Work in progress on DIR-45 Added: incubator/directory/janus/trunk/core/api/src/java/org/apache/janus/authorization/role/MutableRoleManager.java ============================================================================== --- (empty file) +++ incubator/directory/janus/trunk/core/api/src/java/org/apache/janus/authorization/role/MutableRoleManager.java Tue Mar 9 05:09:20 2004 @@ -0,0 +1,27 @@ +/* + * Copyright 2004 The Apache Software Foundation + * + * 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.apache.janus.authorization.role; + +import java.security.Principal; + +/** + * @author Apache Directory Project + */ +public interface MutableRoleManager extends RoleManager +{ + void addPrincipalToRole( String roleName, Principal p ); +} Modified: incubator/directory/janus/trunk/core/impl/src/java/org/apache/janus/authorization/role/DefaultRoleManager.java ============================================================================== --- incubator/directory/janus/trunk/core/impl/src/java/org/apache/janus/authorization/role/DefaultRoleManager.java (original) +++ incubator/directory/janus/trunk/core/impl/src/java/org/apache/janus/authorization/role/DefaultRoleManager.java Tue Mar 9 05:09:20 2004 @@ -20,15 +20,21 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; +import java.util.Collections; /** * @author Apache Directory Project */ -public class DefaultRoleManager implements RoleManager +public class DefaultRoleManager implements MutableRoleManager { private final Collection m_roles; - public DefaultRoleManager( Collection roles ) + public DefaultRoleManager() + { + this( Collections.EMPTY_SET ); + } + + protected DefaultRoleManager( Collection roles ) { m_roles = new ArrayList( roles ); } @@ -44,4 +50,16 @@ return false; } + public void addRole( String roleName ) + { + RoleMapping mapping = new RoleMapping( roleName ); + m_roles.add( mapping ); + } + + public void addPrincipalToRole( String roleName, Principal p ) + { + RoleMapping mapping = new RoleMapping( roleName ); + mapping.addPrincipal( p ); + m_roles.add( mapping ); + } } Modified: incubator/directory/janus/trunk/core/impl/src/java/org/apache/janus/authorization/role/RoleMapping.java ============================================================================== --- incubator/directory/janus/trunk/core/impl/src/java/org/apache/janus/authorization/role/RoleMapping.java (original) +++ incubator/directory/janus/trunk/core/impl/src/java/org/apache/janus/authorization/role/RoleMapping.java Tue Mar 9 05:09:20 2004 @@ -19,6 +19,7 @@ import java.security.Principal; import java.util.Collection; import java.util.HashSet; +import java.util.Collections; /** * @author Apache Directory Project @@ -28,7 +29,12 @@ private final String m_roleName; private final Collection m_principals; - public RoleMapping( String roleName, Collection principals ) + public RoleMapping( String roleName ) + { + this( roleName, Collections.EMPTY_SET ); + } + + protected RoleMapping( String roleName, Collection principals ) { m_roleName = roleName; m_principals = new HashSet( principals ); @@ -42,5 +48,10 @@ public boolean given( Grant g ) { return g.given( m_roleName ); + } + + public void addPrincipal( Principal p ) + { + m_principals.add( p ); } } Modified: incubator/directory/janus/trunk/core/impl/src/test/org/apache/janus/authorization/DefaultAuthorizerTest.java ============================================================================== --- incubator/directory/janus/trunk/core/impl/src/test/org/apache/janus/authorization/DefaultAuthorizerTest.java (original) +++ incubator/directory/janus/trunk/core/impl/src/test/org/apache/janus/authorization/DefaultAuthorizerTest.java Tue Mar 9 05:09:20 2004 @@ -19,9 +19,9 @@ import com.mockobjects.dynamic.C; import com.mockobjects.dynamic.Mock; import junit.framework.TestCase; +import org.apache.janus.authentication.realm.UsernamePrincipal; import org.apache.janus.authorization.policy.PolicyContext; import org.apache.janus.authorization.role.RoleManager; -import org.apache.janus.authentication.realm.UsernamePrincipal; import javax.security.auth.Subject; @@ -44,7 +44,7 @@ m_mockPolicyContext = new Mock( PolicyContext.class ); m_mockRoleManager = new Mock( RoleManager.class ); m_authorizer = new DefaultAuthorizer( (PolicyContext) m_mockPolicyContext.proxy(), - (RoleManager) m_mockRoleManager.proxy() ); + (org.apache.janus.authorization.role.RoleManager) m_mockRoleManager.proxy() ); } Modified: incubator/directory/janus/trunk/core/impl/src/test/org/apache/janus/authorization/role/DefaultRoleManagerTest.java ============================================================================== --- incubator/directory/janus/trunk/core/impl/src/test/org/apache/janus/authorization/role/DefaultRoleManagerTest.java (original) +++ incubator/directory/janus/trunk/core/impl/src/test/org/apache/janus/authorization/role/DefaultRoleManagerTest.java Tue Mar 9 05:09:20 2004 @@ -25,6 +25,13 @@ import org.apache.janus.authentication.realm.UsernamePrincipal; /** + * test: Role added twice is ignored + * test: Can't add principal to unknow role - what should be thrown? + * test: Adding role to role + * test: Can't add role unknown role + * test: Cant't add unknown role to role + * test: Prevents role circular dependencies + * * @author Apache Directory Project */ public class DefaultRoleManagerTest extends TestCase @@ -39,25 +46,61 @@ public void testPrincipalWithNoRoleIsNeverInRole() { m_roleManager = new DefaultRoleManager( Collections.EMPTY_SET ); - assertFalse( "Principal with no role was in role", m_roleManager.isPrincipalInRole( new UsernamePrincipal( "johnDoe" ), new Right() ) ); + assertFalse( "Principal with no role was in role", + m_roleManager.isPrincipalInRole( john(), new Right() ) ); } public void testSingleRole() { - RoleMapping role = new RoleMapping( "member", Collections.singleton( new UsernamePrincipal( "johnDoe" ) ) ); + RoleMapping role = new RoleMapping( "member", Collections.singleton( john() ) ); m_roleManager = new DefaultRoleManager( Collections.singletonList( role ) ); - assertTrue( "Principal did not get right", m_roleManager.isPrincipalInRole( new UsernamePrincipal( "johnDoe" ), new Right() ) ); - assertFalse( "Principal did not get interdiction", m_roleManager.isPrincipalInRole( new UsernamePrincipal( "johnDoe" ), new Interdiction() ) ); + assertTrue( "Principal did not get right", + m_roleManager.isPrincipalInRole( john(), new Right() ) ); + assertFalse( "Principal did not get interdiction", + m_roleManager.isPrincipalInRole( john(), new Interdiction() ) ); } public void testMultipleRole() { Collection roles = new ArrayList(); - roles.add( new RoleMapping( "guest", Collections.singleton( new UsernamePrincipal( "johnDoe" ) ) ) ); - roles.add( new RoleMapping( "member", Collections.singleton( new UsernamePrincipal( "johnDoe" ) ) ) ); + roles.add( new RoleMapping( "guest", Collections.singleton( john() ) ) ); + roles.add( new RoleMapping( "member", Collections.singleton( john() ) ) ); m_roleManager = new DefaultRoleManager( roles ); - assertTrue( "Role was not matched", m_roleManager.isPrincipalInRole( new UsernamePrincipal( "johnDoe" ), new RoleGrant( "member" ) ) ); + assertTrue( "Role was not matched", + m_roleManager.isPrincipalInRole( john(), new RoleGrant( "member" ) ) ); + } + + private UsernamePrincipal john() + { + return new UsernamePrincipal( "johnDoe" ); + } + + public void testRoleHasNoPrincipalByDefault() + { + m_roleManager = new DefaultRoleManager(); + m_roleManager.addRole( "member" ); + assertFalse( m_roleManager.isPrincipalInRole( john(), new RoleGrant( "member" ) ) ); + } + + public void testAddingPrincipalToRoleMakesItInRole() + { + m_roleManager = new DefaultRoleManager(); + m_roleManager.addPrincipalToRole( "member", john() ); + assertTrue( m_roleManager.isPrincipalInRole( john(), new RoleGrant( "member" ) ) ); + } + + public void testAddingPrincipalToExistingRolePreservesPreviousPrincipals() + { + m_roleManager = new DefaultRoleManager(); + m_roleManager.addPrincipalToRole( "member", john() ); + m_roleManager.addPrincipalToRole( "member", jane() ); + assertTrue( m_roleManager.isPrincipalInRole( john(), new RoleGrant( "member" ) ) ); + } + + private UsernamePrincipal jane() + { + return new UsernamePrincipal( "janeDoe" ); } } Added: incubator/directory/janus/trunk/script/src/java/org/apache/janus/script/xml/Dom4JRoleManagerBuilder.java ============================================================================== --- (empty file) +++ incubator/directory/janus/trunk/script/src/java/org/apache/janus/script/xml/Dom4JRoleManagerBuilder.java Tue Mar 9 05:09:20 2004 @@ -0,0 +1,60 @@ +/* + * Copyright 2004 The Apache Software Foundation + * + * 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.apache.janus.script.xml; + +import org.apache.janus.authorization.role.MutableRoleManager; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +import java.io.IOException; +import java.io.Reader; + +/** + * Warning: Document is assumed to be valid. + * + * @author Apache Directory Project + */ +public class Dom4JRoleManagerBuilder +{ + private Document m_doc; + + public Dom4JRoleManagerBuilder( Reader reader ) throws DocumentException + { + m_doc = readDocument( reader ); + } + + public void buildRoleManager( MutableRoleManager roleManager ) throws IOException + { + Element root = m_doc.getRootElement(); + Element roles = root.element( "roles" ); + addRoles( roleManager, roles ); + } + + private void addRoles( MutableRoleManager roleManager, Element roles ) + { + } + + private Document readDocument( Reader reader ) throws DocumentException + { + SAXReader xmlReader = new SAXReader(); + Document doc = xmlReader.read( reader ); + + return doc; + } +} Modified: incubator/directory/janus/trunk/script/src/test/org/apache/janus/script/xml/Dom4JRealmBuilderTest.java ============================================================================== --- incubator/directory/janus/trunk/script/src/test/org/apache/janus/script/xml/Dom4JRealmBuilderTest.java (original) +++ incubator/directory/janus/trunk/script/src/test/org/apache/janus/script/xml/Dom4JRealmBuilderTest.java Tue Mar 9 05:09:20 2004 @@ -37,10 +37,6 @@ junit.textui.TestRunner.run( Dom4JRealmBuilderTest.class ); } - protected void setUp() throws Exception - { - } - public void testSimpleBuild() throws Exception { Dom4JRealmBuilder builder = new Dom4JRealmBuilder( new StringReader( simpleRealm() ) ); Added: incubator/directory/janus/trunk/script/src/test/org/apache/janus/script/xml/Dom4JRoleManagerBuilderTest.java ============================================================================== --- (empty file) +++ incubator/directory/janus/trunk/script/src/test/org/apache/janus/script/xml/Dom4JRoleManagerBuilderTest.java Tue Mar 9 05:09:20 2004 @@ -0,0 +1,72 @@ +/* + * Copyright 2004 The Apache Software Foundation + * + * 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.apache.janus.script.xml; + +import com.mockobjects.dynamic.C; +import com.mockobjects.dynamic.Mock; +import junit.framework.TestCase; +import org.apache.janus.authentication.realm.UsernamePrincipal; +import org.apache.janus.authorization.role.MutableRoleManager; + +import java.io.StringReader; + +/** + * @author Apache Directory Project + */ +public class Dom4JRoleManagerBuilderTest extends TestCase +{ + public static void main( String[] args ) + { + junit.textui.TestRunner.run( Dom4JRoleManagerBuilderTest.class ); + } + + public void testSimpleBuild() throws Exception + { + Dom4JRoleManagerBuilder builder = new Dom4JRoleManagerBuilder( new StringReader( simpleRoles() ) ); + + Mock mockRoleManager = new Mock( MutableRoleManager.class ); + mockRoleManager.expectAndReturn( "addPrincipalToRole", C.args( C.eq( "member"), C.eq( john()) ), true ); + mockRoleManager.expectAndReturn( "addPrincipalToRole", C.args( C.eq( "member"), C.eq( jane()) ), true ); + + builder.buildRoleManager( (MutableRoleManager) mockRoleManager.proxy() ); + + mockRoleManager.verify(); + } + + private String simpleRoles() + { + String content = "\n" + + "\n" + + " \n" + + " \n" + + " \n" + + " \n" + + ""; + return content; + } + + private UsernamePrincipal john() + { + return new UsernamePrincipal( "john" ); + } + + + private UsernamePrincipal jane() + { + return new UsernamePrincipal( "jane" ); + } +}