package edu.uiowa.icts.delegate;

import java.util.Collection;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.switchuser.SwitchUserFilter;
import org.springframework.security.web.authentication.switchuser.SwitchUserGrantedAuthority;

/**
 * @author rrlorent
 * Basic implementation of edu.uiowa.icts.delegate.DelegateService.
 * Extend this class if you wish to have more information other than username in the switched session.
 */
public class SwitchUserImpl implements DelegateService {

	private static final Log log = LogFactory.getLog( SwitchUserImpl.class );

	@Override
	public boolean checkAuthentication( HttpServletRequest request ) throws AuthenticationException {

		Authentication current_authentication = SecurityContextHolder.getContext().getAuthentication();
		Collection<? extends GrantedAuthority> current_authorities = current_authentication.getAuthorities();

		for ( GrantedAuthority ga : current_authorities ) {
			if ( ( ga instanceof SwitchUserGrantedAuthority ) && ( ga.getAuthority().equals( SwitchUserFilter.ROLE_PREVIOUS_ADMINISTRATOR ) ) ) {
				log.debug( "user already switched" );
				return false;
			}
		}

		String user = request.getParameter( "j_username" );
		if ( user != null ) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public boolean checkExitAuthentication( HttpServletRequest request ) throws AuthenticationException {

		Authentication current_authentication = SecurityContextHolder.getContext().getAuthentication();
		Collection<? extends GrantedAuthority> current_authorities = current_authentication.getAuthorities();

		for ( GrantedAuthority ga : current_authorities ) {
			if ( ( ga instanceof SwitchUserGrantedAuthority ) && ( ga.getAuthority().equals( SwitchUserFilter.ROLE_PREVIOUS_ADMINISTRATOR ) ) ) {
				SwitchUserGrantedAuthority sw = (SwitchUserGrantedAuthority) ga;
				String user = sw.getSource().getName();
				if ( user != null ) {
					return true;
				} else {
					return false;
				}
			}
		}
		return false;
	}

}
@@ -955,16 +955,67 @@
 ass );%0A%0A
+%09private String usernameParameter = %22j_username%22;%0A%0A
 %09@Overri
@@ -1009,32 +1009,32 @@
 e%22;%0A%0A%09@Override%0A
-
 %09public boolean 
@@ -1619,27 +1619,24 @@
 ameter( 
-%22j_
 username
 %22 );%0A%09%09i
@@ -1627,17 +1627,25 @@
 username
-%22
+Parameter
  );%0A%09%09if
@@ -2429,16 +2429,16 @@
 %09%09%7D%0A%09%09%7D%0A
-
 %09%09return
@@ -2449,9 +2449,195 @@
 se;%0A%09%7D%0A%0A
+%09public String getUsernameParameter() %7B%0A%09%09return usernameParameter;%0A%09%7D%0A%0A%09public void setUsernameParameter( String usernameParameter ) %7B%0A%09%09this.usernameParameter = usernameParameter;%0A%09%7D%0A%0A
 %7D

update switch user impl