Search in sources :

Example 1 with UserSession

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

the class ControllerUtils method refreshSession.

/**
	 * Do a refresh in the user's session. This method will update the last visit time for the
	 * current user, as well checking for authentication if the session is new or the SSO user has
	 * changed
	 */
public void refreshSession() {
    UserSession userSession = SessionFacade.getUserSession();
    RequestContext request = JForumExecutionContext.getRequest();
    if (userSession == null) {
        userSession = new UserSession();
        userSession.registerBasicInfo();
        userSession.setSessionId(request.getSessionContext().getId());
        userSession.setIp(request.getRemoteAddr());
        SessionFacade.makeUnlogged();
        if (!JForumExecutionContext.getForumContext().isBot()) {
            // Non-SSO authentications can use auto login
            if (!ConfigKeys.TYPE_SSO.equals(SystemGlobals.getValue(ConfigKeys.AUTHENTICATION_TYPE))) {
                if (SystemGlobals.getBoolValue(ConfigKeys.AUTO_LOGIN_ENABLED)) {
                    this.checkAutoLogin(userSession);
                } else {
                    userSession.makeAnonymous();
                }
            } else {
                this.checkSSO(userSession);
            }
        }
        SessionFacade.add(userSession);
    } else if (ConfigKeys.TYPE_SSO.equals(SystemGlobals.getValue(ConfigKeys.AUTHENTICATION_TYPE))) {
        SSO sso;
        try {
            sso = (SSO) Class.forName(SystemGlobals.getValue(ConfigKeys.SSO_IMPLEMENTATION)).newInstance();
        } catch (Exception e) {
            throw new ForumException(e);
        }
        // If SSO, then check if the session is valid
        if (!sso.isSessionValid(userSession, request)) {
            SessionFacade.remove(userSession.getSessionId());
            refreshSession();
        }
    } else {
        SessionFacade.getUserSession().updateSessionTime();
    }
}
Also used : ForumException(net.jforum.exceptions.ForumException) UserSession(net.jforum.entities.UserSession) RequestContext(net.jforum.context.RequestContext) SSO(net.jforum.sso.SSO) DatabaseException(net.jforum.exceptions.DatabaseException) ForumException(net.jforum.exceptions.ForumException)

Example 2 with UserSession

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

the class SessionFacade method isUserInSession.

/**
	 * Verify if the user in already loaded
	 * 
	 * @param username The username to check
	 * @return The session id if the user is already registered into the session, 
	 * or <code>null</code> if it is not.
	 */
public static String isUserInSession(String username) {
    int aid = SystemGlobals.getIntValue(ConfigKeys.ANONYMOUS_USER_ID);
    synchronized (FQN) {
        for (Iterator iter = cache.getValues(FQN).iterator(); iter.hasNext(); ) {
            UserSession us = (UserSession) iter.next();
            String thisUsername = us.getUsername();
            if (thisUsername == null) {
                continue;
            }
            if (us.getUserId() != aid && thisUsername.equals(username)) {
                return us.getSessionId();
            }
        }
    }
    return null;
}
Also used : UserSession(net.jforum.entities.UserSession) Iterator(java.util.Iterator)

Example 3 with UserSession

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

the class SessionFacade method remove.

/**
	 * Remove an entry fro the session map
	 * 
	 * @param sessionId The session id to remove
	 */
public static void remove(String sessionId) {
    if (cache == null) {
        logger.warn("Got a null cache instance. #" + sessionId);
        return;
    }
    logger.debug("Removing session " + sessionId);
    synchronized (FQN) {
        UserSession us = getUserSession(sessionId);
        if (us != null) {
            cache.remove(FQN_LOGGED, sessionId);
            cache.remove(FQN_USER_ID, Integer.toString(us.getUserId()));
            if (us.getUserId() != SystemGlobals.getIntValue(ConfigKeys.ANONYMOUS_USER_ID)) {
                changeUserCount(LOGGED_COUNT, false);
            } else {
                changeUserCount(ANONYMOUS_COUNT, false);
            }
        }
        cache.remove(FQN, sessionId);
    }
}
Also used : UserSession(net.jforum.entities.UserSession)

Example 4 with UserSession

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

the class POPPostAction method insertMessages.

public void insertMessages(POPParser parser) {
    long ms = System.currentTimeMillis();
    int counter = 0;
    try {
        JForumExecutionContext ex = JForumExecutionContext.get();
        RequestContext request = new StandardRequestContext();
        ex.setForumContext(new JForumContext("/", "", request, null));
        JForumExecutionContext.set(ex);
        SessionFacade.setAttribute(ConfigKeys.TOPICS_READ_TIME, new HashMap());
        for (Iterator iter = parser.getMessages().iterator(); iter.hasNext(); ) {
            POPMessage m = (POPMessage) iter.next();
            String sessionId = ms + m.getSender() + counter++;
            request.getSessionContext().setAttribute(StandardSessionContext.SESSION_ID, sessionId);
            User user = this.findUser(m.getSender());
            if (user == null) {
                logger.warn("Could not find user with email " + m.getSender() + ". Ignoring his message.");
                continue;
            }
            try {
                UserSession us = new UserSession();
                us.setUserId(user.getId());
                us.setUsername(us.getUsername());
                us.setSessionId(sessionId);
                SessionFacade.add(us, sessionId);
                SessionFacade.setAttribute(ConfigKeys.LOGGED, "1");
                SessionFacade.removeAttribute(ConfigKeys.LAST_POST_TIME);
                SessionFacade.setAttribute(ConfigKeys.REQUEST_IGNORE_CAPTCHA, "1");
                this.insertMessage(m, user);
            } finally {
                SessionFacade.remove(sessionId);
            }
        }
    } finally {
        JForumExecutionContext.finish();
    }
}
Also used : User(net.jforum.entities.User) JForumExecutionContext(net.jforum.JForumExecutionContext) HashMap(java.util.HashMap) UserSession(net.jforum.entities.UserSession) Iterator(java.util.Iterator) RequestContext(net.jforum.context.RequestContext) StandardRequestContext(net.jforum.context.standard.StandardRequestContext) StandardRequestContext(net.jforum.context.standard.StandardRequestContext) JForumContext(net.jforum.context.JForumContext)

Example 5 with UserSession

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

the class UserAction method insertSave.

public void insertSave() {
    UserSession userSession = SessionFacade.getUserSession();
    int userId = userSession.getUserId();
    if ((!SystemGlobals.getBoolValue(ConfigKeys.REGISTRATION_ENABLED) && !SecurityRepository.get(userId).canAccess(SecurityConstants.PERM_ADMINISTRATION)) || ConfigKeys.TYPE_SSO.equals(SystemGlobals.getValue(ConfigKeys.AUTHENTICATION_TYPE))) {
        this.registrationDisabled();
        return;
    }
    User u = new User();
    UserDAO dao = DataAccessDriver.getInstance().newUserDAO();
    String username = this.request.getParameter("username");
    String password = this.request.getParameter("password");
    String email = this.request.getParameter("email");
    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 (username != null) {
        username = username.trim();
    }
    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 && dao.isUsernameRegistered(username)) {
        this.context.put("error", I18n.getMessage("UsernameExists"));
        error = true;
    }
    if (!error && dao.findByEmail(email) != null) {
        this.context.put("error", I18n.getMessage("User.emailExists", new String[] { email }));
        error = true;
    }
    if (!error && !userSession.validateCaptchaResponse(captchaResponse)) {
        this.context.put("error", I18n.getMessage("CaptchaResponseFails"));
        error = true;
    }
    if (error) {
        this.insert(true);
        return;
    }
    u.setUsername(username);
    u.setPassword(MD5.crypt(password));
    u.setEmail(email);
    boolean requiresMailActivation = SystemGlobals.getBoolValue(ConfigKeys.MAIL_USER_EMAIL_AUTH);
    if (requiresMailActivation) {
        u.setActivationKey(MD5.crypt(username + System.currentTimeMillis()));
    }
    int newUserId = dao.addNew(u);
    if (requiresMailActivation) {
        Executor.execute(new EmailSenderTask(new ActivationKeySpammer(u)));
        this.setTemplateName(TemplateKeys.USER_INSERT_ACTIVATE_MAIL);
        this.context.put("message", I18n.getMessage("User.GoActivateAccountMessage"));
    } else if (SecurityRepository.get(userId).canAccess(SecurityConstants.PERM_ADMINISTRATION)) {
        JForumExecutionContext.setRedirect(this.request.getContextPath() + "/adminUsers/list" + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION));
    } else {
        this.logNewRegisteredUserIn(newUserId, u);
    }
    if (!requiresMailActivation) {
        dao.writeUserActive(newUserId);
    }
}
Also used : EmailSenderTask(net.jforum.util.mail.EmailSenderTask) ActivationKeySpammer(net.jforum.util.mail.ActivationKeySpammer) User(net.jforum.entities.User) UserDAO(net.jforum.dao.UserDAO) UserSession(net.jforum.entities.UserSession)

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