⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 useraction.java

📁 个人认为是最好的Java论坛源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Copyright (c) 2003, 2004 Rafael Steil
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, 
 * with or without modification, are permitted provided 
 * that the following conditions are met:
 * 
 * 1) Redistributions of source code must retain the above 
 * copyright notice, this list of conditions and the 
 * following  disclaimer.
 * 2)  Redistributions in binary form must reproduce the 
 * above copyright notice, this list of conditions and 
 * the following disclaimer in the documentation and/or 
 * other materials provided with the distribution.
 * 3) Neither the name of "Rafael Steil" nor 
 * the names of its contributors may be used to endorse 
 * or promote products derived from this software without 
 * specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT 
 * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
 * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
 * IN CONTRACT, STRICT LIABILITY, OR TORT 
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
 * 
 * This file creation date: May 12, 2003 / 8:31:25 PM
 * The JForum Project
 * http://www.jforum.net
 */
package net.jforum.view.forum;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;

import net.jforum.Command;
import net.jforum.JForum;
import net.jforum.SessionFacade;
import net.jforum.entities.User;
import net.jforum.entities.UserSession;
import net.jforum.model.DataAccessDriver;
import net.jforum.model.UserModel;
import net.jforum.model.UserSessionModel;
import net.jforum.repository.RankingRepository;
import net.jforum.repository.SecurityRepository;
import net.jforum.security.SecurityConstants;
import net.jforum.util.I18n;
import net.jforum.util.MD5;
import net.jforum.util.concurrent.executor.QueuedExecutor;
import net.jforum.util.mail.ActivationKeySpammer;
import net.jforum.util.mail.EmailException;
import net.jforum.util.mail.EmailSenderTask;
import net.jforum.util.mail.LostPasswordSpammer;
import net.jforum.util.preferences.ConfigKeys;
import net.jforum.util.preferences.SystemGlobals;
import net.jforum.view.forum.common.UserCommon;
import net.jforum.view.forum.common.ViewCommon;

/**
 * @author Rafael Steil
 * @version $Id: UserAction.java,v 1.34 2005/03/07 23:12:53 rafaelsteil Exp $
 */
public class UserAction extends Command 
{
	private static final Logger logger = Logger.getLogger(UserAction.class);
	
	public void edit() throws Exception 
	{
		int tmpId = SessionFacade.getUserSession().getUserId();
		if (SessionFacade.isLogged() 
				&& tmpId == this.request.getIntParameter("user_id")) {
			int userId = this.request.getIntParameter("user_id");
			UserModel um = DataAccessDriver.getInstance().newUserModel();
			User u = um.selectById(userId);

			this.context.put("u", u);
			this.context.put("action", "editSave");
			this.context.put("moduleAction", "user_form.htm");
		} 
		else {
			this.profile();
		}
	}

	public void editDone() throws Exception
	{
		this.context.put("editDone", true);
		this.edit();
	}

	public void editSave() throws Exception 
	{
		int userId = this.request.getIntParameter("user_id");
		List warns = UserCommon.saveUser(userId);

		if (warns.size() > 0) {
			this.context.put("warns", warns);
			this.edit();
		} 
		else {
			JForum.setRedirect(this.request.getContextPath()
					+ "/user/editDone/" + userId
					+ SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION));
		}
	}
	
	private void registrationDisabled()
	{
		this.context.put("moduleAction", "message.htm");
		this.context.put("message", I18n.getMessage("User.registrationDisabled"));
	}

	public void insert() 
	{
		if (!SystemGlobals.getBoolValue(ConfigKeys.REGISTRATION_ENABLED)) {
			this.registrationDisabled();
			return;
		}
		
		this.context.put("action", "insertSave");
		this.context.put("moduleAction", "user_new.htm");
		this.context.put("username", this.request.getParameter("username"));
		this.context.put("email", this.request.getParameter("email"));
		
		if (SystemGlobals.getBoolValue(ConfigKeys.CAPTCHA_REGISTRATION)){
			//create a new image captcha
			SessionFacade.getUserSession().createNewCaptcha();
			this.context.put("captcha_reg", true);
		}
	}

	public void insertSave() throws Exception 
	{
		if (!SystemGlobals.getBoolValue(ConfigKeys.REGISTRATION_ENABLED)) {
			this.registrationDisabled();
			return;
		}
		
		User u = new User();
		UserModel um = DataAccessDriver.getInstance().newUserModel();

		String username = this.request.getParameter("username");
		String password = this.request.getParameter("password");
		String captchaResponse = this.request.getParameter("captchaResponse");

		boolean error = false;
		if (username == null || username.trim().equals("") 
				|| password == null || password.trim().equals("")) {
			this.context.put("error", I18n.getMessage("UsernamePasswordCannotBeNull"));
			error = true;
		}
		
		if (!error && username.length() > SystemGlobals.getIntValue(ConfigKeys.USERNAME_MAX_LENGTH)) {
			this.context.put("error", I18n.getMessage("User.usernameTooBig"));
			error = true;
		}
		
		if (!error && username.indexOf('<') > -1 || username.indexOf('>') > -1) {
			this.context.put("error", I18n.getMessage("User.usernameInvalidChars"));
			error = true;
		}

		if (!error && um.isUsernameRegistered(this.request.getParameter("username"))) {
			this.context.put("error", I18n.getMessage("UsernameExists"));
			error = true;
		}
		
		if (!error && !SessionFacade.getUserSession().validateCaptchaResponse(captchaResponse)){
			this.context.put("error", I18n.getMessage("CaptchaResponseFails"));
			error = true;
		}

		if (error) {
			this.insert();

			return;
		}

		u.setUsername(username);
		u.setPassword(MD5.crypt(password));
		u.setEmail(this.request.getParameter("email"));

		if (SystemGlobals.getBoolValue(ConfigKeys.MAIL_USER_EMAIL_AUTH)) {
			u.setActivationKey(MD5.crypt(username + System.currentTimeMillis()));
		}

		int userId = um.addNew(u);

		if (SystemGlobals.getBoolValue(ConfigKeys.MAIL_USER_EMAIL_AUTH)) {
			try {
				//Send an email to new user
				QueuedExecutor.getInstance().execute(
						new EmailSenderTask(new ActivationKeySpammer(u)));
			} 
			catch (Exception e) {
				logger.warn("Error while trying to send an email: " + e);
				e.printStackTrace();
			}

			this.context.put("moduleAction", "message.htm");
			this.context.put("message", I18n.getMessage("User.GoActivateAccountMessage"));
		} 
		else {
			this.logNewRegisteredUserIn(userId, u);
		}
	}

	public void activateAccount() throws Exception 
	{
		String hash = this.request.getParameter("hash");
		int userId = (new Integer(this.request.getParameter("user_id"))).intValue();

		String message = "";

		UserModel um = DataAccessDriver.getInstance().newUserModel();
		User u = um.selectById(userId);

		boolean isOk = um.validateActivationKeyHash(userId, hash);
		if (isOk) {
			// make account active
			um.writeUserActive(userId);
			this.logNewRegisteredUserIn(userId, u);
		} 
		else {
			message = I18n.getMessage("User.invalidActivationKey");
			this.context.put("moduleAction", "message.htm");
			this.context.put("message", message);
		}

	}

	private void logNewRegisteredUserIn(int userId, User u) 
	{
		SessionFacade.setAttribute("logged", "1");

		UserSession userSession = new UserSession();
		userSession.setAutoLogin(true);
		userSession.setUserId(userId);
		userSession.setUsername(u.getUsername());
		userSession.setLastVisit(new Date(System.currentTimeMillis()));
		userSession.setStartTime(new Date(System.currentTimeMillis()));

		SessionFacade.add(userSession);

		// Finalizing.. show to user the congrats page
		JForum.setRedirect(this.request.getContextPath()
				+ "/user/registrationComplete"
				+ SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION));
	}

	public void registrationComplete() throws Exception 
	{
		int userId = SessionFacade.getUserSession().getUserId();

		String profilePage = JForum.encodeUrlWithPathAndExtension("/user/edit/" + userId);
		String homePage = JForum.encodeUrlWithPathAndExtension("/forums/list");

		String message = I18n.getMessage("User.RegistrationCompleteMessage", 
				new Object[] { profilePage, homePage });
		this.context.put("message", message);
		this.context.put("moduleAction", "registration_complete.htm");
	}

	public void validateLogin() throws Exception 
	{
		boolean validInfo = false;

		String password = this.request.getParameter("password");
		if (password.length() > 0) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -