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();
}
}
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;
}
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);
}
}
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();
}
}
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);
}
}
Aggregations