1 20 21 package com.methodhead.reg; 22 23 import org.apache.struts.action.Action; 24 import org.apache.struts.action.ActionMapping; 25 import org.apache.struts.action.ActionForm; 26 import org.apache.struts.action.DynaActionForm; 27 import org.apache.struts.action.ActionForward; 28 import org.apache.commons.lang.StringUtils; 29 import org.apache.commons.lang.RandomStringUtils; 30 31 import javax.servlet.http.HttpServletRequest ; 32 import javax.servlet.http.HttpServletResponse ; 33 34 import com.methodhead.auth.AuthUtil; 35 import com.methodhead.auth.AuthUser; 36 import com.methodhead.auth.AuthAction; 37 import com.methodhead.event.Event; 38 import com.methodhead.util.OperationContext; 39 import com.methodhead.util.StrutsUtil; 40 import com.methodhead.sitecontext.SiteContext; 41 42 public class SendPasswordAction 43 extends 44 Action { 45 46 48 50 52 54 protected ActionForward doSendPasswordForm( 55 OperationContext op, 56 RegPolicy policy ) { 57 58 return op.mapping.findForward( "form" ); 59 } 60 61 protected ActionForward doSendPassword( 62 OperationContext op, 63 RegPolicy policy ) { 64 65 User user = policy.newRegUser(); 69 70 if ( !user.loadForLogin( ( String )op.form.get( "email" ) ) ) 71 throw new RuntimeException ( 72 "Couldn't load user for login " + op.form.get( "email" ) ); 73 74 String password = null; 75 76 if ( user.getPasswordEncrypted() ) { 80 81 password = RandomStringUtils.randomAlphabetic( 8 ); 85 86 user.setString( "password", password ); 87 user.save(); 88 } 89 else { 90 password = user.getString( "password" ); 91 } 92 93 policy.sendPassword( user, password, op ); 97 98 Event.log( 102 SiteContext.getDefaultContext(), 103 user.getLogin(), 104 "reg", 105 "User requested password to be sent." ); 106 107 StrutsUtil.addMessage( 111 op.request, "reg.sendpassword.passwordSent", null, null, null ); 112 113 return op.mapping.findForward( "success" ); 114 } 115 116 public ActionForward execute( 117 ActionMapping mapping, 118 ActionForm form, 119 HttpServletRequest request, 120 HttpServletResponse response ) 121 throws 122 Exception { 123 124 DynaActionForm dynaForm = ( DynaActionForm )form; 128 RegPolicy policy = ( RegPolicy )StrutsUtil.getPolicy( mapping ); 129 AuthUser user = AuthUtil.getUser( request ); 130 131 OperationContext op = 132 new OperationContext( mapping, dynaForm, request, response, user ); 133 134 if ( mapping.getPath().equals( "/sendPasswordForm" ) ) { 138 return doSendPasswordForm( op, policy ); 139 } 140 if ( mapping.getPath().equals( "/sendPassword" ) ) { 141 return doSendPassword( op, policy ); 142 } 143 144 throw 145 new Exception ( "Unexpected mapping path \"" + mapping.getPath() + "\"" ); 146 } 147 148 150 } 152 | Popular Tags |