Subversion Repositories general

Rev

Rev 1056 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
924 dev 1
package ak.hostadmiral.core.action;
919 dev 2
 
3
import javax.servlet.http.HttpServletRequest;
4
import javax.servlet.http.HttpServletResponse;
5
 
950 dev 6
import org.apache.struts.Globals;
919 dev 7
import org.apache.struts.action.Action;
8
import org.apache.struts.action.ActionMapping;
9
import org.apache.struts.action.ActionForm;
10
import org.apache.struts.action.DynaActionForm;
11
import org.apache.struts.action.ActionForward;
12
import org.apache.struts.action.ActionMessages;
13
import org.apache.struts.action.ActionErrors;
14
import org.apache.struts.action.ActionError;
15
 
16
import ak.backpath.BackPath;
17
 
1051 dev 18
import ak.hostadmiral.core.resources.CoreResources;
924 dev 19
import ak.hostadmiral.core.model.User;
20
import ak.hostadmiral.core.model.UserManager;
1072 dev 21
import ak.hostadmiral.core.servlet.LoginInfo;
1056 dev 22
import ak.hostadmiral.core.servlet.SessionKeys;
919 dev 23
 
24
public final class LoginAction
25
	extends Action
26
{
27
	public ActionForward execute(ActionMapping mapping, ActionForm form,
28
			HttpServletRequest request, HttpServletResponse response)
29
		throws Exception
30
	{
1014 dev 31
		ActionUtils.prepare(request, response);
919 dev 32
 
1014 dev 33
		if("show".equals(mapping.getParameter())) {
34
			return mapping.findForward("default");
35
		}
36
		else if("submit".equals(mapping.getParameter())) {
37
			DynaActionForm theForm = (DynaActionForm)form;
38
			User user = UserManager.getInstance().loginUser((String)theForm.get("login"),
39
				(String)theForm.get("password"), request.getRemoteAddr());
919 dev 40
 
1014 dev 41
			if(user == null) {
42
				Thread.sleep(1000); // FIXME: make this delay configurable
951 dev 43
 
1014 dev 44
				ActionErrors errors = new ActionErrors();
45
				errors.add(ActionMessages.GLOBAL_MESSAGE,
46
					new ActionError(CoreResources.LOGIN_FAILED));
47
				saveErrors(request, errors);
48
				return mapping.getInputForward();
49
			}
50
		    else {
1056 dev 51
		    	request.getSession().setAttribute(SessionKeys.USER, user);
1072 dev 52
		    	request.getSession().setAttribute(SessionKeys.LOGIN_INFO,
53
		    		new LoginInfo(user.getId()));
1014 dev 54
		    	request.getSession().setAttribute(Globals.LOCALE_KEY, user.getLocale());
919 dev 55
 
1014 dev 56
		    	String origin = BackPath.findBackPath(request).getBackwardUrl();
57
		    	if(origin == null || origin.length() <= 0) {
58
					return mapping.findForward("default");
59
				}
60
				else {
61
					response.sendRedirect(origin);
62
					return null;
63
				}
919 dev 64
			}
65
		}
1014 dev 66
		else {
67
			throw new Exception("unknown mapping parameter");
68
		}
919 dev 69
	}
70
}