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

📄 useraction.java

📁 个人认为是最好的Java论坛源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			User user = this.validateLogin(this.request.getParameter("username"), password);

			if (user != null) {
				JForum.setRedirect(this.request.getContextPath()
						+ "/forums/list"
						+ SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION));

				SessionFacade.setAttribute("logged", "1");
				
				UserSession tmpUs = null;
				String sessionId = SessionFacade.isUserInSession(user.getId());
				
				UserSession userSession = SessionFacade.getUserSession();
				userSession.dataToUser(user);

				// Check if the user is returning to the system
				// before its last session has expired ( hypothesis )
				if (sessionId != null) {
					// Write its old session data
					SessionFacade.storeSessionData(sessionId, JForum.getConnection());
					tmpUs = SessionFacade.getUserSession(sessionId);
					SessionFacade.remove(sessionId);
				}
				else {
					UserSessionModel sm = DataAccessDriver.getInstance().newUserSessionModel();
					tmpUs = sm.selectById(userSession, JForum.getConnection());
				}

				I18n.load(user.getLang());

				// Autologin
				if (this.request.getParameter("autologin") != null) {
					userSession.setAutoLogin(true);
					JForum.addCookie(SystemGlobals.getValue(ConfigKeys.COOKIE_AUTO_LOGIN), "1");
					JForum.addCookie(SystemGlobals.getValue(ConfigKeys.COOKIE_USER_HASH), 
							MD5.crypt(SystemGlobals.getValue(ConfigKeys.USER_HASH_SEQUENCE) + user.getId()));
				}
				else {
					// Remove cookies for safety
					JForum.addCookie(SystemGlobals.getValue(ConfigKeys.COOKIE_USER_HASH), null);
					JForum.addCookie(SystemGlobals.getValue(ConfigKeys.COOKIE_AUTO_LOGIN), null);
				}
				
				// TODO: copy'n paste from JForum.java. Move all to an helper class
				if (tmpUs == null) {
					userSession.setLastVisit(new Date(System.currentTimeMillis()));
				}
				else {
					// Update last visit and session start time
					userSession.setLastVisit(new Date(tmpUs.getStartTime().getTime() + tmpUs.getSessionTime()));
				}
				
				SessionFacade.add(userSession);
				SessionFacade.setAttribute(ConfigKeys.TOPICS_TRACKING, new HashMap());

				JForum.addCookie(SystemGlobals.getValue(ConfigKeys.COOKIE_NAME_DATA), 
						Integer.toString(user.getId()));

				SecurityRepository.load(user.getId(), true);
				validInfo = true;
			}
		}

		// Invalid login
		if (validInfo == false) {
			this.context.put("invalidLogin", "1");
			this.context.put("moduleAction", "forum_login.htm");

			if (this.request.getParameter("returnPath") != null) {
				this.context.put("returnPath",
						this.request.getParameter("returnPath"));
			}
		} 
		else if (ViewCommon.needReprocessRequest()) {
			ViewCommon.reprocessRequest();
		}
		else if (this.request.getParameter("returnPath") != null) {
			JForum.setRedirect(this.request.getParameter("returnPath"));
		}
	}

	private User validateLogin(String name, String password) throws Exception 
	{
		UserModel um = DataAccessDriver.getInstance().newUserModel();
		User user = um.validateLogin(name, password);
		return user;
	}

	public void profile() throws Exception 
	{
		UserModel um = DataAccessDriver.getInstance().newUserModel();

		User u = um.selectById(this.request.getIntParameter("user_id"));
		if (u.getId() == 0) {
			this.userNotFound();
		}
		else {
			this.context.put("moduleAction", "user_profile.htm");
			this.context.put("karmaEnabled", SecurityRepository.canAccess(SecurityConstants.PERM_KARMA_ENABLED));
			this.context.put("rank", new RankingRepository());
			this.context.put("u", u);
		}
	}
	
	private void userNotFound()
	{
		this.context.put("message", I18n.getMessage("User.notFound"));
		this.context.put("moduleAction", "message.htm");
	}

	public void logout() throws Exception 
	{
		JForum.setRedirect(this.request.getContextPath()
				+ "/forums/list"
				+ SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION));

		UserSession userSession = SessionFacade.getUserSession();
		SessionFacade.storeSessionData(userSession.getSessionId(), JForum.getConnection());
		
		// Disable auto login
		userSession.setAutoLogin(false);
		userSession.setUserId(SystemGlobals.getIntValue(ConfigKeys.ANONYMOUS_USER_ID));

		SessionFacade.setAttribute("logged", "0");
		SessionFacade.add(userSession);

		JForum.addCookie(SystemGlobals.getValue(ConfigKeys.COOKIE_AUTO_LOGIN), null);
		JForum.addCookie(SystemGlobals.getValue(ConfigKeys.COOKIE_NAME_DATA),
				SystemGlobals.getValue(ConfigKeys.ANONYMOUS_USER_ID));
	}

	public void login() throws Exception 
	{
		if (this.request.getParameter("returnPath") != null) {
			this.context.put("returnPath", this.request.getParameter("returnPath"));
		}

		this.context.put("moduleAction", "forum_login.htm");
	}

	// Lost password form
	public void lostPassword() 
	{
		this.context.put("moduleAction", "lost_password.htm");
	}
	
	public User prepareLostPassword(String username, String email) throws Exception
	{
		User user = null;
		UserModel um = DataAccessDriver.getInstance().newUserModel();

		if (email != null && !email.trim().equals("")) {
			username = um.getUsernameByEmail(email);
		}

		if (username != null && !username.trim().equals("")) {
			List l = um.findByName(username, true);
			if (l.size() > 0) {
				user = (User)l.get(0);
			}
		}
		
		if (user == null) {
			return null;
		}
		
		String hash = MD5.crypt(user.getEmail() + System.currentTimeMillis());
		um.writeLostPasswordHash(user.getEmail(), hash);
		
		user.setActivationKey(hash);
		
		return user;
	}

	// Send lost password email
	public void lostPasswordSend() throws Exception 
	{
		String email = this.request.getParameter("email");
		String username = this.request.getParameter("username");

		User user = this.prepareLostPassword(username, email);
		if (user == null) {
			// user could not be found
			this.context.put("message",
					I18n.getMessage("PasswordRecovery.invalidUserEmail"));
			this.lostPassword();
			return;
		}

		try {
			QueuedExecutor.getInstance().execute(
					new EmailSenderTask(new LostPasswordSpammer(user, 
							SystemGlobals.getValue(ConfigKeys.MAIL_LOST_PASSWORD_SUBJECT))));
		} 
		catch (EmailException e) {
			logger.warn("Error while sending email: " + e);
		}

		this.context.put("moduleAction", "message.htm");
		this.context.put("message", I18n.getMessage(
										"PasswordRecovery.emailSent",
										new String[] { 
												this.request.getContextPath()
												+ "/user/login"
												+ SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION) 
											}));
	}

	// Recover user password ( aka, ask him a new one )
	public void recoverPassword() throws Exception 
	{
		String hash = this.request.getParameter("hash");

		this.context.put("moduleAction", "recover_password.htm");
		this.context.put("recoverHash", hash);
	}

	public void recoverPasswordValidate() throws Exception 
	{
		String hash = this.request.getParameter("recoverHash");
		String email = this.request.getParameter("email");

		String message = "";

		boolean isOk = DataAccessDriver.getInstance().newUserModel().validateLostPasswordHash(email, hash);
		if (isOk) {
			String password = this.request.getParameter("newPassword");
			DataAccessDriver.getInstance().newUserModel().saveNewPassword(MD5.crypt(password), email);

			message = I18n.getMessage("PasswordRecovery.ok",
					new String[] { this.request.getContextPath()
							+ "/user/login"
							+ SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION) });
		} 
		else {
			message = I18n.getMessage("PasswordRecovery.invalidData");
		}

		this.context.put("moduleAction", "message.htm");
		this.context.put("message", message);
	}

		
	public void list() throws Exception
	{
		int start = this.preparePagination(DataAccessDriver.getInstance().newUserModel().getTotalUsers());
		int usersPerPage = SystemGlobals.getIntValue(ConfigKeys.USERS_PER_PAGE);
							
		List users = DataAccessDriver.getInstance().newUserModel().selectAll(start ,usersPerPage);
		this.context.put("users", users);
		this.context.put("moduleAction", "user_list.htm");
	}
	
	/**
	 * @deprecated probably will be removed. Use KarmaAction to load Karma
	 * @throws Exception
	 */
	public void searchKarma() throws Exception
	{
		int start = this.preparePagination(DataAccessDriver.getInstance().newUserModel().getTotalUsers());
		int usersPerPage = SystemGlobals.getIntValue(ConfigKeys.USERS_PER_PAGE);
		//Load all users with your karma
		List users = DataAccessDriver.getInstance().newUserModel().selectAllWithKarma(start ,usersPerPage);
		this.context.put("users", users);
		this.context.put("moduleAction", "user_list_karma.htm");
	}
	
	
	private int preparePagination(int totalUsers)
	{
		int start = ViewCommon.getStartPage();
		int usersPerPage = SystemGlobals.getIntValue(ConfigKeys.USERS_PER_PAGE);
		
		this.context.put("totalPages", new Double(Math.ceil( (double)totalUsers / usersPerPage )));
		this.context.put("recordsPerPage", new Integer(usersPerPage));
		this.context.put("totalRecords", new Integer(totalUsers));
		this.context.put("thisPage", new Double(Math.ceil( (double)(start + 1) / usersPerPage )));
		this.context.put("start", new Integer(start));
		
		return start;
	}	
}

⌨️ 快捷键说明

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