001: /*
002: * (C) Copyright 2000 - 2006 Nabh Information Systems, Inc.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License
006: * as published by the Free Software Foundation; either version 2
007: * of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: */
019: package com.nabhinc.portal.processor;
020:
021: import java.io.IOException;
022: import java.rmi.RemoteException;
023: import java.util.HashMap;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Locale;
027: import java.util.Map;
028:
029: import javax.security.auth.login.LoginException;
030: import javax.servlet.ServletException;
031: import javax.servlet.http.HttpServletRequest;
032: import javax.servlet.http.HttpServletResponse;
033:
034: import org.apache.commons.fileupload.FileItem;
035: import org.apache.commons.fileupload.FileUploadException;
036: import org.apache.commons.fileupload.disk.DiskFileItemFactory;
037: import org.apache.commons.fileupload.servlet.ServletFileUpload;
038:
039: import com.nabhinc.portal.api.PortalInformationStore;
040: import com.nabhinc.portal.api.PortalInformationStoreLocator;
041: import com.nabhinc.portal.core.SessionCache;
042: import com.nabhinc.portal.model.PortalApplicationView;
043: import com.nabhinc.portal.model.PortalConfiguration;
044: import com.nabhinc.portal.model.PortalPageState;
045: import com.nabhinc.portal.model.UserPreferences;
046: import com.nabhinc.portal.spi.AuthenticationServiceLocator;
047: import com.nabhinc.portal.spi.UserAdminServiceLocator;
048: import com.nabhinc.portlet.mvcportlet.validator.EmailValidator;
049: import com.nabhinc.spi.EntityExistsException;
050: import com.nabhinc.spi.EntityUniqueException;
051: import com.nabhinc.spi.MissingRequiredAttributeException;
052: import com.nabhinc.spi.User;
053: import com.nabhinc.spi.UserAdminService;
054: import com.nabhinc.util.StringUtil;
055:
056: /**
057: *
058: *
059: * @author Padmanabh Dabke
060: * (c) 2006 Nabh Information Systems, Inc. All Rights Reserved.
061: */
062: public class ManageAccountProcessor extends BasePortalActionProcessor {
063:
064: public static final String ACTION_PARAM = "action";
065: public static final String ACTION_UPDATE_PROFILE = "update_profile";
066: public static final String ACTION_CHANGE_PASSWORD = "change_password";
067: public static final String ACTION_UPDATE_PREFERENCE = "update_pref";
068: public static final String ACTION_UPDATE_AVATAR = "update_avatar";
069: public static final String FIRST_NAME_PARAM = "fname";
070: public static final String LAST_NAME_PARAM = "lname";
071: public static final String MIDDLE_NAME_PARAM = "mname";
072: public static final String EMAIL_PARAM = "oemail";
073: public static final String TITLE_PARAM = "utitle";
074: public static final String SUFFIX_PARAM = "suffix";
075: public static final String GENDER_PARAM = "gender";
076: public static final String SIGNATURE_PARAM = "sig";
077: public static final String ADRRESS1_PARAM = "address1";
078: public static final String ADRRESS2_PARAM = "address2";
079: public static final String STATE_PARAM = "state";
080: public static final String CITY_PARAM = "city";
081: public static final String ZIPCODE_PARAM = "zipcode";
082: public static final String COUNTRY_PARAM = "country";
083: public static final String BUSINESS_PHONE_PARAM = "ophone";
084: public static final String BUSINESS_FAX_PARAM = "ofax";
085: public static final String HOME_EMAIL_PARAM = "hemail";
086: public static final String HOME_PHONE_PARAM = "hphone";
087: public static final String HOME_FAX_PARAM = "hfax";
088: public static final String CELL_PHONE_PARAM = "cphone";
089: public static final String PAGER_PARAM = "pager";
090: public static final String WEBSITE_URL_PARAM = "website";
091: public static final String AOL_IM_PARAM = "aim";
092: public static final String YAHOO_IM_PARAM = "yim";
093: public static final String MSN_IM_PARAM = "msnm";
094: public static final String ICQ_IM_PARAM = "icq";
095: public static final String BIRTH_DATE_PARAM = "bdate";
096: public static final String LOCALE_PARAM = "locale";
097: public static final String TIMEZONE_PARAM = "timezone";
098: public static final String SHOW_EMAIL_PARAM = "showemail";
099: public static final String SHOW_NAME_PARAM = "showname";
100: public static final String AVATAR_PARAM = "avatar";
101:
102: public static final String OLD_PASSWORD_PARAM = "old_password";
103: public static final String NEW_PASSWORD_PARAM = "new_password";
104: public static final String CONFIRM_PASSWORD_PARAM = "confirm_password";
105:
106: public void process(HttpServletRequest request,
107: HttpServletResponse response, SessionCache sCache,
108: PortalApplicationView portalAppView,
109: PortalPageState pageState, int startIndex,
110: String[] portalParams, String displayMode,
111: String targetWindowId, boolean isAJAXRequest)
112: throws ServletException, IOException {
113:
114: String systemError = "Your request cannot be processed at this time. Please try again later. If the problems persist please contact your portal administrator at "
115: + PortalConfiguration.getInstance()
116: .getPortalAdminEmail() + ".";
117: if (request.getParameter("is_submit") == null) {
118: request.getRequestDispatcher("/admin/manage_account.jsp")
119: .forward(request, response);
120: return;
121: } else {
122: String action = request
123: .getParameter(ManageAccountProcessor.ACTION_PARAM);
124: UserAdminService service = UserAdminServiceLocator
125: .getUserAdminService();
126: String userName = request.getRemoteUser();
127: if (userName == null) {
128: response
129: .getWriter()
130: .write(
131: "You must be signed in to manage your account.");
132: return;
133: }
134:
135: try {
136: if (ManageAccountProcessor.ACTION_UPDATE_PROFILE
137: .equals(action)) {
138: User user = service.getUser(userName);
139: String fname = request
140: .getParameter(ManageAccountProcessor.FIRST_NAME_PARAM);
141: String lname = request
142: .getParameter(ManageAccountProcessor.LAST_NAME_PARAM);
143: String email = request
144: .getParameter(ManageAccountProcessor.EMAIL_PARAM);
145:
146: if (StringUtil.isNullOrEmpty(fname)) {
147: response.getWriter().write(
148: "You must provide your first name.");
149: return;
150: }
151: if (StringUtil.isNullOrEmpty(lname)) {
152: response.getWriter().write(
153: "You must provide your last name. ");
154: return;
155: }
156: if (StringUtil.isNullOrEmpty(email)
157: || !EmailValidator.getInstance().validate(
158: email, null, null)) {
159: response
160: .getWriter()
161: .write(
162: "You must provide a valid email address. ");
163: return;
164: }
165: user.setFirstName(fname);
166: user.setLastName(lname);
167: user.setPrimaryEmail(email);
168: Map profile = user.getProfile();
169: /*
170: for (int i=0; i<UserAdminService.BOOLEAN_USER_ATTRIBUTES.length; i++) {
171: setBooleanParam(UserAdminService.BOOLEAN_USER_ATTRIBUTES[i], request, profile);
172: }
173: */
174: for (int i = 0; i < UserAdminService.STRING_USER_ATTRIBUTES.length; i++) {
175: setStringParam(
176: UserAdminService.STRING_USER_ATTRIBUTES[i],
177: request, profile);
178: }
179: for (int i = 0; i < UserAdminService.INTEGER_USER_ATTRIBUTES.length; i++) {
180: setIntegerParam(
181: UserAdminService.INTEGER_USER_ATTRIBUTES[i],
182: request, profile);
183: }
184: try {
185: service.updateUserProfile(user);
186: sCache.setUserInfo(userName);
187: } catch (EntityExistsException e1) {
188: response.getWriter().write(
189: "The username is already exists.");
190: return;
191: } catch (EntityUniqueException e1) {
192: response.getWriter().write(
193: "The email address is already exists.");
194: return;
195: } catch (MissingRequiredAttributeException e1) {
196: response
197: .getWriter()
198: .write(
199: "One or more required fields is missing.");
200: return;
201: }
202: } else if (ManageAccountProcessor.ACTION_UPDATE_PREFERENCE
203: .equals(action)) {
204: String loc = request
205: .getParameter(ManageAccountProcessor.LOCALE_PARAM);
206: String tz = request
207: .getParameter(ManageAccountProcessor.TIMEZONE_PARAM);
208: boolean showEmail = request
209: .getParameter(ManageAccountProcessor.SHOW_EMAIL_PARAM) != null;
210: boolean showName = request
211: .getParameter(ManageAccountProcessor.SHOW_NAME_PARAM) != null;
212:
213: PortalInformationStore storeInfo = PortalInformationStoreLocator
214: .getPortalInformationStore();
215: UserPreferences userPrefs = storeInfo
216: .getUserPreferences(userName);
217: if (userPrefs == null)
218: userPrefs = new UserPreferences();
219: userPrefs.setName(userName);
220: userPrefs.setShowEmail(showEmail);
221: userPrefs.setShowName(showName);
222: if (StringUtil.isNullOrEmpty(tz))
223: userPrefs.setTimezone(null);
224: else
225: userPrefs.setTimezone(tz);
226: setUserLocale(loc, userPrefs);
227: storeInfo.saveUserPreferences(userPrefs);
228:
229: } else if (ManageAccountProcessor.ACTION_CHANGE_PASSWORD
230: .equals(action)) {
231: String oldPassword = request
232: .getParameter(ManageAccountProcessor.OLD_PASSWORD_PARAM);
233: if (StringUtil.isNullOrEmpty(oldPassword)) {
234: response
235: .getWriter()
236: .write(
237: "You must provide your current password.");
238: return;
239: }
240: String newPassword = request
241: .getParameter(ManageAccountProcessor.NEW_PASSWORD_PARAM);
242: if (StringUtil.isNullOrEmpty(newPassword)) {
243: response.getWriter().write(
244: "You must provide a new password.");
245: return;
246: }
247: String confirmPassword = request
248: .getParameter(ManageAccountProcessor.CONFIRM_PASSWORD_PARAM);
249: if (StringUtil.isNullOrEmpty(newPassword)) {
250: response
251: .getWriter()
252: .write(
253: "You must retype your new password in \"Confirm Password\" field.");
254: return;
255: }
256:
257: try {
258: AuthenticationServiceLocator
259: .getAuthenticationService()
260: .authenticateUserByName(userName,
261: oldPassword, new HashMap());
262: } catch (LoginException e) {
263: response.getWriter().write(
264: "Invalid current password.");
265: return;
266: }
267:
268: if (!newPassword.equals(confirmPassword)) {
269: response.getWriter().write(
270: "New password entries must match.");
271: return;
272: }
273: service.setPassword(userName, newPassword);
274: } else if (ManageAccountProcessor.ACTION_UPDATE_AVATAR
275: .equals(action)) {
276: uploadAvatar(request, response);
277: return;
278: }
279: } catch (Exception e) {
280: this .LOGGER
281: .error(
282: "Failed to obtain Stringbeans user instance",
283: e);
284: response.getWriter().write(systemError);
285: return;
286: }
287:
288: response.getWriter().write("ok");
289:
290: }
291: }
292:
293: public boolean isLoginRequired(PortalApplicationView portalAppView,
294: PortalPageState pageState) {
295: return true;
296: }
297:
298: /*
299: @SuppressWarnings("unchecked")
300: private void setBooleanParam(String paramName, HttpServletRequest request, Map profile) {
301: if (request.getParameter(paramName) != null) profile.put(paramName, Boolean.TRUE);
302: }
303: */
304: @SuppressWarnings("unchecked")
305: private void setStringParam(String paramName,
306: HttpServletRequest request, Map profile) {
307: String paramValue = request.getParameter(paramName);
308: if (StringUtil.isNotNullOrEmpty(paramValue))
309: profile.put(paramName, paramValue);
310: else
311: profile.remove(paramName);
312: }
313:
314: @SuppressWarnings("unchecked")
315: private void setIntegerParam(String paramName,
316: HttpServletRequest request, Map profile) {
317: String paramValue = request.getParameter(paramName);
318: if (StringUtil.isNotNullOrEmpty(paramValue))
319: profile.put(paramName, Integer.valueOf(paramValue));
320: else
321: profile.remove(paramName);
322: }
323:
324: private void setUserLocale(String loc, UserPreferences userPrefs) {
325: if (StringUtil.isNullOrEmpty(loc))
326: userPrefs.setPreferredLocale(null);
327: else {
328: String[] theLoc = StringUtil.split(loc, "_");
329: if (theLoc.length == 2) {
330: userPrefs.setPreferredLocale(new Locale(theLoc[0],
331: theLoc[1]));
332: } else {
333: userPrefs.setPreferredLocale(new Locale(theLoc[0]));
334: }
335: }
336: }
337:
338: public void uploadAvatar(HttpServletRequest request,
339: HttpServletResponse response) throws IOException {
340: try {
341: DiskFileItemFactory factory = new DiskFileItemFactory();
342: ServletFileUpload fileUpload = new ServletFileUpload(
343: factory);
344: List items = fileUpload.parseRequest(request);
345:
346: Iterator iter = items.iterator();
347: FileItem fileItem = null;
348: while (iter.hasNext()) {
349: fileItem = (FileItem) iter.next();
350: if (AVATAR_PARAM.equals(fileItem.getName()))
351: break;
352: }
353: String mimeType = fileItem.getContentType();
354: if (mimeType == null || !mimeType.startsWith("image")) {
355: throw new FileUploadException(
356: "Invalid mime type for image: " + mimeType);
357: }
358:
359: byte[] contents = fileItem.get();
360: PortalInformationStoreLocator.getPortalInformationStore()
361: .setUserIcon(request.getRemoteUser(), contents);
362: // response.getWriter().write("ok");
363: } catch (FileUploadException e) {
364: response.getWriter().write(e.getLocalizedMessage());
365: return;
366: } catch (RemoteException e) {
367: response
368: .getWriter()
369: .write(
370: "Your request cannot be processed at this time. Please try again later.");
371: }
372:
373: }
374:
375: }
|