KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > reg > SendPasswordAction


1 /*
2  * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
3  *
4  * This file is part of TransferCM.
5  *
6  * TransferCM is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation; either version 2 of the License, or (at your option) any later
9  * version.
10  *
11  * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
18  * Fifth Floor, Boston, MA 02110-1301 USA
19  */

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 JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
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   // constructors /////////////////////////////////////////////////////////////
47

48   // constants ////////////////////////////////////////////////////////////////
49

50   // classes //////////////////////////////////////////////////////////////////
51

52   // methods //////////////////////////////////////////////////////////////////
53

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     //
66
// load the user
67
//
68
User user = policy.newRegUser();
69
70     if ( !user.loadForLogin( ( String JavaDoc )op.form.get( "email" ) ) )
71       throw new RuntimeException JavaDoc(
72         "Couldn't load user for login " + op.form.get( "email" ) );
73
74     String JavaDoc password = null;
75
76     //
77
// are passwords encrypted?
78
//
79
if ( user.getPasswordEncrypted() ) {
80
81       //
82
// generate a new password
83
//
84
password = RandomStringUtils.randomAlphabetic( 8 );
85
86       user.setString( "password", password );
87       user.save();
88     }
89     else {
90       password = user.getString( "password" );
91     }
92
93     //
94
// send the password
95
//
96
policy.sendPassword( user, password, op );
97
98     //
99
// log the event
100
//
101
Event.log(
102       SiteContext.getDefaultContext(),
103       user.getLogin(),
104       "reg",
105       "User requested password to be sent." );
106
107     //
108
// add a message
109
//
110
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 JavaDoc request,
120     HttpServletResponse JavaDoc response )
121   throws
122     Exception JavaDoc {
123
124     //
125
// get some things we'll need
126
//
127
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     //
135
// execute the appopriate method
136
//
137
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 JavaDoc( "Unexpected mapping path \"" + mapping.getPath() + "\"" );
146   }
147
148   // properties ///////////////////////////////////////////////////////////////
149

150   // attributes ///////////////////////////////////////////////////////////////
151
}
152
Popular Tags