📄 signupportlet.java
字号:
package org.gridsphere.portlets.core.registration;import org.gridsphere.portlet.impl.PortletURLImpl;import org.gridsphere.portlet.service.PortletServiceException;import org.gridsphere.provider.event.jsr.ActionFormEvent;import org.gridsphere.provider.event.jsr.RenderFormEvent;import org.gridsphere.provider.portlet.jsr.ActionPortlet;import org.gridsphere.provider.portletui.beans.MessageBoxBean;import org.gridsphere.provider.portletui.beans.TextFieldBean;import org.gridsphere.services.core.mail.MailMessage;import org.gridsphere.services.core.mail.MailService;import org.gridsphere.services.core.portal.PortalConfigService;import org.gridsphere.services.core.request.Request;import org.gridsphere.services.core.request.RequestService;import org.gridsphere.services.core.security.password.PasswordEditor;import org.gridsphere.services.core.security.password.PasswordManagerService;import org.gridsphere.services.core.security.role.PortletRole;import org.gridsphere.services.core.security.role.RoleManagerService;import org.gridsphere.services.core.user.User;import org.gridsphere.services.core.user.UserManagerService;import javax.portlet.*;import javax.servlet.http.HttpServletRequest;import java.util.*;public class SignupPortlet extends ActionPortlet { private static String ACTIVATE_ACCOUNT_LABEL = "activateaccount"; private static long REQUEST_LIFETIME = 1000 * 60 * 60 * 24 * 3; // 3 days public static final String LOGIN_ERROR_FLAG = "LOGIN_FAILED"; public static final Integer LOGIN_ERROR_UNKNOWN = new Integer(-1); public static final String DO_VIEW_USER_EDIT_LOGIN = "signup/createaccount.jsp"; //edit user private UserManagerService userManagerService = null; private RoleManagerService roleService = null; private PasswordManagerService passwordManagerService = null; private PortalConfigService portalConfigService = null; private RequestService requestService = null; private MailService mailService = null; private String activateAccountURL = null; private String denyAccountURL = null; private String notificationURL = null; public void init(PortletConfig config) throws PortletException { super.init(config); userManagerService = (UserManagerService) createPortletService(UserManagerService.class); roleService = (RoleManagerService) createPortletService(RoleManagerService.class); passwordManagerService = (PasswordManagerService) createPortletService(PasswordManagerService.class); requestService = (RequestService) createPortletService(RequestService.class); mailService = (MailService) createPortletService(MailService.class); portalConfigService = (PortalConfigService) createPortletService(PortalConfigService.class); DEFAULT_VIEW_PAGE = "doNewUser"; } protected String getLocalizedText(HttpServletRequest req, String key) { Locale locale = req.getLocale(); ResourceBundle bundle = ResourceBundle.getBundle("gridsphere.resources.Portlet", locale); return bundle.getString(key); } public void cantRead(ActionFormEvent evt) { TextFieldBean userName = evt.getTextFieldBean("userName"); userName.setValue(userName.getValue()); TextFieldBean firstName = evt.getTextFieldBean("firstName"); firstName.setValue(firstName.getValue()); TextFieldBean lastName = evt.getTextFieldBean("lastName"); lastName.setValue(lastName.getValue()); TextFieldBean emailAddress = evt.getTextFieldBean("emailAddress"); emailAddress.setValue(emailAddress.getValue()); TextFieldBean organization = evt.getTextFieldBean("organization"); organization.setValue(organization.getValue()); TextFieldBean password = evt.getPasswordBean("password"); password.setValue(password.getValue()); TextFieldBean password2 = evt.getPasswordBean("confirmPassword"); password2.setValue(password2.getValue()); setNextState(evt.getActionRequest(), DEFAULT_VIEW_PAGE); } public void doNewUser(RenderFormEvent evt) throws PortletException { RenderRequest req = evt.getRenderRequest(); RenderResponse res = evt.getRenderResponse(); if (notificationURL == null) { notificationURL = res.createActionURL().toString(); } if (activateAccountURL == null) { PortletURL accountURL = res.createActionURL(); ((PortletURLImpl) accountURL).setAction("approveAccount"); ((PortletURLImpl) accountURL).setLayout("register"); ((PortletURLImpl) accountURL).setEncoding(false); activateAccountURL = accountURL.toString(); } if (denyAccountURL == null) { PortletURL denyURL = res.createActionURL(); ((PortletURLImpl) denyURL).setAction("denyAccount"); ((PortletURLImpl) denyURL).setLayout("register"); ((PortletURLImpl) denyURL).setEncoding(false); denyAccountURL = denyURL.toString(); } boolean canUserCreateAccount = Boolean.valueOf(portalConfigService.getProperty(PortalConfigService.CAN_USER_CREATE_ACCOUNT)).booleanValue(); if (!canUserCreateAccount) return; MessageBoxBean msg = evt.getMessageBoxBean("msg"); String savePasswds = portalConfigService.getProperty(PortalConfigService.SAVE_PASSWORDS); if (savePasswds.equals(Boolean.TRUE.toString())) { req.setAttribute("savePass", "true"); } String error = (String) req.getPortletSession(true).getAttribute("error"); if (error != null) { msg.setValue(error); req.getPortletSession(true).removeAttribute("error"); } else { String adminApproval = portalConfigService.getProperty("ADMIN_ACCOUNT_APPROVAL"); if (adminApproval.equals(Boolean.TRUE.toString())) { msg.setKey("LOGIN_ACCOUNT_CREATE_APPROVAL"); } else { msg.setKey("LOGIN_CREATE_ACCT"); } } setNextState(req, DO_VIEW_USER_EDIT_LOGIN); log.debug("in doViewNewUser"); } public void doSaveAccount(ActionFormEvent evt) throws PortletException { ActionRequest req = evt.getActionRequest(); String savePasswds = portalConfigService.getProperty(PortalConfigService.SAVE_PASSWORDS); if (savePasswds.equals(Boolean.TRUE.toString())) { req.setAttribute("savePass", "true"); } boolean canUserCreateAccount = Boolean.valueOf(portalConfigService.getProperty(PortalConfigService.CAN_USER_CREATE_ACCOUNT)).booleanValue(); if (!canUserCreateAccount) return; try { //check if the user is new or not validateUser(evt); //new and valid user and will save it notifyNewUser(evt); setNextState(req, "doConfirmSave"); } catch (PortletException e) { //invalid user, an exception was thrown //back to edit log.error("Could not create account: ", e); req.getPortletSession(true).setAttribute("error", e.getMessage()); setNextState(req, DEFAULT_VIEW_PAGE); } } public void doConfirmSave(RenderFormEvent evt) { MessageBoxBean msg = evt.getMessageBoxBean("msg"); msg.setKey("SIGNUP_CONFIRM"); setNextState(evt.getRenderRequest(), "signup/confirmsave.jsp"); } private void validateUser(ActionFormEvent event) throws PortletException { log.debug("Entering validateUser()"); PortletRequest req = event.getActionRequest(); // Validate user name String userName = event.getTextFieldBean("userName").getValue(); if (userName.equals("")) { createErrorMessage(event, this.getLocalizedText(req, "USER_NAME_BLANK") + "<br />"); throw new PortletException("user name is blank!"); } if (this.userManagerService.existsUserName(userName)) { createErrorMessage(event, this.getLocalizedText(req, "USER_EXISTS") + "<br />"); throw new PortletException("user exists already"); } // Validate full name String firstName = event.getTextFieldBean("firstName").getValue(); if (firstName.equals("")) { createErrorMessage(event, this.getLocalizedText(req, "USER_GIVENNAME_BLANK") + "<br />"); throw new PortletException("first name is blank"); } String lastName = event.getTextFieldBean("lastName").getValue(); if (lastName.equals("")) { createErrorMessage(event, this.getLocalizedText(req, "USER_FAMILYNAME_BLANK") + "<br />"); throw new PortletException("last name is blank"); } // Validate e-mail String eMail = event.getTextFieldBean("emailAddress").getValue(); if (eMail.equals("")) { createErrorMessage(event, this.getLocalizedText(req, "USER_NEED_EMAIL") + "<br />"); throw new PortletException("email is blank"); } else if ((eMail.indexOf("@") < 0)) { createErrorMessage(event, this.getLocalizedText(req, "USER_NEED_EMAIL") + "<br />"); throw new PortletException("email address invalid"); } else if ((eMail.indexOf(".") < 0)) { createErrorMessage(event, this.getLocalizedText(req, "USER_NEED_EMAIL") + "<br />"); throw new PortletException("email address invalid"); } //Validate password String savePasswds = portalConfigService.getProperty(PortalConfigService.SAVE_PASSWORDS); if (savePasswds.equals(Boolean.TRUE.toString())) { if (isInvalidPassword(event)) throw new PortletException("password no good!"); } //retrieve the response String response = event.getTextFieldBean("captchaTF").getValue(); String captchaValue = (String) req.getPortletSession(true).getAttribute(nl.captcha.servlet.Constants.SIMPLE_CAPCHA_SESSION_KEY, PortletSession.APPLICATION_SCOPE); if (!response.equals(captchaValue)) { createErrorMessage(event, this.getLocalizedText(req, "USER_CAPTCHA_MISMATCH")); throw new PortletException("captcha challenge mismatch!"); } log.debug("Exiting validateUser()"); } private boolean isInvalidPassword(ActionFormEvent event) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -