<%@page import="oneit.utils.*,oneit.logging.*"%>
<%@page import="oneit.security.*, oneit.servlets.security.SessionSecUserDecorator"%>
<%@page import="java.util.Date"%>
<%@page import="oneit.objstore.services.TransactionServicesFactory"%>
<%@page import="performa.utils.*, oneit.objstore.*, oneit.appservices.config.*" %>

<%
    SecUser pageUser    = (SecUser)(session.getAttribute(SecUser.SEC_USER_ID));
    String  userName    = request.getParameter("userName");

    if (userName != null)
    {
        if(ConfigMgr.getKeyfileBoolean("test.sso", false))
        {
            TransactionServicesFactory  tsf     = (TransactionServicesFactory)ConfigMgr.getConfigObject(ConfigMgr.GLOBAL_CONFIG_SYSTEM, "TransactionServices");
            ObjectTransaction           objTran = new ObjectTransaction (tsf);

            try
            {
                SecUser secUser =   SecUser.searchNAME(objTran, userName);
                
                if(Utils.checkAdminPortalAccess(secUser))
                {
                    request.getSession().invalidate();
                    request.getSession().setAttribute (AuthorisationToken.AUTH_TOKEN_ID, secUser);

                    // place the user on the session
                    request.getSession ().setAttribute (SecUser.SEC_USER_ID, secUser);

                    // This is only to help with the tomcat manager, so you can see which user belongs to which session
                    request.getSession ().setAttribute (SecUser.SEC_USER_ID+".desc", secUser.getDescription());
                    request.getSession ().setAttribute (SecUser.SEC_USER_ID+".loggedin", ""+new Date());
                    request.getSession ().setAttribute (SessionSecUserDecorator.REFRESH_SECURITY, Boolean.TRUE);

                    String  url =   WebUtils.getArticleLink(request, objTran, WebUtils.ADMIN_HOME, "Page");
                    
                    objTran.commit();
                    objTran.commitResources();

                    response.sendRedirect(url);
                }
                else
                {
                    response.setStatus(404);
                    return;
                }
            }
            finally
            {
                objTran.releaseResources();
            }
        }
        else
        {
            response.setStatus(404);
            return;
        }
    }
%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
    </head>
    <body>
        <form method="POST">
            <input name="userName" value="" required="required" >
            <input type="submit" name="Go" value="Submit">
        </form>
    </body>
</html>
