Author: vtence Date: Fri Nov 12 08:11:10 2004 New Revision: 57529 Added: incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/attribute/ incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/attribute/AttributeProvider.java incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/authentication/attribute/ incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/authentication/attribute/AttributeProviderTest.java Removed: incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/group/ incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authorization/effect/IndeterminateEffect.java incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/authentication/group/ Modified: incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/testmodel/Subjects.java Log: o GroupProvider renamed to AttributeProvider to reflect its more generic nature\noAdded feature to add attributes
Added: incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/attribute/AttributeProvider.java ============================================================================== --- (empty file) +++ incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/attribute/AttributeProvider.java Fri Nov 12 08:11:10 2004 @@ -0,0 +1,68 @@ +/* + * 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.authentication.attribute; + +import org.apache.janus.authentication.InformationProvider; + +import javax.security.auth.Subject; +import java.security.Principal; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +public class AttributeProvider implements InformationProvider +{ + private final Map m_attributes; + + public AttributeProvider() + { + this( new HashMap() ); + } + + public AttributeProvider( Map attributes ) + { + m_attributes = attributes; + } + + public void populate( Subject s ) + { + Principal[] principals = ( Principal[] ) s.getPrincipals().toArray( new Principal[s.getPrincipals().size()] ); + for ( int i = 0; i < principals.length; i++ ) + { + Principal p = principals[i]; + Collection attributes = ( Collection ) m_attributes.get( p ); + if ( attributes != null ) s.getPrincipals().addAll( attributes ); + } + } + + public void addAttribute( Principal principal, Principal attribute ) + { + if ( !m_attributes.containsKey( principal ) ) m_attributes.put( principal, new ArrayList() ); + + Collection principalAttributes = ( Collection ) m_attributes.get( principal ); + principalAttributes.add( attribute ); + } + + public void addAllAttributes( Principal principal, Collection attributes ) + { + if ( !m_attributes.containsKey( principal ) ) m_attributes.put( principal, new ArrayList() ); + + Collection principalAttributes = ( Collection ) m_attributes.get( principal ); + principalAttributes.addAll( attributes ); + } +} Added: incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/authentication/attribute/AttributeProviderTest.java ============================================================================== --- (empty file) +++ incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/authentication/attribute/AttributeProviderTest.java Fri Nov 12 08:11:10 2004 @@ -0,0 +1,79 @@ +/* + * 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.authentication.attribute; + +import junit.framework.TestCase; +import org.apache.commons.collections.CollectionUtils; +import org.apache.janus.testmodel.Groups; +import org.apache.janus.testmodel.Subjects; +import org.apache.janus.testmodel.Usernames; +import org.apache.janus.authentication.attribute.AttributeProvider; + +import javax.security.auth.Subject; +import java.security.Principal; +import java.util.Arrays; +import java.util.Collection; + +public class AttributeProviderTest extends TestCase +{ + AttributeProvider m_provider; + + protected void setUp() throws Exception + { + m_provider = new AttributeProvider(); + } + + /** + * o add attribute principal to a principal + * o add a group of attributes + * o ignore duplicates + * + * o population traverses entire graph of associations + */ + + public void testPopulatesSubjectWithAttributesOfSubjectPrincipal() + { + Collection groups = Arrays.asList( new Principal[] { Groups.geeks(), Groups.men() }); + m_provider.addAllAttributes( Usernames.joe(), groups ); + + Subject joe = Subjects.joe(); + m_provider.populate( joe ); + assertTrue( CollectionUtils.isSubCollection( groups, joe.getPrincipals() ) ); + } + + public void testLooksAtAllSubjectPrincipalsWhenPopulating() + { + m_provider = new AttributeProvider(); + m_provider.addAttribute( Usernames.joe(), Groups.geeks() ); + m_provider.addAttribute( Groups.canadians(), Groups.men() ); + + Subject joe = Subjects.with( Usernames.joe(), Groups.canadians() ); + m_provider.populate( joe ); + + Collection groups = Arrays.asList( new Principal[] { Groups.geeks(), Groups.men() }); + assertTrue( CollectionUtils.isSubCollection( groups, joe.getPrincipals() ) ); + } + + public void testPrincipalHasNoAttributeByDefault() + { + m_provider = new AttributeProvider(); + Subject s = new Subject(); + m_provider.populate( s ); + + assertTrue( s.getPrincipals( GroupPrincipal.class ).isEmpty() ); + } +} Modified: incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/testmodel/Subjects.java ============================================================================== --- incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/testmodel/Subjects.java (original) +++ incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/testmodel/Subjects.java Fri Nov 12 08:11:10 2004 @@ -21,7 +21,7 @@ public class Subjects { - public static Subject joeBlow() + public static Subject joe() { return with( Usernames.joe() ); }
