[seam-commits] Seam SVN: r12806 - in modules/security/trunk: impl/src/main/java/org/jboss/seam/security/management and 1 other directories.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Wed May 26 05:33:31 EDT 2010


Author: shane.bryzak at jboss.com
Date: 2010-05-26 05:33:30 -0400 (Wed, 26 May 2010)
New Revision: 12806

Modified:
   modules/security/trunk/api/src/main/java/org/jboss/seam/security/management/IdentityStore.java
   modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/JpaIdentityStore.java
   modules/security/trunk/impl/src/main/java/org/jboss/seam/security/util/AnnotatedBeanProperty.java
Log:
implement some of the JpaIdentityStore configuration


Modified: modules/security/trunk/api/src/main/java/org/jboss/seam/security/management/IdentityStore.java
===================================================================
--- modules/security/trunk/api/src/main/java/org/jboss/seam/security/management/IdentityStore.java	2010-05-26 08:59:51 UTC (rev 12805)
+++ modules/security/trunk/api/src/main/java/org/jboss/seam/security/management/IdentityStore.java	2010-05-26 09:33:30 UTC (rev 12806)
@@ -3,8 +3,10 @@
 import java.io.Serializable;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 
+import org.picketlink.idm.api.Credential;
 import org.picketlink.idm.api.Group;
 import org.picketlink.idm.api.IdentityType;
 import org.picketlink.idm.api.Role;
@@ -80,19 +82,12 @@
    boolean supportsFeature(Feature feature);
 
    /**
-    * Creates a new user with the specified username and password.
+    * Creates a new user with the specified username and credential.
     * @return true if the user was successfully created.
     */
-   boolean createUser(String username, String password);
-   
+   boolean createUser(String username, Credential credential, Map<String,?> attributes);
+     
    /**
-    * Creates a new user with the specified username, password, first name and last name.
-    * 
-    * @return true if the user was successfully created.
-    */
-   boolean createUser(String username, String password, String firstname, String lastname);
-   
-   /**
     * Deletes the user with the specified username.
     * @return true if the user was successfully deleted.
     */
@@ -116,10 +111,10 @@
    boolean isUserEnabled(String username);
    
    /**
-    * Changes the password of the specified user to the specified password.
-    * @return true if the user's password was successfully changed.
+    * Updates the credential of the specified user.
+    * @return true if the user's credential was successfully changed.
     */
-   boolean changePassword(String username, String password);   
+   boolean updateCredential(String username, Credential credential);   
    
    /**
     * Returns true if the specified user exists.
@@ -250,9 +245,9 @@
    List<IdentityType> listGroupMembers(Group group);
 
    /**
-    * Authenticates the specified user, using the specified password.
+    * Authenticates the specified user, using the specified credential.
     * 
     * @return true if authentication is successful.
     */
-   boolean authenticate(String username, String password);
+   boolean authenticate(String username, Credential credential);
 }

Modified: modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/JpaIdentityStore.java
===================================================================
--- modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/JpaIdentityStore.java	2010-05-26 08:59:51 UTC (rev 12805)
+++ modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/JpaIdentityStore.java	2010-05-26 09:33:30 UTC (rev 12806)
@@ -2,10 +2,18 @@
 
 import java.io.Serializable;
 import java.util.List;
+import java.util.Map;
 
 import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.inject.Instance;
 import javax.inject.Inject;
+import javax.persistence.EntityManager;
 
+import org.jboss.seam.security.annotations.management.IdentityProperty;
+import org.jboss.seam.security.annotations.management.PropertyType;
+import org.jboss.seam.security.util.AnnotatedBeanProperty;
+import org.jboss.seam.security.util.AnnotatedBeanProperty.AttributeValue;
+import org.picketlink.idm.api.Credential;
 import org.picketlink.idm.api.Group;
 import org.picketlink.idm.api.IdentityType;
 import org.picketlink.idm.api.Role;
@@ -24,19 +32,124 @@
    
    private static final String DEFAULT_USER_IDENTITY_TYPE = "USER";
    private static final String DEFAULT_ROLE_IDENTITY_TYPE = "ROLE";
-   private static final String DEFAULT_GROUP_IDENTITY_TYPE = "GROUP";
+   private static final String DEFAULT_GROUP_IDENTITY_TYPE = "GROUP";   
    
+   private static final String DEFAULT_RELATIONSHIP_TYPE_MEMBERSHIP = "MEMBERSHIP";
+   private static final String DEFAULT_RELATIONSHIP_TYPE_ROLE = "ROLE";
+   
+   private static final AttributeValue NAME_ATTRIBUTE = new AttributeValue("value", PropertyType.NAME);
+   private static final AttributeValue VALUE_ATTRIBUTE = new AttributeValue("value", PropertyType.VALUE);
+   private static final AttributeValue TYPE_ATTRIBUTE = new AttributeValue("value", PropertyType.TYPE);
+   private static final AttributeValue RELATIONSHIP_FROM_ATTRIBUTE = new AttributeValue("value", PropertyType.RELATIONSHIP_FROM);
+   private static final AttributeValue RELATIONSHIP_TO_ATTRIBUTE = new AttributeValue("value", PropertyType.RELATIONSHIP_TO);
+   
    private Logger log = LoggerFactory.getLogger(JpaIdentityStore.class);
    
+   // The following entity classes are configurable
    private Class<?> identityObjectEntity;
    private Class<?> identityObjectRelationshipEntity;
    private Class<?> identityObjectCredentialEntity;
    private Class<?> identityObjectAttributeEntity;
+   private Class<?> identityRoleTypeEntity;
    
+   // The following entity classes may be determined automatically
+   private Class<?> identityObjectTypeEntity;
+   private Class<?> identityObjectRelationshipTypeEntity;
+   private Class<?> identityObjectCredentialTypeEntity;
+   
+   
+   private AnnotatedBeanProperty<IdentityProperty> identityNameProperty;
+   private AnnotatedBeanProperty<IdentityProperty> identityTypeProperty;
+   private AnnotatedBeanProperty<IdentityProperty> identityTypeNameProperty;
+   private AnnotatedBeanProperty<IdentityProperty> relationshipNameProperty;
+   private AnnotatedBeanProperty<IdentityProperty> relationshipTypeProperty;
+   private AnnotatedBeanProperty<IdentityProperty> relationshipFromProperty;
+   private AnnotatedBeanProperty<IdentityProperty> relationshipToProperty;
+   private AnnotatedBeanProperty<IdentityProperty> relationshipTypeNameProperty;
+   private AnnotatedBeanProperty<IdentityProperty> credentialTypeProperty;
+   private AnnotatedBeanProperty<IdentityProperty> credentialValueProperty;
+   private AnnotatedBeanProperty<IdentityProperty> credentialTypeNameProperty;
+   private AnnotatedBeanProperty<IdentityProperty> attributeNameProperty;
+   private AnnotatedBeanProperty<IdentityProperty> attributeValueProperty;
+   private AnnotatedBeanProperty<IdentityProperty> roleTypeNameProperty;
+   
    private String userIdentityType = DEFAULT_USER_IDENTITY_TYPE;
    private String roleIdentityType = DEFAULT_ROLE_IDENTITY_TYPE;
    private String groupIdentityType = DEFAULT_GROUP_IDENTITY_TYPE;
    
+   private String relationshipTypeMembership = DEFAULT_RELATIONSHIP_TYPE_MEMBERSHIP;
+   private String relationshipTypeRole = DEFAULT_RELATIONSHIP_TYPE_ROLE;
+   
+   @Inject
+   public void init()
+   {
+      if (identityObjectEntity == null)
+      {
+         throw new IdentityManagementException(
+               "Error initializing JpaIdentityStore - identityObjectEntity not set");
+      }
+      
+      if (identityObjectRelationshipEntity == null)
+      {
+         throw new IdentityManagementException(
+               "Error initializing JpaIdentityStore - identityObjectRelationshipEntity not set");
+      }
+      
+      identityNameProperty = new AnnotatedBeanProperty<IdentityProperty>(
+            identityObjectEntity, IdentityProperty.class, NAME_ATTRIBUTE);
+      identityTypeProperty = new AnnotatedBeanProperty<IdentityProperty>(
+            identityObjectEntity, IdentityProperty.class, TYPE_ATTRIBUTE);
+      
+      if (!String.class.equals(identityTypeProperty.getPropertyType()))
+      {
+         // If the identity type property isn't a String, it must be a related entity
+         identityObjectTypeEntity = (Class<?>) identityTypeProperty.getPropertyType();         
+         identityTypeNameProperty = new AnnotatedBeanProperty<IdentityProperty>(
+               identityObjectTypeEntity, IdentityProperty.class, NAME_ATTRIBUTE);
+      }
+      
+      relationshipNameProperty = new AnnotatedBeanProperty<IdentityProperty>(
+            identityObjectRelationshipEntity, IdentityProperty.class, NAME_ATTRIBUTE);
+      relationshipFromProperty = new AnnotatedBeanProperty<IdentityProperty>(
+            identityObjectRelationshipEntity, IdentityProperty.class, RELATIONSHIP_FROM_ATTRIBUTE);
+      relationshipToProperty = new AnnotatedBeanProperty<IdentityProperty>(
+            identityObjectRelationshipEntity, IdentityProperty.class, RELATIONSHIP_TO_ATTRIBUTE);
+      relationshipTypeProperty = new AnnotatedBeanProperty<IdentityProperty>(
+            identityObjectRelationshipEntity, IdentityProperty.class, TYPE_ATTRIBUTE);
+      
+      if (!String.class.equals(relationshipTypeProperty.getPropertyType()))
+      {
+         identityObjectRelationshipTypeEntity = (Class<?>) relationshipTypeProperty.getPropertyType(); 
+         relationshipTypeNameProperty = new AnnotatedBeanProperty<IdentityProperty>(
+               identityObjectRelationshipTypeEntity, IdentityProperty.class, NAME_ATTRIBUTE);
+      }
+      
+      // If a credential entity has been configured, scan it
+      if (identityObjectCredentialEntity != null)
+      {
+         credentialTypeProperty = new AnnotatedBeanProperty<IdentityProperty>(
+               identityObjectCredentialEntity, IdentityProperty.class, TYPE_ATTRIBUTE);
+         
+         if (!String.class.equals(credentialTypeProperty.getPropertyType()))
+         {
+            identityObjectCredentialTypeEntity = (Class<?>) credentialTypeProperty.getPropertyType();
+            credentialTypeNameProperty = new AnnotatedBeanProperty<IdentityProperty>(
+                  identityObjectCredentialTypeEntity, IdentityProperty.class, NAME_ATTRIBUTE);
+         }
+         
+         credentialValueProperty = new AnnotatedBeanProperty<IdentityProperty>(
+               identityObjectCredentialEntity, IdentityProperty.class, VALUE_ATTRIBUTE);
+      }
+      // otherwise assume that the credential value is stored in the identityObjectEntity
+      else
+      {
+         // TODO implement this, we'll probably need some new PropertyType enums to support it
+      }
+      
+      
+      
+   }
+   
    public Class<?> getIdentityObjectEntity()
    {
       return identityObjectEntity;
@@ -77,6 +190,16 @@
       this.identityObjectAttributeEntity = identityObjectAttributeEntity;
    }
    
+   public Class<?> getIdentityRoleTypeEntity()
+   {
+      return identityRoleTypeEntity;
+   }
+   
+   public void setIdentityRoleTypeEntity(Class<?> identityRoleTypeEntity)
+   {
+      this.identityRoleTypeEntity = identityRoleTypeEntity;
+   }
+   
    public String getUserIdentityType()
    {
       return userIdentityType;
@@ -107,21 +230,50 @@
       this.groupIdentityType = groupIdentityType;
    }
    
-   @Inject PasswordEncoder passwordEncoder;
+   public String getRelationshipTypeMembership()
+   {
+      return relationshipTypeMembership;
+   }
+   
+   public void setRelationshipTypeMembership(String relationshipTypeMembership)
+   {
+      this.relationshipTypeMembership = relationshipTypeMembership;
+   }
+   
+   public String getRelationshipTypeRole()
+   {
+      return relationshipTypeRole;
+   }
+   
+   public void setRelationshipTypeRole(String relationshipTypeRole)
+   {
+      this.relationshipTypeRole = relationshipTypeRole;
+   }
 
+   /**
+    * 
+    */
+   @Inject Instance<EntityManager> entityManagerInstance;
+   
+   /**
+    * 
+    */
+   @Inject PasswordEncoder passwordEncoder;   
+
+
    public boolean addUserToGroup(String username, Group group)
    {
       // TODO Auto-generated method stub
       return false;
    }
 
-   public boolean authenticate(String username, String password)
+   public boolean authenticate(String username, Credential credential)
    {
       // TODO Auto-generated method stub
       return false;
    }
 
-   public boolean changePassword(String username, String password)
+   public boolean updateCredential(String username, Credential credential)
    {
       // TODO Auto-generated method stub
       return false;
@@ -139,19 +291,12 @@
       return false;
    }
 
-   public boolean createUser(String username, String password)
+   public boolean createUser(String username, Credential credential, Map<String,?> attributes)
    {
       // TODO Auto-generated method stub
       return false;
    }
 
-   public boolean createUser(String username, String password,
-         String firstname, String lastname)
-   {
-      // TODO Auto-generated method stub
-      return false;
-   }
-
    public boolean deleteGroup(String name, String groupType)
    {
       // TODO Auto-generated method stub

Modified: modules/security/trunk/impl/src/main/java/org/jboss/seam/security/util/AnnotatedBeanProperty.java
===================================================================
--- modules/security/trunk/impl/src/main/java/org/jboss/seam/security/util/AnnotatedBeanProperty.java	2010-05-26 08:59:51 UTC (rev 12805)
+++ modules/security/trunk/impl/src/main/java/org/jboss/seam/security/util/AnnotatedBeanProperty.java	2010-05-26 09:33:30 UTC (rev 12806)
@@ -5,6 +5,8 @@
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 import java.lang.reflect.Type;
+import java.util.HashMap;
+import java.util.Map;
 
 /**
  * A convenience class for working with an annotated property (either a field or method) of
@@ -24,12 +26,35 @@
    private boolean isFieldProperty;
    private boolean set = false;
    
-   public AnnotatedBeanProperty(Class<?> cls, Class<T> annotationClass)
-   {      
+   public static class AttributeValue
+   {
+      private String name;
+      private Object value;
+      
+      public AttributeValue(String name, Object value)
+      {
+         this.name = name;
+         this.value = value;
+      }
+      
+      public String getName()
+      {
+         return name;
+      }
+      
+      public Object getValue()
+      {
+         return value;
+      }
+   }
+   
+   public AnnotatedBeanProperty(Class<?> cls, Class<T> annotationClass, AttributeValue... attributes)
+   {            
       // First check declared fields
       for (Field f : cls.getDeclaredFields())
       {
-         if (f.isAnnotationPresent(annotationClass)) 
+         if (f.isAnnotationPresent(annotationClass) && 
+               attributesMatch(f.getAnnotation(annotationClass), attributes)) 
          {
             setupFieldProperty(f);
             this.annotation = f.getAnnotation(annotationClass);            
@@ -41,7 +66,8 @@
       // Then check public fields, in case it's inherited
       for (Field f : cls.getFields())
       {
-         if (f.isAnnotationPresent(annotationClass)) 
+         if (f.isAnnotationPresent(annotationClass) && 
+               attributesMatch(f.getAnnotation(annotationClass), attributes)) 
          {
             this.annotation = f.getAnnotation(annotationClass);
             setupFieldProperty(f);
@@ -53,7 +79,8 @@
       // Then check public methods (we ignore private methods)
       for (Method m : cls.getMethods())
       {
-         if (m.isAnnotationPresent(annotationClass))
+         if (m.isAnnotationPresent(annotationClass) && 
+               attributesMatch(m.getAnnotation(annotationClass), attributes))
          {
             this.annotation = m.getAnnotation(annotationClass);
             String methodName = m.getName();
@@ -83,6 +110,32 @@
          }
       }      
    }
+   
+   private boolean attributesMatch(T annotation, AttributeValue[] attributes)
+   {
+      Class<?> cls = annotation.getClass();
+      for (AttributeValue attrib : attributes)
+      {
+         try
+         {
+            Field f = cls.getField(attrib.getName());
+            if (!f.get(annotation).equals(attrib.getValue()))
+            {
+               return false;
+            }
+         }
+         catch (IllegalAccessException e)
+         {
+            return false;
+         }
+         catch (NoSuchFieldException e)
+         {
+            return false;
+         }         
+      }
+      
+      return true;
+   }
 
    private void setupFieldProperty(Field propertyField)
    {



More information about the seam-commits mailing list