package com.eforce.baby.auth.action;
/**
*
* @author Anish Biswas
* @version 1.0, 07/08/2004
* @since Baby architecture refresh
*/
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import javax.security.auth.login.LoginException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.DispatchAction;
import com.eforce.baby.auth.delegates.UserBD;
import com.eforce.baby.auth.vo.SessionUserVO;
import com.eforce.baby.auth.vo.UserPrivilegeList;
import com.eforce.baby.auth.vo.UserVO;
import com.eforce.baby.common.config.ConfigurationManager;
import com.eforce.baby.common.dao.DAOException;
import com.eforce.baby.common.factory.BusinessDelegateFactory;
import com.eforce.baby.common.vo.CustomerVO;
import com.eforce.baby.utils.EEMSHTTPSessionBindingListener;
import com.eforce.baby.utils.IConstants;
import com.eforce.baby.utils.IErrorMessageKeys;
//import com.lowagie.text.html.HtmlEncoder;
import org.apache.commons.beanutils.BeanUtils;
public class LoginAction extends DispatchAction
{
private Logger log = (Logger)Logger.getInstance(this.getClass().getName());
/**
* Called when a user tries to login to the eems application.
*
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws IOException
* @throws ServletException
*/
public ActionForward login( ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response
) throws IOException, ServletException
{
log.debug("login() : ENTER");
ActionForward actionFrwd = null;
DynaActionForm dynaForm = (DynaActionForm)form;
UserVO userVO = new UserVO();
try
{
BeanUtils.copyProperties(userVO, form);
log.debug("URL: " + request.getRequestURL().toString());
String customerName = (String)request.getSession().getAttribute(IConstants.SESS_ATTR_CUST_NAME);
HashMap map = ConfigurationManager.getInstance().getCustomerList();
log.debug("Cust Name: " + customerName);
CustomerVO cvo = (CustomerVO) map.get(customerName);
log.debug("Cust VO: " + cvo);
UserBD userBD = (UserBD)BusinessDelegateFactory.getInstance().getDelegate("com.eteam.ems.auth.delegates.UserBD");
log.debug(cvo.getDatasourceName()+ cvo.getDatabaseType());
log.debug(userVO.getUserName()+ userVO.getPassword());
// Authenticate
userBD.authenticate(cvo.getDatasourceName(), cvo.getDatabaseType(), userVO.getUserName(), userVO.getPassword());
//Authentication succeeded
// Get user specific info from DB and save this in the user session
SessionUserVO userSess = userBD.findUserSessionInfo(cvo.getDatasourceName(), cvo.getDatabaseType(), userVO.getUserName());
userSess.setCustomer(cvo);
userSess.setDsName(cvo.getDatasourceName());
userSess.setDbType(cvo.getDatabaseType());
// get user privileges
UserPrivilegeList userPrivileges = userBD.findUserPrivileges(cvo.getDatasourceName(), cvo.getDatabaseType(), userVO.getUserName());
HttpSession session = request.getSession();
if(userSess.getProfileId() == null)
{
log.debug("Profile Id is null");
/* Get UserID */
String userID = userBD.findUserID(cvo.getDatasourceName(),cvo.getDatabaseType(),userVO.getUserName());
userSess.setUserId(userID);
userSess.setUserPrivilegeList(userPrivileges);
session.setAttribute(IConstants.SESSION_ATTR_USER_SESSION_INFO, userSess);
session.setAttribute(IConstants.SESSION_ATTR_HTTP_BINDING_LISTENER, new EEMSHTTPSessionBindingListener());
log.debug("USER SESSION INFO: " + userSess.toString());
log.debug("Setting User Login Id: " + userVO.getUserName());
userSess.setUserLoginId(userVO.getUserName());
//session.setAttribute("UserLoginID", userVO.getUserName());
////////////////////////////////////////////////////////////
//request.setAttribute("redURL",dynaForm.getString("redURL"));
if(dynaForm.getString("redURL").equals("/admin/list_customers.jsp"))
actionFrwd = mapping.findForward("profileRedURL");
////////////////////////////////////////////////////////////
else
actionFrwd = mapping.findForward("profileURL");
}
else
{
/* If autoProfileUpdateEnabled is on */
log.debug("Configuration : "+cvo.getConfigurationType());
log.debug("Found Profile Id");
userSess.setUserPrivilegeList(userPrivileges);
session.setAttribute(IConstants.SESSION_ATTR_USER_SESSION_INFO, userSess);
session.setAttribute(IConstants.SESSION_ATTR_HTTP_BINDING_LISTENER, new EEMSHTTPSessionBindingListener());
log.debug("USER SESSION INFO: " + userSess.toString());
/* Check if the forceprofile is enabled or not */
String profileEnabled = "";
profileEnabled = userBD.findProfileEnabled(cvo.getDatasourceName(), cvo.getDatabaseType());
//if(cvo.getAutoProfileUpdateEnabled()!= null && cvo.getAutoProfileUpdateEnabled().equals("Y")){
if(profileEnabled!= null && profileEnabled.equals(IConstants.FORCE_PROFILE_ENABLED))
{
////////////////////////////////////////////////////////////
//request.setAttribute("redURL",dynaForm.getString("redURL"));
////////////////////////////////////////////////////////////
log.debug("redurl in login action = " + "/common/profile.do?target=viewReportForUpdate&mode=update&id="+userSess.getProfileId()+"&fromLogin=yesToUpdate&isNonUser=N&redURL="+ URLEncoder.encode(dynaForm.getString("redURL")));
actionFrwd = new ActionForward("/common/profile.do?target=viewReportForUpdate&mode=update&id="+userSess.getProfileId()+"&fromLogin=yesToUpdate&isNonUser=N&redURL="+URLEncoder.encode(dynaForm.getString("redURL")), true);
}
else
{
request.setAttribute("fwd2",dynaForm.getString("redURL"));
actionFrwd=mapping.findForward("pop_success");
}
}
}
catch (LoginException le)
{
log.error("Login Failure: ", le);
ActionMessage message = new ActionMessage(IErrorMessageKeys.KEY_AUTHENTICATION_FAILURE);
ActionMessages messages = new ActionMessages();
messages.add(IConstants.PAGE_ERROR_MSG_ERROR_MESSAGE, message);
this.saveErrors(request, messages);
actionFrwd = new ActionForward(mapping.getInput());
}
catch (DAOException de)
{
log.error("Login Failure: ", de);
ActionMessage message = new ActionMessage(IErrorMessageKeys.KEY_DATABASE_ERROR);
ActionMessages messages = new ActionMessages();
messages.add(IConstants.PAGE_ERROR_MSG_ERROR_MESSAGE, message);
this.saveErrors(request, messages);
actionFrwd = new ActionForward(mapping.getInput());
}
catch (Exception fe)
{
log.error("Login Failure: ", fe);
ActionMessage message = new ActionMessage(IErrorMessageKeys.KEY_FATAL_ERROR);
ActionMessages messages = new ActionMessages();
messages.add(IConstants.PAGE_ERROR_MSG_ERROR_MESSAGE, message);
this.saveErrors(request, messages);
actionFrwd = new ActionForward(mapping.getInput());
}
log.debug("login() : EXIT");
return actionFrwd;
}
/**
* Called when a user tries to logout of the eems application.
*
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws IOException
* @throws ServletException
*/
public ActionForward logout( ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response
) throws IOException, ServletException
{
ActionForward actionFrwd = null;
request.getSession().removeAttribute(IConstants.SESSION_ATTR_USER_SESSION_INFO);
request.getSession().removeAttribute(IConstants.SESSION_ATTR_HTTP_BINDING_LISTENER);
request.getSession().invalidate();
log.debug("Logout successful....");
return mapping.findForward("logoutURL");
}
}