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.reg;
022:
023: import org.apache.struts.action.Action;
024: import org.apache.struts.action.ActionMapping;
025: import org.apache.struts.action.ActionForm;
026: import org.apache.struts.action.DynaActionForm;
027: import org.apache.struts.action.ActionForward;
028: import org.apache.commons.lang.StringUtils;
029: import org.apache.commons.lang.RandomStringUtils;
030:
031: import javax.servlet.http.HttpServletRequest;
032: import javax.servlet.http.HttpServletResponse;
033:
034: import com.methodhead.auth.AuthUtil;
035: import com.methodhead.auth.AuthUser;
036: import com.methodhead.auth.AuthAction;
037: import com.methodhead.event.Event;
038: import com.methodhead.util.OperationContext;
039: import com.methodhead.util.StrutsUtil;
040: import com.methodhead.sitecontext.SiteContext;
041:
042: public class SendPasswordAction extends Action {
043:
044: // constructors /////////////////////////////////////////////////////////////
045:
046: // constants ////////////////////////////////////////////////////////////////
047:
048: // classes //////////////////////////////////////////////////////////////////
049:
050: // methods //////////////////////////////////////////////////////////////////
051:
052: protected ActionForward doSendPasswordForm(OperationContext op,
053: RegPolicy policy) {
054:
055: return op.mapping.findForward("form");
056: }
057:
058: protected ActionForward doSendPassword(OperationContext op,
059: RegPolicy policy) {
060:
061: //
062: // load the user
063: //
064: User user = policy.newRegUser();
065:
066: if (!user.loadForLogin((String) op.form.get("email")))
067: throw new RuntimeException("Couldn't load user for login "
068: + op.form.get("email"));
069:
070: String password = null;
071:
072: //
073: // are passwords encrypted?
074: //
075: if (user.getPasswordEncrypted()) {
076:
077: //
078: // generate a new password
079: //
080: password = RandomStringUtils.randomAlphabetic(8);
081:
082: user.setString("password", password);
083: user.save();
084: } else {
085: password = user.getString("password");
086: }
087:
088: //
089: // send the password
090: //
091: policy.sendPassword(user, password, op);
092:
093: //
094: // log the event
095: //
096: Event.log(SiteContext.getDefaultContext(), user.getLogin(),
097: "reg", "User requested password to be sent.");
098:
099: //
100: // add a message
101: //
102: StrutsUtil.addMessage(op.request,
103: "reg.sendpassword.passwordSent", null, null, null);
104:
105: return op.mapping.findForward("success");
106: }
107:
108: public ActionForward execute(ActionMapping mapping,
109: ActionForm form, HttpServletRequest request,
110: HttpServletResponse response) throws Exception {
111:
112: //
113: // get some things we'll need
114: //
115: DynaActionForm dynaForm = (DynaActionForm) form;
116: RegPolicy policy = (RegPolicy) StrutsUtil.getPolicy(mapping);
117: AuthUser user = AuthUtil.getUser(request);
118:
119: OperationContext op = new OperationContext(mapping, dynaForm,
120: request, response, user);
121:
122: //
123: // execute the appopriate method
124: //
125: if (mapping.getPath().equals("/sendPasswordForm")) {
126: return doSendPasswordForm(op, policy);
127: }
128: if (mapping.getPath().equals("/sendPassword")) {
129: return doSendPassword(op, policy);
130: }
131:
132: throw new Exception("Unexpected mapping path \""
133: + mapping.getPath() + "\"");
134: }
135:
136: // properties ///////////////////////////////////////////////////////////////
137:
138: // attributes ///////////////////////////////////////////////////////////////
139: }
|