1 43 package net.jforum.dao.generic; 44 45 import java.security.NoSuchAlgorithmException ; 46 import java.sql.PreparedStatement ; 47 import java.sql.ResultSet ; 48 import java.sql.Timestamp ; 49 import java.util.ArrayList ; 50 import java.util.Iterator ; 51 import java.util.List ; 52 53 import net.jforum.JForumExecutionContext; 54 import net.jforum.dao.DataAccessDriver; 55 import net.jforum.entities.Group; 56 import net.jforum.entities.KarmaStatus; 57 import net.jforum.entities.User; 58 import net.jforum.sso.LoginAuthenticator; 59 import net.jforum.util.preferences.ConfigKeys; 60 import net.jforum.util.preferences.SystemGlobals; 61 62 66 public class GenericUserDAO extends AutoKeys implements net.jforum.dao.UserDAO 67 { 68 private static LoginAuthenticator loginAuthenticator; 69 70 public GenericUserDAO() 71 { 72 String className = SystemGlobals.getValue(ConfigKeys.LOGIN_AUTHENTICATOR); 73 74 try { 75 loginAuthenticator = (LoginAuthenticator)Class.forName(className).newInstance(); 76 loginAuthenticator.setUserModel(this); 77 } 78 catch (Exception e) { 79 throw new RuntimeException ("Error while trying to instantiate a " 80 + "login.authenticator instance (" + className + "): " + e); 81 } 82 } 83 84 87 public User selectById(int userId) throws Exception 88 { 89 String q = SystemGlobals.getSql("UserModel.selectById"); 90 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(q); 91 p.setInt(1, userId); 92 93 ResultSet rs = p.executeQuery(); 94 User u = new User(); 95 96 if (rs.next()) { 97 this.fillUserFromResultSet(u, rs); 98 u.setPrivateMessagesCount(rs.getInt("private_messages")); 99 100 rs.close(); 101 p.close(); 102 103 p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.selectGroups")); 105 p.setInt(1, userId); 106 107 rs = p.executeQuery(); 108 while (rs.next()) { 109 Group g = new Group(); 110 g.setName(rs.getString("group_name")); 111 g.setId(rs.getInt("group_id")); 112 113 u.getGroupsList().add(g); 114 } 115 } 116 117 rs.close(); 118 p.close(); 119 120 return u; 121 } 122 123 public User selectByName(String username) throws Exception 124 { 125 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.selectByName")); 126 p.setString(1, username); 127 128 ResultSet rs = p.executeQuery(); 129 User u = null; 130 131 if (rs.next()) { 132 u = new User(); 133 fillUserFromResultSet(u, rs); 134 } 135 136 rs.close(); 137 p.close(); 138 139 return u; 140 } 141 142 protected void fillUserFromResultSet(User u, ResultSet rs) throws Exception 143 { 144 u.setAim(rs.getString("user_aim")); 145 u.setAvatar(rs.getString("user_avatar")); 146 u.setGender(rs.getString("gender")); 147 u.setRankId(rs.getInt("rank_id")); 148 u.setThemeId(rs.getInt("themes_id")); 149 u.setPrivateMessagesEnabled(rs.getInt("user_allow_pm") == 1); 150 u.setNotifyOnMessagesEnabled(rs.getInt("user_notify") == 1); 151 u.setViewOnlineEnabled(rs.getInt("user_viewonline") == 1); 152 u.setPassword(rs.getString("user_password")); 153 u.setViewEmailEnabled(rs.getInt("user_viewemail") == 1); 154 u.setViewOnlineEnabled(rs.getInt("user_allow_viewonline") == 1); 155 u.setAvatarEnabled(rs.getInt("user_allowavatar") == 1); 156 u.setBbCodeEnabled(rs.getInt("user_allowbbcode") == 1); 157 u.setHtmlEnabled(rs.getInt("user_allowhtml") == 1); 158 u.setSmiliesEnabled(rs.getInt("user_allowsmilies") == 1); 159 u.setEmail(rs.getString("user_email")); 160 u.setFrom(rs.getString("user_from")); 161 u.setIcq(rs.getString("user_icq")); 162 u.setId(rs.getInt("user_id")); 163 u.setInterests(rs.getString("user_interests")); 164 u.setBiography(rs.getString("user_biography")); 165 u.setLastVisit(rs.getTimestamp("user_lastvisit")); 166 u.setOccupation(rs.getString("user_occ")); 167 u.setTotalPosts(rs.getInt("user_posts")); 168 u.setRegistrationDate(rs.getTimestamp("user_regdate")); 169 u.setSignature(rs.getString("user_sig")); 170 u.setWebSite(rs.getString("user_website")); 171 u.setYim(rs.getString("user_yim")); 172 u.setUsername(rs.getString("username")); 173 u.setAttachSignatureEnabled(rs.getInt("user_attachsig") == 1); 174 u.setMsnm(rs.getString("user_msnm")); 175 u.setLang(rs.getString("user_lang")); 176 u.setActive(rs.getInt("user_active")); 177 u.setKarma(new KarmaStatus(u.getId(), rs.getDouble("user_karma"))); 178 u.setNotifyPrivateMessagesEnabled(rs.getInt("user_notify_pm") == 1); 179 u.setDeleted(rs.getInt("deleted")); 180 181 String actkey = rs.getString("user_actkey"); 182 u.setActivationKey(actkey == null || "".equals(actkey) ? null : actkey); 183 } 184 185 188 public void delete(int userId) throws Exception 189 { 190 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.deletedStatus")); 191 p.setInt(1, 1); 192 p.setInt(2, userId); 193 194 p.executeUpdate(); 195 p.close(); 196 } 197 198 201 public void update(User user) throws Exception 202 { 203 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.update")); 204 205 p.setString(1, user.getAim()); 206 p.setString(2, user.getAvatar()); 207 p.setString(3, user.getGender()); 208 p.setInt(4, user.getThemeId()); 209 p.setInt(5, user.isPrivateMessagesEnabled() ? 1 : 0); 210 p.setInt(6, user.isAvatarEnabled() ? 1 : 0); 211 p.setInt(7, user.isBbCodeEnabled() ? 1 : 0); 212 p.setInt(8, user.isHtmlEnabled() ? 1 : 0); 213 p.setInt(9, user.isSmiliesEnabled() ? 1 : 0); 214 p.setString(10, user.getEmail()); 215 p.setString(11, user.getFrom()); 216 p.setString(12, user.getIcq()); 217 p.setString(13, user.getInterests()); 218 p.setString(14, user.getOccupation()); 219 p.setString(15, user.getSignature()); 220 p.setString(16, user.getWebSite()); 221 p.setString(17, user.getYim()); 222 p.setString(18, user.getMsnm()); 223 p.setString(19, user.getPassword()); 224 p.setInt(20, user.isViewEmailEnabled() ? 1 : 0); 225 p.setInt(21, user.isViewOnlineEnabled() ? 1 : 0); 226 p.setInt(22, user.isNotifyOnMessagesEnabled() ? 1 : 0); 227 p.setInt(23, user.getAttachSignatureEnabled() ? 1 : 0); 228 p.setString(24, user.getUsername()); 229 p.setString(25, user.getLang()); 230 p.setInt(26, user.isNotifyPrivateMessagesEnabled() ? 1 : 0); 231 p.setString(27, user.getBiography()); 232 p.setInt(28, user.getId()); 233 234 p.executeUpdate(); 235 p.close(); 236 } 237 238 241 public int addNew(User user) throws Exception 242 { 243 PreparedStatement p = this.getStatementForAutoKeys("UserModel.addNew"); 244 245 this.initNewUser(user, p); 246 247 this.setAutoGeneratedKeysQuery(SystemGlobals.getSql("UserModel.lastGeneratedUserId")); 248 int id = this.executeAutoKeysQuery(p); 249 p.close(); 250 251 this.addToGroup(id, new int[] { SystemGlobals.getIntValue(ConfigKeys.DEFAULT_USER_GROUP) }); 252 253 user.setId(id); 254 return id; 255 } 256 257 protected void initNewUser(User user, PreparedStatement p) throws Exception 258 { 259 p.setString(1, user.getUsername()); 260 p.setString(2, user.getPassword()); 261 p.setString(3, user.getEmail()); 262 p.setTimestamp(4, new Timestamp (System.currentTimeMillis())); 263 p.setString(5, user.getActivationKey()); 264 } 265 266 269 public void addNewWithId(User user) throws Exception 270 { 271 PreparedStatement p = this.getStatementForAutoKeys("UserModel.addNewWithId"); 272 273 this.initNewUser(user, p); 274 p.setInt(6, user.getId()); 275 276 p.executeUpdate(); 277 p.close(); 278 279 this.addToGroup(user.getId(), new int[] { SystemGlobals.getIntValue(ConfigKeys.DEFAULT_USER_GROUP) }); 280 } 281 282 285 public void decrementPosts(int userId) throws Exception 286 { 287 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.decrementPosts")); 288 p.setInt(1, userId); 289 290 p.executeUpdate(); 291 p.close(); 292 } 293 294 297 public void incrementPosts(int userId) throws Exception 298 { 299 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.incrementPosts")); 300 p.setInt(1, userId); 301 302 p.executeUpdate(); 303 p.close(); 304 } 305 306 309 public void setRanking(int userId, int rankingId) throws Exception 310 { 311 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.rankingId")); 312 p.setInt(1, rankingId); 313 p.setInt(2, userId); 314 315 p.executeUpdate(); 316 p.close(); 317 } 318 319 322 public void setActive(int userId, boolean active) throws Exception 323 { 324 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.activeStatus")); 325 p.setInt(1, active ? 1 : 0); 326 p.setInt(2, userId); 327 328 p.executeUpdate(); 329 p.close(); 330 } 331 332 335 public void undelete(int userId) throws Exception 336 { 337 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.deletedStatus")); 338 p.setInt(1, 0); 339 p.setInt(2, userId); 340 341 p.executeUpdate(); 342 p.close(); 343 } 344 345 348 public List selectAll() throws Exception 349 { 350 return selectAll(0, 0); 351 } 352 353 356 public List selectAll(int startFrom, int count) throws Exception 357 { 358 PreparedStatement p; 359 360 if (count > 0) { 361 p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.selectAllByLimit")); 362 p.setInt(1, startFrom); 363 p.setInt(2, count); 364 } 365 else { 366 p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.selectAll")); 367 } 368 369 ResultSet rs = p.executeQuery(); 370 List list = this.processSelectAll(rs); 371 rs.close(); 372 p.close(); 373 374 return list; 375 } 376 377 380 public List selectAllWithKarma() throws Exception 381 { 382 return this.selectAllWithKarma(0, 0); 383 } 384 385 388 public List selectAllWithKarma(int startFrom, int count) throws Exception 389 { 390 return this.loadKarma( this.selectAll(startFrom, count) ); 391 } 392 393 protected List processSelectAll(ResultSet rs) throws Exception 394 { 395 List list = new ArrayList (); 396 397 while (rs.next()) { 398 User u = new User(); 399 400 u.setEmail(rs.getString("user_email")); 401 u.setId(rs.getInt("user_id")); 402 u.setTotalPosts(rs.getInt("user_posts")); 403 u.setRegistrationDate(rs.getTimestamp("user_regdate")); 404 u.setUsername(rs.getString("username")); 405 u.setDeleted(rs.getInt("deleted")); 406 KarmaStatus karma = new KarmaStatus(); 407 karma.setKarmaPoints(rs.getInt("user_karma")); 408 u.setKarma( karma ); 409 u.setFrom(rs.getString("user_from")); 410 u.setWebSite(rs.getString("user_website")); 411 u.setViewEmailEnabled(rs.getInt("user_viewemail") == 1); 412 413 list.add(u); 414 } 415 416 return list; 417 } 418 419 422 public List selectAllByGroup(int groupId, int start, int count) throws Exception 423 { 424 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.selectAllByGroup")); 425 p.setInt(1, groupId); 426 p.setInt(2, start); 427 p.setInt(3, count); 428 429 ResultSet rs = p.executeQuery(); 430 List l = this.processSelectAll(rs); 431 rs.close(); 432 433 return l; 434 } 435 436 439 public User getLastUserInfo() throws Exception 440 { 441 User u = new User(); 442 443 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.lastUserRegistered")); 444 ResultSet rs = p.executeQuery(); 445 rs.next(); 446 447 u.setUsername(rs.getString("username")); 448 u.setId(rs.getInt("user_id")); 449 450 rs.close(); 451 p.close(); 452 453 return u; 454 } 455 456 459 public int getTotalUsers() throws Exception 460 { 461 return this.getTotalUsersCommon(JForumExecutionContext.getConnection().prepareStatement( 462 SystemGlobals.getSql("UserModel.totalUsers"))); 463 } 464 465 468 public int getTotalUsersByGroup(int groupId) throws Exception 469 { 470 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement( 471 SystemGlobals.getSql("UserModel.totalUsersByGroup")); 472 p.setInt(1, groupId); 473 474 return this.getTotalUsersCommon(p); 475 } 476 477 protected int getTotalUsersCommon(PreparedStatement p) throws Exception 478 { 479 ResultSet rs = p.executeQuery(); 480 rs.next(); 481 482 int total = rs.getInt(1); 483 484 rs.close(); 485 p.close(); 486 487 return total; 488 } 489 490 493 public boolean isDeleted(int userId) throws Exception 494 { 495 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.isDeleted")); 496 p.setInt(1, userId); 497 498 int deleted = 0; 499 500 ResultSet rs = p.executeQuery(); 501 if (rs.next()) { 502 deleted = rs.getInt("deleted"); 503 } 504 505 rs.close(); 506 p.close(); 507 508 return deleted == 1; 509 } 510 511 514 public boolean isUsernameRegistered(String username) throws Exception 515 { 516 boolean status = false; 517 518 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.isUsernameRegistered")); 519 p.setString(1, username); 520 521 ResultSet rs = p.executeQuery(); 522 if (rs.next() && rs.getInt("registered") > 0) { 523 status = true; 524 } 525 526 rs.close(); 527 p.close(); 528 529 return status; 530 } 531 532 535 public User validateLogin(String username, String password) throws NoSuchAlgorithmException , Exception 536 { 537 return loginAuthenticator.validateLogin(username, password, null); 538 } 539 540 543 public void addToGroup(int userId, int[] groupId) throws Exception 544 { 545 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.addToGroup")); 546 p.setInt(1, userId); 547 548 for (int i = 0; i < groupId.length; i++) { 549 p.setInt(2, groupId[i]); 550 p.executeUpdate(); 551 } 552 553 p.close(); 554 } 555 556 559 public void removeFromGroup(int userId, int[] groupId) throws Exception 560 { 561 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.removeFromGroup")); 562 p.setInt(1, userId); 563 564 for (int i = 0; i < groupId.length; i++) { 565 p.setInt(2, groupId[i]); 566 p.executeUpdate(); 567 } 568 569 p.close(); 570 } 571 572 575 public void saveNewPassword(String password, String email) throws Exception 576 { 577 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.saveNewPassword")); 578 p.setString(1, password); 579 p.setString(2, email); 580 p.executeUpdate(); 581 p.close(); 582 } 583 584 587 public boolean validateLostPasswordHash(String email, String hash) throws Exception 588 { 589 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement( 590 SystemGlobals.getSql("UserModel.validateLostPasswordHash")); 591 p.setString(1, hash); 592 p.setString(2, email); 593 594 boolean status = false; 595 596 ResultSet rs = p.executeQuery(); 597 if (rs.next() && rs.getInt("valid") > 0) { 598 status = true; 599 600 this.writeLostPasswordHash(email, ""); 601 } 602 603 rs.close(); 604 p.close(); 605 606 return status; 607 } 608 609 612 public void writeLostPasswordHash(String email, String hash) throws Exception 613 { 614 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.writeLostPasswordHash")); 615 p.setString(1, hash); 616 p.setString(2, email); 617 p.executeUpdate(); 618 p.close(); 619 } 620 621 624 public String getUsernameByEmail(String email) throws Exception 625 { 626 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.getUsernameByEmail")); 627 p.setString(1, email); 628 629 String username = ""; 630 631 ResultSet rs = p.executeQuery(); 632 if (rs.next()) { 633 username = rs.getString("username"); 634 } 635 636 rs.close(); 637 p.close(); 638 639 return username; 640 } 641 642 645 public List findByName(String input, boolean exactMatch) throws Exception 646 { 647 List namesList = new ArrayList (); 648 649 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.findByName")); 650 p.setString(1, exactMatch ? input : "%" + input + "%"); 651 652 ResultSet rs = p.executeQuery(); 653 while (rs.next()) { 654 User u = new User(); 655 656 u.setId(rs.getInt("user_id")); 657 u.setUsername(rs.getString("username")); 658 u.setEmail(rs.getString("user_email")); 659 u.setDeleted(rs.getInt("deleted")); 660 661 namesList.add(u); 662 } 663 664 rs.close(); 665 p.close(); 666 667 return namesList; 668 } 669 670 673 public boolean validateActivationKeyHash(int userId , String hash) throws Exception 674 { 675 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.validateActivationKeyHash")); 676 p.setString(1, hash); 677 p.setInt(2, userId); 678 679 boolean status = false; 680 681 ResultSet rs = p.executeQuery(); 682 if (rs.next() && rs.getInt("valid") == 1) { 683 status = true; 684 } 685 686 rs.close(); 687 p.close(); 688 689 return status; 690 } 691 692 695 public void writeUserActive(int userId) throws Exception 696 { 697 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.writeUserActive")); 698 p.setInt(1, userId); 699 p.executeUpdate(); 700 p.close(); 701 } 702 703 706 public void updateUsername(int userId, String username) throws Exception 707 { 708 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.updateUsername")); 709 p.setString(1, username); 710 p.setInt(2, userId); 711 p.executeUpdate(); 712 p.close(); 713 } 714 715 718 public boolean hasUsernameChanged(int userId, String usernameToCheck) throws Exception 719 { 720 boolean status = false; 721 722 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.getUsername")); 723 p.setString(1, usernameToCheck); 724 p.setInt(2, userId); 725 726 String dbUsername = null; 727 728 ResultSet rs = p.executeQuery(); 729 if (rs.next()) { 730 dbUsername = rs.getString("username"); 731 } 732 733 if (!usernameToCheck.equals(dbUsername)) { 734 status = true; 735 } 736 737 rs.close(); 738 p.close(); 739 740 return status; 741 } 742 743 749 protected List loadKarma(List users) throws Exception { 750 List result = new ArrayList (users.size()); 751 752 User user = null; 753 Iterator iter = users.iterator(); 754 while (iter.hasNext()) { 755 user = (User) iter.next(); 756 DataAccessDriver.getInstance().newKarmaDAO().getUserTotalKarma(user); 758 result.add(user); 759 } 760 return result; 761 } 762 } 763 | Popular Tags |