1 23 24 package org.infoglue.cms.security; 25 26 import java.io.Serializable ; 27 import java.sql.Connection ; 28 import java.sql.Driver ; 29 import java.sql.PreparedStatement ; 30 import java.sql.ResultSet ; 31 import java.sql.SQLException ; 32 import java.util.ArrayList ; 33 import java.util.Collection ; 34 import java.util.Iterator ; 35 import java.util.List ; 36 import java.util.Properties ; 37 38 import org.apache.log4j.Logger; 39 import org.exolab.castor.jdo.Database; 40 import org.infoglue.cms.controllers.kernel.impl.simple.BaseController; 41 import org.infoglue.cms.controllers.kernel.impl.simple.CastorDatabaseService; 42 import org.infoglue.cms.controllers.kernel.impl.simple.GroupController; 43 import org.infoglue.cms.controllers.kernel.impl.simple.RoleController; 44 import org.infoglue.cms.controllers.kernel.impl.simple.SystemUserController; 45 import org.infoglue.cms.entities.kernel.BaseEntityVO; 46 import org.infoglue.cms.entities.management.Group; 47 import org.infoglue.cms.entities.management.GroupVO; 48 import org.infoglue.cms.entities.management.Role; 49 import org.infoglue.cms.entities.management.RoleVO; 50 import org.infoglue.cms.entities.management.SystemUser; 51 import org.infoglue.cms.entities.management.SystemUserVO; 52 import org.infoglue.cms.exception.SystemException; 53 import org.infoglue.cms.util.CmsPropertyHandler; 54 55 60 61 public class InfoGlueJDBCAuthorizationModule extends BaseController implements AuthorizationModule, Serializable 62 { 63 private final static Logger logger = Logger.getLogger(InfoGlueJDBCAuthorizationModule.class.getName()); 64 65 private Properties extraProperties = null; 66 private transient Database transactionObject = null; 67 68 protected String connectionName = null; 69 protected String connectionPassword = null; 70 protected String connectionURL = null; 71 protected Driver driver = null; 72 protected String driverName = null; 73 74 77 78 public boolean getSupportUpdate() 79 { 80 return false; 81 } 82 83 86 87 public boolean getSupportDelete() 88 { 89 return false; 90 } 91 92 95 96 public boolean getSupportCreate() 97 { 98 return false; 99 } 100 101 107 protected Connection getConnection() throws SQLException 108 { 109 if(connectionURL == null) 110 connectionURL = this.extraProperties.getProperty("jdbc.connectionURL"); 111 112 if(connectionName == null) 113 connectionName = this.extraProperties.getProperty("jdbc.connectionName"); 114 115 if(connectionPassword == null) 116 connectionPassword = this.extraProperties.getProperty("jdbc.connectionPassword"); 117 118 if(driverName == null) 119 driverName = this.extraProperties.getProperty("jdbc.driverName"); 120 121 Connection conn = null; 122 123 if (driver == null) 125 { 126 try 127 { 128 Class clazz = Class.forName(driverName); 129 driver = (Driver ) clazz.newInstance(); 130 } 131 catch (Throwable e) 132 { 133 throw new SQLException (e.getMessage()); 134 } 135 } 136 137 Properties props = new Properties (); 139 if (connectionName != null) 140 props.put("user", connectionName); 141 142 if (connectionPassword != null) 143 props.put("password", connectionPassword); 144 145 conn = driver.connect(connectionURL, props); 146 conn.setAutoCommit(false); 147 148 return (conn); 149 150 } 151 152 156 157 public InfoGluePrincipal getAuthorizedInfoGluePrincipal(String userName) throws Exception 158 { 159 logger.info("getAuthorizedInfoGluePrincipal with userName:" + userName); 160 161 if(userName == null || userName.equals("")) 162 { 163 logger.warn("userName was null or empty - fix your templates:" + userName); 164 return null; 165 } 166 167 InfoGluePrincipal infogluePrincipal = null; 168 169 String administratorUserName = CmsPropertyHandler.getAdministratorUserName(); 170 String administratorEmail = CmsPropertyHandler.getAdministratorEmail(); 171 172 final boolean isAdministrator = (userName != null && userName.equalsIgnoreCase(administratorUserName)) ? true : false; 173 if(isAdministrator) 174 { 175 infogluePrincipal = new InfoGluePrincipal(userName, "System", "Administrator", administratorEmail, new ArrayList (), new ArrayList (), isAdministrator, this); 176 } 177 else 178 { 179 List roles = new ArrayList (); 180 List groups = new ArrayList (); 181 182 ResultSet rs = null; 183 Connection conn = null; 184 PreparedStatement ps = null; 185 186 try 187 { 188 String userFirstNameColumn = this.extraProperties.getProperty("jdbc.userFirstNameColumn"); 189 if(userFirstNameColumn == null || userFirstNameColumn.equals("")) 190 userFirstNameColumn = "USER_FIRSTNAME"; 191 192 String userLastNameColumn = this.extraProperties.getProperty("jdbc.userLastNameColumn"); 193 if(userLastNameColumn == null || userLastNameColumn.equals("")) 194 userLastNameColumn = "USER_LASTNAME"; 195 196 String userEmailColumn = this.extraProperties.getProperty("jdbc.userEmailColumn"); 197 if(userEmailColumn == null || userEmailColumn.equals("")) 198 userEmailColumn = "USER_EMAIL"; 199 200 String roleNameColumn = this.extraProperties.getProperty("jdbc.roleNameColumn"); 201 if(roleNameColumn == null || roleNameColumn.equals("")) 202 roleNameColumn = "ROLE_NAME"; 203 204 String roleDescriptionColumn = this.extraProperties.getProperty("jdbc.roleDescriptionColumn"); 205 if(roleDescriptionColumn == null || roleDescriptionColumn.equals("")) 206 roleDescriptionColumn = "ROLE_DESCRIPTION"; 207 208 String sql = this.extraProperties.getProperty("jdbc.userRolesSQL"); 209 if(sql == null || sql.equals("")) 210 sql = "SELECT * from USER, ROLE_USER, ROLE where ROLE_USER.USER = USER.ID AND ROLE_USER.ROLE = ROLE.ID AND USER.USER_NAME = ?"; 211 212 conn = getConnection(); 213 214 ps = conn.prepareStatement(sql); 215 ps.setString(1, userName); 216 217 rs = ps.executeQuery(); 218 while(rs.next()) 219 { 220 logger.info("infoGluePrincipal:" + infogluePrincipal); 221 if(infogluePrincipal != null) 222 { 223 String roleName = rs.getString(roleNameColumn); 224 String description = rs.getString(roleDescriptionColumn); 225 226 InfoGlueRole infoGlueRole = new InfoGlueRole(roleName, description, this); 227 infogluePrincipal.getRoles().add(infoGlueRole); 228 logger.info("Added role:" + infoGlueRole.getName()); 229 } 230 else 231 { 232 String userFirstName = rs.getString(userFirstNameColumn); 233 String userLastName = rs.getString(userLastNameColumn); 234 String userEmail = rs.getString(userEmailColumn); 235 236 if(userFirstName == null) 237 userFirstName = userName; 238 239 if(userLastName == null) 240 userLastName = userName; 241 242 if(userEmail == null) 243 userEmail = userName; 244 245 String roleName = rs.getString(roleNameColumn); 246 String description = rs.getString(roleDescriptionColumn); 247 248 InfoGlueRole infoGlueRole = new InfoGlueRole(roleName, description, this); 249 250 infogluePrincipal = new InfoGluePrincipal(userName, userFirstName, userLastName, userEmail, new ArrayList (), groups, false, this); 251 infogluePrincipal.getRoles().add(infoGlueRole); 252 253 logger.info("User read:" + infogluePrincipal.getName()); 254 } 255 256 } 257 258 } 259 catch (Exception e) 260 { 261 e.printStackTrace(); 262 logger.info("An error occurred trying to get jdbc user for " + userName + ":" + e); 263 throw new SystemException(e.getMessage()); 264 } 265 finally 266 { 267 if (rs != null) 268 { 269 try 270 { 271 rs.close(); 272 } 273 catch (SQLException e) {} 274 } 275 if (ps != null) 276 { 277 try 278 { 279 ps.close(); 280 } 281 catch (SQLException e) {} 282 } 283 if (conn != null) 284 { 285 try 286 { 287 conn.close(); 288 } 289 catch (Exception ex) {} 290 } 291 } 292 293 logger.info("returning from getAuthorizedInfoGluePrincipal with userName:" + userName); 294 } 295 296 return infogluePrincipal; 297 } 298 299 302 303 public InfoGlueRole getAuthorizedInfoGlueRole(String roleName) throws Exception 304 { 305 InfoGlueRole infoglueRole = null; 306 307 ResultSet rs = null; 308 Connection conn = null; 309 PreparedStatement ps = null; 310 311 try 312 { 313 String roleDescriptionColumn = this.extraProperties.getProperty("jdbc.roleDescriptionColumn"); 314 if(roleDescriptionColumn == null || roleDescriptionColumn.equals("")) 315 roleDescriptionColumn = "ROLE_DESCRIPTION"; 316 317 String sql = this.extraProperties.getProperty("jdbc.roleSQL"); 318 if(sql == null || sql.equals("")) 319 sql = "SELECT * from ROLE where ROLE.ROLE_NAME = ?"; 320 321 conn = getConnection(); 322 323 ps = conn.prepareStatement(sql); 324 ps.setString(1, roleName); 325 326 rs = ps.executeQuery(); 327 while(rs.next()) 328 { 329 String description = rs.getString(roleDescriptionColumn); 330 331 infoglueRole = new InfoGlueRole(roleName, description, this); 332 } 333 334 logger.info("Role created:" + infoglueRole.getName()); 335 } 336 catch (Exception e) 337 { 338 logger.info("An error occurred trying to get jdbc user for " + roleName + ":" + e); 339 throw new SystemException(e.getMessage()); 340 } 341 finally 342 { 343 if (rs != null) 344 { 345 try 346 { 347 rs.close(); 348 } 349 catch (SQLException e) {} 350 } 351 if (ps != null) 352 { 353 try 354 { 355 ps.close(); 356 } 357 catch (SQLException e) {} 358 } 359 if (conn != null) 360 { 361 try 362 { 363 conn.close(); 364 } 365 catch (Exception ex) {} 366 } 367 } 368 369 return infoglueRole; 370 } 371 372 375 376 public InfoGlueGroup getAuthorizedInfoGlueGroup(String groupName) throws Exception 377 { 378 InfoGlueGroup infoglueGroup = null; 379 380 return infoglueGroup; 381 } 382 383 384 387 388 public List authorizeUser(String userName) throws Exception 389 { 390 List roles = new ArrayList (); 391 List groups = new ArrayList (); 392 393 String administratorUserName = CmsPropertyHandler.getAdministratorUserName(); 394 395 boolean isAdministrator = userName.equalsIgnoreCase(administratorUserName) ? true : false; 396 if(isAdministrator) 397 return roles; 398 399 if(transactionObject == null) 400 { 401 List roleVOList = RoleController.getController().getRoleVOList(userName); 402 Iterator roleVOListIterator = roleVOList.iterator(); 403 while(roleVOListIterator.hasNext()) 404 { 405 RoleVO roleVO = (RoleVO)roleVOListIterator.next(); 406 InfoGlueRole infoGlueRole = new InfoGlueRole(roleVO.getRoleName(), roleVO.getDescription(), this); 407 roles.add(infoGlueRole); 408 } 409 410 List groupVOList = GroupController.getController().getGroupVOList(userName); 411 Iterator groupVOListIterator = groupVOList.iterator(); 412 while(groupVOListIterator.hasNext()) 413 { 414 GroupVO groupVO = (GroupVO)groupVOListIterator.next(); 415 InfoGlueGroup infoGlueGroup = new InfoGlueGroup(groupVO.getGroupName(), groupVO.getDescription(), this); 416 groups.add(infoGlueGroup); 417 } 418 } 419 else 420 { 421 Collection roleList = RoleController.getController().getRoleList(userName, transactionObject); 422 Iterator roleListIterator = roleList.iterator(); 423 while(roleListIterator.hasNext()) 424 { 425 Role role = (Role)roleListIterator.next(); 426 InfoGlueRole infoGlueRole = new InfoGlueRole(role.getRoleName(), role.getDescription(), this); 427 roles.add(infoGlueRole); 428 } 429 430 Collection groupList = GroupController.getController().getGroupList(userName, transactionObject); 431 Iterator groupListIterator = groupList.iterator(); 432 while(groupListIterator.hasNext()) 433 { 434 Group group = (Group)groupListIterator.next(); 435 InfoGlueGroup infoGlueGroup = new InfoGlueGroup(group.getGroupName(), group.getDescription(), this); 436 groups.add(infoGlueGroup); 437 } 438 } 439 440 return groups; 441 } 442 443 446 447 public List getRoles() throws Exception 448 { 449 List roles = new ArrayList (); 450 451 ResultSet rs = null; 452 Connection conn = null; 453 PreparedStatement ps = null; 454 455 try 456 { 457 String roleNameColumn = this.extraProperties.getProperty("jdbc.roleNameColumn"); 458 if(roleNameColumn == null || roleNameColumn.equals("")) 459 roleNameColumn = "ROLE_NAME"; 460 461 String roleDescriptionColumn = this.extraProperties.getProperty("jdbc.roleDescriptionColumn"); 462 if(roleDescriptionColumn == null || roleDescriptionColumn.equals("")) 463 roleDescriptionColumn = "ROLE_DESCRIPTION"; 464 465 String sql = this.extraProperties.getProperty("jdbc.rolesSQL"); 466 if(sql == null || sql.equals("")) 467 sql = "SELECT * from ROLE ORDER BY ROLE_NAME"; 468 469 conn = getConnection(); 470 471 ps = conn.prepareStatement(sql); 472 473 rs = ps.executeQuery(); 474 while(rs.next()) 475 { 476 String roleName = rs.getString(roleNameColumn); 477 String description = rs.getString(roleDescriptionColumn); 478 479 InfoGlueRole infoGlueRole = new InfoGlueRole(roleName, description, this); 480 roles.add(infoGlueRole); 481 482 logger.info("Role created:" + infoGlueRole.getName()); 483 } 484 } 485 catch (Exception e) 486 { 487 logger.info("An error occurred trying to get all roles:" + e); 488 throw new SystemException(e.getMessage()); 489 } 490 finally 491 { 492 if (rs != null) 493 { 494 try 495 { 496 rs.close(); 497 } 498 catch (SQLException e) {} 499 } 500 if (ps != null) 501 { 502 try 503 { 504 ps.close(); 505 } 506 catch (SQLException e) {} 507 } 508 if (conn != null) 509 { 510 try 511 { 512 conn.close(); 513 } 514 catch (Exception ex) {} 515 } 516 } 517 518 return roles; 519 } 520 521 public List getGroups() throws Exception 522 { 523 List groups = new ArrayList (); 524 525 return groups; 526 } 527 528 529 532 533 public List getUsers() throws Exception 534 { 535 List users = new ArrayList (); 536 537 ResultSet rs = null; 538 Connection conn = null; 539 PreparedStatement ps = null; 540 541 try 542 { 543 String userNameColumn = this.extraProperties.getProperty("jdbc.userNameColumn"); 544 if(userNameColumn == null || userNameColumn.equals("")) 545 userNameColumn = "USER_NAME"; 546 547 String userFirstNameColumn = this.extraProperties.getProperty("jdbc.userFirstNameColumn"); 548 if(userFirstNameColumn == null || userFirstNameColumn.equals("")) 549 userFirstNameColumn = "USER_FIRSTNAME"; 550 551 String userLastNameColumn = this.extraProperties.getProperty("jdbc.userLastNameColumn"); 552 if(userLastNameColumn == null || userLastNameColumn.equals("")) 553 userLastNameColumn = "USER_LASTNAME"; 554 555 String userEmailColumn = this.extraProperties.getProperty("jdbc.userEmailColumn"); 556 if(userEmailColumn == null || userEmailColumn.equals("")) 557 userEmailColumn = "USER_EMAIL"; 558 559 String roleNameColumn = this.extraProperties.getProperty("jdbc.roleNameColumn"); 560 if(roleNameColumn == null || roleNameColumn.equals("")) 561 roleNameColumn = "ROLE_NAME"; 562 563 String roleDescriptionColumn = this.extraProperties.getProperty("jdbc.roleDescriptionColumn"); 564 if(roleDescriptionColumn == null || roleDescriptionColumn.equals("")) 565 roleDescriptionColumn = "ROLE_DESCRIPTION"; 566 567 String sql = this.extraProperties.getProperty("jdbc.usersRolesSQL"); 568 if(sql == null || sql.equals("")) 569 sql = "SELECT * from USER, ROLE_USER, ROLE where ROLE_USER.USER = USER.ID AND ROLE_USER.ROLE = ROLE.ID ORDER BY USER.USER_NAME"; 570 571 conn = getConnection(); 572 573 ps = conn.prepareStatement(sql); 574 575 String oldUserName = ""; 576 577 List roles = new ArrayList (); 578 List groups = new ArrayList (); 579 580 String userFirstName = null; 581 String userLastName = null; 582 String userEmail = null; 583 584 InfoGluePrincipal infoGluePrincipal = null; 585 586 rs = ps.executeQuery(); 587 while(rs.next()) 588 { 589 String userName = rs.getString(userNameColumn); 590 591 logger.info("userName:" + userName); 592 logger.info("oldUserName:" + oldUserName); 593 if(userName.equals(oldUserName)) 594 { 595 String roleName = rs.getString(roleNameColumn); 596 String description = rs.getString(roleDescriptionColumn); 597 598 InfoGlueRole infoGlueRole = new InfoGlueRole(roleName, description, this); 599 infoGluePrincipal.getRoles().add(infoGlueRole); 600 } 601 else 602 { 603 userFirstName = rs.getString(userFirstNameColumn); 604 userLastName = rs.getString(userLastNameColumn); 605 userEmail = rs.getString(userEmailColumn); 606 607 610 if(userFirstName == null) 611 userFirstName = userName; 612 613 if(userLastName == null) 614 userLastName = userName; 615 616 if(userEmail == null) 617 userEmail = userName; 618 619 String roleName = rs.getString(roleNameColumn); 620 String description = rs.getString(roleDescriptionColumn); 621 622 InfoGlueRole infoGlueRole = new InfoGlueRole(roleName, description, this); 623 624 infoGluePrincipal = new InfoGluePrincipal(userName, userFirstName, userLastName, userEmail, new ArrayList (), groups, false, this); 625 infoGluePrincipal.getRoles().add(infoGlueRole); 626 users.add(infoGluePrincipal); 627 628 logger.info("User read:" + infoGluePrincipal.getName()); 629 } 630 631 oldUserName = userName; 632 } 633 634 } 635 catch (Exception e) 636 { 637 logger.info("An error occurred trying to get all roles:" + e); 638 throw new SystemException(e.getMessage()); 639 } 640 finally 641 { 642 if (rs != null) 643 { 644 try 645 { 646 rs.close(); 647 } 648 catch (SQLException e) {} 649 } 650 if (ps != null) 651 { 652 try 653 { 654 ps.close(); 655 } 656 catch (SQLException e) {} 657 } 658 if (conn != null) 659 { 660 try 661 { 662 conn.close(); 663 } 664 catch (Exception ex) {} 665 } 666 } 667 668 return users; 669 } 670 671 public List getFilteredUsers(String firstName, String lastName, String userName, String email, String [] roleIds) throws Exception 672 { 673 return getUsers(); 674 } 675 676 public List getUsers(String roleName) throws Exception 677 { 678 return getRoleUsers(roleName); 679 } 680 681 public List getRoleUsers(String roleName) throws Exception 682 { 683 logger.info("roleName:" + roleName); 684 List users = new ArrayList (); 685 686 return users; 687 } 688 689 public List getGroupUsers(String groupName) throws Exception 690 { 691 logger.info("groupName:" + groupName); 692 List users = new ArrayList (); 693 694 return users; 695 } 696 697 public void createInfoGluePrincipal(SystemUserVO systemUserVO) throws Exception 698 { 699 throw new SystemException("The JDBC BASIC Authorization module does not support creation of users yet..."); 700 } 701 702 public void updateInfoGluePrincipal(SystemUserVO systemUserVO, String [] roleNames) throws Exception 703 { 704 throw new SystemException("The JDBC BASIC Authorization module does not support updating of users yet..."); 705 } 706 707 public void updateInfoGluePrincipalPassword(String userName) throws Exception 708 { 709 throw new SystemException("The JDBC BASIC Authorization module does not support updates of users yet..."); 710 } 711 712 public void updateInfoGluePrincipalPassword(String userName, String oldPassword, String newPassword) throws Exception 713 { 714 throw new SystemException("The JDBC BASIC Authorization module does not support updates of user password yet..."); 715 } 716 717 public void deleteInfoGluePrincipal(String userName) throws Exception 718 { 719 throw new SystemException("The JDBC BASIC Authorization module does not support deletion of users yet..."); 720 } 721 722 public void createInfoGlueRole(RoleVO roleVO) throws Exception 723 { 724 throw new SystemException("The JDBC BASIC Authorization module does not support creation of users yet..."); 725 } 726 727 public void updateInfoGlueRole(RoleVO roleVO, String [] userNames) throws Exception 728 { 729 throw new SystemException("The JDBC BASIC Authorization module does not support updates of users yet..."); 730 } 731 732 public void deleteInfoGlueRole(String roleName) throws Exception 733 { 734 throw new SystemException("The JDBC BASIC Authorization module does not support deletion of roles yet..."); 735 } 736 737 public void updateInfoGluePrincipal(SystemUserVO systemUserVO, String [] roleNames, String [] groupNames) throws Exception 738 { 739 throw new SystemException("The JDBC BASIC Authorization module does not support deletion of roles yet..."); 740 } 741 742 public void createInfoGlueGroup(GroupVO groupVO) throws Exception 743 { 744 throw new SystemException("The JDBC BASIC Authorization module does not support deletion of roles yet..."); 745 } 746 747 public void updateInfoGlueGroup(GroupVO roleVO, String [] userNames) throws Exception 748 { 749 throw new SystemException("The JDBC BASIC Authorization module does not support deletion of roles yet..."); 750 } 751 752 public void deleteInfoGlueGroup(String groupName) throws Exception 753 { 754 throw new SystemException("The JDBC BASIC Authorization module does not support deletion of roles yet..."); 755 } 756 757 public Properties getExtraProperties() 758 { 759 return extraProperties; 760 } 761 762 public void setExtraProperties(Properties extraProperties) 763 { 764 this.extraProperties = extraProperties; 765 } 766 767 public Object getTransactionObject() 768 { 769 return this.transactionObject; 770 } 771 772 public void setTransactionObject(Object transactionObject) 773 { 774 this.transactionObject = (Database)transactionObject; 775 } 776 777 public BaseEntityVO getNewVO() 778 { 779 return null; 780 } 781 782 783 } 784 | Popular Tags |