directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vte...@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
Date Tue, 09 Mar 2004 13:09:21 GMT
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 <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ */
+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 <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
  */
-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 <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
@@ -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 <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
  */
 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;
+
+/**
+ * <strong>Warning:</strong> Document is assumed to be valid.
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ */
+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 <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ */
+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 = "<?xml version=\"1.0\"?>\n"
+                         + "<roles>\n"
+                         + "    <role name=\"member\">\n"
+                         + "        <user username=\"john\"/>\n"
+                         + "        <user username=\"jane\"/>\n"
+                         + "    </role>\n"
+                         + "</roles>";
+        return content;
+    }
+
+    private UsernamePrincipal john()
+    {
+        return new UsernamePrincipal( "john" );
+    }
+
+
+    private UsernamePrincipal jane()
+    {
+        return new UsernamePrincipal( "jane" );
+    }
+}

Mime
View raw message