[jboss-cvs] JBossAS SVN: r68522 - in projects/security/security-jboss-sx/trunk/identity: src/main/org/jboss/security/identity/plugins and 6 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Fri Dec 21 13:47:07 EST 2007


Author: anil.saldhana at jboss.com
Date: 2007-12-21 13:47:06 -0500 (Fri, 21 Dec 2007)
New Revision: 68522

Added:
   projects/security/security-jboss-sx/trunk/identity/src/tests/org/
   projects/security/security-jboss-sx/trunk/identity/src/tests/org/jboss/
   projects/security/security-jboss-sx/trunk/identity/src/tests/org/jboss/test/
   projects/security/security-jboss-sx/trunk/identity/src/tests/org/jboss/test/identity/
   projects/security/security-jboss-sx/trunk/identity/src/tests/org/jboss/test/identity/impl/
   projects/security/security-jboss-sx/trunk/identity/src/tests/org/jboss/test/identity/impl/RoleUnitTestCase.java
Modified:
   projects/security/security-jboss-sx/trunk/identity/.classpath
   projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/identity/plugins/IdentityFactory.java
   projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/identity/plugins/SecurityActions.java
   projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/identity/plugins/SimpleRole.java
   projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/identity/plugins/SimpleRoleGroup.java
Log:
SECURITY-103: role/rolegroup contains logic

Modified: projects/security/security-jboss-sx/trunk/identity/.classpath
===================================================================
--- projects/security/security-jboss-sx/trunk/identity/.classpath	2007-12-21 18:44:41 UTC (rev 68521)
+++ projects/security/security-jboss-sx/trunk/identity/.classpath	2007-12-21 18:47:06 UTC (rev 68522)
@@ -1,14 +1,14 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<classpath>
-	<classpathentry kind="src" path="src/main"/>
-	<classpathentry kind="src" path="src/resources"/>
-	<classpathentry excluding="resources/" kind="src" output="target/test-classes" path="src/tests"/>
-	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
-	<classpathentry kind="var" path="M2_REPO/apache-log4j/log4j/1.2.14/log4j-1.2.14.jar"/>
-	<classpathentry kind="var" path="M2_REPO/org/jboss/security/jboss-security-spi/2.0.2.beta/jboss-security-spi-2.0.2.beta.jar" sourcepath="M2_REPO/org/jboss/security/jboss-security-spi/2.0.2-SNAPSHOT/spi-2.0.2-SNAPSHOT-sources.jar"/>
-	<classpathentry kind="var" path="M2_REPO/jboss/jboss-test/1.0.3.GA/jboss-test-1.0.3.GA.jar"/>
-	<classpathentry kind="var" path="M2_REPO/junit/junit/3.8.1/junit-3.8.1.jar"/>
-	<classpathentry kind="var" path="M2_REPO/org/jboss/jboss-common-core/2.2.1.GA/jboss-common-core-2.2.1.GA.jar"/>
-	<classpathentry kind="var" path="M2_REPO/jboss/jboss-logging-spi/2.0.2.GA/jboss-logging-spi-2.0.2.GA.jar"/>
-	<classpathentry kind="output" path="target/eclipse-classes"/>
-</classpath>
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="src" path="src/main"/>
+	<classpathentry kind="src" path="src/resources"/>
+	<classpathentry excluding="resources/" kind="src" output="target/test-classes" path="src/tests"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+	<classpathentry kind="var" path="M2_REPO/apache-log4j/log4j/1.2.14/log4j-1.2.14.jar"/>
+	<classpathentry kind="var" path="M2_REPO/org/jboss/security/identity-spi/2.0.2-SNAPSHOT/identity-spi-2.0.2-SNAPSHOT.jar" sourcepath="M2_REPO/org/jboss/security/jboss-security-spi/2.0.2-SNAPSHOT/spi-2.0.2-SNAPSHOT-sources.jar"/>
+	<classpathentry kind="var" path="M2_REPO/jboss/jboss-test/1.0.3.GA/jboss-test-1.0.3.GA.jar"/>
+	<classpathentry kind="var" path="M2_REPO/junit/junit/3.8.1/junit-3.8.1.jar"/>
+	<classpathentry kind="var" path="M2_REPO/org/jboss/jboss-common-core/2.2.1.GA/jboss-common-core-2.2.1.GA.jar"/>
+	<classpathentry kind="var" path="M2_REPO/jboss/jboss-logging-spi/2.0.2.GA/jboss-logging-spi-2.0.2.GA.jar"/>
+	<classpathentry kind="output" path="target/eclipse-classes"/>
+</classpath>

Modified: projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/identity/plugins/IdentityFactory.java
===================================================================
--- projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/identity/plugins/IdentityFactory.java	2007-12-21 18:44:41 UTC (rev 68521)
+++ projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/identity/plugins/IdentityFactory.java	2007-12-21 18:47:06 UTC (rev 68522)
@@ -42,15 +42,15 @@
 
    public static Principal createPrincipal(String name) throws Exception
    {
-     Class<Principal> clazz = SecurityActions.getClass(PRINCIPAL_CLASS);
-     Constructor<Principal> ctr = clazz.getConstructor(new Class[]{String.class});
+     Class<?> clazz = SecurityActions.getClass(PRINCIPAL_CLASS);
+     Constructor<?> ctr = clazz.getConstructor(new Class[]{String.class});
      return (Principal) ctr.newInstance(new Object[]{name});
    }
    
    public static Group createGroup(String name) throws Exception
    {
-      Class<Group> clazz = SecurityActions.getClass(GROUP_CLASS);
-      Constructor<Group> ctr = clazz.getConstructor(new Class[]{String.class});
+      Class<?> clazz = SecurityActions.getClass(GROUP_CLASS);
+      Constructor<?> ctr = clazz.getConstructor(new Class[]{String.class});
       return (Group) ctr.newInstance(new Object[]{name});
    }
 }
\ No newline at end of file

Modified: projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/identity/plugins/SecurityActions.java
===================================================================
--- projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/identity/plugins/SecurityActions.java	2007-12-21 18:44:41 UTC (rev 68521)
+++ projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/identity/plugins/SecurityActions.java	2007-12-21 18:47:06 UTC (rev 68522)
@@ -31,13 +31,13 @@
  *  @author Anil.Saldhana at redhat.com
  *  @since  Nov 18, 2007 
  *  @version $Revision$
- */
- at SuppressWarnings("unused")
+ */ 
 public class SecurityActions
 {
-   public static Class getClass(final String FQN)
+   @SuppressWarnings("unchecked")
+   public static Class<?> getClass(final String FQN)
    {
-     return (Class) AccessController.doPrivileged(new PrivilegedAction()
+     return (Class<?>) AccessController.doPrivileged(new PrivilegedAction()
      {
       public Object run()
       {
@@ -49,11 +49,7 @@
          catch(Exception e)
          {
             throw new RuntimeException(e);
-         }
-         finally
-         {
-            return null;
-         }
+         } 
       }
     });  
    }

Modified: projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/identity/plugins/SimpleRole.java
===================================================================
--- projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/identity/plugins/SimpleRole.java	2007-12-21 18:44:41 UTC (rev 68521)
+++ projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/identity/plugins/SimpleRole.java	2007-12-21 18:47:06 UTC (rev 68522)
@@ -52,5 +52,28 @@
    public RoleType getType()
    { 
       return RoleType.simple;
+   }
+
+   /**
+    * @see Role#contains(Role)
+    */
+   public boolean containsAll(Role anotherRole)
+   {
+      if(anotherRole.getType() == RoleType.simple)
+         return roleName.equals(anotherRole.getRoleName());
+      return false; 
+   }
+
+   @Override
+   protected Object clone() throws CloneNotSupportedException
+   {
+      // TODO Auto-generated method stub
+      return super.clone();
+   }
+
+   @Override
+   public String toString()
+   {
+     return roleName;
    } 
 }
\ No newline at end of file

Modified: projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/identity/plugins/SimpleRoleGroup.java
===================================================================
--- projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/identity/plugins/SimpleRoleGroup.java	2007-12-21 18:44:41 UTC (rev 68521)
+++ projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/identity/plugins/SimpleRoleGroup.java	2007-12-21 18:47:06 UTC (rev 68522)
@@ -21,8 +21,12 @@
   */
 package org.jboss.security.identity.plugins;
 
+import java.security.Principal;
+import java.security.acl.Group;
 import java.util.ArrayList;
+import java.util.Enumeration;
 import java.util.List;
+import java.util.Set;
 
 import org.jboss.security.identity.Role;
 import org.jboss.security.identity.RoleGroup;
@@ -39,7 +43,7 @@
 public class SimpleRoleGroup extends SimpleRole implements RoleGroup
 {
    private static final long serialVersionUID = 1L;
-   private ArrayList<Role> roles;
+   private ArrayList<Role> roles = new ArrayList<Role>();
 
    public SimpleRoleGroup(String roleName)
    {
@@ -54,6 +58,25 @@
       this.roles.addAll(roles);
    }
    
+   public SimpleRoleGroup(Group rolesGroup)
+   {
+     super(rolesGroup.getName());
+     Enumeration<? extends Principal> principals = rolesGroup.members();
+     while(principals.hasMoreElements())
+     {
+        roles.add(new SimpleRole(principals.nextElement().getName()));
+     }
+   }
+   
+   public SimpleRoleGroup(Set<Principal> rolesAsPrincipals)
+   {
+      super("Roles");
+      for(Principal p: rolesAsPrincipals)
+      {
+         roles.add(new SimpleRole(p.getName()));
+      }
+   }
+   
    @Override
    public RoleType getType()
    {
@@ -65,11 +88,85 @@
       return roles;
    }
    
+   @SuppressWarnings("unchecked")
    public synchronized Object clone() throws CloneNotSupportedException  
    {  
       SimpleRoleGroup clone = (SimpleRoleGroup) super.clone();  
       if(clone != null) 
         clone.roles = (ArrayList<Role>)this.roles.clone();   
       return clone;  
+   }
+
+   @Override
+   public boolean containsAll(Role anotherRole)
+   {
+      boolean isContained = false;
+      
+      if(anotherRole.getType() == RoleType.simple)
+      {
+         for(Role r: roles)
+         {
+            isContained = r.containsAll(anotherRole);
+            if(isContained)
+               return true;
+         } 
+      }
+      else
+      {
+         //Dealing with another roleGroup
+         RoleGroup anotherRG = (RoleGroup) anotherRole;
+         List<Role> anotherRoles = anotherRG.getRoles();
+         for(Role r: anotherRoles)
+         {
+            //if any of the roles are not there, no point checking further
+            if(!this.containsAll(r)) 
+               return false;
+         }
+         return true;
+      }
+      return false;
+   }
+
+   /**
+    * @see RoleGroup#containsAtleastOneRole(RoleGroup)
+    */
+   public boolean containsAtleastOneRole(RoleGroup anotherRoleGroup)
+   { 
+      if(anotherRoleGroup == null)
+         throw new IllegalArgumentException("anotherRoleGroup is null");
+      List<Role> roleList = anotherRoleGroup.getRoles();
+      for(Role r: roleList)
+      {
+         if(this.containsAll(r))
+            return true;
+      }
+      return false;
+   }
+    
+   /**
+    * @see RoleGroup#containsRole(Role)
+    */
+   public boolean containsRole(Role role)
+   {
+      for(Role r: roles)
+      {
+         if(r.containsAll(role))
+            return true;
+      }
+      return false;
+   }
+
+   @Override
+   public String toString()
+   {
+      StringBuilder builder = new StringBuilder();
+      builder.append(this.getRoleName());
+      builder.append("(");
+      for(Role role: roles)
+      { 
+         builder.append(role.toString()).append(",");
+      }
+      builder.append(")"); 
+      return builder.toString();
    } 
 }
\ No newline at end of file

Added: projects/security/security-jboss-sx/trunk/identity/src/tests/org/jboss/test/identity/impl/RoleUnitTestCase.java
===================================================================
--- projects/security/security-jboss-sx/trunk/identity/src/tests/org/jboss/test/identity/impl/RoleUnitTestCase.java	                        (rev 0)
+++ projects/security/security-jboss-sx/trunk/identity/src/tests/org/jboss/test/identity/impl/RoleUnitTestCase.java	2007-12-21 18:47:06 UTC (rev 68522)
@@ -0,0 +1,68 @@
+/*
+  * JBoss, Home of Professional Open Source
+  * Copyright 2007, JBoss Inc., and individual contributors as indicated
+  * by the @authors tag. See the copyright.txt in the distribution for a
+  * full listing of individual contributors.
+  *
+  * This is free software; you can redistribute it and/or modify it
+  * under the terms of the GNU Lesser General Public License as
+  * published by the Free Software Foundation; either version 2.1 of
+  * the License, or (at your option) any later version.
+  *
+  * This software is distributed in the hope that it will be useful,
+  * but WITHOUT ANY WARRANTY; without even the implied warranty of
+  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  * Lesser General Public License for more details.
+  *
+  * You should have received a copy of the GNU Lesser General Public
+  * License along with this software; if not, write to the Free
+  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+  */
+package org.jboss.test.identity.impl;
+
+import org.jboss.security.identity.Role;
+import org.jboss.security.identity.plugins.SimpleRole;
+import org.jboss.security.identity.plugins.SimpleRoleGroup;
+
+import junit.framework.TestCase;
+
+//$Id$
+
+/**
+ *  Tests for the Role classes
+ *  @author Anil.Saldhana at redhat.com
+ *  @since  Dec 20, 2007 
+ *  @version $Revision$
+ */
+public class RoleUnitTestCase extends TestCase
+{
+   public void testSimpleRoleContains() throws Exception
+   {
+      Role firstRole = new SimpleRole("A");
+      Role secondRole = new SimpleRole("B");
+      
+      assertTrue(firstRole.containsAll(firstRole));
+      assertFalse(firstRole.containsAll(secondRole));
+      assertFalse(secondRole.containsAll(firstRole));
+   } 
+   
+   public void testSimpleRoleGroupContains() throws Exception
+   {
+      SimpleRoleGroup firstRoleGroup = new SimpleRoleGroup("firstrg");
+      firstRoleGroup.getRoles().add(new SimpleRole("A"));
+      firstRoleGroup.getRoles().add(new SimpleRole("B"));
+      firstRoleGroup.getRoles().add(new SimpleRole("C"));
+      
+      SimpleRoleGroup secondRoleGroup = new SimpleRoleGroup("secondrg");
+      secondRoleGroup.getRoles().add(new SimpleRole("A"));
+      secondRoleGroup.getRoles().add(new SimpleRole("B"));
+      
+      assertTrue(firstRoleGroup.containsAll(firstRoleGroup));
+      assertTrue(secondRoleGroup.containsAll(secondRoleGroup));
+      assertTrue(firstRoleGroup.containsAll(secondRoleGroup));
+      assertFalse(secondRoleGroup.containsAll(firstRoleGroup));
+      
+      assertTrue(firstRoleGroup.containsAtleastOneRole(secondRoleGroup));
+   }
+}




More information about the jboss-cvs-commits mailing list