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:
029: import javax.servlet.http.HttpServletRequest;
030: import javax.servlet.http.HttpServletResponse;
031: import com.methodhead.aikp.AikpAction;
032: import com.methodhead.aikp.AutoIntKeyPersistable;
033: import com.methodhead.aikp.IntKey;
034: import com.methodhead.util.OperationContext;
035: import com.methodhead.sitecontext.SiteContext;
036: import com.methodhead.util.StrutsUtil;
037: import com.methodhead.event.Event;
038: import com.methodhead.auth.AuthUser;
039: import com.methodhead.auth.AuthUtil;
040: import java.util.Iterator;
041: import java.util.List;
042: import java.util.Collections;
043: import org.apache.commons.lang.StringUtils;
044:
045: public class UserAction extends AikpAction {
046:
047: // constructors /////////////////////////////////////////////////////////////
048:
049: // constants ////////////////////////////////////////////////////////////////
050:
051: // classes //////////////////////////////////////////////////////////////////
052:
053: // methods //////////////////////////////////////////////////////////////////
054:
055: protected AutoIntKeyPersistable createPersistable(
056: OperationContext op) {
057: return new User();
058: }
059:
060: protected void populateForm(DynaActionForm form,
061: AutoIntKeyPersistable persistable) {
062:
063: User user = (User) persistable;
064:
065: form.set("id", "" + user.getInt("id"));
066: form.set("firstname", user.getContact().getString("firstname"));
067: form.set("middlename", user.getContact()
068: .getString("middlename"));
069: form.set("lastname", user.getContact().getString("lastname"));
070: form.set("company", user.getContact().getString("company"));
071: form.set("address1", user.getContact().getString("address1"));
072: form.set("address2", user.getContact().getString("address2"));
073: form.set("city", user.getContact().getString("city"));
074: form.set("state", user.getContact().getString("state"));
075: form.set("zip", user.getContact().getString("zip"));
076: form.set("country", user.getContact().getString("country"));
077: form.set("phone", user.getContact().getString("phone"));
078: form.set("fax", user.getContact().getString("fax"));
079: form.set("email", user.getContact().getString("email"));
080: form.set("url", user.getContact().getString("url"));
081: form.set("password", "");
082: form.set("verifypassword", "");
083: }
084:
085: protected void populatePersistable(
086: AutoIntKeyPersistable persistable, DynaActionForm form) {
087:
088: User user = (User) persistable;
089:
090: user.getContact().setString("firstname",
091: (String) form.get("firstname"));
092: user.getContact().setString("middlename",
093: (String) form.get("middlename"));
094: user.getContact().setString("lastname",
095: (String) form.get("lastname"));
096: user.getContact().setString("company",
097: (String) form.get("company"));
098: user.getContact().setString("address1",
099: (String) form.get("address1"));
100: user.getContact().setString("address2",
101: (String) form.get("address2"));
102: user.getContact().setString("city", (String) form.get("city"));
103: user.getContact()
104: .setString("state", (String) form.get("state"));
105: user.getContact().setString("zip", (String) form.get("zip"));
106: user.getContact().setString("country",
107: (String) form.get("country"));
108: user.getContact()
109: .setString("phone", (String) form.get("phone"));
110: user.getContact().setString("fax", (String) form.get("fax"));
111: user.getContact()
112: .setString("email", (String) form.get("email"));
113: user.getContact().setString("url", (String) form.get("url"));
114:
115: if (!StringUtils.isBlank((String) form.get("password")))
116: user.setString("password", (String) form.get("password"));
117: }
118:
119: protected ActionForward getForwardForSave(OperationContext op,
120: Object policy) {
121:
122: return new ActionForward("/user.do?action=list");
123: }
124:
125: protected ActionForward getForwardForDelete(OperationContext op,
126: Object policy) {
127:
128: return new ActionForward("/user.do?action=list");
129: }
130:
131: protected ActionForward doCancelSave(OperationContext op,
132: Object policy) {
133:
134: return new ActionForward("/user.do?action=list");
135: }
136:
137: protected ActionForward doList(OperationContext op, Object policy) {
138:
139: String msg = ((RegPolicy) policy).isListUsersAuthorized(op);
140:
141: if (msg != null) {
142: StrutsUtil.addMessage(op.request, msg, null, null, null);
143: return op.mapping.findForward("accessDenied");
144: }
145:
146: ActionForward forward = super .doList(op, policy);
147:
148: List list = (List) op.form.get("list");
149:
150: //
151: // fully load each user
152: //
153: for (Iterator iter = (list).iterator(); iter.hasNext();) {
154: User u = (User) iter.next();
155: u.load(new IntKey(u.getInt("id")));
156: }
157:
158: //
159: // sort the users
160: //
161: Collections.sort(list);
162:
163: return forward;
164: }
165:
166: protected ActionForward doDelete(OperationContext op, Object policy) {
167:
168: String msg = ((RegPolicy) policy).isDeleteUserAuthorized(op);
169:
170: if (msg != null) {
171: StrutsUtil.addMessage(op.request, msg, null, null, null);
172: return op.mapping.findForward("accessDenied");
173: }
174:
175: String email = StringUtils.defaultString(op.request
176: .getParameter("email"), "" + op.form.get("id"));
177:
178: ActionForward forward = super .doDelete(op, policy);
179:
180: Event.log(SiteContext.getDefaultContext(), op.user.getLogin(),
181: email, "User deleted.");
182:
183: return forward;
184: }
185:
186: protected ActionForward doSave(OperationContext op, Object policy) {
187:
188: String msg = ((RegPolicy) policy).isSaveUserAuthorized(op);
189:
190: if (msg != null) {
191: StrutsUtil.addMessage(op.request, msg, null, null, null);
192: return op.mapping.findForward("accessDenied");
193: }
194:
195: ActionForward forward = super .doSave(op, policy);
196:
197: Event.log(SiteContext.getDefaultContext(), op.user.getLogin(),
198: (String) op.form.get("email"), "User updated.");
199:
200: return forward;
201: }
202:
203: protected ActionForward doSaveNew(OperationContext op, Object policy) {
204:
205: String msg = ((RegPolicy) policy).isSaveNewUserAuthorized(op);
206:
207: if (msg != null) {
208: StrutsUtil.addMessage(op.request, msg, null, null, null);
209: return op.mapping.findForward("accessDenied");
210: }
211:
212: ActionForward forward = super .doSaveNew(op, policy);
213:
214: Event.log(SiteContext.getDefaultContext(), op.user.getLogin(),
215: (String) op.form.get("email"), "User created.");
216:
217: return forward;
218: }
219:
220: protected ActionForward doEdit(OperationContext op, Object policy) {
221:
222: String msg = ((RegPolicy) policy).isEditUserAuthorized(op);
223:
224: if (msg != null) {
225: StrutsUtil.addMessage(op.request, msg, null, null, null);
226: return op.mapping.findForward("accessDenied");
227: }
228:
229: return super .doEdit(op, policy);
230: }
231:
232: protected ActionForward doNew(OperationContext op, Object policy) {
233:
234: String msg = ((RegPolicy) policy).isNewUserAuthorized(op);
235:
236: if (msg != null) {
237: StrutsUtil.addMessage(op.request, msg, null, null, null);
238: return op.mapping.findForward("accessDenied");
239: }
240:
241: return super .doNew(op, policy);
242: }
243:
244: // properties ///////////////////////////////////////////////////////////////
245:
246: // attributes ///////////////////////////////////////////////////////////////
247: }
|