You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jetspeed-dev@portals.apache.org by at...@apache.org on 2004/06/17 00:47:01 UTC
cvs commit: jakarta-jetspeed-2/components/security/src/test/org/apache/jetspeed/security TestGeneralizationHierarchy.java TestAggregationHierarchy.java TestGroupManager.java TestRoleManager.java AbstractSecurityTestcase.java
ate 2004/06/16 15:47:01
Modified: components/security/src/java/org/apache/jetspeed/security/impl
BaseSecurityImpl.java UserManagerImpl.java
components/security/src/test/org/apache/jetspeed/security
TestGroupManager.java TestRoleManager.java
AbstractSecurityTestcase.java
Added: components/security/src/java/org/apache/jetspeed/security/impl
AggregationHierarchyResolver.java
HierarchyResolver.java
GeneralizationHierarchyResolver.java
components/security/src/test/org/apache/jetspeed/security
TestGeneralizationHierarchy.java
TestAggregationHierarchy.java
Log:
New group and role hierarchy resolution submitted by Artem Grinshtein
CVS: ----------------------------------------------------------------------
CVS: PR:
CVS: If this change addresses a PR in the problem report tracking
CVS: database, then enter the PR number(s) here.
CVS: Obtained from:
CVS: If this change has been taken from another system, such as NCSA,
CVS: then name the system in this line, otherwise delete it.
CVS: Submitted by:
CVS: If this code has been contributed to Apache by someone else; i.e.,
CVS: they sent us a patch or a new module, then include their name/email
CVS: address here. If this is your work then delete this line.
CVS: Reviewed by:
CVS: If we are doing pre-commit code reviews and someone else has
CVS: reviewed your changes, include their name(s) here.
CVS: If you have not had it reviewed then delete this line.
Revision Changes Path
1.3 +27 -2 jakarta-jetspeed-2/components/security/src/java/org/apache/jetspeed/security/impl/BaseSecurityImpl.java
Index: BaseSecurityImpl.java
===================================================================
RCS file: /home/cvs/jakarta-jetspeed-2/components/security/src/java/org/apache/jetspeed/security/impl/BaseSecurityImpl.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- BaseSecurityImpl.java 27 May 2004 19:44:50 -0000 1.2
+++ BaseSecurityImpl.java 16 Jun 2004 22:47:00 -0000 1.3
@@ -41,11 +41,15 @@
/**
* <p>Base class for the security services.</p>
* @author <a href="mailto:dlestrat@apache.org">David Le Strat</a>
+ * @version $Id$
*/
public class BaseSecurityImpl
{
PersistenceStore persistenceStore;
+
+ HierarchyResolver roleHierarchyResolver=new GeneralizationHierarchyResolver();
+ HierarchyResolver groupHierarchyResolver=new GeneralizationHierarchyResolver();
/**
* <p>Constructor providing access to the persistence component.</p>
@@ -59,6 +63,16 @@
this.persistenceStore = persistenceStore;
}
+
+ /**
+ * <p>Constructor providing access to the persistence component and role/group hierarchy resolvers</p>
+ */
+ public BaseSecurityImpl(PersistenceStore persistenceStore, HierarchyResolver roleHierarchyResolver,HierarchyResolver groupHierarchyResolver)
+ {
+ this(persistenceStore);
+ this.roleHierarchyResolver=roleHierarchyResolver;
+ this.groupHierarchyResolver=groupHierarchyResolver;
+ }
/**
* <p>Returns the {@link JetspeedGroupPrincipal} from the group full path name.</p>
@@ -294,7 +308,12 @@
while (omRolesIter.hasNext())
{
JetspeedRolePrincipal omRole = (JetspeedRolePrincipal) omRolesIter.next();
- rolePrincipals.add(new RolePrincipalImpl(RolePrincipalImpl.getPrincipalNameFromFullPath(omRole.getFullPath())));
+ Preferences preferences = Preferences.userRoot().node(omRole.getFullPath());
+ String [] fullPaths=roleHierarchyResolver.resolve(preferences);
+ for (int i = 0; i < fullPaths.length; i++)
+ {
+ rolePrincipals.add(new RolePrincipalImpl(RolePrincipalImpl.getPrincipalNameFromFullPath(fullPaths[i])));
+ }
}
}
return rolePrincipals;
@@ -316,7 +335,13 @@
while (omGroupsIter.hasNext())
{
JetspeedGroupPrincipal omGroup = (JetspeedGroupPrincipal) omGroupsIter.next();
- groupPrincipals.add(new GroupPrincipalImpl(GroupPrincipalImpl.getPrincipalNameFromFullPath(omGroup.getFullPath())));
+
+ Preferences preferences = Preferences.userRoot().node(omGroup.getFullPath());
+ String [] fullPaths=groupHierarchyResolver.resolve(preferences);
+ for (int i = 0; i < fullPaths.length; i++)
+ {
+ groupPrincipals.add(new GroupPrincipalImpl(GroupPrincipalImpl.getPrincipalNameFromFullPath(fullPaths[i])));
+ }
}
}
return groupPrincipals;
1.6 +11 -0 jakarta-jetspeed-2/components/security/src/java/org/apache/jetspeed/security/impl/UserManagerImpl.java
Index: UserManagerImpl.java
===================================================================
RCS file: /home/cvs/jakarta-jetspeed-2/components/security/src/java/org/apache/jetspeed/security/impl/UserManagerImpl.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- UserManagerImpl.java 27 May 2004 19:44:50 -0000 1.5
+++ UserManagerImpl.java 16 Jun 2004 22:47:00 -0000 1.6
@@ -38,6 +38,7 @@
* <p>Implementation for managing users and provides access
* to the {@link User}.</p>
* @author <a href="mailto:dlestrat@apache.org">David Le Strat</a>
+ * @version $Id$
*/
public class UserManagerImpl extends BaseSecurityImpl implements UserManager
{
@@ -52,6 +53,16 @@
super(persistenceStore);
}
+
+ /**
+ * @param persistenceStore
+ */
+ public UserManagerImpl( PersistenceStore persistenceStore , HierarchyResolver roleHierarchyResolver,HierarchyResolver groupHierarchyResolver)
+ {
+ super(persistenceStore,roleHierarchyResolver,groupHierarchyResolver);
+
+ }
+
/**
* @see org.apache.jetspeed.security.UserManager#authenticate(java.lang.String, java.lang.String)
*/
1.1 jakarta-jetspeed-2/components/security/src/java/org/apache/jetspeed/security/impl/AggregationHierarchyResolver.java
Index: AggregationHierarchyResolver.java
===================================================================
/* Copyright 2004 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.jetspeed.security.impl;
import java.util.ArrayList;
import java.util.List;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.jetspeed.util.ArgUtil;
/**
* <p>Implementation for "part of" hierarchy. For Example:
* There're roles:
* <ul>
* <li>roleA</li>
* <li>roleA.roleB</li>
* <li>roleA.roleB.roleC</li>
* </ul>
* if a user has the role [roleA] than</p>
* <code>user.getSubject().getPrincipals()</code>
* returns:
* <ul>
* <li>/role/roleA</li>
* <li>/role/roleA/roleB</li>
* <li>/role/roleA/roleB/roleC</li>
* </ul>
* @author <a href="mailto:Artem.Grinshtein@t-systems.com">Artem Grinshtein</a>
* @version $Id: AggregationHierarchyResolver.java,v 1.1 2004/06/16 22:47:00 ate Exp $
*/
public class AggregationHierarchyResolver implements HierarchyResolver
{
private static final Log log = LogFactory.getLog(AggregationHierarchyResolver.class);
/**
* @see org.apache.jetspeed.security.impl.HierarchyResolver#resolve()
*/
public String[] resolve( Preferences prefs ) {
ArgUtil.notNull(
new Object[] { prefs },
new String[] { "preferences" },
"resolve(java.util.prefs.Preferences)");
List list=new ArrayList();
processPreferences(prefs,list);
return (String [])list.toArray(new String[0]) ;
}
protected void processPreferences(Preferences prefs,List list) {
list.add(prefs.absolutePath());
try
{
String [] names=prefs.childrenNames();
for (int i = 0; i < names.length; i++)
{
processPreferences(prefs.node(names[i]),list);
}
}catch(BackingStoreException bse) {
log.warn("can't find children of "+prefs.absolutePath(),bse);
}
}
}
1.1 jakarta-jetspeed-2/components/security/src/java/org/apache/jetspeed/security/impl/HierarchyResolver.java
Index: HierarchyResolver.java
===================================================================
/* Copyright 2004 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.jetspeed.security.impl;
import java.util.prefs.Preferences;
/**
* <p>This class allows to implement different types of groups/roles hierarchy.</p>
* @author <a href="mailto:Artem.Grinshtein@t-systems.com">Artem Grinshtein</a>
* @version $Id: HierarchyResolver.java,v 1.1 2004/06/16 22:47:00 ate Exp $
*/
public interface HierarchyResolver
{
/**
* <p>Returns absolute path names of the dependcy roles/groups.</p>
* @param prefs Preferences for the role/group
* @return Returns absolute path names of the dependcy roles/groups.
*/
public String[] resolve( Preferences prefs );
}
1.1 jakarta-jetspeed-2/components/security/src/java/org/apache/jetspeed/security/impl/GeneralizationHierarchyResolver.java
Index: GeneralizationHierarchyResolver.java
===================================================================
/* Copyright 2004 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.jetspeed.security.impl;
import java.util.ArrayList;
import java.util.List;
import java.util.prefs.Preferences;
import org.apache.jetspeed.util.ArgUtil;
/**
* <p>Implementation for "is a" hierarchy. For Example:
* if a user has the role [roleA.roleB.roleC] than</p>
* <code>user.getSubject().getPrincipals()</code>
* returns:
* <ul>
* <li>/role/roleA</li>
* <li>/role/roleA/roleB</li>
* <li>/role/roleA/roleB/roleC</li>
* </ul>
* @author <a href="mailto:Artem.Grinshtein@t-systems.com">Artem Grinshtein</a>
* @version $Id: GeneralizationHierarchyResolver.java,v 1.1 2004/06/16 22:47:00 ate Exp $
*/
public class GeneralizationHierarchyResolver implements HierarchyResolver
{
/**
* @see org.apache.jetspeed.security.impl.HierarchyResolver#resolve()
*/
public String[] resolve( Preferences prefs ) {
ArgUtil.notNull(
new Object[] { prefs },
new String[] { "preferences" },
"resolve(java.util.prefs.Preferences)");
List list=new ArrayList();
Preferences preferences=prefs;
while( (preferences.parent()!=null) && (preferences.parent().parent()!=null) ) {
list.add(preferences.absolutePath());
preferences=preferences.parent();
}
return (String [])list.toArray(new String[0]) ;
}
}
1.5 +10 -16 jakarta-jetspeed-2/components/security/src/test/org/apache/jetspeed/security/TestGroupManager.java
Index: TestGroupManager.java
===================================================================
RCS file: /home/cvs/jakarta-jetspeed-2/components/security/src/test/org/apache/jetspeed/security/TestGroupManager.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- TestGroupManager.java 27 May 2004 19:45:32 -0000 1.4
+++ TestGroupManager.java 16 Jun 2004 22:47:00 -0000 1.5
@@ -14,13 +14,9 @@
*/
package org.apache.jetspeed.security;
-import java.security.Principal;
import java.util.Collection;
-import java.util.HashSet;
import java.util.prefs.Preferences;
-import javax.security.auth.Subject;
-
import junit.framework.Test;
import junit.framework.TestSuite;
@@ -30,6 +26,7 @@
* <p>Unit testing for {@link GroupManager}.</p>
*
* @author <a href="mailto:dlestrat@apache.org">David Le Strat</a>
+ * @version $Id$
*/
public class TestGroupManager extends AbstractSecurityTestcase
{
@@ -123,15 +120,10 @@
try
{
gms.addUserToGroup("anonuser1", "testusertogroup1.group1");
- Collection principals = ums.getUser("anonuser1").getSubject().getPrincipals();
- Principal found =
- SecurityHelper.getPrincipal(
- new Subject(false, new HashSet(principals), new HashSet(), new HashSet()),
- GroupPrincipal.class);
- assertNotNull("found principal is null", found);
+ Collection principals = ums.getUser("anonuser1").getSubject().getPrincipals();
assertTrue(
- "found principal should be testusertogroup1.group1, " + found.getName(),
- found.getName().equals("testusertogroup1.group1"));
+ "anonuser1 should contain testusertogroup1.group1",
+ principals.contains(new GroupPrincipalImpl("testusertogroup1.group1")));
}
catch (SecurityException sex)
{
@@ -208,10 +200,12 @@
{
gms.removeGroup("testgroup1.group1");
Collection principals = ums.getUser("anonuser2").getSubject().getPrincipals();
- assertEquals(
- "principal size should be == 3 after removing testgroup1.group1, for principals: " + principals.toString(),
- 3,
- principals.size());
+ // because of hierarchical groups
+ //
+ //assertEquals(
+ // "principal size should be == 3 after removing testgroup1.group1, for principals: " + principals.toString(),
+ // 3,
+ // principals.size());
assertFalse(
"anonuser2 should not contain testgroup1.group1",
principals.contains(new GroupPrincipalImpl("testgroup1.group1")));
1.5 +11 -15 jakarta-jetspeed-2/components/security/src/test/org/apache/jetspeed/security/TestRoleManager.java
Index: TestRoleManager.java
===================================================================
RCS file: /home/cvs/jakarta-jetspeed-2/components/security/src/test/org/apache/jetspeed/security/TestRoleManager.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- TestRoleManager.java 27 May 2004 19:45:32 -0000 1.4
+++ TestRoleManager.java 16 Jun 2004 22:47:00 -0000 1.5
@@ -14,13 +14,9 @@
*/
package org.apache.jetspeed.security;
-import java.security.Principal;
import java.util.Collection;
-import java.util.HashSet;
import java.util.prefs.Preferences;
-import javax.security.auth.Subject;
-
import junit.framework.Test;
import junit.framework.TestSuite;
@@ -30,6 +26,7 @@
* <p>Unit testing for {@link RoleManager}.</p>
*
* @author <a href="mailto:dlestrat@apache.org">David Le Strat</a>
+ * @version $Id$
*/
public class TestRoleManager extends AbstractSecurityTestcase
{
@@ -121,15 +118,11 @@
try
{
rms.addRoleToUser("anonuser1", "testusertorole1.role1");
+
Collection principals = ums.getUser("anonuser1").getSubject().getPrincipals();
- Principal found =
- SecurityHelper.getPrincipal(
- new Subject(false, new HashSet(principals), new HashSet(), new HashSet()),
- RolePrincipal.class);
- assertNotNull("found principal is null", found);
assertTrue(
- "found principal should be testusertorole1.role1, " + found.getName(),
- found.getName().equals("testusertorole1.role1"));
+ "anonuser1 should contain testusertorole1.role1",
+ principals.contains(new RolePrincipalImpl("testusertorole1.role1")));
}
catch (SecurityException sex)
{
@@ -207,10 +200,12 @@
{
rms.removeRole("testrole1.role1");
Collection principals = ums.getUser("anonuser2").getSubject().getPrincipals();
- assertEquals(
- "principal size should be == 3 after removing testrole1.role1, for principals: " + principals.toString(),
- 3,
- principals.size());
+ // because of hierarchical roles
+ //
+ // assertEquals(
+ // "principal size should be == 3 after removing testrole1.role1, for principals: " + principals.toString(),
+ // 3,
+ // principals.size());
assertFalse(
"anonuser2 should not contain testrole1.role1",
principals.contains(new RolePrincipalImpl("testrole1.role1")));
@@ -636,6 +631,7 @@
rms.removeRole("testgetrole");
rms.removeRole("testuserrolemapping");
gms.removeGroup("testrolegroupmapping");
+ rms.removeRole("testusertorole1");
}
catch (SecurityException sex)
{
1.2 +27 -0 jakarta-jetspeed-2/components/security/src/test/org/apache/jetspeed/security/AbstractSecurityTestcase.java
Index: AbstractSecurityTestcase.java
===================================================================
RCS file: /home/cvs/jakarta-jetspeed-2/components/security/src/test/org/apache/jetspeed/security/AbstractSecurityTestcase.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- AbstractSecurityTestcase.java 27 May 2004 19:45:32 -0000 1.1
+++ AbstractSecurityTestcase.java 16 Jun 2004 22:47:01 -0000 1.2
@@ -6,6 +6,13 @@
*/
package org.apache.jetspeed.security;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.security.auth.Subject;
+
import org.apache.jetspeed.components.persistence.store.util.PersistenceSupportedTestCase;
import org.apache.jetspeed.security.impl.GroupManagerImpl;
import org.apache.jetspeed.security.impl.PermissionManagerImpl;
@@ -16,6 +23,7 @@
/**
* @author <a href="mailto:sweaver@einnovation.com">Scott T. Weaver</a>
+ * @version $Id$
*
*/
public class AbstractSecurityTestcase extends PersistenceSupportedTestCase
@@ -53,6 +61,25 @@
public AbstractSecurityTestcase( String arg0 )
{
super(arg0);
+ }
+
+ /**
+ * Returns subject's principals of type claz
+ *
+ * @param subject
+ * @param claz
+ * @return Returns subject's principals of type claz
+ */
+ protected Collection getPrincipals( Subject subject, Class claz){
+ List principals=new ArrayList();
+ for (Iterator iter = subject.getPrincipals().iterator(); iter.hasNext();)
+ {
+ Object element = iter.next();
+ if ( claz.isInstance(element) )
+ principals.add(element);
+
+ }
+ return principals;
}
}
1.1 jakarta-jetspeed-2/components/security/src/test/org/apache/jetspeed/security/TestGeneralizationHierarchy.java
Index: TestGeneralizationHierarchy.java
===================================================================
/* Copyright 2004 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.jetspeed.security;
import java.util.Collection;
import javax.security.auth.Subject;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.jetspeed.security.impl.GeneralizationHierarchyResolver;
import org.apache.jetspeed.security.impl.RolePrincipalImpl;
/**
* <p>Unit testing for {@link GeneralizationHierarchyResolver}.</p>
*
* @author <a href="mailto:Artem.Grinshtein@t-systems.com">Artem Grinshtein</a>
* @version $Id: TestGeneralizationHierarchy.java,v 1.1 2004/06/16 22:47:00 ate Exp $
*/
public class TestGeneralizationHierarchy extends AbstractSecurityTestcase
{
/**
* <p>Defines the test case name for junit.</p>
* @param testName The test case name.
*/
public TestGeneralizationHierarchy(String testName)
{
super(testName);
}
/**
* @see junit.framework.TestCase#tearDown()
*/
public void tearDown() throws Exception
{
destroyUserObject();
super.tearDown();
}
public static Test suite()
{
return new TestSuite(TestGeneralizationHierarchy.class);
}
/**
* <p>Test RoleManager.</p>
*/
public void testRoleMenager()
{
User user = null;
try
{
ums.addUser("test", "password");
user = ums.getUser("test");
}
catch (SecurityException sex)
{
assertTrue("user exists. should not have thrown an exception.", false);
}
assertNotNull("user is null", user);
try
{
rms.addRole("rootrole");
rms.addRole("rootrole.childrole1");
rms.addRole("rootrole.childrole2");
}
catch (SecurityException sex)
{
assertTrue("add roles. should not have thrown an exception.", false);
}
try
{
rms.addRoleToUser("test","rootrole");
user = ums.getUser("test");
Subject subject = user.getSubject();
assertNotNull("subject is null", subject);
Collection principals=getPrincipals(subject,RolePrincipal.class);
assertEquals("shoud have one principal;", 1,principals.size());
assertTrue(
"should contain rootrole",
principals.contains(new RolePrincipalImpl("rootrole")));
rms.removeRoleFromUser("test","rootrole");
user = ums.getUser("test");
principals= getPrincipals(user.getSubject(),RolePrincipal.class);
assertEquals("shoud not have any principals;", 0,principals.size());
}
catch (SecurityException sex)
{
assertTrue("test with parent role "+sex.getMessage(), false);
}
try
{
rms.addRoleToUser("test","rootrole.childrole1");
user = ums.getUser("test");
Subject subject = user.getSubject();
assertNotNull("subject is null", subject);
Collection principals=getPrincipals(subject,RolePrincipal.class);
assertEquals("expected 2 principals;", 2,principals.size());
assertTrue(
"should contain rootrole",
principals.contains(new RolePrincipalImpl("rootrole")));
assertTrue(
"should contain rootrole",
principals.contains(new RolePrincipalImpl("rootrole.childrole1")));
rms.removeRoleFromUser("test","rootrole.childrole1");
user = ums.getUser("test");
principals=getPrincipals(user.getSubject(),RolePrincipal.class);
assertEquals("shoud not have any principals;", 0,principals.size());
}
catch (SecurityException sex)
{
assertTrue("test with child role "+sex.getMessage(), false);
}
}
/**
* <p>Destroy user test object.</p>
*/
protected void destroyUserObject()
{
try
{
if (ums.userExists("test")) ums.removeUser("test");
if (rms.roleExists("rootrole")) rms.removeRole("rootrole");
}
catch (SecurityException sex)
{
System.out.println("could not remove test users. exception caught: " + sex);
}
}
}
1.1 jakarta-jetspeed-2/components/security/src/test/org/apache/jetspeed/security/TestAggregationHierarchy.java
Index: TestAggregationHierarchy.java
===================================================================
/* Copyright 2004 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.jetspeed.security;
import java.util.Collection;
import javax.security.auth.Subject;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.jetspeed.security.impl.AggregationHierarchyResolver;
import org.apache.jetspeed.security.impl.GroupManagerImpl;
import org.apache.jetspeed.security.impl.PermissionManagerImpl;
import org.apache.jetspeed.security.impl.RdbmsPolicy;
import org.apache.jetspeed.security.impl.RoleManagerImpl;
import org.apache.jetspeed.security.impl.RolePrincipalImpl;
import org.apache.jetspeed.security.impl.SecurityProviderImpl;
import org.apache.jetspeed.security.impl.UserManagerImpl;
/**
* <p>Unit testing for {@link AggregationHierarchyResolver}.</p>
*
* @author <a href="mailto:Artem.Grinshtein@t-systems.com">Artem Grinshtein</a>
* @version $Id: TestAggregationHierarchy.java,v 1.1 2004/06/16 22:47:01 ate Exp $
*/
public class TestAggregationHierarchy extends AbstractSecurityTestcase
{
/**
* <p>Defines the test case name for junit.</p>
* @param testName The test case name.
*/
public TestAggregationHierarchy(String testName)
{
super(testName);
}
/**
* @see junit.framework.TestCase#setUp()
*/
protected void setUp() throws Exception
{
super.setUp();
ums = new UserManagerImpl(persistenceStore, new AggregationHierarchyResolver(),new AggregationHierarchyResolver());
gms = new GroupManagerImpl(persistenceStore);
rms =new RoleManagerImpl(persistenceStore);
pms = new PermissionManagerImpl(persistenceStore);
new SecurityProviderImpl("login.conf", new RdbmsPolicy(pms), ums);
}
/**
* @see junit.framework.TestCase#tearDown()
*/
public void tearDown() throws Exception
{
destroyUserObject();
super.tearDown();
}
public static Test suite()
{
return new TestSuite(TestAggregationHierarchy.class);
}
/**
* <p>Test RoleManager.</p>
*/
public void testRoleMenager()
{
User user = null;
try
{
ums.addUser("test", "password");
user = ums.getUser("test");
}
catch (SecurityException sex)
{
assertTrue("user exists. should not have thrown an exception.", false);
}
assertNotNull("user is null", user);
try
{
rms.addRole("rootrole");
rms.addRole("rootrole.childrole1");
rms.addRole("rootrole.childrole2");
}
catch (SecurityException sex)
{
assertTrue("add roles. should not have thrown an exception.", false);
}
try
{
rms.addRoleToUser("test","rootrole");
user = ums.getUser("test");
Subject subject = user.getSubject();
assertNotNull("subject is null", subject);
Collection principals=getPrincipals(subject,RolePrincipal.class);
assertEquals("should have 3 principals;", 3,principals.size());
assertTrue(
"should contain rootrole",
principals.contains(new RolePrincipalImpl("rootrole")));
assertTrue(
"should contain rootrole.childrole1",
principals.contains(new RolePrincipalImpl("rootrole.childrole1")));
assertTrue(
"should contain rootrole.childrole2",
principals.contains(new RolePrincipalImpl("rootrole.childrole2")));
rms.removeRoleFromUser("test","rootrole");
user = ums.getUser("test");
principals= getPrincipals(user.getSubject(),RolePrincipal.class);
assertEquals("should not have any principals;", 0,principals.size());
}
catch (SecurityException sex)
{
assertTrue("test with parent role "+sex.getMessage(), false);
}
try
{
rms.addRoleToUser("test","rootrole.childrole1");
user = ums.getUser("test");
Subject subject = user.getSubject();
assertNotNull("subject is null", subject);
Collection principals=getPrincipals(subject,RolePrincipal.class);
assertEquals("shoud have 1 principal;", 1,principals.size());
assertTrue(
"should contain rootrole.childrole1",
principals.contains(new RolePrincipalImpl("rootrole.childrole1")));
rms.removeRoleFromUser("test","rootrole.childrole1");
user = ums.getUser("test");
principals=getPrincipals(user.getSubject(),RolePrincipal.class);
assertEquals("should not have any principals;", 0,principals.size());
}
catch (SecurityException sex)
{
assertTrue("test with child role "+sex.getMessage(), false);
}
}
/**
* <p>Destroy user test object.</p>
*/
protected void destroyUserObject()
{
try
{
if (ums.userExists("test")) ums.removeUser("test");
if (rms.roleExists("rootrole")) rms.removeRole("rootrole");
}
catch (SecurityException sex)
{
System.out.println("could not remove test users. exception caught: " + sex);
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jetspeed-dev-help@jakarta.apache.org