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.portlet.login;
020:
021: import java.io.IOException;
022:
023: import javax.mail.MessagingException;
024: import javax.mail.internet.AddressException;
025: import javax.portlet.ActionRequest;
026: import javax.portlet.ActionResponse;
027: import javax.portlet.PortletException;
028:
029: import com.nabhinc.portal.core.LocaleUtil;
030: import com.nabhinc.portal.core.PortalUtil;
031: import com.nabhinc.portal.login.LoginConstants;
032: import com.nabhinc.portal.model.PortalConfiguration;
033: import com.nabhinc.portal.spi.UserAdminServiceLocator;
034: import com.nabhinc.portlet.mvcportlet.core.ActionConfig;
035: import com.nabhinc.portlet.mvcportlet.core.ActionProcessor;
036: import com.nabhinc.portlet.mvcportlet.core.BaseRequestProcessor;
037: import com.nabhinc.spi.NoSuchEntityException;
038: import com.nabhinc.spi.User;
039: import com.nabhinc.spi.UserAdminService;
040: import com.nabhinc.util.EmailUtil;
041: import com.nabhinc.util.RandomKeyGenerator;
042: import com.nabhinc.util.StringUtil;
043:
044: /**
045: *
046: *
047: * @author Padmanabh Dabke
048: * (c) 2006 Nabh Information Systems, Inc. All Rights Reserved.
049: */
050: public class ResetPassword extends BaseRequestProcessor implements
051: ActionProcessor {
052:
053: public String process(ActionRequest request,
054: ActionResponse response, ActionConfig actionConfig)
055: throws PortletException, IOException {
056: //for temporary password
057: int MIN_PASSWORD_LENGTH = LoginConstants.PASSWORD_MIN_LENGTH;
058: int MAX_PASSWORD_LENGTH = LoginConstants.PASSWORD_MAX_LENGTH;
059: String password = RandomKeyGenerator.generateAlphaNumericKey(
060: (double) MIN_PASSWORD_LENGTH,
061: (double) MAX_PASSWORD_LENGTH);
062:
063: String userName = request.getParameter("username");
064: String userEmail = request.getParameter("oemail");
065:
066: UserAdminService adminService = UserAdminServiceLocator
067: .getUserAdminService();
068: if (StringUtil.isNullOrEmpty(userName)) {
069: try {
070: userName = adminService.getUserNameFromEmail(userEmail);
071: } catch (NoSuchEntityException e) {
072: return "user-not-found";
073: }
074: } else {
075: User user;
076: try {
077: user = adminService.getUser(userName);
078: } catch (NoSuchEntityException e) {
079: return "user-not-found";
080: }
081: userEmail = user.getPrimaryEmail();
082: }
083:
084: adminService.setPassword(userName, password);
085:
086: try {
087: sendConfirmationEmail(request, userEmail, userName,
088: password);
089:
090: } catch (AddressException e) {
091: return "invalid-address";
092: } catch (MessagingException e) {
093: this .brpLog.error(
094: "Failed to send password reset email for user "
095: + userName, e);
096: return "email-failure";
097: }
098:
099: return "success";
100: }
101:
102: private void sendConfirmationEmail(ActionRequest request,
103: String emailAddress, String userName, String password)
104: throws AddressException, MessagingException {
105:
106: PortalConfiguration pConfig = PortalConfiguration.getInstance();
107: String[] localeLabels = LocaleUtil
108: .getLocalizedStringArray(request.getLocale());
109:
110: String emailSubject = localeLabels[LocaleUtil.RESET_PASSWORD_EMAIL_SUBJECT];
111:
112: String message = localeLabels[LocaleUtil.RESET_PASSWORD_EMAIL_MESSAGE]
113: .replaceAll("\\$SERVER_URL", PortalUtil
114: .getPortalURLPrefix(request))
115: + localeLabels[LocaleUtil.TEMPORARY_PASSWORD]
116: + password
117: + (pConfig.isSendUsername() ? localeLabels[LocaleUtil.REQUESTED_USERNAME]
118: + userName
119: : "")
120: + localeLabels[LocaleUtil.RESET_PASSWORD_EMAIL_NOTES]
121: + localeLabels[LocaleUtil.EMAIL_REGARDS];
122:
123: EmailUtil.send(pConfig.getPortalAdminEmail(), emailAddress,
124: emailSubject, message.toString());
125:
126: }
127:
128: }
|