[jboss-svn-commits] JBL Code SVN: r23076 - in labs/jbosslabs/labs-3.0-build/integration/cs-nukes-login: resources and 1 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Thu Sep 25 09:57:46 EDT 2008


Author: szimano
Date: 2008-09-25 09:57:46 -0400 (Thu, 25 Sep 2008)
New Revision: 23076

Added:
   labs/jbosslabs/labs-3.0-build/integration/cs-nukes-login/src/main/java/org/jboss/labs/clearspace/UserImporter.java
Modified:
   labs/jbosslabs/labs-3.0-build/integration/cs-nukes-login/pom.xml
   labs/jbosslabs/labs-3.0-build/integration/cs-nukes-login/resources/spring.xml.sample
   labs/jbosslabs/labs-3.0-build/integration/cs-nukes-login/src/main/java/org/jboss/labs/clearspace/LabsAuthenticationProvider.java
Log:
user import outside

Modified: labs/jbosslabs/labs-3.0-build/integration/cs-nukes-login/pom.xml
===================================================================
--- labs/jbosslabs/labs-3.0-build/integration/cs-nukes-login/pom.xml	2008-09-25 12:30:03 UTC (rev 23075)
+++ labs/jbosslabs/labs-3.0-build/integration/cs-nukes-login/pom.xml	2008-09-25 13:57:46 UTC (rev 23076)
@@ -22,7 +22,7 @@
 		<dependency>
 			<groupId>com.jivesoftware</groupId>
 			<artifactId>clearspace</artifactId>
-			<version>2.5.0.rc</version>
+			<version>2.5.1</version>
 			<scope>provided</scope>
 		</dependency>
 		<dependency>

Modified: labs/jbosslabs/labs-3.0-build/integration/cs-nukes-login/resources/spring.xml.sample
===================================================================
--- labs/jbosslabs/labs-3.0-build/integration/cs-nukes-login/resources/spring.xml.sample	2008-09-25 12:30:03 UTC (rev 23075)
+++ labs/jbosslabs/labs-3.0-build/integration/cs-nukes-login/resources/spring.xml.sample	2008-09-25 13:57:46 UTC (rev 23076)
@@ -2,33 +2,38 @@
 <beans xmlns="http://www.springframework.org/schema/beans"
 	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
-<bean id="labsAuthProvider" class="org.jboss.labs.clearspace.LabsAuthenticationProvider">
+
+<bean id="labsAuthProvider" class="org.jboss.labs.clearspace.LabsAuthenticationProvider" >
+	<property name="userImporter" ref="userImporter" />
+</bean>
+
+<!--
+
+<bean id="csMigration" class="org.jboss.labs.clearspace.CSMigration">
+	<property name="userImporter" ref="userImporter" />
 	<property name="userManager" ref="userManagerImpl" />
+	<property name="documentManager" ref="documentManagerImpl" />
+	<property name="communityManager" ref="communityManagerImpl" />
+	<property name="documentTypeManager" ref="documentTypeManagerImpl"/>
+	<property name="attachmentManager" ref="attachmentManagerImpl"/>
+	<property name="pagesToTranslateRegexp" value="RESTEasyLog.*"/>
+</bean>
+
+<bean id="csMigrationStart" class="org.jboss.labs.clearspace.CSMigrationStarter">
+	<property name="migration" ref="csMigration"/>
+</bean>-->
+
+<bean id="userImporter" class="org.jboss.labs.clearspace.UserImporter">
+	<property name="userManager" ref="userManagerImpl" />
 	<property name="userDetailsService" ref="jiveUserDetailsService"/>
 	<property name="jiveAuthProvider" ref="authenticationProvider" />
 	<property name="profileManager" ref="profileManagerImpl"/>
 	<property name="profileFieldManager" ref="profileFieldManagerImpl"/>
 	<property name="dbUserName" value="" />
 	<property name="dbPass" value="" />
-	<property name="dbUrl" value="" />
+	<property name="dbUrl" value="jdbc:mysql://{host}:3306/{db}" />
 </bean>
 
-<!--<bean id="csMigration" class="org.jboss.labs.clearspace.CSMigration">
-        <property name="userManager" ref="userManagerImpl" />
-        <property name="documentManager" ref="documentManagerImpl" />
-        <property name="communityManager" ref="communityManagerImpl" />
-        <property name="documentTypeManager" ref="documentTypeManagerImpl"/>
-        <property name="attachmentManager" ref="attachmentManagerImpl"/>
-	<property name="dbNukesUserName" value="" />
-        <property name="dbNukesPass" value="" />
-        <property name="dbNukesUrl" value="" />
-	<property name="pagesToTranslateRegexp" value=".*"/>
-</bean>
-
-<bean id="csMigrationStart" class="org.jboss.labs.clearspace.CSMigrationStarter">
-	<property name="migration" ref="csMigration"/>
-</bean>-->
-
 <bean id="authenticationManager" 
     class="org.acegisecurity.providers.ProviderManager">
     <property name="providers">

Modified: labs/jbosslabs/labs-3.0-build/integration/cs-nukes-login/src/main/java/org/jboss/labs/clearspace/LabsAuthenticationProvider.java
===================================================================
--- labs/jbosslabs/labs-3.0-build/integration/cs-nukes-login/src/main/java/org/jboss/labs/clearspace/LabsAuthenticationProvider.java	2008-09-25 12:30:03 UTC (rev 23075)
+++ labs/jbosslabs/labs-3.0-build/integration/cs-nukes-login/src/main/java/org/jboss/labs/clearspace/LabsAuthenticationProvider.java	2008-09-25 13:57:46 UTC (rev 23076)
@@ -38,27 +38,13 @@
 
 public class LabsAuthenticationProvider implements AuthenticationProvider {
 
-	private UserManager userManager;
+	private UserImporter userImporter;
 
-	private UserDetailsService userDetailsService;
-	
-	private ProfileManager profileManager;
-	
-	private ProfileFieldManager profileFieldManager;
-
-	private String dbUserName = null;
-	private String dbPass = null;
-	private String dbUrl = null;
-
-	protected com.jivesoftware.base.aaa.AuthenticationProvider jiveAuthProvider;
-
 	private final static Logger log = Logger
 			.getLogger(LabsAuthenticationProvider.class.getName());
 
-	private boolean migrationDone = false;
-
 	public LabsAuthenticationProvider() {
-		
+
 	}
 
 	public Authentication authenticate(Authentication authentication)
@@ -70,357 +56,26 @@
 
 		if (!username.equals("admin")) {
 
-			String passHashed = toHexString(md5(password));
+			UserDetails ud = userImporter
+					.authenticate(username, password, true);
 
-			Connection c = null;
+			UsernamePasswordAuthenticationToken newAuthentication = new UsernamePasswordAuthenticationToken(
+					ud, authentication.getCredentials(), ud.getAuthorities());
+			newAuthentication.setDetails(ud);
+			
+			return newAuthentication;
 
-			try {
-				c = getConnection();
-				
-				String sqlQuery = "SELECT * FROM nuke_users WHERE pn_uname = ?";
-				
-				PreparedStatement prepStmt = c.prepareStatement(sqlQuery);
-				prepStmt.setString(1, username);
-				
-				ResultSet rs = prepStmt.executeQuery();
-				
-				if (rs.next()) {
-					LabsUser nukeUser = new LabsUser(rs);
-
-					// swap this for a JiveUserDetails
-					UserDetails ud = null;
-
-					try {
-						ud = userDetailsService.loadUserByUsername(username);
-					} catch (Exception unfe) {
-						// this is ok, if we haven't seen a user before, we
-						// won't necessarily be
-						// able to load them
-					}
-
-					if (null == ud) {
-						log.info("Creating new application user for user '"
-								+ username + "'.");
-						try {
-							UserTemplate ut = new UserTemplate(username);
-							ut.setFederated(true);
-
-							ut = mapDetails(ut, nukeUser);
-							userManager.createApplicationUser(ut);
-
-							// try again to load the user from the details
-							// service, this time it should work
-							ud = userDetailsService
-									.loadUserByUsername(username);
-						} catch (Exception ex) {
-							log.log(Level.WARNING,
-									"Failed to create application user for '"
-											+ username + "'.", ex);
-						}
-					} else {
-						// just update password
-						try {
-							log
-									.info("Updating user properties from Nukes attribute for authenticated user.");
-							UserTemplate ut = new UserTemplate(
-									((JiveUserDetails) ud).getUser());
-							// CS-3223 - the user should always be marked as
-							// federated
-							ut.setFederated(true);
-							// final UserTemplate toUpdate =
-							// mapAttributesToTemplate(attrs, ut);
-							
-							ut = mapDetails(ut, nukeUser);
-							
-							//ut.setPasswordHash(nukeUser.getPasswordHash());
-							
-							final UserTemplate toUpdate = ut;
-							
-							toUpdate.setID(ut.getID());
-
-							toUpdate.setLastLoggedIn(new Date());
-							SystemExecutor exec = new SystemExecutor(
-									jiveAuthProvider);
-							exec.executeCallable(new Callable() {
-								public Object call() throws Exception {
-									userManager.updateUser(toUpdate);
-									return null;
-								}
-							});
-						} catch (Exception ex) {
-							log
-									.log(
-											Level.INFO,
-											"Failed to update user data from Nukes attributes.",
-											ex);
-						}
-					}
-					
-					// update profile
-					Map<String, String> profile = nukeUser.getProfile();
-					User u = userManager.getUser(nukeUser.getUsername());
-					Set<ProfileFieldValue> pfvSet = new HashSet<ProfileFieldValue>();
-					
-					System.out.println("Default fields:");
-					// map default fields
-					mapProfile(profile, pfvSet, profileFieldManager.getDefaultFields());
-					
-					System.out.println("Custom fields:");
-					// map custom fields
-					mapProfile(profile, pfvSet, profileFieldManager.getCustomFields());
-					
-					// create unexisting fields
-					if (!profile.isEmpty()) {
-						
-						for (String key : profile.keySet()) {
-							// add field
-							
-							ProfileField field = new ProfileField();
-							field.setName(key);
-							field.setTypeID(ProfileField.Type.TEXT.getID());
-							field.setSearchable(true);
-							field.setVisible(true);
-							field.setEditable(true);
-							field.setDescription("en_US", key);
-							profileFieldManager.createProfileField(field);
-							
-							// add profile var
-							ProfileFieldValue pfv = new ProfileFieldValue(field);
-							
-							pfv.setValue(profile.get(key));
-							
-							pfvSet.add(pfv);
-						}
-					}
-					
-					if (!pfvSet.isEmpty()) {
-						profileManager.setProfile(u, pfvSet);
-					}
-
-					if (!nukeUser.getPasswordHash().equals(passHashed)) {
-						throw new BadCredentialsException("Username:"
-								+ username + " was not authenticated");
-					} else {
-						UsernamePasswordAuthenticationToken newAuthentication = new UsernamePasswordAuthenticationToken(
-								ud, authentication.getCredentials(), ud
-										.getAuthorities());
-						newAuthentication.setDetails(ud);
-						return newAuthentication;
-					}
-				}
-			} catch (Exception e) {
-				// TODO Auto-generated catch block
-				e.printStackTrace();
-			} finally {
-				try {
-					c.close();
-				} catch (SQLException e) {
-					// TODO Auto-generated catch block
-					e.printStackTrace();
-				}
-			}
-
-			return null;
-
 		} else {
 			return null;
 		}
 	}
 
-	private void mapProfile(Map<String, String> profile, Collection<ProfileFieldValue> pfvSet, List<ProfileField> fields) {
-		for (ProfileField pf : fields) {
-			System.out.println(pf);
-			if (profile.get(pf.getName()) != null) {
-				ProfileFieldValue pfv = new ProfileFieldValue(pf);
-				pfv.setValue(profile.get(pf.getName()));
-
-				pfvSet.add(pfv);
-				
-				// remove field as added
-				profile.remove(pf.getName());
-			}
-		}
-	}
-
-	private UserTemplate mapDetails(UserTemplate ut, LabsUser nukeUser) {
-		ut.setEmail(nukeUser.getEmail());
-		ut.setFirstName(nukeUser.getFirstName());
-		ut.setLastName(nukeUser.getLastName());
-		ut.setName(nukeUser.getName());
-		ut.setPasswordHash(nukeUser.getPasswordHash());
-		ut.setProperties(nukeUser.getProperties());
-		
-		return ut;
-	}
-
 	public boolean supports(Class authentication) {
 		return authentication == UsernamePasswordAuthenticationToken.class;
 	}
 
-	private Connection getConnection() throws Exception {
-		Class.forName("com.mysql.jdbc.Driver").newInstance();
-		return DriverManager.getConnection(dbUrl, dbUserName, dbPass);
+	public void setUserImporter(UserImporter userImporter) {
+		this.userImporter = userImporter;
 	}
 
-	/**
-	 * Returns a string in the hexadecimal format.
-	 * 
-	 * @param bytes
-	 *            the converted bytes
-	 * @return the hexadecimal string representing the bytes data
-	 * @throws IllegalArgumentException
-	 *             if the byte array is null
-	 */
-	public static String toHexString(byte[] bytes) {
-		if (bytes == null) {
-			throw new IllegalArgumentException("byte array must not be null");
-		}
-		StringBuffer hex = new StringBuffer(bytes.length * 2);
-		for (int i = 0; i < bytes.length; i++) {
-			hex.append(Character.forDigit((bytes[i] & 0XF0) >> 4, 16));
-			hex.append(Character.forDigit((bytes[i] & 0X0F), 16));
-		}
-		return hex.toString();
-	}
-
-	/**
-	 * Computes an md5 hash of a string.
-	 * 
-	 * @param text
-	 *            the hashed string
-	 * @return the string hash
-	 * @throws NullPointerException
-	 *             if text is null
-	 */
-	public static byte[] md5(String text) {
-		// arguments check
-		if (text == null) {
-			throw new NullPointerException("null text");
-		}
-
-		try {
-			MessageDigest md = MessageDigest.getInstance("MD5");
-			md.update(text.getBytes());
-			return md.digest();
-		} catch (NoSuchAlgorithmException e) {
-			System.err.println("Cannot find MD5 algorithm" + e);
-			throw new RuntimeException("Cannot find MD5 algorithm");
-		}
-	}
-
-	public void setUserManager(UserManager userManager) {
-		this.userManager = userManager;
-		checkMigration();
-	}
-
-	public void setUserDetailsService(UserDetailsService userDetailsService) {
-		this.userDetailsService = userDetailsService;
-		checkMigration();
-	}
-
-	public void setJiveAuthProvider(
-			com.jivesoftware.base.aaa.AuthenticationProvider jiveAuthProvider) {
-		this.jiveAuthProvider = jiveAuthProvider;
-		checkMigration();
-	}
-
-	public void setDbUserName(String dbUserName) {
-		this.dbUserName = dbUserName;
-		checkMigration();
-	}
-
-	public void setDbPass(String dbPass) {
-		this.dbPass = dbPass;
-		checkMigration();
-	}
-
-	public void setDbUrl(String dbUrl) {
-		this.dbUrl = dbUrl;
-		checkMigration();
-	}
-
-	private void checkMigration() {
-		if (!migrationDone && userManager != null && userDetailsService != null
-				&& jiveAuthProvider != null && dbPass != null && dbUrl != null
-				&& dbUserName != null) {
-			if (System.getenv("MIGRATE_ALL_USERS") != null) {
-				Connection c = null;
-
-				try {
-					c = getConnection();
-
-					ResultSet rs = c.createStatement().executeQuery(
-							"SELECT * FROM nuke_users n ORDER BY n.pn_uname");
-
-					List<String> userNames = new ArrayList<String>();
-
-					for (User u : userManager.getUsers()) {
-						userNames.add(u.getUsername());
-					}
-
-					while (rs.next()) {
-						String username = null;
-						try {
-							username = rs.getObject("pn_uname").toString();
-
-							if (username.length() > 0
-									&& !userNames.contains(username)) {
-
-								LabsUser nukeUser = new LabsUser(rs);
-
-								UserTemplate ut = new UserTemplate(username);
-								ut.setFederated(true);
-
-								ut = mapDetails(ut, nukeUser);
-								userManager.createApplicationUser(ut);
-
-								// try again to load the user from the details
-								// service, this time it should work
-								userDetailsService.loadUserByUsername(username);
-								
-								log.info("Users " + username + " added");
-							}
-						} catch (SQLException e) {
-							// TODO Auto-generated catch block
-							e.printStackTrace();
-						} catch (UserAlreadyExistsException e) {
-							log.info("User "+username+" already exists");
-						}
-					}
-
-					migrationDone = true;
-				} catch (Exception e) {
-					// TODO Auto-generated catch block
-					e.printStackTrace();
-				} finally {
-					if (c != null) {
-						try {
-							c.close();
-						} catch (SQLException e) {
-							// TODO Auto-generated catch block
-							e.printStackTrace();
-						}
-					}
-				}
-
-			}
-		}
-	}
-
-	public ProfileManager getProfileManager() {
-		return profileManager;
-	}
-
-	public void setProfileManager(ProfileManager profileManager) {
-		this.profileManager = profileManager;
-	}
-
-	public ProfileFieldManager getProfileFieldManager() {
-		return profileFieldManager;
-	}
-
-	public void setProfileFieldManager(ProfileFieldManager profileFieldManager) {
-		this.profileFieldManager = profileFieldManager;
-	}
-
 }

Added: labs/jbosslabs/labs-3.0-build/integration/cs-nukes-login/src/main/java/org/jboss/labs/clearspace/UserImporter.java
===================================================================
--- labs/jbosslabs/labs-3.0-build/integration/cs-nukes-login/src/main/java/org/jboss/labs/clearspace/UserImporter.java	                        (rev 0)
+++ labs/jbosslabs/labs-3.0-build/integration/cs-nukes-login/src/main/java/org/jboss/labs/clearspace/UserImporter.java	2008-09-25 13:57:46 UTC (rev 23076)
@@ -0,0 +1,434 @@
+package org.jboss.labs.clearspace;
+
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Date;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.Callable;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import org.acegisecurity.BadCredentialsException;
+import org.acegisecurity.userdetails.UserDetails;
+import org.acegisecurity.userdetails.UserDetailsService;
+
+import com.jivesoftware.base.User;
+import com.jivesoftware.base.UserAlreadyExistsException;
+import com.jivesoftware.base.UserManager;
+import com.jivesoftware.base.UserNotFoundException;
+import com.jivesoftware.base.UserTemplate;
+import com.jivesoftware.community.aaa.JiveUserDetails;
+import com.jivesoftware.community.aaa.authz.SystemExecutor;
+import com.jivesoftware.community.user.profile.ProfileField;
+import com.jivesoftware.community.user.profile.ProfileFieldManager;
+import com.jivesoftware.community.user.profile.ProfileFieldValue;
+import com.jivesoftware.community.user.profile.ProfileManager;
+
+public class UserImporter {
+
+	private UserManager userManager;
+
+	private UserDetailsService userDetailsService;
+
+	private ProfileManager profileManager;
+
+	private ProfileFieldManager profileFieldManager;
+
+	protected com.jivesoftware.base.aaa.AuthenticationProvider jiveAuthProvider;
+
+	private String dbUserName = null;
+	private String dbPass = null;
+	private String dbUrl = null;
+
+	private final static Logger log = Logger.getLogger(UserImporter.class
+			.getName());
+
+	public UserImporter() {
+
+	}
+
+	private void mapProfile(Map<String, String> profile,
+			Collection<ProfileFieldValue> pfvSet, List<ProfileField> fields) {
+		for (ProfileField pf : fields) {
+
+			if (profile.get(pf.getName()) != null) {
+				ProfileFieldValue pfv = new ProfileFieldValue(pf);
+				pfv.setValue(profile.get(pf.getName()));
+
+				pfvSet.add(pfv);
+
+				// remove field as added
+				profile.remove(pf.getName());
+			}
+		}
+	}
+
+	private UserTemplate mapDetails(UserTemplate ut, LabsUser nukeUser) {
+		ut.setEmail(nukeUser.getEmail());
+		ut.setFirstName(nukeUser.getFirstName());
+		ut.setLastName(nukeUser.getLastName());
+		ut.setName(nukeUser.getName());
+		ut.setPasswordHash(nukeUser.getPasswordHash());
+		ut.setProperties(nukeUser.getProperties());
+		ut.setCreationDate(nukeUser.getCreationDate());
+
+		return ut;
+	}
+
+	private Connection getConnection() throws Exception {
+		Class.forName("com.mysql.jdbc.Driver").newInstance();
+		return DriverManager.getConnection(dbUrl, dbUserName, dbPass);
+	}
+
+	/**
+	 * Returns a string in the hexadecimal format.
+	 * 
+	 * @param bytes
+	 *            the converted bytes
+	 * @return the hexadecimal string representing the bytes data
+	 * @throws IllegalArgumentException
+	 *             if the byte array is null
+	 */
+	public static String toHexString(byte[] bytes) {
+		if (bytes == null) {
+			throw new IllegalArgumentException("byte array must not be null");
+		}
+		StringBuffer hex = new StringBuffer(bytes.length * 2);
+		for (int i = 0; i < bytes.length; i++) {
+			hex.append(Character.forDigit((bytes[i] & 0XF0) >> 4, 16));
+			hex.append(Character.forDigit((bytes[i] & 0X0F), 16));
+		}
+		return hex.toString();
+	}
+
+	/**
+	 * Computes an md5 hash of a string.
+	 * 
+	 * @param text
+	 *            the hashed string
+	 * @return the string hash
+	 * @throws NullPointerException
+	 *             if text is null
+	 */
+	public static byte[] md5(String text) {
+		// arguments check
+		if (text == null) {
+			throw new NullPointerException("null text");
+		}
+
+		try {
+			MessageDigest md = MessageDigest.getInstance("MD5");
+			md.update(text.getBytes());
+			return md.digest();
+		} catch (NoSuchAlgorithmException e) {
+			System.err.println("Cannot find MD5 algorithm" + e);
+			throw new RuntimeException("Cannot find MD5 algorithm");
+		}
+	}
+
+	public User getUser(String username, boolean updateProfile)
+			throws UserNotFoundException {
+		UserDetails ud = getUserDetails(username, updateProfile);
+
+		return userManager.getUser(ud.getUsername());
+	}
+
+	public UserDetails authenticate(String username, String password,
+			boolean updateProfile) {
+		return getUserDetailsAndAuth(username, password, updateProfile);
+	}
+
+	public UserDetails getUserDetails(String username, boolean updateProfile) {
+		return getUserDetailsAndAuth(username, null, updateProfile);
+	}
+
+	public UserDetails getUserDetailsAndAuth(String username, String password, boolean updateProfile) {
+		Connection c = null;
+
+		try {
+			c = getConnection();
+
+			String sqlQuery = "SELECT * FROM nuke_users WHERE pn_uname = ?";
+
+			PreparedStatement prepStmt = c.prepareStatement(sqlQuery);
+			prepStmt.setString(1, username);
+
+			ResultSet rs = prepStmt.executeQuery();
+
+			boolean newUser = false;
+
+			if (rs.next()) {
+				LabsUser nukeUser = new LabsUser(rs);
+
+				// swap this for a JiveUserDetails
+				UserDetails ud = null;
+
+				try {
+					ud = userDetailsService.loadUserByUsername(username);
+				} catch (Exception unfe) {
+					// this is ok, if we haven't seen a user before, we
+					// won't necessarily be
+					// able to load them
+				}
+
+				if (null == ud) {
+
+					newUser = true;
+
+					try {
+						UserTemplate ut = new UserTemplate(username);
+						ut.setFederated(true);
+
+						ut = mapDetails(ut, nukeUser);
+						userManager.createApplicationUser(ut);
+
+						// try again to load the user from the details
+						// service, this time it should work
+						ud = userDetailsService.loadUserByUsername(username);
+					} catch (Exception ex) {
+						log.log(Level.WARNING,
+								"Failed to create application user for '"
+										+ username + "'.", ex);
+					}
+				} else {
+					// just update password
+					try {
+						UserTemplate ut = new UserTemplate(
+								((JiveUserDetails) ud).getUser());
+						// CS-3223 - the user should always be marked as
+						// federated
+						ut.setFederated(true);
+						// final UserTemplate toUpdate =
+						// mapAttributesToTemplate(attrs, ut);
+
+						if (updateProfile) {
+							ut = mapDetails(ut, nukeUser);
+						} else {
+							// just update pass hash
+							ut.setPasswordHash(nukeUser.getPasswordHash());
+						}
+
+						final UserTemplate toUpdate = ut;
+
+						toUpdate.setID(ut.getID());
+
+						toUpdate.setLastLoggedIn(new Date());
+
+						SystemExecutor exec = new SystemExecutor(
+								jiveAuthProvider);
+						exec.executeCallable(new Callable() {
+							public Object call() throws Exception {
+								userManager.updateUser(toUpdate);
+								return null;
+							}
+						});
+					} catch (Exception ex) {
+						log
+								.log(
+										Level.INFO,
+										"Failed to update user data from Nukes attributes.",
+										ex);
+					}
+				}
+
+				if (newUser || updateProfile) {
+					// update profile
+					Map<String, String> profile = nukeUser.getProfile();
+					User u = userManager.getUser(nukeUser.getUsername());
+					Set<ProfileFieldValue> pfvSet = new HashSet<ProfileFieldValue>();
+
+					// map default fields
+					mapProfile(profile, pfvSet, profileFieldManager
+							.getDefaultFields());
+
+					// map custom fields
+					mapProfile(profile, pfvSet, profileFieldManager
+							.getCustomFields());
+
+					// create unexisting fields
+					if (!profile.isEmpty()) {
+
+						for (String key : profile.keySet()) {
+							// add field
+
+							ProfileField field = new ProfileField();
+							field.setName(key);
+							field.setTypeID(ProfileField.Type.TEXT.getID());
+							field.setSearchable(true);
+							field.setVisible(true);
+							field.setEditable(true);
+							field.setDescription("en_US", key);
+							profileFieldManager.createProfileField(field);
+
+							// add profile var
+							ProfileFieldValue pfv = new ProfileFieldValue(field);
+
+							pfv.setValue(profile.get(key));
+
+							pfvSet.add(pfv);
+						}
+					}
+
+					if (!pfvSet.isEmpty()) {
+						profileManager.setProfile(u, pfvSet);
+					}
+				}
+				
+				if (password != null) {
+					String passHashed = toHexString(md5(password));
+					
+					if (!nukeUser.getPasswordHash().equals(passHashed)) {
+						throw new BadCredentialsException("Username:" + username
+								+ " was not authenticated");
+					}
+				}
+
+				return ud;
+
+			}
+		} catch (Exception e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+			
+			throw new BadCredentialsException("Username:" + username
+					+ " not found");
+		} finally {
+			try {
+				c.close();
+			} catch (SQLException e) {
+				// TODO Auto-generated catch block
+				e.printStackTrace();
+			}
+		}
+
+		throw new BadCredentialsException("Username:" + username
+				+ " not found");
+	}
+
+	public void setJiveAuthProvider(
+			com.jivesoftware.base.aaa.AuthenticationProvider jiveAuthProvider) {
+		this.jiveAuthProvider = jiveAuthProvider;
+		checkMigration();
+	}
+
+	public ProfileManager getProfileManager() {
+		return profileManager;
+	}
+
+	public void setProfileManager(ProfileManager profileManager) {
+		this.profileManager = profileManager;
+	}
+
+	public ProfileFieldManager getProfileFieldManager() {
+		return profileFieldManager;
+	}
+
+	public void setProfileFieldManager(ProfileFieldManager profileFieldManager) {
+		this.profileFieldManager = profileFieldManager;
+	}
+
+	public void setDbUserName(String dbUserName) {
+		this.dbUserName = dbUserName;
+		checkMigration();
+	}
+
+	public void setDbPass(String dbPass) {
+		this.dbPass = dbPass;
+		checkMigration();
+	}
+
+	public void setDbUrl(String dbUrl) {
+		this.dbUrl = dbUrl;
+		checkMigration();
+	}
+
+	public void setUserManager(UserManager userManager) {
+		this.userManager = userManager;
+		checkMigration();
+	}
+
+	public void setUserDetailsService(UserDetailsService userDetailsService) {
+		this.userDetailsService = userDetailsService;
+		checkMigration();
+	}
+
+	private boolean migrationDone = false;
+
+	private void checkMigration() {
+		if (!migrationDone && userManager != null && userDetailsService != null
+				&& jiveAuthProvider != null && dbPass != null && dbUrl != null
+				&& dbUserName != null) {
+			if (System.getenv("MIGRATE_ALL_USERS") != null) {
+				Connection c = null;
+
+				try {
+					c = getConnection();
+
+					ResultSet rs = c.createStatement().executeQuery(
+							"SELECT * FROM nuke_users n ORDER BY n.pn_uname");
+
+					List<String> userNames = new ArrayList<String>();
+
+					for (User u : userManager.getUsers()) {
+						userNames.add(u.getUsername());
+					}
+
+					while (rs.next()) {
+						String username = null;
+						try {
+							username = rs.getObject("pn_uname").toString();
+
+							if (username.length() > 0
+									&& !userNames.contains(username)) {
+
+								LabsUser nukeUser = new LabsUser(rs);
+
+								UserTemplate ut = new UserTemplate(username);
+								ut.setFederated(true);
+
+								ut = mapDetails(ut, nukeUser);
+								userManager.createApplicationUser(ut);
+
+								// try again to load the user from the details
+								// service, this time it should work
+								userDetailsService.loadUserByUsername(username);
+
+								log.info("Users " + username + " added");
+							}
+						} catch (SQLException e) {
+							// TODO Auto-generated catch block
+							e.printStackTrace();
+						} catch (UserAlreadyExistsException e) {
+							log.info("User " + username + " already exists");
+						}
+					}
+
+					migrationDone = true;
+				} catch (Exception e) {
+					// TODO Auto-generated catch block
+					e.printStackTrace();
+				} finally {
+					if (c != null) {
+						try {
+							c.close();
+						} catch (SQLException e) {
+							// TODO Auto-generated catch block
+							e.printStackTrace();
+						}
+					}
+				}
+
+			}
+		}
+	}
+}




More information about the jboss-svn-commits mailing list