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: import java.rmi.RemoteException;
023: import java.util.HashMap;
024: import java.util.List;
025:
026: import javax.mail.MessagingException;
027: import javax.mail.internet.AddressException;
028: import javax.portlet.ActionRequest;
029: import javax.portlet.ActionResponse;
030: import javax.portlet.PortletException;
031:
032: import org.w3c.dom.Element;
033:
034: import com.nabhinc.core.Constants;
035: import com.nabhinc.portal.core.LocaleUtil;
036: import com.nabhinc.portal.core.PortalUtil;
037: import com.nabhinc.portal.login.LoginConstants;
038: import com.nabhinc.portal.model.PortalConfiguration;
039: import com.nabhinc.portal.spi.RoleAdminServiceLocator;
040: import com.nabhinc.portal.spi.UserAdminServiceLocator;
041: import com.nabhinc.portlet.mvcportlet.core.ActionConfig;
042: import com.nabhinc.portlet.mvcportlet.core.ActionProcessor;
043: import com.nabhinc.portlet.mvcportlet.core.BaseRequestProcessor;
044: import com.nabhinc.portlet.mvcportlet.core.ControllerPortletConfig;
045: import com.nabhinc.spi.EntityExistsException;
046: import com.nabhinc.spi.EntityUniqueException;
047: import com.nabhinc.spi.MissingRequiredAttributeException;
048: import com.nabhinc.spi.RoleAdminService;
049: import com.nabhinc.spi.UserAdminService;
050: import com.nabhinc.spi.UserImpl;
051: import com.nabhinc.spi.UserRole;
052: import com.nabhinc.util.EmailUtil;
053: import com.nabhinc.util.RandomKeyGenerator;
054: import com.nabhinc.util.StringUtil;
055: import com.nabhinc.util.XMLUtil;
056:
057: /**
058: *
059: *
060: * @author Padmanabh Dabke
061: * (c) 2006 Nabh Information Systems, Inc. All Rights Reserved.
062: */
063: public class RegisterUser extends BaseRequestProcessor implements
064: ActionProcessor {
065: public static final int[] DEFAULT_USER_ROLES = { Constants.USER_ROLE_ID };
066: private int[] ruRoles = DEFAULT_USER_ROLES;
067: private boolean ruRollbackOnError = false;
068:
069: public void init(Element config,
070: ControllerPortletConfig controllerConfig)
071: throws PortletException {
072: super .init(config, controllerConfig);
073: String roles = XMLUtil.getSubElementText(config, "roles");
074: if (roles != null) {
075: String[] temp = StringUtil.split(roles, ",");
076: if (temp.length > 0) {
077: List roleList = getRoleList();
078: if (roleList == null)
079: throw new PortletException(
080: "Unable to obtain roles.");
081: ruRoles = new int[temp.length];
082: for (int i = 0; i < temp.length; i++) {
083: for (int j = 0; j < roleList.size(); j++) {
084: // case-sensitive roles
085: UserRole aRole = (UserRole) roleList.get(j);
086: if (temp[i].equals(aRole.getName())) {
087: ruRoles[i] = aRole.getId();
088: break;
089: }
090: }
091: }
092: }
093: }
094: String rollback = (XMLUtil.getSubElementText(config,
095: "rollback-on-error"));
096: if ("true".equals(rollback))
097: ruRollbackOnError = true;
098:
099: }
100:
101: @SuppressWarnings("unchecked")
102: public String process(ActionRequest request,
103: ActionResponse response, ActionConfig actionConfig)
104: throws PortletException, IOException {
105:
106: //for temporary password
107: int MIN_PASSWORD_LENGTH = LoginConstants.PASSWORD_MIN_LENGTH;
108: int MAX_PASSWORD_LENGTH = LoginConstants.PASSWORD_MAX_LENGTH;
109: String password = RandomKeyGenerator.generateAlphaNumericKey(
110: (double) MIN_PASSWORD_LENGTH,
111: (double) MAX_PASSWORD_LENGTH);
112:
113: String userName = request.getParameter("username");
114: String userEmail = request.getParameter("oemail");
115: String fName = request.getParameter("fname");
116: String lName = request.getParameter("lname");
117: String phone = request.getParameter("ophone");
118:
119: UserAdminService adminService = UserAdminServiceLocator
120: .getUserAdminService();
121:
122: UserImpl user = new UserImpl();
123: user.setFirstName(fName);
124: user.setLastName(lName);
125: user.setUserName(userName);
126: user.setPrimaryEmail(userEmail);
127:
128: HashMap profileMap = new HashMap();
129: profileMap.put("ophone", phone);
130: user.setProfile(profileMap);
131:
132: try {
133: adminService.createUser(user, password, ruRoles);
134: sendConfirmationEmail(request, userEmail, fName, lName,
135: password);
136:
137: } catch (AddressException e) {
138: if (ruRollbackOnError)
139: adminService.deleteUsers(new String[] { userName }); // rollback
140: return "invalid-address";
141: } catch (MessagingException e) {
142: if (ruRollbackOnError)
143: adminService.deleteUsers(new String[] { userName }); //rollback
144: this .brpLog.error(
145: "Failed to send new user registration email for user "
146: + userName, e);
147: return "email-failure";
148: } catch (EntityExistsException e1) {
149: return "user-exists";
150: } catch (MissingRequiredAttributeException e1) {
151: return "missing-data";
152: } catch (EntityUniqueException ex) {
153: return "email-exists";
154: }
155:
156: return "success";
157: }
158:
159: private List getRoleList() throws PortletException {
160: RoleAdminService adminService = RoleAdminServiceLocator
161: .getRoleAdminService();
162: if (adminService == null) {
163: throw new PortletException(
164: "Cannot retrieve roles since role admin service is not running");
165: }
166: List roleList = null;
167: try {
168: roleList = adminService.getUserRoles(0, Integer.MAX_VALUE,
169: "rolename", false);
170: } catch (RemoteException ex) {
171: throw new PortletException(
172: "Failed to retrieve role information.", ex);
173: }
174:
175: return roleList;
176: }
177:
178: private void sendConfirmationEmail(ActionRequest request,
179: String emailAddress, String fName, String lName,
180: String password) throws AddressException,
181: MessagingException {
182:
183: String[] localeLabels = LocaleUtil
184: .getLocalizedStringArray(request.getLocale());
185:
186: String emailSubject = localeLabels[LocaleUtil.NEW_USER_EMAIL_SUBJECT]
187: .replaceFirst("\\$FIRST_NAME", fName);
188:
189: String message = localeLabels[LocaleUtil.NEW_USER_EMAIL_MESSAGE]
190: .replaceFirst("\\$FIRST_NAME", fName);
191: message = message.replaceAll("\\$SERVER_URL", PortalUtil
192: .getPortalURLPrefix(request))
193: + localeLabels[LocaleUtil.TEMPORARY_PASSWORD]
194: + password
195: + localeLabels[LocaleUtil.NEW_USER_EMAIL_NOTES]
196: + localeLabels[LocaleUtil.EMAIL_REGARDS];
197:
198: EmailUtil.send(PortalConfiguration.getInstance()
199: .getPortalAdminEmail(), emailAddress, emailSubject,
200: message.toString());
201:
202: }
203:
204: }
|