📄 register.java
字号:
/* * 作者: 胡李青 * qq: 31703299 * Copyright (c) 2007 huliqing * 主页 http://www.tbuy.biz/ * 你可以免费使用该软件,未经许可请勿作用于任何商业目的 */package biz.tbuy.user.bean;import biz.tbuy.common.Constants;import biz.tbuy.common.Encoder;import biz.tbuy.common.MailOper;import biz.tbuy.common.Utils;import biz.tbuy.user.UserAction;import biz.tbuy.user.UserModel;import biz.tbuy.user.UserinfoAction;import biz.tbuy.user.UserinfoModel;import java.io.UnsupportedEncodingException;import java.security.NoSuchAlgorithmException;import javax.faces.application.FacesMessage;import javax.faces.component.UIComponent;import javax.faces.context.FacesContext;import javax.faces.validator.ValidatorException;import javax.servlet.http.HttpSession;/** * @author huliqing * <p><b>qq:</b>31703299 * <p><b>E-mail:</b><a href="mailto:huliqing.cn@gmail.com">huliqing.cn@gmail.com</a> * <p><b>Homepage:</b><a href="http://www.tbuy.biz/">http://www.tbuy.biz/</a> */public class Register extends BaseBean{ private boolean _register; // 是否允许注册新用户 private String id; private String password1; private String password2; private String email; private String validateCode; // 验证码,当系统启用了验证码功能时使用 public Register() { // 检查系统是否允许注册新用户 _register = getComApplication().isRegister(); if (!_register) { Utils.addErrorMessage(getBundle(), "对不起,系统已经关闭了用户注册!"); } } public boolean isRegister() { return _register; } public void setId(String id) { this.id = id; } public String getId() { return id; } public void setPassword1(String password1) { this.password1 = password1; } public String getPassword1() { return password1; } public void setPassword2(String password2) { this.password2 = password2; } public String getPassword2() { return password2; } public void setEmail(String email) { this.email = email; } public String getEmail() { return email; } public void setValidateCode(String validateCode) { this.validateCode = validateCode; } public String getValidateCode() { return validateCode; } /** * method * ***********************************************************************/ /** * @return String */ public String newMember() throws NoSuchAlgorithmException, UnsupportedEncodingException{ // 如果系统已经关闭了注册新用户,则发出警告并直接返回 if (!_register) { Utils.addErrorMessage(getBundle(), "对不起,系统已经关闭了用户注册!"); return Constants.OUT_FAILURE; } // 检查两次密码输入是否一致 if (!isEqualsPassword()) { Utils.addErrorMessage(getBundle(), "register_passwordDiffer"); return Constants.OUT_FAILURE; } // 检相验证码,如果需要注册验证 if (getComApplication().isValidateRegister()) { HttpSession session = (HttpSession)getFacesContext(). getExternalContext().getSession(false); if (validateCode == null || !validateCode.toUpperCase().equals(session.getAttribute("validateCode"))) { Utils.addErrorMessage(getBundle(), "register_validateCodeError"); return Constants.OUT_FAILURE; } } // 检查ID是否已经存在于数据库中 if (isExistUserId()) { Utils.addErrorMessage(getBundle(), "register_idIsHave"); return Constants.OUT_FAILURE; } // 注册新用户 if (addMember()) { Utils.addInfoMessage(getBundle(), "register_regSuccess"); } return Constants.OUT_SUCCESS; } /** * @return boolean */ private boolean isEqualsPassword() { return password1.equals(password2); } /** * @return boolean */ private boolean isExistUserId() { return UserAction.isExistUserId(id); } /** * 注册新用户!该方法会考虑以下几点: * 1.将用户的密码password进行MD5加密<br> * 2.检查系统是否开启了邮件认证,如果开启,则将激活状态设为false,否则为true * @return true, 如果注册成功,否则返回false; */ private boolean addMember() throws NoSuchAlgorithmException, UnsupportedEncodingException { boolean isOk = false; UserModel user = new UserModel(); UserinfoModel userinfo = new UserinfoModel(); user.setId(id); user.setPassword(Encoder.encodeByMd5(password1));// 将密码进行MD5加密 if (getComApplication().isEmailRegister()) { user.setEnabled(false); } else { user.setEnabled(true); } // 创建用户的详细信息 if (UserAction.addMember(user)) { userinfo.setByUser(id); userinfo.setEmail(email); isOk = UserinfoAction.addUserinfo(userinfo); } // 检查是否需要发送确认邮件,如果添加用户成功,同时开启了email认证,则发邮件 // 否则使用户自动登录 if (isOk) { if (getComApplication().isEmailRegister()) { sendEmail(user, email); } else { getComApplication().getVisitorsOper().login(user, true); } getComApplication().reloadUserinfo(); // 重新载入系统用户信息 } return isOk; } /** * 当系统开启了email认证时,发送一封确认邮件到用户的邮箱中。 */ private void sendEmail(UserModel user, String to) { MailOper mOper = new MailOper(); if (mOper.sendEnableEmail(user, to)) { Utils.addInfoMessage(getBundle(), "register_sendEmail"); } else { Utils.addErrorMessage(getBundle(), "register_sendEmailError"); } } /** * validator * ************************************************************************/ /** * 检查用户的注册id格式是否符合要求 * @param facesContext * @param component * @param value */ public void idFormatValidator(FacesContext facesContext, UIComponent component, Object value) { String tid = (String) value; String idAllow = "^[_a-zA-Z]+[_a-zA-Z0-9]*$"; if(!tid.matches(idAllow)) { String text = Utils.getDisplayString(getBundle(), "register_idFormatError", null); FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", text); throw new ValidatorException(message); } } /** * 检查用户注册的电子邮箱格式是否标准 * @param facesContext * @param component * @param value */ public void emailFormatValidator(FacesContext facesContext, UIComponent component, Object value) { String myMail = (String) value; String emailAllow = "^[_a-zA-Z0-9]+[\\._a-zA-Z0-9]*@[_a-zA-Z0-9]+[\\.a-zA-Z]*\\.[a-zA-Z]{1,4}$"; if(!myMail.matches(emailAllow)) { String text = Utils.getDisplayString(getBundle(), "register_emailFormatError", null); FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", text); throw new ValidatorException(message); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -