package gov.nih.nci.security.acegi.csm.authorization;

import java.util.Iterator;
import java.util.Set;

import gov.nih.nci.security.UserProvisioningManager;
import gov.nih.nci.security.authorization.domainobjects.Group;
import gov.nih.nci.security.exceptions.CSObjectNotFoundException;

import org.acegisecurity.AuthenticationServiceException;
import org.acegisecurity.GrantedAuthority;
import org.acegisecurity.GrantedAuthorityImpl;
import org.acegisecurity.userdetails.User;
import org.acegisecurity.userdetails.UserDetails;
import org.acegisecurity.userdetails.UserDetailsService;
import org.acegisecurity.userdetails.UsernameNotFoundException;
import org.springframework.dao.DataAccessException;

public class CSMUserDetailsService implements UserDetailsService {

	private String rolePrefix = "ROLE_";

	private UserProvisioningManager csmUserProvisioningManager;

	public UserDetails loadUserByUsername(String userName)
			throws UsernameNotFoundException, DataAccessException {

		GrantedAuthority[] authorities = null;

		UserProvisioningManager mgr = getCsmUserProvisioningManager();
		Set groups;
		try {
			groups = mgr
					.getGroups(mgr.getUser(userName).getUserId().toString());
		} catch (CSObjectNotFoundException ex) {
			throw new AuthenticationServiceException("Error getting groups: "
					+ ex.getMessage(), ex);
		}
		if (groups == null || groups.size() == 0) {
			authorities = new GrantedAuthority[0];
		} else {
			String prefix = getRolePrefix();
			if (prefix == null) {
				prefix = "";
			}
			authorities = new GrantedAuthority[groups.size()];
			int idx = 0;
			for (Iterator i = groups.iterator(); i.hasNext(); idx++) {
				Group group = (Group) i.next();
				authorities[idx] = new GrantedAuthorityImpl(prefix
						+ group.getGroupName());
			}
		}

		return new User(userName, "ignored", true, true, true, true,
				authorities);

	}

	public String getRolePrefix() {
		return rolePrefix;
	}

	public void setRolePrefix(String rolePrefix) {
		this.rolePrefix = rolePrefix;
	}

	public UserProvisioningManager getCsmUserProvisioningManager() {
		return csmUserProvisioningManager;
	}

	public void setCsmUserProvisioningManager(
			UserProvisioningManager userProvisioningManager) {
		this.csmUserProvisioningManager = userProvisioningManager;
	}

}

@@ -698,78 +698,311 @@
 on;%0A
-%0Apublic class CSMUserDetailsService implements UserDetailsService %7B%0A%0A%09
+import org.springframework.beans.factory.annotation.Required;%0Aimport org.apache.commons.logging.Log;%0Aimport org.apache.commons.logging.LogFactory;%0A%0Apublic class CSMUserDetailsService implements UserDetailsService %7B%0A    private static final Log log = LogFactory.getLog(CSMUserDetailsService.class);%0A%0A    
 priv
@@ -1212,16 +1212,74 @@
 ption %7B%0A
+        log.debug(%22Getting user details for %22 + userName);
 %0A%09%09Grant
@@ -1404,61 +1404,287 @@
 y %7B%0A
-%09%09%09groups = mgr%0A%09%09%09%09%09.getGroups(mgr.getUser(userName)
+            gov.nih.nci.security.authorization.domainobjects.User loadedUser = mgr.getUser(userName);%0A            log.debug(%22Retrieved user obj %22 + loadedUser + %22 with ID %22 + (loadedUser == null ? %22%3Cnull%3E%22 : loadedUser.getUserId()));%0A            groups = mgr.getGroups(loadedUser
 .get
@@ -2627,25 +2627,42 @@
 anager;%0A%09%7D%0A%0A
-%09
+    @Required%0A    
 public void 
@@ -2691,20 +2691,16 @@
 Manager(
-%0A%09%09%09
 UserProv
@@ -2806,11 +2806,10 @@
 ger;%0A%09%7D%0A
-%0A
 %7D%0A

Add some more debug logging