1 44 package net.jforum; 45 46 import java.util.Date ; 47 import java.util.HashMap ; 48 49 import javax.servlet.http.Cookie ; 50 import javax.servlet.http.HttpSession ; 51 52 import net.jforum.dao.DataAccessDriver; 53 import net.jforum.dao.UserSessionDAO; 54 import net.jforum.entities.User; 55 import net.jforum.entities.UserSession; 56 import net.jforum.exceptions.DatabaseException; 57 import net.jforum.exceptions.ForumException; 58 import net.jforum.repository.SecurityRepository; 59 import net.jforum.security.SecurityConstants; 60 import net.jforum.sso.SSO; 61 import net.jforum.sso.SSOUtils; 62 import net.jforum.util.I18n; 63 import net.jforum.util.MD5; 64 import net.jforum.util.preferences.ConfigKeys; 65 import net.jforum.util.preferences.SystemGlobals; 66 import freemarker.template.SimpleHash; 67 68 74 public class ControllerUtils 75 { 76 81 public void prepareTemplateContext(SimpleHash context, JForumContext jforumcontext) 82 { 83 ActionServletRequest request = JForumExecutionContext.getRequest(); 84 85 context.put("karmaEnabled", SecurityRepository.canAccess(SecurityConstants.PERM_KARMA_ENABLED)); 86 context.put("dateTimeFormat", SystemGlobals.getValue(ConfigKeys.DATE_TIME_FORMAT)); 87 context.put("autoLoginEnabled", SystemGlobals.getBoolValue(ConfigKeys.AUTO_LOGIN_ENABLED)); 88 context.put("sso", ConfigKeys.TYPE_SSO.equals(SystemGlobals.getValue(ConfigKeys.AUTHENTICATION_TYPE))); 89 context.put("contextPath", request.getContextPath()); 90 context.put("serverName", request.getServerName()); 91 context.put("templateName", SystemGlobals.getValue(ConfigKeys.TEMPLATE_DIR)); 92 context.put("extension", SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION)); 93 context.put("serverPort", Integer.toString(request.getServerPort())); 94 context.put("I18n", I18n.getInstance()); 95 context.put("version", SystemGlobals.getValue(ConfigKeys.VERSION)); 96 context.put("forumTitle", SystemGlobals.getValue(ConfigKeys.FORUM_PAGE_TITLE)); 97 context.put("pageTitle", SystemGlobals.getValue(ConfigKeys.FORUM_PAGE_TITLE)); 98 context.put("metaKeywords", SystemGlobals.getValue(ConfigKeys.FORUM_PAGE_METATAG_KEYWORDS)); 99 context.put("metaDescription", SystemGlobals.getValue(ConfigKeys.FORUM_PAGE_METATAG_DESCRIPTION)); 100 context.put("forumLink", SystemGlobals.getValue(ConfigKeys.FORUM_LINK)); 101 context.put("homepageLink", SystemGlobals.getValue(ConfigKeys.HOMEPAGE_LINK)); 102 context.put("encoding", SystemGlobals.getValue(ConfigKeys.ENCODING)); 103 context.put("bookmarksEnabled", SecurityRepository.canAccess(SecurityConstants.PERM_BOOKMARKS_ENABLED)); 104 context.put("JForumContext", jforumcontext); 105 } 106 107 115 protected boolean checkAutoLogin(UserSession userSession) 116 { 117 String cookieName = SystemGlobals.getValue(ConfigKeys.COOKIE_NAME_DATA); 118 119 Cookie cookie = this.getCookieTemplate(cookieName); 120 Cookie hashCookie = this.getCookieTemplate(SystemGlobals.getValue(ConfigKeys.COOKIE_USER_HASH)); 121 Cookie autoLoginCookie = this.getCookieTemplate(SystemGlobals.getValue(ConfigKeys.COOKIE_AUTO_LOGIN)); 122 123 if (hashCookie != null && cookie != null 124 && !cookie.getValue().equals(SystemGlobals.getValue(ConfigKeys.ANONYMOUS_USER_ID)) 125 && autoLoginCookie != null && "1".equals(autoLoginCookie.getValue())) { 126 String uid = cookie.getValue(); 127 String uidHash = hashCookie.getValue(); 128 129 String securityHash = SystemGlobals.getValue(ConfigKeys.USER_HASH_SEQUENCE); 130 131 if ((uid != null && !uid.equals("")) && (securityHash != null && !securityHash.equals("")) 132 && (MD5.crypt(securityHash + uid).equals(uidHash))) { 133 int userId = Integer.parseInt(uid); 134 userSession.setUserId(userId); 135 136 try { 137 User user = DataAccessDriver.getInstance().newUserDAO().selectById(userId); 138 139 if (user == null || user.getId() != userId) { 140 userSession.makeAnonymous(); 141 return false; 142 } 143 144 this.configureUserSession(userSession, user); 145 } 146 catch (Exception e) { 147 throw new DatabaseException(e); 148 } 149 150 return true; 151 } 152 153 userSession.makeAnonymous(); 154 } 155 156 return false; 157 } 158 159 166 protected void configureUserSession(UserSession userSession, User user) throws Exception 167 { 168 userSession.dataToUser(user); 169 170 String sessionId = SessionFacade.isUserInSession(user.getId()); 175 176 UserSession tmpUs = new UserSession(); 177 if (sessionId != null) { 178 SessionFacade.storeSessionData(sessionId, JForumExecutionContext.getConnection()); 179 tmpUs = SessionFacade.getUserSession(sessionId); 180 SessionFacade.remove(sessionId); 181 } 182 else { 183 UserSessionDAO sm = DataAccessDriver.getInstance().newUserSessionDAO(); 184 tmpUs = sm.selectById(userSession, JForumExecutionContext.getConnection()); 185 } 186 187 if (tmpUs == null) { 188 userSession.setLastVisit(new Date (System.currentTimeMillis())); 189 } 190 else { 191 userSession.setLastVisit(new Date (tmpUs.getStartTime().getTime() + tmpUs.getSessionTime())); 193 } 194 195 userSession.setAutoLogin(true); 198 SessionFacade.setAttribute("logged", "1"); 199 200 I18n.load(user.getLang()); 201 } 202 203 206 protected void checkSSO(UserSession userSession) 207 { 208 try { 209 SSO sso = (SSO) Class.forName(SystemGlobals.getValue(ConfigKeys.SSO_IMPLEMENTATION)).newInstance(); 210 String username = sso.authenticateUser(JForumExecutionContext.getRequest()); 211 212 if (username == null || username.trim().equals("")) { 213 userSession.makeAnonymous(); 214 } 215 else { 216 SSOUtils utils = new SSOUtils(); 217 218 if (!utils.userExists(username)) { 219 HttpSession session = JForumExecutionContext.getRequest().getSession(); 220 221 String email = (String ) session.getAttribute(SystemGlobals.getValue(ConfigKeys.SSO_EMAIL_ATTRIBUTE)); 222 String password = (String ) session.getAttribute(SystemGlobals.getValue(ConfigKeys.SSO_PASSWORD_ATTRIBUTE)); 223 224 if (email == null) { 225 email = SystemGlobals.getValue(ConfigKeys.SSO_DEFAULT_EMAIL); 226 } 227 228 if (password == null) { 229 password = SystemGlobals.getValue(ConfigKeys.SSO_DEFAULT_PASSWORD); 230 } 231 232 utils.register(password, email); 233 } 234 235 this.configureUserSession(userSession, utils.getUser()); 236 } 237 } 238 catch (Exception e) { 239 e.printStackTrace(); 240 throw new ForumException("Error while executing SSO actions: " + e); 241 } 242 } 243 244 251 public void refreshSession() throws Exception 252 { 253 UserSession userSession = SessionFacade.getUserSession(); 254 ActionServletRequest request = JForumExecutionContext.getRequest(); 255 256 if (userSession == null) { 257 userSession = new UserSession(); 258 userSession.setSessionId(request.getSession().getId()); 259 userSession.setIp(request.getRemoteAddr()); 260 261 userSession.makeAnonymous(); 262 263 if (!userSession.isBot()) { 264 if (!ConfigKeys.TYPE_SSO.equals(SystemGlobals.getValue(ConfigKeys.AUTHENTICATION_TYPE))) { 266 if (SystemGlobals.getBoolValue(ConfigKeys.AUTO_LOGIN_ENABLED)) { 267 this.checkAutoLogin(userSession); 268 } 269 } 270 else { 271 this.checkSSO(userSession); 272 } 273 } 274 275 SessionFacade.add(userSession); 276 SessionFacade.setAttribute(ConfigKeys.TOPICS_TRACKING, new HashMap ()); 277 } 278 else if (ConfigKeys.TYPE_SSO.equals(SystemGlobals.getValue(ConfigKeys.AUTHENTICATION_TYPE))) { 279 SSO sso = (SSO) Class.forName(SystemGlobals.getValue(ConfigKeys.SSO_IMPLEMENTATION)).newInstance(); 280 281 if (!sso.isSessionValid(userSession, request)) { 283 SessionFacade.remove(userSession.getSessionId()); 284 refreshSession(); 285 } 286 } 287 else { 288 SessionFacade.getUserSession().updateSessionTime(); 289 } 290 } 291 292 298 public static Cookie getCookie(String name) 299 { 300 Cookie [] cookies = JForumExecutionContext.getRequest().getCookies(); 301 302 if (cookies != null) { 303 for (int i = 0; i < cookies.length; i++) { 304 Cookie c = cookies[i]; 305 306 if (c.getName().equals(name)) { 307 return c; 308 } 309 } 310 } 311 312 return null; 313 } 314 315 324 protected Cookie getCookieTemplate(String name) 325 { 326 return ControllerUtils.getCookie(name); 327 } 328 329 335 public static void addCookie(String name, String value) 336 { 337 Cookie cookie = new Cookie (name, value); 338 cookie.setMaxAge(3600 * 24 * 365); 339 cookie.setPath("/"); 340 341 JForumExecutionContext.getResponse().addCookie(cookie); 342 } 343 344 353 protected void addCookieTemplate(String name, String value) 354 { 355 ControllerUtils.addCookie(name, value); 356 } 357 } 358 | Popular Tags |