001: /*
002: * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
003: *
004: * This file is part of TransferCM.
005: *
006: * TransferCM is free software; you can redistribute it and/or modify it under the
007: * terms of the GNU General Public License as published by the Free Software
008: * Foundation; either version 2 of the License, or (at your option) any later
009: * version.
010: *
011: * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
012: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
013: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
014: * details.
015: *
016: * You should have received a copy of the GNU General Public License along with
017: * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
018: * Fifth Floor, Boston, MA 02110-1301 USA
019: */
020:
021: package com.methodhead.auth;
022:
023: import javax.servlet.http.HttpServletRequest;
024: import javax.servlet.http.HttpServletResponse;
025: import javax.servlet.http.Cookie;
026:
027: import org.apache.struts.action.Action;
028: import org.apache.struts.action.ActionForm;
029: import org.apache.struts.action.ActionForward;
030: import org.apache.struts.action.ActionMapping;
031: import com.methodhead.util.StrutsUtil;
032:
033: /**
034: * <tt>AuthAction</tt> makes sure a user is logged in before calling {@link
035: * #doExecute doExecute()}. <tt>AuthAction</tt> checks for a user in the
036: * session using {@link com.methodhead.auth.AuthUtil#getUser
037: * AuthUtil.getUser()}. If if no such user is in the session, the url is
038: * stored in the <tt>AuthGlobals.URL_KEY</tt> request attribute and the
039: * <tt>loginForm</tt> forward is returned. If a user is in the session,
040: * <tt>doExecute()</tt> is called.
041: */
042: public abstract class AuthAction extends Action {
043:
044: // constructors /////////////////////////////////////////////////////////////
045:
046: // constants ////////////////////////////////////////////////////////////////
047:
048: // classes //////////////////////////////////////////////////////////////////
049:
050: // methods //////////////////////////////////////////////////////////////////
051:
052: public ActionForward execute(ActionMapping mapping,
053: ActionForm form, HttpServletRequest request,
054: HttpServletResponse response) throws Exception {
055:
056: //
057: // see if a user is logged in
058: //
059: AuthUser user = AuthUtil.getUser(request);
060:
061: if (user == null) {
062:
063: //
064: // remember me?
065: //
066: Cookie[] cookies = request.getCookies();
067:
068: if (cookies != null) {
069: for (int i = 0; i < cookies.length; i++) {
070: if (cookies[i].getName().equals("rememberme")) {
071: String[] parts = cookies[i].getValue().split(
072: ":");
073:
074: //
075: // get policy
076: //
077: AuthPolicy policy = (AuthPolicy) StrutsUtil
078: .getPolicy(mapping);
079:
080: user = policy.newUser();
081:
082: if (user.loadForLogin(parts[0])) {
083:
084: if (user.getPublicSecret().equals(parts[1])) {
085:
086: //
087: // process auto login
088: //
089: if (!policy.autoLogin(user, request,
090: form)) {
091:
092: //
093: // make a note of the destination
094: //
095: request
096: .setAttribute(
097: AuthGlobals.URL_KEY,
098: AuthUtil
099: .getRelativeUrl(request));
100:
101: return StrutsUtil.findForward(
102: mapping, "loginForm");
103: }
104:
105: //
106: // log the user in
107: //
108: AuthUtil.setUser(request, user);
109:
110: //
111: // perform the action
112: //
113: return doExecute(mapping, form,
114: request, response);
115: }
116: }
117: }
118: }
119: }
120:
121: //
122: // make a note of the destination
123: //
124: StringBuffer url = request.getRequestURL();
125: url.append(request.getQueryString() == null ? "" : "?"
126: + request.getQueryString());
127: request.setAttribute(AuthGlobals.URL_KEY, url.toString());
128:
129: //
130: // forward to the login form
131: //
132: return StrutsUtil.findForward(mapping, "loginForm");
133: }
134:
135: //
136: // execute the action
137: //
138: return doExecute(mapping, form, request, response);
139: }
140:
141: /**
142: * Executes the action.
143: */
144: protected abstract ActionForward doExecute(ActionMapping mapping,
145: ActionForm form, HttpServletRequest request,
146: HttpServletResponse response) throws Exception;
147:
148: // properties ///////////////////////////////////////////////////////////////
149:
150: // attributes ///////////////////////////////////////////////////////////////
151: }
|