Author: prabath
Date: Mon Feb 18 02:47:45 2008
New Revision: 13853

Log:

multiple profile support, implementation within the IS itself.

Added:
   
trunk/solutions/identity/modules/identity-provider/src/main/java/org/wso2/solutions/identity/users/IdentityDefaultRealm.java
   
trunk/solutions/identity/modules/identity-provider/src/main/java/org/wso2/solutions/identity/users/IdentityUserStoreAdmin.java
   
trunk/solutions/identity/modules/identity-provider/src/main/java/org/wso2/solutions/identity/users/IdentityUserStoreReader.java
Modified:
   
trunk/solutions/identity/modules/identity-provider/src/main/java/org/wso2/solutions/identity/Initializer.java

Modified: 
trunk/solutions/identity/modules/identity-provider/src/main/java/org/wso2/solutions/identity/Initializer.java
==============================================================================
--- 
trunk/solutions/identity/modules/identity-provider/src/main/java/org/wso2/solutions/identity/Initializer.java
       (original)
+++ 
trunk/solutions/identity/modules/identity-provider/src/main/java/org/wso2/solutions/identity/Initializer.java
       Mon Feb 18 02:47:45 2008
@@ -35,6 +35,7 @@
 import 
org.wso2.solutions.identity.persistence.dataobject.RealmConfigurationPropertyDO;
 import org.wso2.solutions.identity.persistence.dataobject.RealmDO;
 import org.wso2.solutions.identity.persistence.dataobject.RelyingPartyDO;
+import org.wso2.solutions.identity.users.IdentityDefaultRealm;
 import org.wso2.solutions.identity.users.wsas.WSASRealm;
 import org.wso2.usermanager.Realm;
 import org.wso2.usermanager.custom.jdbc.JDBCRealm;
@@ -143,15 +144,15 @@
         wsasRealmConfig.setRealm(wsasRealm);
         db.create(wsasRealmConfig);
 
-        RealmDO defaultRalm = new RealmDO();
-        defaultRalm.setClassName(DefaultRealm.class.getName());
-        defaultRalm.setConfigClassName(DefaultRealmConfig.class.getName());
-        db.create(defaultRalm);
+        RealmDO defaultRealm = new RealmDO();
+        defaultRealm.setClassName(IdentityDefaultRealm.class.getName());
+        defaultRealm.setConfigClassName(DefaultRealmConfig.class.getName());
+        db.create(defaultRealm);
 
         // Add default realm configuration
         RealmConfigurationDO realmConfig = new RealmConfigurationDO();
         realmConfig.setName("defaultRealm");
-        realmConfig.setRealm(defaultRalm);
+        realmConfig.setRealm(defaultRealm);
         realmConfig.setEffective(true);
         db.create(realmConfig);
 

Added: 
trunk/solutions/identity/modules/identity-provider/src/main/java/org/wso2/solutions/identity/users/IdentityDefaultRealm.java
==============================================================================
--- (empty file)
+++ 
trunk/solutions/identity/modules/identity-provider/src/main/java/org/wso2/solutions/identity/users/IdentityDefaultRealm.java
        Mon Feb 18 02:47:45 2008
@@ -0,0 +1,17 @@
+package org.wso2.solutions.identity.users;
+
+import org.wso2.usermanager.UserManagerException;
+import org.wso2.usermanager.readwrite.DefaultRealm;
+
+public class IdentityDefaultRealm extends DefaultRealm {
+
+    /**
+     * 
+     */
+    public void init(Object configBean) throws UserManagerException {
+        super.init(configBean);
+        usAdmin = new IdentityUserStoreAdmin(dataSource, strategyObject);
+        usReader = new IdentityUserStoreReader(dataSource, strategyObject);
+    }
+
+}

Added: 
trunk/solutions/identity/modules/identity-provider/src/main/java/org/wso2/solutions/identity/users/IdentityUserStoreAdmin.java
==============================================================================
--- (empty file)
+++ 
trunk/solutions/identity/modules/identity-provider/src/main/java/org/wso2/solutions/identity/users/IdentityUserStoreAdmin.java
      Mon Feb 18 02:47:45 2008
@@ -0,0 +1,195 @@
+package org.wso2.solutions.identity.users;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import javax.sql.DataSource;
+
+import org.wso2.usermanager.UserManagerException;
+import org.wso2.usermanager.readwrite.DefaultStrategy;
+import org.wso2.usermanager.readwrite.DefaultUserStoreAdmin;
+import org.wso2.usermanager.readwrite.util.UUIDGenerator;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+public class IdentityUserStoreAdmin extends DefaultUserStoreAdmin {
+
+    private static Log log = LogFactory.getLog(DefaultUserStoreAdmin.class);
+
+    /**
+     * 
+     * @param dataSource
+     */
+    public IdentityUserStoreAdmin(DataSource dataSource) {
+        super(dataSource);
+        // TODO Auto-generated constructor stub
+    }
+
+    /**
+     * 
+     * @param dataSource
+     * @param store
+     */
+    public IdentityUserStoreAdmin(DataSource dataSource, DefaultStrategy 
store) {
+        super(dataSource, store);
+    }
+
+    /**
+     * 
+     */
+    public void setUserProperties(String userName, Map properties)
+            throws UserManagerException {
+        setUserProperties(userName, properties, null);
+    }
+
+    /**
+     * 
+     * @param userName
+     * @param properties
+     * @param profileName
+     * @throws UserManagerException
+     */
+    public void setUserProperties(String userName, Map properties,
+            String profileName) throws UserManagerException {
+
+        String userid = data.getUserId(userName);
+
+        if (userid == null) {
+            throw new UserManagerException("nullUser");
+        }
+
+        Connection dbConnection = null;
+        boolean isDefaultProfile = false;
+
+        try {
+
+            dbConnection = dataSource.getConnection();
+
+            if (dbConnection == null) {
+                throw new UserManagerException("null_connection");
+            }
+
+            dbConnection.setAutoCommit(false);
+
+            PreparedStatement sqlStatement = null;
+
+            sqlStatement = dbConnection
+                    .prepareStatement("insert into user_profile_values 
(attr_name, attr_value, profile_id, id) values (?, ?, ?, ?)");
+
+            String profileId = UUIDGenerator.getUUID();
+
+            if (properties != null) {
+                Iterator ite = properties.entrySet().iterator();
+                while (ite.hasNext()) {
+                    Entry entry = (Entry) ite.next();
+                    String key = (String) entry.getKey();
+                    String value = (String) entry.getValue();
+                    if (value != null) {
+                        String idUserAttribute = UUIDGenerator.getUUID();
+                        sqlStatement.setString(1, key);
+                        sqlStatement.setString(2, value);
+                        sqlStatement.setString(3, profileId);
+                        sqlStatement.setString(4, idUserAttribute);
+                        sqlStatement.executeUpdate();
+                    }
+                }
+            }
+
+            sqlStatement = dbConnection
+                    .prepareStatement("select * from user_profile where 
user_id=? AND is_default=?");
+            sqlStatement.setString(1, userid);
+            sqlStatement.setBoolean(2, true);
+
+            ResultSet results = sqlStatement.executeQuery();
+
+            if (results != null && results.next()) {
+                isDefaultProfile = false;
+            } else {
+                // this is the first time user sets the attributes
+                isDefaultProfile = true;
+                profileName = "Default Profile";
+            }
+
+            dbConnection.commit();
+
+            addUserProfile(userid, profileId, isDefaultProfile, profileName);
+
+        } catch (SQLException e) {
+            try {
+                dbConnection.rollback();
+            } catch (SQLException e1) {
+                log.debug(e);
+                throw new UserManagerException("errorModifyingUserStore", e);
+            }
+            log.debug(e);
+            throw new UserManagerException("errorModifyingUserStore", e);
+        } finally {
+            try {
+                if (dbConnection != null) {
+                    dbConnection.close();
+                }
+            } catch (SQLException e) {
+                throw new UserManagerException("errorClosingConnection", e);
+            }
+        }
+    }
+
+    /**
+     * 
+     * @param userId
+     * @param profileId
+     * @param isDefault
+     * @param profileName
+     * @throws UserManagerException
+     */
+    protected void addUserProfile(String userId, String profileId,
+            boolean isDefault, String profileName) throws UserManagerException 
{
+
+        Connection dbConnection = null;
+        PreparedStatement sqlStatement = null;
+
+        try {
+
+            dbConnection = dataSource.getConnection();
+            dbConnection.setAutoCommit(false);
+
+            sqlStatement= dbConnection
+                    .prepareStatement("insert into user_profile (profile_id, 
user_id, profile_name, is_default,id) values (?, ?, ?, ?,?)");
+
+            sqlStatement.setString(1, profileId);
+            sqlStatement.setString(2, userId);
+            sqlStatement.setString(3, profileName);
+            sqlStatement.setBoolean(4, isDefault);
+            sqlStatement.setString(5, UUIDGenerator.getUUID());
+
+            sqlStatement.executeUpdate();
+
+            dbConnection.commit();
+
+        } catch (SQLException e) {
+            try {
+                dbConnection.rollback();
+            } catch (SQLException e1) {
+                log.debug(e);
+                throw new UserManagerException("errorModifyingUserStore", e1);
+            }
+            log.debug(e);
+            throw new UserManagerException("errorModifyingUserStore", e);
+        } finally {
+            try {
+                if (dbConnection != null) {
+                    dbConnection.close();
+                }
+            } catch (SQLException e) {
+                log.debug(e);
+                throw new UserManagerException("errorModifyingUserStore", e);
+            }
+        }
+    }
+}

Added: 
trunk/solutions/identity/modules/identity-provider/src/main/java/org/wso2/solutions/identity/users/IdentityUserStoreReader.java
==============================================================================
--- (empty file)
+++ 
trunk/solutions/identity/modules/identity-provider/src/main/java/org/wso2/solutions/identity/users/IdentityUserStoreReader.java
     Mon Feb 18 02:47:45 2008
@@ -0,0 +1,144 @@
+package org.wso2.solutions.identity.users;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.sql.DataSource;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import org.wso2.usermanager.UserManagerException;
+import org.wso2.usermanager.readwrite.DefaultStrategy;
+import org.wso2.usermanager.readwrite.DefaultUserStoreReader;
+
+public class IdentityUserStoreReader extends DefaultUserStoreReader {
+
+    private static Log log = LogFactory.getLog(IdentityUserStoreReader.class);
+
+    /**
+     * 
+     * @param dataSource
+     */
+    public IdentityUserStoreReader(DataSource dataSource) {
+        super(dataSource);
+        // TODO Auto-generated constructor stub
+    }
+
+    /**
+     * 
+     * @param dataSource
+     * @param store
+     */
+    public IdentityUserStoreReader(DataSource dataSource, DefaultStrategy 
store) {
+        super(dataSource, store);
+    }
+
+    /**
+     * 
+     */
+    public Map getUserProperties(String userName) throws UserManagerException {
+
+        Map props = null;
+        Connection dbConnection = null;
+        PreparedStatement sqlStatement = null;
+
+        try {
+
+            dbConnection = dataSource.getConnection();
+
+            if (dbConnection == null) {
+                throw new UserManagerException("null_connection");
+            }
+            dbConnection.setAutoCommit(false);
+
+            props = new HashMap();
+            String userid = data.getUserId(userName);
+
+            sqlStatement = dbConnection
+                    .prepareStatement("select attr_name,attr_value from 
user_profile_values where profile_id = (select profile_id from user_profile 
where user_id=? and is_default=?)");
+            sqlStatement.setString(1, userid);
+            sqlStatement.setBoolean(2, true);
+
+            ResultSet rs = sqlStatement.executeQuery();
+
+            while (rs.next()) {
+                String name = rs.getString(1);
+                String value = rs.getString(2);
+                props.put(name, value);
+            }
+
+            if (sqlStatement != null)
+                sqlStatement.close();
+
+        } catch (SQLException e) {
+            log.debug(e);
+            throw new UserManagerException("errorReadingFromUserStore", e);
+        } finally {
+            try {
+                if (dbConnection != null) {
+                    dbConnection.close();
+                }
+            } catch (SQLException e) {
+                throw new UserManagerException("errorClosingConnection", e);
+            }
+        }
+        return props;
+    }
+
+    /**
+     * 
+     */
+    public String[] getUserPropertyNames() throws UserManagerException {
+
+        String[] propNames = null;
+        Connection dbConnection = null;
+        PreparedStatement sqlStatement = null;
+
+        try {
+
+            dbConnection = dataSource.getConnection();
+
+            if (dbConnection == null) {
+                throw new UserManagerException("null_connection");
+            }
+
+            dbConnection.setAutoCommit(false);
+            propNames = new String[0];
+
+            sqlStatement = dbConnection
+                    .prepareStatement("select distinct attr_name from 
user_profile_values");
+            ResultSet rs = sqlStatement.executeQuery();
+
+            List lst = new ArrayList();
+
+            while (rs.next()) {
+                lst.add(rs.getString(1));
+            }
+            propNames = (String[]) lst.toArray(new String[lst.size()]);
+
+            if (sqlStatement != null)
+                sqlStatement.close();
+
+        } catch (SQLException e) {
+            log.debug(e);
+            throw new UserManagerException("errorReadingFromUserStore", e);
+        } finally {
+            try {
+                if (dbConnection != null) {
+                    dbConnection.close();
+                }
+            } catch (SQLException e) {
+                throw new UserManagerException("errorClosingConnection", e);
+            }
+        }
+        return propNames;
+    }
+
+}

_______________________________________________
Identity-dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/identity-dev

Reply via email to