1 23 24 package org.infoglue.cms.applications.managementtool.actions; 25 26 import java.io.IOException ; 27 import java.util.ArrayList ; 28 import java.util.HashSet ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Set ; 32 33 import org.apache.log4j.Logger; 34 import org.exolab.castor.jdo.Database; 35 import org.infoglue.cms.applications.common.actions.InfoGlueAbstractAction; 36 import org.infoglue.cms.controllers.kernel.impl.simple.AccessRightController; 37 import org.infoglue.cms.controllers.kernel.impl.simple.CastorDatabaseService; 38 import org.infoglue.cms.controllers.kernel.impl.simple.GroupControllerProxy; 39 import org.infoglue.cms.controllers.kernel.impl.simple.RoleControllerProxy; 40 import org.infoglue.cms.controllers.kernel.impl.simple.UserControllerProxy; 41 import org.infoglue.cms.entities.management.AccessRight; 42 import org.infoglue.cms.entities.management.AccessRightGroup; 43 import org.infoglue.cms.entities.management.AccessRightGroupVO; 44 import org.infoglue.cms.entities.management.AccessRightRole; 45 import org.infoglue.cms.entities.management.AccessRightRoleVO; 46 import org.infoglue.cms.entities.management.AccessRightUser; 47 import org.infoglue.cms.entities.management.AccessRightUserVO; 48 import org.infoglue.cms.entities.management.impl.simple.AccessRightUserImpl; 49 import org.infoglue.cms.exception.SystemException; 50 import org.infoglue.cms.security.InfoGlueGroup; 51 import org.infoglue.cms.security.InfoGluePrincipal; 52 import org.infoglue.cms.security.InfoGlueRole; 53 import org.infoglue.cms.util.CmsPropertyHandler; 54 55 58 59 public class AuthorizationSwitchManagementAction extends InfoGlueAbstractAction 60 { 61 private static final long serialVersionUID = 1L; 62 63 private final static Logger logger = Logger.getLogger(AuthorizationSwitchManagementAction.class.getName()); 64 65 private List invalidUsers = new ArrayList (); 66 private List invalidRoles = new ArrayList (); 67 private List invalidGroups = new ArrayList (); 68 69 private List users = new ArrayList (); 70 private List roles = new ArrayList (); 71 private List groups = new ArrayList (); 72 73 private List accessRights = new ArrayList (); 74 75 private String userName; 76 private String roleName; 77 private String groupName; 78 79 private String newUserName; 80 private String newRoleName; 81 private String newGroupName; 82 83 public String doInputUser() throws Exception 84 { 85 this.users = UserControllerProxy.getController().getAllUsers(); 86 this.accessRights = getAccessRightsUser(); 87 88 return INPUT + "User"; 89 } 90 91 public String doInputRole() throws Exception 92 { 93 this.roles = RoleControllerProxy.getController().getAllRoles(); 94 this.accessRights = getAccessRightsRole(); 95 96 return INPUT + "Role"; 97 } 98 99 public String doInputGroup() throws Exception 100 { 101 this.groups = GroupControllerProxy.getController().getAllGroups(); 102 this.accessRights = getAccessRightsGroup(); 103 104 return INPUT + "Group"; 105 } 106 107 public String doUpdateUser() throws Exception 108 { 109 if(this.getInfoGluePrincipal().getIsAdministrator()) 110 updateAccessRightsUser(userName, newUserName); 111 else 112 throw new SystemException("You are not allowed to perform this action."); 113 114 return "successUser"; 115 } 116 117 public String doUpdateRole() throws Exception 118 { 119 if(this.getInfoGluePrincipal().getIsAdministrator()) 120 updateAccessRightsRole(roleName, newRoleName); 121 else 122 throw new SystemException("You are not allowed to perform this action."); 123 124 return "successRole"; 125 } 126 127 public String doUpdateGroup() throws Exception 128 { 129 if(this.getInfoGluePrincipal().getIsAdministrator()) 130 updateAccessRightsGroup(groupName, newGroupName); 131 else 132 throw new SystemException("You are not allowed to perform this action."); 133 134 return "successGroup"; 135 } 136 137 public String doExecute() throws Exception 138 { 139 144 145 return SUCCESS; 146 } 147 148 149 private List getAccessRightsUser() throws Exception 150 { 151 List accessRightUserList = new ArrayList (); 152 153 Database db = CastorDatabaseService.getDatabase(); 154 155 db.begin(); 156 157 try 158 { 159 160 try 161 { 162 accessRightUserList = AccessRightController.getController().getAccessRightUserList(userName, db); 163 } 164 catch(Exception e) 165 { 166 e.printStackTrace(); 167 } 168 169 db.commit(); 170 } 171 catch(Exception e) 172 { 173 logger.error("An error occurred so we should not complete the transaction:" + e, e); 174 db.rollback(); 175 throw new SystemException(e.getMessage()); 176 } 177 finally 178 { 179 db.close(); 180 } 181 182 return accessRightUserList; 183 } 184 185 private List getAccessRightsRole() throws Exception 186 { 187 List accessRightRoleList = new ArrayList (); 188 189 Database db = CastorDatabaseService.getDatabase(); 190 191 db.begin(); 192 193 try 194 { 195 try 196 { 197 accessRightRoleList = AccessRightController.getController().getAccessRightRoleList(roleName, db, false); 198 } 199 catch(Exception e) 200 { 201 logger.error("An error occurred so we should not complete the transaction:" + e, e); 202 } 203 204 db.commit(); 205 } 206 catch(Exception e) 207 { 208 logger.error("An error occurred so we should not complete the transaction:" + e, e); 209 try 210 { 211 db.rollback(); 212 } 213 catch(Exception e2) 214 { 215 logger.error("An error occurred so we should not complete the transaction:" + e2.getMessage()); 216 } 217 throw new SystemException(e.getMessage()); 218 } 219 finally 220 { 221 try 222 { 223 db.close(); 224 } 225 catch(Exception e2) 226 { 227 logger.error("An error occurred so we should not complete the transaction:" + e2.getMessage()); 228 } 229 } 230 231 return accessRightRoleList; 232 } 233 234 private List getAccessRightsGroup() throws Exception 235 { 236 List accessRightGroupList = new ArrayList (); 237 238 Database db = CastorDatabaseService.getDatabase(); 239 240 db.begin(); 241 242 try 243 { 244 245 try 246 { 247 accessRightGroupList = AccessRightController.getController().getAccessRightGroupList(groupName, db); 248 } 249 catch(Exception e) 250 { 251 e.printStackTrace(); 252 } 253 254 db.commit(); 255 } 256 catch(Exception e) 257 { 258 logger.error("An error occurred so we should not complete the transaction:" + e, e); 259 db.rollback(); 260 throw new SystemException(e.getMessage()); 261 } 262 finally 263 { 264 db.close(); 265 } 266 267 return accessRightGroupList; 268 } 269 270 271 private List getInvalidAccessRightsUser() throws Exception 272 { 273 Set invalidUsers = new HashSet (); 274 275 Database db = CastorDatabaseService.getDatabase(); 276 277 db.begin(); 278 279 try 280 { 281 282 try 283 { 284 List users = UserControllerProxy.getController(db).getAllUsers(); 285 List systemUserVOList = AccessRightController.getController().getAccessRightUserVOList(db); 286 287 Iterator i = systemUserVOList.iterator(); 288 289 while(i.hasNext()) 290 { 291 AccessRightUserVO accessRightUserVO = (AccessRightUserVO)i.next(); 292 293 boolean isValid = false; 294 295 Iterator userIterator = users.iterator(); 296 297 while(userIterator.hasNext()) 298 { 299 InfoGluePrincipal principal = (InfoGluePrincipal)userIterator.next(); 300 if(principal.getName().equalsIgnoreCase(accessRightUserVO.getUserName())) 301 { 302 isValid = true; 303 break; 304 } 305 } 306 307 if(!isValid) 308 invalidUsers.add(accessRightUserVO.getUserName()); 309 } 310 } 311 catch(Exception e) 312 { 313 e.printStackTrace(); 314 } 315 316 db.commit(); 317 } 318 catch(Exception e) 319 { 320 logger.error("An error occurred so we should not complete the transaction:" + e, e); 321 db.rollback(); 322 throw new SystemException(e.getMessage()); 323 } 324 finally 325 { 326 db.close(); 327 } 328 329 List invalidUsersList = new ArrayList (); 330 invalidUsersList.addAll(invalidUsers); 331 332 return invalidUsersList; 333 } 334 335 private List getInvalidAccessRightsRole() throws Exception 336 { 337 Set invalidRoles = new HashSet (); 338 339 Database db = CastorDatabaseService.getDatabase(); 340 341 db.begin(); 342 343 try 344 { 345 346 String name = "AccessRightRole names"; 347 String description = "Checks if the Role names given exists in the current authorizationModule."; 348 349 try 350 { 351 List users = RoleControllerProxy.getController(db).getAllRoles(); 352 List systemRoleVOList = AccessRightController.getController().getAccessRightRoleVOList(db); 353 354 Iterator i = systemRoleVOList.iterator(); 355 356 while(i.hasNext()) 357 { 358 AccessRightRoleVO accessRightRoleVO = (AccessRightRoleVO)i.next(); 359 360 boolean isValid = false; 361 362 Iterator userIterator = users.iterator(); 363 364 while(userIterator.hasNext()) 365 { 366 InfoGlueRole role = (InfoGlueRole)userIterator.next(); 367 if(role.getName().equalsIgnoreCase(accessRightRoleVO.getRoleName())) 368 { 369 isValid = true; 370 break; 371 } 372 } 373 374 if(!isValid) 375 invalidRoles.add(accessRightRoleVO.getRoleName()); 376 } 377 } 378 catch(Exception e) 379 { 380 e.printStackTrace(); 381 } 382 383 db.commit(); 384 } 385 catch(Exception e) 386 { 387 logger.error("An error occurred so we should not complete the transaction:" + e, e); 388 db.rollback(); 389 throw new SystemException(e.getMessage()); 390 } 391 finally 392 { 393 db.close(); 394 } 395 396 List invalidRolesList = new ArrayList (); 397 invalidRolesList.addAll(invalidRoles); 398 399 return invalidRolesList; 400 } 401 402 private List getInvalidAccessRightsGroup() throws Exception 403 { 404 Set invalidGroups = new HashSet (); 405 406 Database db = CastorDatabaseService.getDatabase(); 407 408 db.begin(); 409 410 try 411 { 412 413 String name = "AccessRightGroup names"; 414 String description = "Checks if the user names given exists in the current authorizationModule."; 415 416 try 417 { 418 List users = GroupControllerProxy.getController(db).getAllGroups(); 419 List systemGroupVOList = AccessRightController.getController().getAccessRightGroupVOList(db); 420 421 Iterator i = systemGroupVOList.iterator(); 422 423 while(i.hasNext()) 424 { 425 AccessRightGroupVO accessRightGroupVO = (AccessRightGroupVO)i.next(); 426 427 boolean isValid = false; 428 429 Iterator userIterator = users.iterator(); 430 431 while(userIterator.hasNext()) 432 { 433 InfoGlueGroup group = (InfoGlueGroup)userIterator.next(); 434 if(group.getName().equalsIgnoreCase(accessRightGroupVO.getGroupName())) 435 { 436 isValid = true; 437 break; 438 } 439 } 440 441 if(!isValid) 442 invalidGroups.add(accessRightGroupVO.getGroupName()); 443 } 444 } 445 catch(Exception e) 446 { 447 e.printStackTrace(); 448 } 449 450 db.commit(); 451 } 452 catch(Exception e) 453 { 454 logger.error("An error occurred so we should not complete the transaction:" + e, e); 455 db.rollback(); 456 throw new SystemException(e.getMessage()); 457 } 458 finally 459 { 460 db.close(); 461 } 462 463 List invalidGroupsList = new ArrayList (); 464 invalidGroupsList.addAll(invalidGroupsList); 465 466 return invalidGroupsList; 467 } 468 469 private void updateAccessRightsUser(String userName, String newUserName) throws Exception 470 { 471 Database db = CastorDatabaseService.getDatabase(); 472 473 db.begin(); 474 475 try 476 { 477 try 478 { 479 List accessRightUserList = AccessRightController.getController().getAccessRightUserList(userName, db); 480 481 Iterator i = accessRightUserList.iterator(); 482 483 while(i.hasNext()) 484 { 485 AccessRightUser accessRightUser = (AccessRightUser)i.next(); 486 AccessRight accessRight = accessRightUser.getAccessRight(); 487 488 boolean exists = false; 489 Iterator usersIterator = accessRight.getUsers().iterator(); 490 while(usersIterator.hasNext()) 491 { 492 AccessRightUser currentAccessRightUser = (AccessRightUser)usersIterator.next(); 493 if(currentAccessRightUser.getUserName().equals(newUserName)) 494 exists = true; 495 } 496 497 if(!exists) 498 { 499 AccessRightUserVO accessRightUserVO = new AccessRightUserVO(); 501 accessRightUserVO.setUserName(newUserName); 502 AccessRightUser newAccessRightUser = AccessRightController.getController().createAccessRightUser(db, accessRightUserVO, accessRight); 503 accessRight.getUsers().add(newAccessRightUser); 504 } 505 } 506 } 507 catch(Exception e) 508 { 509 e.printStackTrace(); 510 } 511 512 db.commit(); 513 } 514 catch(Exception e) 515 { 516 logger.error("An error occurred so we should not complete the transaction:" + e, e); 517 db.rollback(); 518 throw new SystemException(e.getMessage()); 519 } 520 finally 521 { 522 db.close(); 523 } 524 } 525 526 private void updateAccessRightsRole(String roleName, String newRoleName) throws Exception 527 { 528 Database db = CastorDatabaseService.getDatabase(); 529 530 db.begin(); 531 532 try 533 { 534 try 535 { 536 List accessRightRoleList = AccessRightController.getController().getAccessRightRoleList(roleName, db, false); 537 538 Iterator i = accessRightRoleList.iterator(); 539 540 while(i.hasNext()) 541 { 542 AccessRightRole accessRightRole = (AccessRightRole)i.next(); 543 AccessRight accessRight = accessRightRole.getAccessRight(); 544 545 if(accessRight != null) 546 { 547 boolean exists = false; 548 549 Iterator rolesIterator = accessRight.getRoles().iterator(); 550 while(rolesIterator.hasNext()) 551 { 552 AccessRightRole currentAccessRightRole = (AccessRightRole)rolesIterator.next(); 553 if(currentAccessRightRole.getRoleName().equals(newRoleName)) 554 exists = true; 555 } 556 557 if(!exists) 558 { 559 AccessRightRoleVO accessRightRoleVO = new AccessRightRoleVO(); 561 accessRightRoleVO.setRoleName(newRoleName); 562 AccessRightRole newAccessRightRole = AccessRightController.getController().createAccessRightRole(db, accessRightRoleVO, accessRight); 563 accessRight.getRoles().add(newAccessRightRole); 564 } 565 } 566 } 567 } 568 catch(Exception e) 569 { 570 e.printStackTrace(); 571 } 572 573 db.commit(); 574 } 575 catch(Exception e) 576 { 577 logger.error("An error occurred so we should not complete the transaction:" + e, e); 578 db.rollback(); 579 throw new SystemException(e.getMessage()); 580 } 581 finally 582 { 583 db.close(); 584 } 585 } 586 587 private void updateAccessRightsGroup(String groupName, String newGroupName) throws Exception 588 { 589 Database db = CastorDatabaseService.getDatabase(); 590 591 db.begin(); 592 593 try 594 { 595 try 596 { 597 List accessRightGroupList = AccessRightController.getController().getAccessRightGroupList(groupName, db); 598 599 Iterator i = accessRightGroupList.iterator(); 600 601 while(i.hasNext()) 602 { 603 AccessRightGroup accessRightGroup = (AccessRightGroup)i.next(); 604 AccessRight accessRight = accessRightGroup.getAccessRight(); 605 606 boolean exists = false; 607 Iterator groupsIterator = accessRight.getGroups().iterator(); 608 while(groupsIterator.hasNext()) 609 { 610 AccessRightGroup currentAccessRightGroup = (AccessRightGroup)groupsIterator.next(); 611 if(currentAccessRightGroup.getGroupName().equals(newGroupName)) 612 exists = true; 613 } 614 615 if(!exists) 616 { 617 AccessRightGroupVO accessRightGroupVO = new AccessRightGroupVO(); 619 accessRightGroupVO.setGroupName(newGroupName); 620 AccessRightGroup newAccessRightGroup = AccessRightController.getController().createAccessRightGroup(db, accessRightGroupVO, accessRight); 621 accessRight.getGroups().add(newAccessRightGroup); 622 } 623 } 624 } 625 catch(Exception e) 626 { 627 e.printStackTrace(); 628 } 629 630 db.commit(); 631 } 632 catch(Exception e) 633 { 634 logger.error("An error occurred so we should not complete the transaction:" + e, e); 635 db.rollback(); 636 throw new SystemException(e.getMessage()); 637 } 638 finally 639 { 640 db.close(); 641 } 642 } 643 644 public List getInvalidGroups() 645 { 646 return invalidGroups; 647 } 648 649 public List getInvalidRoles() 650 { 651 return invalidRoles; 652 } 653 654 public List getInvalidUsers() 655 { 656 return invalidUsers; 657 } 658 659 public String getGroupName() 660 { 661 return groupName; 662 } 663 664 public void setGroupName(String groupName) 665 { 666 this.groupName = groupName; 667 } 668 669 public String getRoleName() 670 { 671 return roleName; 672 } 673 674 public void setRoleName(String roleName) 675 { 676 this.roleName = roleName; 677 } 678 679 public String getUserName() 680 { 681 return userName; 682 } 683 684 public void setUserName(String userName) 685 { 686 this.userName = userName; 687 } 688 689 public String getNewGroupName() 690 { 691 return newGroupName; 692 } 693 694 public void setNewGroupName(String newGroupName) 695 { 696 this.newGroupName = newGroupName; 697 } 698 699 public String getNewRoleName() 700 { 701 return newRoleName; 702 } 703 704 public void setNewRoleName(String newRoleName) 705 { 706 this.newRoleName = newRoleName; 707 } 708 709 public String getNewUserName() 710 { 711 return newUserName; 712 } 713 714 public void setNewUserName(String newUserName) 715 { 716 this.newUserName = newUserName; 717 } 718 719 public List getGroups() 720 { 721 return groups; 722 } 723 724 public List getRoles() 725 { 726 return roles; 727 } 728 729 public List getUsers() 730 { 731 return users; 732 } 733 734 public List getAccessRights() 735 { 736 return accessRights; 737 } 738 739 } 740 | Popular Tags |