Search in sources :

Example 16 with UserSession

use of net.jforum.entities.UserSession in project jforum2 by rafaelsteil.

the class UserAction method validateLogin.

public void validateLogin() {
    String password;
    String username;
    if (parseBasicAuthentication()) {
        username = (String) this.request.getAttribute("username");
        password = (String) this.request.getAttribute("password");
    } else {
        username = this.request.getParameter("username");
        password = this.request.getParameter("password");
    }
    boolean validInfo = false;
    if (password.length() > 0) {
        User user = this.validateLogin(username, password);
        if (user != null) {
            // Note: here we only want to set the redirect location if it hasn't already been
            // set. This will give the LoginAuthenticator a chance to set the redirect location.
            this.buildSucessfulLoginRedirect();
            SessionFacade.makeLogged();
            String sessionId = SessionFacade.isUserInSession(user.getId());
            UserSession userSession = new UserSession(SessionFacade.getUserSession());
            // Remove the "guest" session
            SessionFacade.remove(userSession.getSessionId());
            userSession.dataToUser(user);
            UserSession currentUs = SessionFacade.getUserSession(sessionId);
            // Check if the user is returning to the system
            // before its last session has expired ( hypothesis )
            UserSession tmpUs;
            if (sessionId != null && currentUs != null) {
                // Write its old session data
                SessionFacade.storeSessionData(sessionId, JForumExecutionContext.getConnection());
                tmpUs = new UserSession(currentUs);
                SessionFacade.remove(sessionId);
            } else {
                UserSessionDAO sm = DataAccessDriver.getInstance().newUserSessionDAO();
                tmpUs = sm.selectById(userSession, JForumExecutionContext.getConnection());
            }
            I18n.load(user.getLang());
            // Autologin
            if (this.request.getParameter("autologin") != null && SystemGlobals.getBoolValue(ConfigKeys.AUTO_LOGIN_ENABLED)) {
                userSession.setAutoLogin(true);
                // Generate the user-specific hash
                String systemHash = MD5.crypt(SystemGlobals.getValue(ConfigKeys.USER_HASH_SEQUENCE) + user.getId());
                String userHash = MD5.crypt(System.currentTimeMillis() + systemHash);
                // Persist the user hash
                UserDAO dao = DataAccessDriver.getInstance().newUserDAO();
                dao.saveUserAuthHash(user.getId(), userHash);
                systemHash = MD5.crypt(userHash);
                ControllerUtils.addCookie(SystemGlobals.getValue(ConfigKeys.COOKIE_AUTO_LOGIN), "1");
                ControllerUtils.addCookie(SystemGlobals.getValue(ConfigKeys.COOKIE_USER_HASH), systemHash);
            } else {
                // Remove cookies for safety
                ControllerUtils.addCookie(SystemGlobals.getValue(ConfigKeys.COOKIE_USER_HASH), null);
                ControllerUtils.addCookie(SystemGlobals.getValue(ConfigKeys.COOKIE_AUTO_LOGIN), null);
            }
            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_READ_TIME, new HashMap());
            ControllerUtils.addCookie(SystemGlobals.getValue(ConfigKeys.COOKIE_NAME_DATA), Integer.toString(user.getId()));
            SecurityRepository.load(user.getId(), true);
            validInfo = true;
        }
    }
    // Invalid login
    if (!validInfo) {
        this.context.put("invalidLogin", "1");
        this.setTemplateName(TemplateKeys.USER_VALIDATE_LOGIN);
        if (this.request.getParameter("returnPath") != null) {
            this.context.put("returnPath", this.request.getParameter("returnPath"));
        }
    } else if (this.request.getParameter("returnPath") != null) {
        JForumExecutionContext.setRedirect(this.request.getParameter("returnPath"));
    }
}
Also used : User(net.jforum.entities.User) UserDAO(net.jforum.dao.UserDAO) HashMap(java.util.HashMap) UserSession(net.jforum.entities.UserSession) UserSessionDAO(net.jforum.dao.UserSessionDAO) Date(java.util.Date)

Example 17 with UserSession

use of net.jforum.entities.UserSession in project jforum2 by rafaelsteil.

the class I18nTest method setUp.

/**
	 * @see TestCase#setUp()
	 */
protected void setUp() throws Exception {
    if (!loaded) {
        TestCaseUtils.loadEnvironment();
        SystemGlobals.setValue(ConfigKeys.RESOURCE_DIR, SystemGlobals.getApplicationResourceDir() + "/tests");
        loaded = true;
    }
    SystemGlobals.setValue(ConfigKeys.I18N_DEFAULT_ADMIN, "default");
    SystemGlobals.setValue(ConfigKeys.I18N_DEFAULT, "default");
    this.us = new UserSession();
    this.us.setSessionId(SESSION_ID);
    SessionFacade.add(this.us, SESSION_ID);
    I18n.reset();
    I18n.load();
}
Also used : UserSession(net.jforum.entities.UserSession)

Example 18 with UserSession

use of net.jforum.entities.UserSession in project jforum2 by rafaelsteil.

the class GenericUserSessionDAO method selectById.

/**
	 * @see net.jforum.dao.UserSessionDAO#selectById(net.jforum.entities.UserSession,
	 *      java.sql.Connection)
	 */
public UserSession selectById(UserSession us, Connection conn) {
    PreparedStatement p = null;
    ResultSet rs = null;
    try {
        p = conn.prepareStatement(SystemGlobals.getSql("UserSessionModel.selectById"));
        p.setInt(1, us.getUserId());
        rs = p.executeQuery();
        boolean found = false;
        UserSession returnUs = new UserSession(us);
        if (rs.next()) {
            returnUs.setSessionTime(rs.getLong("session_time"));
            returnUs.setStartTime(rs.getTimestamp("session_start"));
            found = true;
        }
        return (found ? returnUs : null);
    } catch (SQLException e) {
        throw new DatabaseException(e);
    } finally {
        DbUtils.close(rs, p);
    }
}
Also used : SQLException(java.sql.SQLException) UserSession(net.jforum.entities.UserSession) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) DatabaseException(net.jforum.exceptions.DatabaseException)

Example 19 with UserSession

use of net.jforum.entities.UserSession in project jforum2 by rafaelsteil.

the class ControllerUtils method configureUserSession.

/**
	 * Setup optios and values for the user's session if authentication was ok.
	 * 
	 * @param userSession The UserSession instance of the user
	 * @param user The User instance of the authenticated user
	 */
protected void configureUserSession(UserSession userSession, User user) {
    userSession.dataToUser(user);
    // As an user may come back to the forum before its
    // last visit's session expires, we should check for
    // existent user information and then, if found, store
    // it to the database before getting his information back.
    String sessionId = SessionFacade.isUserInSession(user.getId());
    UserSession tmpUs;
    if (sessionId != null) {
        SessionFacade.storeSessionData(sessionId, JForumExecutionContext.getConnection());
        tmpUs = SessionFacade.getUserSession(sessionId);
        SessionFacade.remove(sessionId);
    } else {
        UserSessionDAO sm = DataAccessDriver.getInstance().newUserSessionDAO();
        tmpUs = sm.selectById(userSession, JForumExecutionContext.getConnection());
    }
    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()));
    }
    // If the execution point gets here, then the user
    // has chosen "autoLogin"
    userSession.setAutoLogin(true);
    SessionFacade.makeLogged();
    I18n.load(user.getLang());
}
Also used : UserSession(net.jforum.entities.UserSession) UserSessionDAO(net.jforum.dao.UserSessionDAO) Date(java.util.Date)

Example 20 with UserSession

use of net.jforum.entities.UserSession in project jforum2 by rafaelsteil.

the class TopicsCommon method prepareTopics.

/**
	 * Prepare the topics for listing.
	 * This method does some preparation for a set ot <code>net.jforum.entities.Topic</code>
	 * instances for the current user, like verification if the user already
	 * read the topic, if pagination is a need and so on.
	 * 
	 * @param topics The topics to process
	 * @return The post-processed topics.
	 */
public static List prepareTopics(List topics) {
    UserSession userSession = SessionFacade.getUserSession();
    long lastVisit = userSession.getLastVisit().getTime();
    int hotBegin = SystemGlobals.getIntValue(ConfigKeys.HOT_TOPIC_BEGIN);
    int postsPerPage = SystemGlobals.getIntValue(ConfigKeys.POSTS_PER_PAGE);
    List newTopics = new ArrayList(topics.size());
    Map topicsReadTime = SessionFacade.getTopicsReadTime();
    Map topicReadTimeByForum = SessionFacade.getTopicsReadTimeByForum();
    boolean checkUnread = (userSession.getUserId() != SystemGlobals.getIntValue(ConfigKeys.ANONYMOUS_USER_ID));
    for (Iterator iter = topics.iterator(); iter.hasNext(); ) {
        Topic t = (Topic) iter.next();
        boolean read = false;
        boolean isReadByForum = false;
        long lastPostTime = t.getLastPostDate().getTime();
        if (topicReadTimeByForum != null) {
            Long currentForumTime = (Long) topicReadTimeByForum.get(new Integer(t.getForumId()));
            isReadByForum = currentForumTime != null && lastPostTime < currentForumTime.longValue();
        }
        boolean isTopicTimeOlder = !isReadByForum && lastPostTime <= lastVisit;
        if (!checkUnread || isReadByForum || isTopicTimeOlder) {
            read = true;
        } else {
            Integer topicId = new Integer(t.getId());
            Long currentTopicTime = (Long) topicsReadTime.get(topicId);
            if (currentTopicTime != null) {
                read = currentTopicTime.longValue() > lastPostTime;
            }
        }
        if (t.getTotalReplies() + 1 > postsPerPage) {
            t.setPaginate(true);
            t.setTotalPages(new Double(Math.floor(t.getTotalReplies() / postsPerPage)));
        } else {
            t.setPaginate(false);
            t.setTotalPages(new Double(0));
        }
        // Check if this is a hot topic
        t.setHot(t.getTotalReplies() >= hotBegin);
        t.setRead(read);
        newTopics.add(t);
    }
    return newTopics;
}
Also used : UserSession(net.jforum.entities.UserSession) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) Topic(net.jforum.entities.Topic) Map(java.util.Map)

Aggregations

UserSession (net.jforum.entities.UserSession)27 User (net.jforum.entities.User)6 List (java.util.List)5 Date (java.util.Date)4 HashMap (java.util.HashMap)4 Iterator (java.util.Iterator)4 Topic (net.jforum.entities.Topic)4 Map (java.util.Map)3 PollDAO (net.jforum.dao.PollDAO)3 UserDAO (net.jforum.dao.UserDAO)3 Forum (net.jforum.entities.Forum)3 PrivateMessage (net.jforum.entities.PrivateMessage)3 PermissionControl (net.jforum.security.PermissionControl)3 SimpleDateFormat (java.text.SimpleDateFormat)2 RequestContext (net.jforum.context.RequestContext)2 ForumDAO (net.jforum.dao.ForumDAO)2 PostDAO (net.jforum.dao.PostDAO)2 TopicDAO (net.jforum.dao.TopicDAO)2 UserSessionDAO (net.jforum.dao.UserSessionDAO)2 Poll (net.jforum.entities.Poll)2