/**
 * ESUP-Portail Helpdesk - Copyright (c) 2004-2008 ESUP-Portail consortium.
 */
package org.esupportail.smsuapiadmin.services.authentication;

import java.io.Serializable;

import org.esupportail.commons.services.authentication.AuthUtils;
import org.esupportail.commons.services.authentication.AuthenticationService;
import org.esupportail.commons.services.authentication.info.AuthInfo;
import org.esupportail.commons.services.logging.Logger;
import org.esupportail.commons.services.logging.LoggerImpl;
import org.esupportail.commons.utils.Assert;
import org.esupportail.commons.utils.ContextUtils;
import org.esupportail.smsuapiadmin.domain.DomainService;
import org.esupportail.smsuapiadmin.dto.beans.UIUser;
import org.springframework.beans.factory.InitializingBean;

/**
 * A basic authenticator implementation.
 */
public class AuthenticatorImpl implements Serializable, InitializingBean,
		Authenticator {

	/**
	 * The serialization id.
	 */
	private static final long serialVersionUID = 6382142726147456592L;

	/**
	 * The session attribute to store the auth info.
	 */
	private static final String AUTH_INFO_ATTRIBUTE = AuthenticatorImpl.class
			.getName()
			+ ".authInfo";

	/**
	 * The session attribute to store the user.
	 */
	private static final String USER_ATTRIBUTE = AuthenticatorImpl.class
			.getName()
			+ ".user";

	/**
	 * A logger.
	 */
	private final Logger logger = new LoggerImpl(getClass());

	/**
	 * The external authenticator.
	 */
	private AuthenticationService authenticationService;

	/**
	 * The domain authenticator.
	 */
	private DomainService domainService;

	/**
	 * Bean constructor.
	 */
	public AuthenticatorImpl() {
		super();
	}

	/**
	 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
	 */
	public void afterPropertiesSet() {
		Assert.notNull(authenticationService,
				"property authenticationService of class "
						+ this.getClass().getName() + " can not be null");
	}


	
	
	public UIUser getUser() {
		AuthInfo authInfo = (AuthInfo) ContextUtils.getSessionAttribute(AUTH_INFO_ATTRIBUTE);
		if (authInfo != null) {
			UIUser user = (UIUser) ContextUtils.getSessionAttribute(USER_ATTRIBUTE);
			if (logger.isDebugEnabled()) {
				logger.debug("found auth info in session: " + user);
			}
			return user;
		}
		logger.debug("no auth info found in session");

		authInfo = authenticationService.getAuthInfo();
		if (authInfo == null) {
			return null;
		}
		if (AuthUtils.CAS.equals(authInfo.getType())) {
			UIUser user = getDomainService().getUserByLogin(authInfo.getId());
			storeToSession(authInfo, user);
			return user;
		} 
		return null;
	}
	
	
	

	/**
	 * Store the authentication information to the session.
	 * 
	 * @param authInfo
	 * @param user
	 */
	protected void storeToSession(final AuthInfo authInfo, final UIUser user) {
		if (logger.isDebugEnabled()) {
			logger.debug("storing to session: " + authInfo);
		}
		ContextUtils.setSessionAttribute(AUTH_INFO_ATTRIBUTE, authInfo);
		ContextUtils.setSessionAttribute(USER_ATTRIBUTE, user);
	}

	/**
	 * @param authenticationService
	 *            the authenticationService to set
	 */
	public void setAuthenticationService(
			final AuthenticationService authenticationService) {
		this.authenticationService = authenticationService;
	}

	/**
	 * @return the authenticationService
	 */
	protected AuthenticationService getAuthenticationService() {
		return authenticationService;
	}

	/**
	 * @return the domainService
	 */
	protected DomainService getDomainService() {
		return domainService;
	}

	/**
	 * @param domainService
	 *            the domainService to set
	 */
	public void setDomainService(final DomainService domainService) {
		this.domainService = domainService;
	}

}

@@ -2527,57 +2527,13 @@
 %0A%09%09%7D
-%0D%0A%09%09if (AuthUtils.CAS.equals(authInfo.getType()))
+ else
  %7B%0D%0A
@@ -2662,32 +2662,16 @@
 %0D%0A%09%09%7D %0D%0A
-%09%09return null;%0D%0A
 %09%7D%0D%0A%09%0D%0A%09

handle shib users