| 1 20 21 package com.methodhead.auth; 22 23 import javax.servlet.http.HttpServletRequest ; 24 import javax.servlet.http.HttpServletResponse ; 25 26 import org.apache.log4j.Logger; 27 28 import org.apache.struts.Globals; 29 import org.apache.struts.action.Action; 30 import org.apache.struts.action.ActionError; 31 import org.apache.struts.action.ActionErrors; 32 import org.apache.struts.action.ActionForm; 33 import org.apache.struts.action.ActionForward; 34 import org.apache.struts.action.ActionMapping; 35 import org.apache.struts.action.DynaActionForm; 36 import com.methodhead.util.StrutsUtil; 37 import com.methodhead.util.OperationContext; 38 import javax.servlet.http.Cookie ; 39 import org.apache.commons.lang.StringUtils; 40 import com.methodhead.sitecontext.SiteContext; 41 42 108 public class LoginAction extends Action { 109 110 116 protected ActionForward doLoginForm( 117 OperationContext op, 118 AuthPolicy policy ) 119 throws 120 Exception { 121 122 String desturl = 126 ( String )op.request.getAttribute( AuthGlobals.URL_KEY ); 127 128 if ( desturl == null ) 129 desturl = ""; 130 131 op.form.set( "desturl", desturl ); 132 133 Cookie [] cookies = op.request.getCookies(); 137 138 op.form.set( "rememberme", "" ); 139 140 if ( cookies != null ) { 141 for ( int i = 0; i < cookies.length; i++ ) { 142 if ( cookies[ i ].getName().equals( "rememberme" ) ) { 143 op.form.set( "rememberme", "on" ); 144 break; 145 } 146 } 147 } 148 149 return StrutsUtil.findForward( op.mapping, "loginForm" ); 150 } 151 152 176 protected ActionForward doLogin( 177 OperationContext op, 178 AuthPolicy policy ) 179 throws 180 Exception { 181 182 if ( logger_.isDebugEnabled() ) { 183 logger_.debug( "Attempting to login " + op.form.get( "login" ) ); 184 } 185 186 AuthUser user = policy.newUser(); 190 191 192 if ( user.loadForLogin( ( String )op.form.get( "login" ) ) ) { 196 197 if ( logger_.isDebugEnabled() ) { 198 logger_.debug( "Successfully loaded " + user ); 199 } 200 201 if ( user.authenticate( 205 ( String )op.form.get( "password" ) ) ) { 206 207 if ( logger_.isDebugEnabled() ) { 208 logger_.debug( "Successfully authenticated " + user ); 209 } 210 211 if ( StringUtils.isNotBlank( ( String )op.form.get( "rememberme" ) ) ) { 215 Cookie cookie = 216 new Cookie ( 217 "rememberme", user.getLogin() + ":" + user.getPublicSecret() ); 218 219 cookie.setMaxAge( 365 * 24 * 60 * 60 ); 221 op.response.addCookie( cookie ); 222 223 if ( logger_.isDebugEnabled() ) { 224 logger_.debug( "Added rememberme cookie \"" + cookie.getValue() + "\"" ); 225 } 226 } 227 else { 228 Cookie cookie = new Cookie ( "rememberme", "" ); 232 cookie.setMaxAge( 0 ); op.response.addCookie( cookie ); 234 235 if ( logger_.isDebugEnabled() ) { 236 logger_.debug( "Deleted rememberme cookie" ); 237 } 238 } 239 240 AuthUtil.setUser( op.request, user ); 244 245 if ( !"".equals( op.form.get( "desturl" ) ) ) { 249 250 if ( logger_.isDebugEnabled() ) { 251 logger_.debug( "desturl set; forwarding to " + op.form.get( "desturl" ) ); 252 } 253 254 return new ActionForward( ( String )op.form.get( "desturl" ), true ); 255 } 256 else { 257 return StrutsUtil.findForward( op.mapping, "loggedIn" ); 258 } 259 } 260 } 261 262 ActionErrors errors = new ActionErrors(); 266 errors.add( 267 ActionErrors.GLOBAL_ERROR, 268 new ActionError( "loginform.invalidlogin" ) ); 269 270 op.request.setAttribute( Globals.ERROR_KEY, errors ); 271 272 return new ActionForward( op.mapping.getInput() ); 273 } 274 275 279 protected ActionForward doLogout( 280 OperationContext op, 281 AuthPolicy policy ) 282 throws 283 Exception { 284 285 AuthUtil.setUser( op.request, null ); 286 return StrutsUtil.findForward( op.mapping, "loggedOut" ); 287 } 288 289 public ActionForward execute( 290 ActionMapping mapping, 291 ActionForm form, 292 HttpServletRequest request, 293 HttpServletResponse response ) 294 throws 295 Exception { 296 297 AuthPolicy policy = 301 ( AuthPolicy )Class.forName( mapping.getParameter() ).newInstance(); 302 303 DynaActionForm loginForm = ( DynaActionForm )form; 304 305 OperationContext op = 306 new OperationContext( 307 mapping, loginForm, request, response, null ); 308 309 if ( mapping.getPath().equals( "/loginForm" ) ) { 310 return doLoginForm( op, policy ); 311 } 312 313 if ( mapping.getPath().equals( "/login" ) ) { 314 return doLogin( op, policy ); 315 } 316 317 else if ( mapping.getPath().equals( "/logout" ) ) { 318 return doLogout( op, policy ); 319 } 320 321 throw new Exception ( 322 "Unexpected action mapping path " + mapping.getPath() ); 323 } 324 325 private Logger logger_ = Logger.getLogger( LoginAction.class ); 326 } 327 | Popular Tags |