1 package org.tigris.scarab.actions; 2 3 48 49 import java.util.Calendar ; 50 import java.util.Locale ; 51 52 import org.apache.turbine.Turbine; 54 import org.apache.turbine.TemplateContext; 55 import org.apache.turbine.RunData; 56 import org.apache.turbine.modules.ContextAdapter; 57 58 import org.apache.fulcrum.security.TurbineSecurity; 59 import org.apache.turbine.tool.IntakeTool; 60 import org.apache.fulcrum.intake.model.Group; 61 import org.apache.fulcrum.security.util.TurbineSecurityException; 62 63 import org.tigris.scarab.om.ScarabUser; 65 import org.tigris.scarab.tools.ScarabLocalizationTool; 66 import org.tigris.scarab.tools.localization.L10NKeySet; 67 import org.tigris.scarab.tools.localization.L10NMessage; 68 import org.tigris.scarab.tools.localization.Localizable; 69 import org.tigris.scarab.util.Email; 70 import org.tigris.scarab.util.Log; 71 import org.tigris.scarab.util.PasswordGenerator; 72 import org.tigris.scarab.util.ScarabConstants; 73 import org.tigris.scarab.actions.base.ScarabTemplateAction; 74 75 81 public class ForgotPassword extends ScarabTemplateAction 82 { 83 86 public void doForgotpassword(RunData data, TemplateContext context) 87 throws Exception 88 { 89 data.setACL(null); 90 IntakeTool intake = getIntakeTool(context); 91 if (intake.isAllValid() && forgotPassword(data, context)) 92 { 93 setTarget(data, "Login.vm"); 94 } 95 } 96 97 101 public boolean forgotPassword(RunData data, TemplateContext context) 102 throws Exception 103 { 104 IntakeTool intake = getIntakeTool(context); 105 106 Group password = intake.get("ForgotPassword", IntakeTool.DEFAULT_KEY); 107 String username = password.get("Username").toString(); 108 109 ScarabUser user = null; 110 try 111 { 112 user = (ScarabUser) TurbineSecurity.getUser(username); 113 114 String tempPassword = PasswordGenerator.generate(); 115 116 user.setPasswordExpire(Calendar.getInstance()); 118 user.setHasLoggedIn(Boolean.FALSE); 119 data.setUser(user); 120 data.save(); 121 122 TurbineSecurity.forcePassword(user, tempPassword); 125 126 sendNotificationEmail(context, user, tempPassword); 127 128 Localizable msg = new L10NMessage(L10NKeySet.PasswordResetMessage, 130 user.getEmail()); 131 getScarabRequestTool(context).setConfirmMessage(msg); 132 } 133 catch (TurbineSecurityException e) 134 { 135 Localizable msg = new L10NMessage(L10NKeySet.InvalidUsername,username); 136 getScarabRequestTool(context).setAlertMessage(msg); 137 Log.get().error("ForgotPassword: ", e); 138 setTarget(data, "ForgotPassword.vm"); 139 return false; 140 } 141 return true; 142 } 143 144 152 public static void sendNotificationEmail(TemplateContext context, ScarabUser user, String tempPassword) throws Exception 153 { 154 context.put("password", tempPassword); 157 158 Email te = new Email(); 159 160 ScarabLocalizationTool l10n = (new ForgotPassword()).getLocalizationTool(context); 162 Locale locale = l10n.getPrimaryLocale(); 163 String charset = Email.getCharset(locale); 164 te.setCharset(charset); 165 166 167 te.setContext(new ContextAdapter(context)); 168 te.setTo(user.getFirstName() + " " + user.getLastName(), user.getEmail()); 169 te.setFrom( 170 Turbine.getConfiguration() 171 .getString("scarab.email.forgotpassword.fromName", 172 "Scarab System"), 173 Turbine.getConfiguration() 174 .getString("scarab.email.forgotpassword.fromAddress", 175 "help@localhost")); 176 te.setSubject( 177 l10n.get(L10NKeySet.ForgotPasswordEmailSubject)); 178 te.setTemplate( 179 Turbine.getConfiguration() 180 .getString("scarab.email.forgotpassword.template", 181 "email/ForgotPassword.vm")); 182 te.send(); 183 } 184 } 185 | Popular Tags |