1 53 54 106 107 package com.Yasna.forum.database; 108 109 import com.Yasna.forum.*; 110 import com.Yasna.forum.locale.YazdLocale; 111 import com.Yasna.util.*; 112 113 import java.util.*; 114 import java.text.*; 115 import java.sql.*; 116 import java.security.*; 117 118 137 public class DbUser implements User, Cacheable { 138 139 140 private static final String LOAD_PROPERTIES = 141 "SELECT name, propValue FROM yazdUserProp WHERE userID=?"; 142 private static final String LOAD_LASTPOST = 143 "select max(creationDate) as lastpost from yazdMessage where userID=?"; 144 private static final String DELETE_PROPERTIES = 145 "DELETE FROM yazdUserProp WHERE userID=?"; 146 private static final String INSERT_PROPERTY = 147 "INSERT INTO yazdUserProp(userID,name,propValue) VALUES(?,?,?)"; 148 private static final String LOAD_USER_BY_USERNAME = 149 "SELECT * FROM "+SystemProperty.getProperty("User.Table")+" WHERE "+SystemProperty.getProperty("User.Column.Username")+"=?"; 150 private static final String LOAD_USER_BY_ID = 151 "SELECT * FROM "+SystemProperty.getProperty("User.Table")+" WHERE "+SystemProperty.getProperty("User.Column.UserID")+"=?"; 152 private static final String INSERT_USER = 153 "INSERT INTO "+SystemProperty.getProperty("User.Table")+"("+SystemProperty.getProperty("User.Column.UserID")+ 154 ","+SystemProperty.getProperty("User.Column.Username")+ 155 ","+SystemProperty.getProperty("User.Column.PasswordHash")+",email)" + 156 "VALUES(?,?,?,?)"; 157 private static final String SAVE_USER = 158 "UPDATE "+SystemProperty.getProperty("User.Table")+" SET "+SystemProperty.getProperty("User.Column.PasswordHash")+ 159 "=?, "+SystemProperty.getProperty("User.Column.Email")+"=?, "+SystemProperty.getProperty("User.Column.Name")+"=? " + 160 " WHERE "+SystemProperty.getProperty("User.Column.UserID")+"=?"; 161 private static final String DELETE_PERMISSIONS = 162 "DELETE FROM yazdUserPerm WHERE userID=?"; 163 private static final String INSERT_PERMISSION = 164 "INSERT INTO yazdUserPerm(userID,forumID,permission) VALUES(?,?,?)"; 165 private static final String INSERT_AUTO_GROUP= "insert into yazdGroupUser(groupID,userID,administrator) select groupID,?,0 from yazdGroup where automember=1"; 166 167 171 private int id = -2; 172 private String username; 173 private String passwordHash; 174 private String name = ""; 175 private String email; 176 private boolean nameVisible = true; 177 private boolean emailVisible = true; 178 private boolean threadSubscribe = false; 179 private long lastlogin; 180 private long regdate; 181 private long lastpost=-1; 182 private Properties properties; 183 private Object propertyLock = new Object (); 184 185 192 protected DbUser(String username, String password, String email) throws UserAlreadyExistsException { 193 this.id = DbSequenceManager.nextID("User"); 194 this.username = username; 195 this.passwordHash = StringUtils.hash(password); 197 this.email = email; 198 properties = new Properties(); 199 insertIntoDb(); 200 insertAutoGroupMembership(); 201 setProperty("namevisible",Boolean.toString(nameVisible)); 202 setProperty("emailvisible",Boolean.toString(emailVisible)); 203 setProperty("lastlogin",Long.toString(Calendar.getInstance().getTimeInMillis())); 204 setProperty("regdate",Long.toString(Calendar.getInstance().getTimeInMillis())); 205 setProperty("ranking","3"); 206 if(!Boolean.valueOf(PropertyManager.getProperty("User.DisableActivation")).booleanValue()){ 207 String code = StringUtils.randomString(7); 208 setProperty("notactive",code); 209 if (this.email!=null && !"".equals(email)){ 210 String emailBody = PropertyManager.getProperty("yazdActivate.MailBody") + 212 " \n\r"+PropertyManager.getProperty("yazdUrl")+ 213 "activate.jsp?user="+this.id+"&code="+code; 214 MailSender.send(PropertyManager.getProperty("yazdMailSMTPServer"), 215 PropertyManager.getProperty("yazdMailFrom"), 216 this.email, 217 PropertyManager.getProperty("yazdActivate.MailSubject"), 218 emailBody); 219 } 220 } 221 222 } 223 224 229 protected DbUser(int userID) throws UserNotFoundException { 230 this.id = userID; 231 loadFromDb(); 232 loadProperties(); 233 loadLastPost(); 234 nameVisible = Boolean.valueOf(getProperty("namevisible")).booleanValue(); 236 emailVisible = Boolean.valueOf(getProperty("emailvisible")).booleanValue(); 237 threadSubscribe = Boolean.valueOf(getProperty("threadSubscribe")).booleanValue(); 238 try { 239 lastlogin = Long.parseLong(getProperty("lastlogin")); 240 } catch(Exception e){ 241 lastlogin=0; 242 } 243 try { 244 regdate = Long.parseLong(getProperty("regdate")); 245 } catch (Exception e){ 246 regdate = 0; 247 } 248 if(getProperty("ranking")==null){ 249 setProperty("ranking","3"); 250 } 251 } 252 253 258 protected DbUser(String username) throws UserNotFoundException { 259 this.username = username; 260 loadFromDb(); 261 loadProperties(); 262 loadLastPost(); 263 nameVisible = Boolean.valueOf(getProperty("namevisible")).booleanValue(); 265 emailVisible = Boolean.valueOf(getProperty("emailvisible")).booleanValue(); 266 threadSubscribe = Boolean.valueOf(getProperty("threadSubscribe")).booleanValue(); 267 try { 268 lastlogin = Long.parseLong(getProperty("lastlogin")); 269 } catch(Exception e){ 270 lastlogin=0; 271 } 272 try { 273 regdate = Long.parseLong(getProperty("regdate")); 274 } catch (Exception e){ 275 regdate = 0; 276 } 277 if(getProperty("ranking")==null){ 278 setProperty("ranking","3"); 279 } 280 281 } 282 283 protected void activateUser(){ 285 properties.remove("notactive"); 286 saveProperties(); 287 } 288 public int getID() { 289 return id; 290 } 291 292 public boolean isAnonymous() { 293 return (id==-1); 294 } 295 296 public String getUsername() { 297 return StringUtils.escapeHTMLTags(username); 298 } 299 300 public String getName() { 301 return StringUtils.escapeHTMLTags(name); 302 } 303 304 public void setName(String name) throws UnauthorizedException { 305 this.name = name; 306 saveToDb(); 307 } 308 309 public boolean isNameVisible() { 310 return nameVisible; 311 } 312 313 public void setNameVisible(boolean visible) throws UnauthorizedException { 314 this.nameVisible = visible; 315 saveToDb(); 316 } 317 318 public boolean getThreadSubscribe() { 319 return threadSubscribe; 320 } 321 322 public Calendar getLastLogin(){ 323 Calendar cal =Calendar.getInstance(); 324 cal.setTimeInMillis(lastlogin); 325 return cal; 326 } 327 public Calendar getLastPost(){ 328 if (lastpost <0){ 330 return null; 332 } else { 333 Calendar cal =Calendar.getInstance(); 334 cal.setTimeInMillis(lastpost); 335 return cal; 336 } 337 } 338 public Locale getUserLocale(){ 339 String locale = this.getProperty("locale"); 340 if(locale==null){ 341 return YazdLocale.getDefaultYazdLocale(); 343 } 344 String lang=null,country=null,variant=null; 345 int pos = 0; 346 int delPos = 0; 347 if ((delPos = locale.indexOf(",", pos)) != -1) { 348 lang = locale.substring(pos, delPos); 349 pos = delPos + 1; }else if (pos <= locale.length()) { 351 lang = locale.substring(pos); 352 pos = locale.length() + 1; 353 } 354 if ((delPos = locale.indexOf(",", pos)) != -1) { 355 country = locale.substring(pos, delPos); 356 pos = delPos + 1; } else if (pos <= locale.length()) { 358 country = locale.substring(pos); 359 pos = locale.length() + 1; 360 } 361 if (pos <= locale.length()) { 362 variant=locale.substring(pos); 364 365 } 366 if (variant!=null){ 367 return new Locale(lang,country,variant); 368 }else if (country != null){ 369 return new Locale(lang,country); 371 }else{ 372 return new Locale(lang); 373 } 374 375 } 376 public TimeZone getUserTimeZone(){ 377 String timezone = this.getProperty("timezone"); 378 if(timezone==null){ 379 return TimeZone.getDefault(); 381 } 382 return TimeZone.getTimeZone(timezone); 383 } 384 public void setUserTimeZone(String timezoneid) throws UnauthorizedException{ 385 this.setProperty("timezone",timezoneid); 386 } 387 public void setUserLocale(Locale locale) throws UnauthorizedException{ 388 this.setProperty("Locale",locale.getLanguage()+("".equals(locale.getCountry())?"":","+locale.getCountry())+("".equals(locale.getVariant())?"":","+locale.getVariant())); 389 } 390 391 public void setThreadSubscribe(boolean emailReply) throws UnauthorizedException { 392 this.threadSubscribe = emailReply; 393 setProperty("threadSubscribe",Boolean.toString(emailReply)); 394 } 395 396 public void setPassword(String password) throws UnauthorizedException { 397 this.passwordHash = StringUtils.hash(password); 399 saveToDb(); 400 } 401 402 public String getPasswordHash() throws UnauthorizedException { 403 return passwordHash; 404 } 405 406 public void setPasswordHash(String passwordHash) { 407 this.passwordHash = passwordHash; 408 saveToDb(); 409 } 410 411 public String getEmail() { 412 return StringUtils.escapeHTMLTags(email); 413 } 414 415 public void setEmail(String email) throws UnauthorizedException { 416 this.email = email; 417 saveToDb(); 418 } 419 420 public boolean isEmailVisible() { 421 return emailVisible; 422 } 423 424 public void setEmailVisible(boolean visible) throws UnauthorizedException { 425 this.emailVisible = visible; 426 setProperty("emailvisible",Boolean.toString(visible)); 427 } 428 429 public String getProperty(String name) { 430 return StringUtils.escapeHTMLTags((String )properties.get(name)); 431 } 432 433 public Enumeration propertyNames() { 434 return properties.propertyNames(); 435 } 436 437 public void setProperty(String name, String value) { 438 properties.put(name, value); 439 saveProperties(); 440 } 441 442 public ForumPermissions getPermissions(Authorization authorization) { 443 if (authorization.getUserID() == id || id==-1 || id==0) { 444 return new ForumPermissions(false,false,false,true,false,false,false,false); 445 } 446 else { 447 return ForumPermissions.none(); 448 } 449 } 450 451 public boolean hasPermission(int type) { 452 return true; 453 } 454 455 457 public int getSize() { 458 int size = 0; 461 size += CacheSizes.sizeOfObject(); size += CacheSizes.sizeOfInt(); size += CacheSizes.sizeOfString(username); size += CacheSizes.sizeOfString(passwordHash); size += CacheSizes.sizeOfString(name); size += CacheSizes.sizeOfString(email); size += CacheSizes.sizeOfBoolean(); size += CacheSizes.sizeOfBoolean(); size += CacheSizes.sizeOfObject(); size += CacheSizes.sizeOfProperties(properties); size += CacheSizes.sizeOfLong(); size += CacheSizes.sizeOfLong(); 474 return size; 475 } 476 477 479 484 public String toString() { 485 return username; 486 } 487 488 public int hashCode() { 489 return id; 490 } 491 492 public boolean equals(Object object) { 493 if (this == object) { 494 return true; 495 } 496 if (object != null && object instanceof DbUser) { 497 return id == ((DbUser)object).getID(); 498 } 499 else { 500 return false; 501 } 502 } 503 504 507 private void loadProperties() { 508 if (id == -1 || id == 0) { 510 properties = new Properties(); 511 return; 512 } 513 synchronized(propertyLock) { 516 Properties newProps = new Properties(); 517 Connection con = null; 518 PreparedStatement pstmt = null; 519 try { 520 con = DbConnectionManager.getConnection(); 521 pstmt = con.prepareStatement(LOAD_PROPERTIES); 522 pstmt.setInt(1, id); 523 ResultSet rs = pstmt.executeQuery(); 524 while(rs.next()) { 525 String name = rs.getString("name"); 526 String value = rs.getString("propValue"); 527 newProps.put(name, value); 528 } 529 } 530 catch( SQLException sqle ) { 531 System.err.println("Error in DbUser:loadProperties():" + sqle); 532 sqle.printStackTrace(); 533 } 534 finally { 535 try { pstmt.close(); } 536 catch (Exception e) { e.printStackTrace(); } 537 try { con.close(); } 538 catch (Exception e) { e.printStackTrace(); } 539 } 540 this.properties = newProps; 541 } 542 } 543 544 547 private void loadLastPost() { 548 if (id == -1 || id == 0) { 549 return; 550 } 551 Connection con = null; 552 PreparedStatement pstmt = null; 553 try { 554 con = DbConnectionManager.getConnection(); 555 pstmt = con.prepareStatement(LOAD_LASTPOST); 556 pstmt.setInt(1, id); 557 ResultSet rs = pstmt.executeQuery(); 558 if(rs.next()) { 559 lastpost = Long.parseLong(rs.getString("lastpost")); 560 } 561 } 562 catch( SQLException sqle ) { 563 System.err.println("Error in DbUser:loadProperties():" + sqle); 564 sqle.printStackTrace(); 565 }catch(NumberFormatException e){ 566 lastpost=-1; 567 }finally { 568 try { pstmt.close(); } 569 catch (Exception e) { e.printStackTrace(); } 570 try { con.close(); } 571 catch (Exception e) { e.printStackTrace(); } 572 } 573 574 } 575 576 579 private void saveProperties() { 580 if (id == -1 || id == 0) { 582 return; 583 } 584 synchronized(propertyLock) { 587 Connection con = null; 588 PreparedStatement pstmt = null; 589 try { 590 con = DbConnectionManager.getConnection(); 591 pstmt = con.prepareStatement(DELETE_PROPERTIES); 593 pstmt.setInt(1, id); 594 pstmt.execute(); 595 pstmt.close(); 596 pstmt = con.prepareStatement(INSERT_PROPERTY); 598 Enumeration enume = properties.keys(); 599 while (enume.hasMoreElements()) { 600 String name = (String )enume.nextElement(); 601 String value = (String )properties.get(name); 602 pstmt.setInt(1, id); 603 pstmt.setString(2, name); 604 pstmt.setString(3, value); 605 pstmt.executeUpdate(); 606 } 607 } 608 catch( SQLException sqle ) { 609 System.err.println(sqle); 610 } 611 finally { 612 try { pstmt.close(); } 613 catch (Exception e) { e.printStackTrace(); } 614 try { con.close(); } 615 catch (Exception e) { e.printStackTrace(); } 616 } 617 } 618 } 619 620 623 private void loadFromDb() throws UserNotFoundException { 624 if (id == -1 || id == 0) { 626 return; 627 } 628 String query; 630 if (username != null) { 632 query = LOAD_USER_BY_USERNAME; 633 } 634 else { 636 query = LOAD_USER_BY_ID; 637 } 638 Connection con = null; 639 PreparedStatement pstmt = null; 640 try { 641 con = DbConnectionManager.getConnection(); 642 pstmt = con.prepareStatement(query); 643 if (username != null) { 644 pstmt.setString(1, username); 645 } 646 else { 647 pstmt.setInt(1, id); 648 } 649 650 ResultSet rs = pstmt.executeQuery(); 651 if (!rs.next()) { 652 throw new UserNotFoundException( 653 "Failed to read user " + id + " from database." 654 ); 655 } 656 this.id = rs.getInt(SystemProperty.getProperty("User.Column.UserID")); 657 this.username = rs.getString(SystemProperty.getProperty("User.Column.Username")); 658 this.passwordHash = rs.getString(SystemProperty.getProperty("User.Column.PasswordHash")); 659 this.name = rs.getString(SystemProperty.getProperty("User.Column.Name")); 660 this.email = rs.getString(SystemProperty.getProperty("User.Column.Email")); 661 } 662 catch( SQLException sqle ) { 663 throw new UserNotFoundException( 664 "Failed to read user " + id + " from database.", sqle 665 ); 666 } 667 finally { 668 try { pstmt.close(); } 669 catch (Exception e) { e.printStackTrace(); } 670 try { con.close(); } 671 catch (Exception e) { e.printStackTrace(); } 672 } 673 } 674 675 678 private void insertIntoDb() throws UserAlreadyExistsException{ 679 Connection con = null; 680 PreparedStatement pstmt = null; 681 try { 682 con = DbConnectionManager.getConnection(); 683 pstmt = con.prepareStatement(INSERT_USER); 684 pstmt.setInt(1, id); 685 pstmt.setString(2, username); 686 pstmt.setString(3, passwordHash); 687 pstmt.setString(4, email); 688 pstmt.executeUpdate(); 689 } 690 catch( SQLException sqle ) { 691 System.err.println("Error in DbUser:insertIntoDb()-" + sqle); 692 sqle.printStackTrace(); 693 throw new UserAlreadyExistsException(); 694 } 695 finally { 696 try { pstmt.close(); } 697 catch (Exception e) { e.printStackTrace(); } 698 try { con.close(); } 699 catch (Exception e) { e.printStackTrace(); } 700 } 701 } 702 703 private void insertAutoGroupMembership(){ 704 Connection con = null; 705 PreparedStatement pstmt = null; 706 try { 707 con = DbConnectionManager.getConnection(); 708 pstmt = con.prepareStatement(INSERT_AUTO_GROUP); 709 pstmt.setInt(1, id); 710 pstmt.executeUpdate(); 711 } 712 catch( SQLException sqle ) { 713 System.err.println("Error in DbUser:insertAutoGroupMembership()-" + sqle); 714 sqle.printStackTrace(); 715 } 716 finally { 717 try { pstmt.close(); } 718 catch (Exception e) { e.printStackTrace(); } 719 try { con.close(); } 720 catch (Exception e) { e.printStackTrace(); } 721 } 722 723 } 724 727 private void saveToDb() { 728 if ( id == -1 || id == 0 ) { 729 return; 731 } 732 Connection con = null; 733 PreparedStatement pstmt = null; 734 try { 735 con = DbConnectionManager.getConnection(); 736 pstmt = con.prepareStatement(SAVE_USER); 737 pstmt.setString(1, passwordHash); 738 pstmt.setString(2, email); 739 pstmt.setString(3, name); 740 pstmt.setInt(4, id); 741 pstmt.executeUpdate(); 742 } 743 catch( SQLException sqle ) { 744 System.err.println( "SQLException in DbUser.java:saveToDb(): " + sqle ); 745 sqle.printStackTrace(); 746 } 747 finally { 748 try { pstmt.close(); } 749 catch (Exception e) { e.printStackTrace(); } 750 try { con.close(); } 751 catch (Exception e) { e.printStackTrace(); } 752 } 753 } 754 } 755 | Popular Tags |