[seam-commits] Seam SVN: r13088 - modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Tue Jun 8 22:01:16 EDT 2010


Author: shane.bryzak at jboss.com
Date: 2010-06-08 22:01:11 -0400 (Tue, 08 Jun 2010)
New Revision: 13088

Added:
   modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/IdentityObjectRelationshipImpl.java
   modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/IdentityObjectTypeImpl.java
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/PasswordHashEncoder.java
Log:
relationship creation

Added: modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/IdentityObjectRelationshipImpl.java
===================================================================
--- modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/IdentityObjectRelationshipImpl.java	                        (rev 0)
+++ modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/IdentityObjectRelationshipImpl.java	2010-06-09 02:01:11 UTC (rev 13088)
@@ -0,0 +1,51 @@
+package org.jboss.seam.security.management;
+
+import java.io.Serializable;
+
+import org.picketlink.idm.spi.model.IdentityObject;
+import org.picketlink.idm.spi.model.IdentityObjectRelationship;
+import org.picketlink.idm.spi.model.IdentityObjectRelationshipType;
+
+/**
+ * 
+ * @author Shane Bryzak
+ */
+public class IdentityObjectRelationshipImpl implements IdentityObjectRelationship, Serializable
+{
+   private static final long serialVersionUID = 487517126125658201L;
+   
+   private IdentityObject fromIdentityObject;
+   private IdentityObject toIdentityObject;
+   private String name;
+   private IdentityObjectRelationshipType type;
+   
+   public IdentityObjectRelationshipImpl(IdentityObject fromIdentityObject,
+         IdentityObject toIdentityObject, String name, 
+         IdentityObjectRelationshipType type)
+   {
+      this.fromIdentityObject = fromIdentityObject;
+      this.toIdentityObject = toIdentityObject;
+      this.name = name;
+      this.type = type;
+   }
+
+   public IdentityObject getFromIdentityObject()
+   {
+      return fromIdentityObject;
+   }
+
+   public IdentityObject getToIdentityObject()
+   {
+      return toIdentityObject;
+   }   
+
+   public String getName()
+   {
+      return name;
+   }
+
+   public IdentityObjectRelationshipType getType()
+   {
+      return type;
+   }
+}

Added: modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/IdentityObjectTypeImpl.java
===================================================================
--- modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/IdentityObjectTypeImpl.java	                        (rev 0)
+++ modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/IdentityObjectTypeImpl.java	2010-06-09 02:01:11 UTC (rev 13088)
@@ -0,0 +1,27 @@
+package org.jboss.seam.security.management;
+
+import java.io.Serializable;
+
+import org.picketlink.idm.spi.model.IdentityObjectType;
+
+/**
+ * Simple implementation of IdentityObjectType
+ * 
+ * @author Shane Bryzak
+ */
+public class IdentityObjectTypeImpl implements IdentityObjectType, Serializable
+{
+   private static final long serialVersionUID = -4364461076493738717L;
+   
+   private String name;
+   
+   public IdentityObjectTypeImpl(String name)
+   {
+      this.name = name;
+   }
+   
+   public String getName()
+   {
+      return name;
+   }
+}

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-06-09 00:20:02 UTC (rev 13087)
+++ modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/JpaIdentityStore.java	2010-06-09 02:01:11 UTC (rev 13088)
@@ -11,6 +11,7 @@
 
 import javax.enterprise.context.ApplicationScoped;
 import javax.enterprise.inject.Instance;
+import javax.enterprise.inject.spi.BeanManager;
 import javax.inject.Inject;
 import javax.persistence.Entity;
 import javax.persistence.EntityManager;
@@ -19,19 +20,15 @@
 
 import org.jboss.seam.security.annotations.management.IdentityProperty;
 import org.jboss.seam.security.annotations.management.PropertyType;
+import org.jboss.seam.security.events.PrePersistUserEvent;
+import org.jboss.seam.security.events.UserCreatedEvent;
 import org.jboss.weld.extensions.util.properties.Property;
 import org.jboss.weld.extensions.util.properties.query.AnnotatedPropertyCriteria;
 import org.jboss.weld.extensions.util.properties.query.NamedPropertyCriteria;
 import org.jboss.weld.extensions.util.properties.query.PropertyCriteria;
 import org.jboss.weld.extensions.util.properties.query.PropertyQueries;
 import org.jboss.weld.extensions.util.properties.query.TypedPropertyCriteria;
-import org.picketlink.idm.api.Credential;
-import org.picketlink.idm.api.Group;
-import org.picketlink.idm.api.IdentityType;
-import org.picketlink.idm.api.Role;
 import org.picketlink.idm.common.exception.IdentityException;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 import org.picketlink.idm.spi.configuration.IdentityStoreConfigurationContext;
 import org.picketlink.idm.spi.configuration.metadata.IdentityObjectAttributeMetaData;
 import org.picketlink.idm.spi.exception.OperationNotSupportedException;
@@ -43,9 +40,10 @@
 import org.picketlink.idm.spi.model.IdentityObjectType;
 import org.picketlink.idm.spi.search.IdentityObjectSearchCriteria;
 import org.picketlink.idm.spi.store.FeaturesMetaData;
-import org.picketlink.idm.spi.store.IdentityStore;
 import org.picketlink.idm.spi.store.IdentityStoreInvocationContext;
 import org.picketlink.idm.spi.store.IdentityStoreSession;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * IdentityStore implementation that allows identity related data to be 
@@ -86,6 +84,21 @@
    private static final String PROPERTY_ATTRIBUTE_NAME = "ATTRIBUTE_NAME";
    private static final String PROPERTY_ATTRIBUTE_VALUE = "ATTRIBUTE_VALUE";
    private static final String PROPERTY_ROLE_TYPE_NAME = "ROLE_TYPE_NAME";
+   
+   /**
+    * The bean manager
+    */
+   @Inject BeanManager beanManager;
+
+   /**
+    * 
+    */
+   @Inject Instance<EntityManager> entityManagerInstance;
+   
+   /**
+    * 
+    */
+   //@Inject CredentialProcessor credentialEncoder;   
       
    // Entity classes
    
@@ -802,95 +815,39 @@
    public void setRelationshipTypeRole(String relationshipTypeRole)
    {
       this.relationshipTypeRole = relationshipTypeRole;
+   }  
+
+   public IdentityObject createIdentityObject(
+         IdentityStoreInvocationContext invocationCtx, String name,
+         IdentityObjectType identityObjectType) throws IdentityException
+   {
+      return createIdentityObject(invocationCtx, name, identityObjectType, null);
    }
-
-   /**
-    * 
-    */
-   @Inject Instance<EntityManager> entityManagerInstance;
    
-   /**
-    * 
-    */
-   @Inject CredentialEncoder credentialEncoder;
-   
-   public boolean createUser(String username, Credential credential,
-         Map<String, ?> attributes) throws IdentityException
+   protected Object lookupIdentityType(String identityType) throws IdentityException
    {      
       try
       {
-         if (identityClass == null)
-         {
-            throw new IdentityException("Could not create user, identityObjectEntity not set.");
-         }
+         Property<Object> typeNameProp = modelProperties.get(PROPERTY_IDENTITY_TYPE_NAME);
          
-         //if (userExists(username))
-         //{
-           // log.warn("Could not create user, already exists.");
-         //}
-         
-         Object userInstance = identityClass.newInstance();
-         Object credentialInstance = null;
-         
-         modelProperties.get(PROPERTY_IDENTITY_NAME).setValue(userInstance, username);
-         
-         Property<Object> identityType = modelProperties.get(PROPERTY_IDENTITY_TYPE);
-         if (String.class.equals(identityType.getJavaClass()))
-         {
-            identityType.setValue(userInstance, userIdentityType);
-         }
-         else
-         {
-            identityType.setValue(userInstance, lookupIdentityType(userIdentityType));
-         }
-         
-         if (credentialClass == null)
-         {
-            modelProperties.get(PROPERTY_CREDENTIAL_VALUE).setValue(userInstance, credential);
-         }
-         else
-         {
-            credentialInstance = credentialClass.newInstance();
-            // TODO implement this
-            //credentialIdentityProperty.setValue(credentialInstance, userInstance);
-            
-            // TODO need to abstract this out
-            modelProperties.get(PROPERTY_CREDENTIAL_VALUE).setValue(credentialInstance, credential);
-            if (modelProperties.containsKey(PROPERTY_CREDENTIAL_TYPE))
-            {
-               // TODO set the credential type - need some kind of mapper?
-               //credentialTypeProperty.setValue(credentialInstance, lookupCredentialType)
-            }                        
-         }
-         
-         // TODO create attributes
-         
-         entityManagerInstance.get().persist(userInstance);
-         
-         if (credentialInstance != null)
-         {
-            entityManagerInstance.get().persist(credentialInstance);
-         }         
+         Object val = entityManagerInstance.get().createQuery(
+               "select t from " + typeNameProp.getDeclaringClass().getName() + 
+               " t where t." + typeNameProp.getName() +
+                " = :identityType")
+               .setParameter("identityType", identityType)
+               .getSingleResult();
+         return val;
       }
-      catch (Exception ex)
+      catch (NoResultException ex)
       {
-         if (ex instanceof IdentityException)
-         {
-            throw (IdentityException) ex;
-         }
-         else
-         {
-            throw new IdentityException("Could not create user.", ex);
-         }
-      }
-      
-      // TODO Auto-generated method stub
-      return false;
-   }   
+         throw new IdentityException("Could not determine identity type [" + identityType + "]");
+      }      
+   }
 
    public IdentityObject createIdentityObject(
          IdentityStoreInvocationContext invocationCtx, String name,
-         IdentityObjectType identityObjectType) throws IdentityException
+         IdentityObjectType identityObjectType, Map<String, String[]> attributes)
+         throws IdentityException
    {
       try
       {
@@ -908,7 +865,13 @@
             typeProp.setValue(identityInstance, lookupIdentityType(identityObjectType.getName()));
          }
                
+         beanManager.fireEvent(new PrePersistUserEvent(identityInstance));
+         
          entityManagerInstance.get().persist(identityInstance);
+         
+         beanManager.fireEvent(new UserCreatedEvent(identityInstance));
+         
+         // TODO persist attributes
 
          IdentityObject obj = new IdentityObjectImpl(
                modelProperties.get(PROPERTY_IDENTITY_ID).getValue(identityInstance).toString(),
@@ -919,45 +882,58 @@
       catch (Exception ex)
       {
          throw new IdentityException("Error creating identity object", ex);
-      }      
+      }    
    }
-   
-   protected Object lookupIdentityType(String identityType) throws IdentityException
-   {      
+
+   public IdentityObjectRelationship createRelationship(
+         IdentityStoreInvocationContext invocationCxt,
+         IdentityObject fromIdentity, IdentityObject toIdentity,
+         IdentityObjectRelationshipType relationshipType,
+         String relationshipName, boolean createNames) throws IdentityException
+   {
       try
       {
-         Property<Object> typeNameProp = modelProperties.get(PROPERTY_IDENTITY_TYPE_NAME);
+         Object relationship = relationshipClass.newInstance();
          
-         Object val = entityManagerInstance.get().createQuery(
-               "select t from " + typeNameProp.getDeclaringClass().getName() + 
-               " t where t." + typeNameProp.getName() +
-                " = :identityType")
-               .setParameter("identityType", identityType)
-               .getSingleResult();
-         return val;
+         modelProperties.get(PROPERTY_RELATIONSHIP_FROM).setValue(relationship, 
+               lookupIdentity(fromIdentity));
+         modelProperties.get(PROPERTY_RELATIONSHIP_TO).setValue(relationship,
+               lookupIdentity(toIdentity));
+         
+         Property<Object> type = modelProperties.get(PROPERTY_RELATIONSHIP_TYPE);
+         if (String.class.equals(modelProperties.get(PROPERTY_RELATIONSHIP_TYPE).getJavaClass()))
+         {
+            type.setValue(relationship, relationshipType.getName());
+         }
+         else
+         {
+            type.setValue(relationship, lookupRelationshipType(relationshipType));
+         }
+         
+         modelProperties.get(PROPERTY_RELATIONSHIP_NAME).setValue(relationship, 
+               relationshipName);
+         
+         entityManagerInstance.get().persist(relationship);
+         
+         return new IdentityObjectRelationshipImpl(fromIdentity, toIdentity,
+               relationshipName, relationshipType);
       }
-      catch (NoResultException ex)
+      catch (Exception ex)
       {
-         throw new IdentityException("Could not determine identity type [" + identityType + "]");
-      }      
+         throw new IdentityException("Exception creating relationship", ex);
+      }
    }
-
-   public IdentityObject createIdentityObject(
-         IdentityStoreInvocationContext invocationCtx, String name,
-         IdentityObjectType identityObjectType, Map<String, String[]> attributes)
-         throws IdentityException
+   
+   protected Object lookupIdentity(IdentityObject obj)
    {
-      // TODO Auto-generated method stub
+      // TODO implement
       return null;
+      
    }
-
-   public IdentityObjectRelationship createRelationship(
-         IdentityStoreInvocationContext invocationCxt,
-         IdentityObject fromIdentity, IdentityObject toIdentity,
-         IdentityObjectRelationshipType relationshipType,
-         String relationshipName, boolean createNames) throws IdentityException
+   
+   protected Object lookupRelationshipType(IdentityObjectRelationshipType relationshipType)
    {
-      // TODO Auto-generated method stub
+      // TODO implement
       return null;
    }
 
@@ -972,7 +948,36 @@
          IdentityStoreInvocationContext invocationContext, String id)
          throws IdentityException
    {
-      // TODO Auto-generated method stub
+      try
+      {
+        Object identity = entityManagerInstance.get().createQuery("select i from " +
+              identityClass.getName() + " i where i." +
+              modelProperties.get(PROPERTY_IDENTITY_ID).getName() +
+              " = :id")
+              .setParameter("id", id)
+              .getSingleResult();
+        
+        IdentityObjectType type = modelProperties.containsKey(PROPERTY_IDENTITY_TYPE_NAME) ?
+              new IdentityObjectTypeImpl(
+                    modelProperties.get(PROPERTY_IDENTITY_TYPE_NAME).getValue(
+                          modelProperties.get(PROPERTY_IDENTITY_TYPE).getValue(identity)).toString()) :
+              new IdentityObjectTypeImpl(modelProperties.get(PROPERTY_IDENTITY_TYPE).getValue(identity).toString());
+        
+        
+        return new IdentityObjectImpl(
+                  modelProperties.get(PROPERTY_IDENTITY_ID).getValue(identity).toString(),
+                  modelProperties.get(PROPERTY_IDENTITY_NAME).getValue(identity).toString(),
+                  type);
+      }
+      catch (NoResultException ex)
+      {
+         return null;
+      }
+   }
+   
+   protected IdentityObjectType convertType(Object obj)
+   {
+      // TODO implement
       return null;
    }
 
@@ -980,8 +985,30 @@
          IdentityStoreInvocationContext invocationContext, String name,
          IdentityObjectType identityObjectType) throws IdentityException
    {
-      // TODO Auto-generated method stub
-      return null;
+      try
+      {
+         Object identityType = modelProperties.containsKey(PROPERTY_IDENTITY_TYPE_NAME) ?
+               lookupIdentityType(identityObjectType.getName()) : 
+                  identityObjectType.getName();
+         
+         Object identity = entityManagerInstance.get().createQuery("select i from " +
+              identityClass.getName() + " i where i." +
+              modelProperties.get(PROPERTY_IDENTITY_NAME).getName() +
+              " = :name and i." + modelProperties.get(PROPERTY_IDENTITY_TYPE).getName() + 
+              " = :type")
+              .setParameter("name", name)
+              .setParameter("type", identityType)              
+              .getSingleResult();        
+        
+        return new IdentityObjectImpl(
+                  modelProperties.get(PROPERTY_IDENTITY_ID).getValue(identity).toString(),
+                  modelProperties.get(PROPERTY_IDENTITY_NAME).getValue(identity).toString(),
+                  identityObjectType);
+      }
+      catch (NoResultException ex)
+      {
+         return null;
+      }
    }
 
    public Collection<IdentityObject> findIdentityObject(
@@ -989,6 +1016,7 @@
          IdentityObjectType identityType, IdentityObjectSearchCriteria criteria)
          throws IdentityException
    {
+      
       // TODO Auto-generated method stub
       return null;
    }
@@ -1227,7 +1255,4 @@
       // TODO Auto-generated method stub
       return null;
    }
-
- 
-
 }

Modified: modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/PasswordHashEncoder.java
===================================================================
--- modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/PasswordHashEncoder.java	2010-06-09 00:20:02 UTC (rev 13087)
+++ modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/PasswordHashEncoder.java	2010-06-09 02:01:11 UTC (rev 13088)
@@ -10,7 +10,7 @@
  * @author Shane Bryzak
  *
  */
-public class PasswordHashEncoder implements CredentialEncoder
+public class PasswordHashEncoder implements CredentialProcessor
 {
    private String passwordHash;
    private int passwordIterations = 1000;



More information about the seam-commits mailing list