Subversion Repositories bacoAlunos

Rev

Rev 1408 | Rev 1686 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package pt.estgp.estgweb.web.controllers.authenticate;

import org.apache.log4j.Logger;
import org.apache.struts.action.*;
import pt.estgp.estgweb.Globals;
import pt.estgp.estgweb.domain.User;
import pt.estgp.estgweb.domain.UserImpl;
import pt.estgp.estgweb.domain.UserSession;
import pt.estgp.estgweb.domain.views.UserView;
import pt.estgp.estgweb.services.authenticate.AuthenticateException;
import pt.estgp.estgweb.web.UserSessionProxy;
import pt.estgp.estgweb.web.controllers.ApplicationDispatchController;
import pt.estgp.estgweb.web.exceptions.NoCookiesException;
import pt.estgp.estgweb.web.form.profile.ProfileForm;
import pt.estgp.estgweb.web.utils.RequestUtils;
import pt.utl.ist.berserk.logic.serviceManager.IServiceManager;
import pt.utl.ist.berserk.logic.serviceManager.ServiceManager;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Jorge Machado
 * @date 26/Fev/2008
 * @time 18:01:54
 * @see pt.estgp.estgweb.web
 */

public class AuthenticateController extends ApplicationDispatchController
{

    private static final 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(AuthenticateController.class);

    public ActionForward login(ActionMapping mapping,
                               ActionForm form ,
                               HttpServletRequest request,
                               HttpServletResponse response)
            throws 1.5.0/docs/api/java/io/IOException.html">IOException,ServletException
    {

        1.5.0/docs/api/java/lang/String.html">String username = request.getParameter("username");
        1.5.0/docs/api/java/lang/String.html">String password = request.getParameter("password");

        try
        {
            IServiceManager sm = ServiceManager.getInstance();
            5+0%2Fdocs%2Fapi+Object">Object[] args = new 5+0%2Fdocs%2Fapi+Object">Object[]{username,password};
            UserSession userSession = (UserSession) sm.execute(RequestUtils.getRequester(request, response),Globals.AUTHENTICATE_SERVICE,args);
            request.setAttribute(Globals.USER_SESSION_KEY,userSession);
            if(!((UserImpl)userSession.getUser()).isNewUser())
                return mapping.findForward("success");
            else
            {
                ProfileForm profileForm = new ProfileForm();
                IServiceManager sm2 = ServiceManager.getInstance();
                1.5.0/docs/api/java/lang/String.html">String[] names = new 1.5.0/docs/api/java/lang/String.html">String[]{"serializable"};
                5+0%2Fdocs%2Fapi+Object">Object[] args2 = new 5+0%2Fdocs%2Fapi+Object">Object[]{userSession.getUser().getId()};
                UserView uV = (UserView) sm2.execute(RequestUtils.getRequester(request, response),"LoadUserById",args2,names);
                profileForm.setUserView(uV);
                request.setAttribute("ProfileForm",profileForm);
                addMessage(request,"profile.enter.first.time",uV.getName());
                return mapping.findForward("firstTimeAuthenticate");
            }

        }
        catch (AuthenticateException e)
        {
            ActionMessages actionMessages = new ActionMessages();
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE,new ActionMessage(e.getMessage()));
            saveErrors(request, actionMessages);
            return mapping.getInputForward();
        }
        catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
        {
            if(e instanceof NoCookiesException)
                return mapping.findForward("errorNoCookies");
            logger.fatal(e,e);
        }
        return mapping.findForward("error500");
    }


    public ActionForward checkin(ActionMapping mapping,
                               ActionForm form ,
                               HttpServletRequest request,
                               HttpServletResponse response)
            throws 1.5.0/docs/api/java/io/IOException.html">IOException,ServletException
    {

        1.5.0/docs/api/java/lang/String.html">String certificate = request.getParameter("certificate");


        try
        {
            IServiceManager sm = ServiceManager.getInstance();
            5+0%2Fdocs%2Fapi+Object">Object[] args = new 5+0%2Fdocs%2Fapi+Object">Object[]{certificate};
            UserSession userSession = (UserSession) sm.execute(RequestUtils.getRequester(request, response),"AuthenticateCertificate",args);
            request.setAttribute(Globals.USER_SESSION_KEY,userSession);
            if(!((UserImpl)userSession.getUser()).isNewUser())
                return mapping.findForward("success");
            else
            {
                ProfileForm profileForm = new ProfileForm();
                IServiceManager sm2 = ServiceManager.getInstance();
                1.5.0/docs/api/java/lang/String.html">String[] names = new 1.5.0/docs/api/java/lang/String.html">String[]{"serializable"};
                5+0%2Fdocs%2Fapi+Object">Object[] args2 = new 5+0%2Fdocs%2Fapi+Object">Object[]{userSession.getUser().getId()};
                UserView uV = (UserView) sm2.execute(RequestUtils.getRequester(request, response),"LoadUserById",args2,names);
                profileForm.setUserView(uV);
                request.setAttribute("ProfileForm",profileForm);
                addMessage(request,"profile.enter.first.time",uV.getName());
                return mapping.findForward("firstTimeAuthenticate");
            }

        }
        catch (AuthenticateException e)
        {
            ActionMessages actionMessages = new ActionMessages();
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE,new ActionMessage(e.getMessage()));
            saveErrors(request, actionMessages);
            return mapping.getInputForward();
        }
        catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
        {
            if(e instanceof NoCookiesException)
                return mapping.findForward("errorNoCookies");
            logger.fatal(e,e);
        }
        return mapping.findForward("error500");
    }

    public ActionForward logout(ActionMapping mapping,
                                ActionForm form ,
                                HttpServletRequest request,
                                HttpServletResponse response)
            throws 1.5.0/docs/api/java/io/IOException.html">IOException, ServletException
    {

        try
        {

            UserSession userSession = UserSessionProxy.loadUserSessionFromRequest(request);
            if(userSession.getUser() == null)
                return mapping.findForward(Globals.SYSTEM_REDIRECTIONS_POLICY_INDEX_WELCOME);
            IServiceManager sm = ServiceManager.getInstance();
            5+0%2Fdocs%2Fapi+Object">Object[] args = new 5+0%2Fdocs%2Fapi+Object">Object[]{};
            //to send good by message
            request.setAttribute("username",userSession.getUser().getUsername());
            if(userSession.getUser().getName()!=null)
                request.setAttribute("name",userSession.getUser().getName());
            else
                request.setAttribute("name",userSession.getUser().getUsername());

            userSession = (UserSession) sm.execute(RequestUtils.getRequester(request, response),"Logout",args);
            request.setAttribute(Globals.USER_SESSION_KEY,userSession);
            return mapping.findForward("logout");
        }
        catch (AuthenticateException e)
        {
            ActionMessages actionMessages = new ActionMessages();
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE,new ActionMessage(e.getMessage()));
            saveErrors(request, actionMessages);
            return mapping.getInputForward();
        }
        catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
        {
            if(e instanceof NoCookiesException)
                return mapping.findForward("errorNoCookies");
            logger.fatal(e,e);
        }
        return mapping.findForward("error500");
    }


    public ActionForward changePassword(ActionMapping mapping,
                               ActionForm form ,
                               HttpServletRequest request,
                               HttpServletResponse response)
            throws 1.5.0/docs/api/java/io/IOException.html">IOException,ServletException
    {

        1.5.0/docs/api/java/lang/String.html">String password = request.getParameter("password");
        1.5.0/docs/api/java/lang/String.html">String password2 = request.getParameter("password2");

        if(password == null || password2 == null || password.trim().length() < 5 || !password.equals(password2))
        {
            addError(request, "fail.change.pass");
            return mapping.findForward("fail.change.pass");
        }
        else{
            boolean okBig = false;
            boolean okSmall = false;
            for(char c: password.toCharArray())
            {
                if(c >= 'A' && c <='Z')
                {
                    okBig = true;

                }
                if(c >= 'a' && c <='z')
                {
                    okSmall = true;
                }
            }
            if(!okBig || !okSmall)
            {
                addError(request, "fail.change.pass");
                return mapping.findForward("fail.change.pass");
            }
            else
            {
                try
                {
                    IServiceManager sm = ServiceManager.getInstance();
                    5+0%2Fdocs%2Fapi+Object">Object[] args = new 5+0%2Fdocs%2Fapi+Object">Object[]{password};
                    sm.execute(RequestUtils.getRequester(request, response),"ChangePassword",args);
                    addError(request, "success.change.pass");
                    return mapping.findForward("success");
                }
                catch (AuthenticateException e)
                {
                    ActionMessages actionMessages = new ActionMessages();
                    actionMessages.add(ActionMessages.GLOBAL_MESSAGE,new ActionMessage(e.getMessage()));
                    saveErrors(request, actionMessages);
                    return mapping.getInputForward();
                }
                catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
                {
                    if(e instanceof NoCookiesException)
                        return mapping.findForward("errorNoCookies");
                    logger.fatal(e,e);
                }
            }

        }

        return mapping.findForward("error500");
    }

    public ActionForward requestChangePassword(ActionMapping mapping,
                                        ActionForm form ,
                                        HttpServletRequest request,
                                        HttpServletResponse response)
            throws 1.5.0/docs/api/java/io/IOException.html">IOException,ServletException
    {

        1.5.0/docs/api/java/lang/String.html">String identifier = request.getParameter("identifier");

        try
        {
            IServiceManager sm = ServiceManager.getInstance();
            5+0%2Fdocs%2Fapi+Object">Object[] args = new 5+0%2Fdocs%2Fapi+Object">Object[]{identifier};
            User u = (User) sm.execute(RequestUtils.getRequester(request, response),"RequestChangePassword",args);
            if(u == null)
            {
                addError(request,"fail.request.change.pass");
            }
            request.setAttribute("UserRequesterPasswordChange",u);
            return mapping.findForward("success");
        }
        catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
        {
            if(e instanceof NoCookiesException)
                return mapping.findForward("errorNoCookies");
            logger.fatal(e,e);
        }


        return mapping.findForward("error500");
    }


}