1 43 package net.jforum.entities; 44 45 import java.awt.image.BufferedImage ; 46 import java.io.Serializable ; 47 import java.util.Date ; 48 49 import net.jforum.JForumExecutionContext; 50 import net.jforum.SessionFacade; 51 import net.jforum.repository.SecurityRepository; 52 import net.jforum.security.PermissionControl; 53 import net.jforum.security.SecurityConstants; 54 import net.jforum.util.Captcha; 55 import net.jforum.util.I18n; 56 import net.jforum.util.preferences.ConfigKeys; 57 import net.jforum.util.preferences.SystemGlobals; 58 59 import com.octo.captcha.image.ImageCaptcha; 60 61 67 public class UserSession implements Serializable 68 { 69 static final long serialVersionUID = 0; 70 71 private long sessionTime; 72 73 private int userId; 74 private int privateMessages; 75 76 private Date startTime; 77 private Date lastVisit; 78 79 private String sessionId; 80 private String username; 81 private String lang; 82 private String ip; 83 84 private boolean autoLogin; 85 86 private ImageCaptcha imageCaptcha = null; 87 88 public UserSession() {} 89 90 public UserSession(UserSession us) 91 { 92 if (us.getStartTime() != null) { 93 this.startTime = new Date (us.getStartTime().getTime()); 94 } 95 96 if (us.getLastVisit() != null) { 97 this.lastVisit = new Date (us.getLastVisit().getTime()); 98 } 99 100 this.sessionTime = us.getSessionTime(); 101 this.userId = us.getUserId(); 102 this.sessionId = us.getSessionId(); 103 this.username = us.getUsername(); 104 this.autoLogin = us.getAutoLogin(); 105 this.lang = us.getLang(); 106 this.privateMessages = us.getPrivateMessages(); 107 this.imageCaptcha = us.imageCaptcha; 108 this.ip = us.getIp(); 109 } 110 111 public Date sessionLastUpdate() 112 { 113 return new Date (this.startTime.getTime() + this.sessionTime); 114 } 115 116 public void setIp(String ip) 117 { 118 this.ip = ip; 119 } 120 121 public String getIp() 122 { 123 return this.ip; 124 } 125 126 131 public void setStartTime(Date startTime) 132 { 133 this.startTime = startTime; 134 } 135 136 139 public int getPrivateMessages() 140 { 141 return this.privateMessages; 142 } 143 144 147 public void setPrivateMessages(int privateMessages) 148 { 149 this.privateMessages = privateMessages; 150 } 151 152 157 public void setLastVisit(Date lastVisit) 158 { 159 this.lastVisit = lastVisit; 160 } 161 162 167 public void setUserId(int userId) 168 { 169 this.userId = userId; 170 } 171 172 177 public void setUsername(String username) 178 { 179 this.username = username; 180 } 181 182 public void setSessionId(String sessionId) 183 { 184 this.sessionId = sessionId; 185 } 186 187 public void setSessionTime(long sessionTime) 188 { 189 this.sessionTime = sessionTime; 190 } 191 192 public void setLang(String lang) 193 { 194 this.lang = lang; 195 } 196 197 200 public void updateSessionTime() 201 { 202 this.sessionTime = System.currentTimeMillis() - this.startTime.getTime(); 203 } 204 205 210 public void setAutoLogin(boolean autoLogin) 211 { 212 this.autoLogin = autoLogin; 213 } 214 215 220 public Date getStartTime() 221 { 222 return this.startTime; 223 } 224 225 public String getLang() 226 { 227 return this.lang; 228 } 229 230 235 public Date getLastVisit() 236 { 237 return this.lastVisit; 238 } 239 240 245 public long getSessionTime() 246 { 247 return this.sessionTime; 248 } 249 250 255 public int getUserId() 256 { 257 return this.userId; 258 } 259 260 265 public String getUsername() 266 { 267 if (this.username == null && this.userId == SystemGlobals.getIntValue(ConfigKeys.ANONYMOUS_USER_ID)) { 268 this.username = I18n.getMessage("Guest"); 269 } 270 271 return this.username; 272 } 273 274 279 public boolean getAutoLogin() 280 { 281 return this.autoLogin; 282 } 283 284 289 public String getSessionId() 290 { 291 return this.sessionId; 292 } 293 294 299 public boolean isAdmin() throws Exception 300 { 301 return SecurityRepository.canAccess(this.userId, SecurityConstants.PERM_ADMINISTRATION); 302 } 303 304 309 public boolean isModerator() throws Exception 310 { 311 return SecurityRepository.canAccess(this.userId, SecurityConstants.PERM_MODERATION); 312 } 313 314 320 public boolean isModerator(int forumId) throws Exception 321 { 322 PermissionControl pc = SecurityRepository.get(this.userId); 323 324 return (pc.canAccess(SecurityConstants.PERM_MODERATION)) 325 && (pc.canAccess(SecurityConstants.PERM_MODERATION_FORUMS, 326 Integer.toString(forumId))); 327 } 328 329 336 public void makeAnonymous() 337 { 338 this.setStartTime(new Date (System.currentTimeMillis())); 339 this.setLastVisit(new Date (System.currentTimeMillis())); 340 this.setUserId(SystemGlobals.getIntValue(ConfigKeys.ANONYMOUS_USER_ID)); 341 342 SessionFacade.setAttribute("logged", "0"); 343 } 344 345 352 public void dataToUser(User user) 353 { 354 this.setUserId(user.getId()); 355 this.setUsername(user.getUsername()); 356 this.setPrivateMessages(user.getPrivateMessagesCount()); 357 this.setStartTime(new Date (System.currentTimeMillis())); 358 this.setLang(user.getLang()); 359 } 360 361 366 public BufferedImage getCaptchaImage() 367 { 368 if (this.imageCaptcha == null) { 369 return null; 370 } 371 372 return (BufferedImage )this.imageCaptcha.getChallenge(); 373 } 374 375 381 public boolean validateCaptchaResponse(String userResponse) 382 { 383 if ((SystemGlobals.getBoolValue(ConfigKeys.CAPTCHA_REGISTRATION) 384 || SystemGlobals.getBoolValue(ConfigKeys.CAPTCHA_POSTS)) 385 && this.imageCaptcha != null) { 386 boolean result = this.imageCaptcha.validateResponse(userResponse).booleanValue(); 387 this.destroyCaptcha(); 388 return result; 389 } 390 391 return true; 392 } 393 394 399 public void createNewCaptcha() 400 { 401 this.destroyCaptcha(); 402 this.imageCaptcha = Captcha.getInstance().getNextImageCaptcha(); 403 } 404 405 410 public void destroyCaptcha() 411 { 412 this.imageCaptcha = null; 413 } 414 415 419 public boolean isBot() 420 { 421 return Boolean.TRUE.equals(JForumExecutionContext.getRequest().getAttribute(ConfigKeys.IS_BOT)); 422 } 423 424 427 public boolean equals(Object o) 428 { 429 if (!(o instanceof UserSession)) { 430 return false; 431 } 432 433 return this.sessionId.equals(((UserSession)o).getSessionId()); 434 } 435 436 439 public int hashCode() 440 { 441 return this.sessionId.hashCode(); 442 } 443 } 444 | Popular Tags |