1 64 package com.jcorporate.expresso.core.dbobj; 65 66 import com.jcorporate.expresso.core.db.DBConnection; 67 import com.jcorporate.expresso.core.db.DBException; 68 import com.jcorporate.expresso.core.registry.MutableRequestRegistry; 69 import com.jcorporate.expresso.core.registry.RequestRegistry; 70 import com.jcorporate.expresso.core.security.User; 71 import com.jcorporate.expresso.services.dbobj.RowGroupPerms; 72 import com.jcorporate.expresso.services.dbobj.RowPermissions; 73 import com.jcorporate.expresso.services.dbobj.UserGroup; 74 75 import java.util.ArrayList ; 76 import java.util.Collection ; 77 import java.util.Iterator ; 78 import java.util.List ; 79 80 81 115 public class RowSecuredDBObject extends SecuredDBObject { 116 117 120 private static final String CREATED_NEW = "CREATED_NEW"; 121 122 123 134 public RowSecuredDBObject() throws DBException { 135 try { 136 User user = RequestRegistry.getUser(); 137 try { 138 user.getUid(); 139 this.setRequestingUid(user.getUid()); 140 } catch (Throwable e) { getLogger().warn("Cannot get user from request registry for class: '" 142 + getClass().getName() 143 + "'. No user has been associated with this thread yet. (see " 144 + MutableRequestRegistry.class.getName() + ")"); 145 146 super.setRequestingUid(User.getIdFromLogin(User.UNKNOWN_USER, 150 getDataContext())); 151 } 152 153 } catch (Exception ex) { 154 155 super.setRequestingUid(User.getIdFromLogin(User.UNKNOWN_USER, 159 getDataContext())); 160 161 getLogger().warn("Problem automatically setting user ID: " 166 + getClass().getName() + ", err: " + ex.getClass().getName() 167 + ", msg: " + ex.getMessage()); 168 } 169 170 checkKeyLength(); 172 } 173 174 185 public RowSecuredDBObject(DBConnection theConnection, int theUser) 186 throws DBException { 187 super(theConnection, theUser); 188 189 checkKeyLength(); 191 } 192 193 204 public RowSecuredDBObject(int theUser) throws DBException { 205 super(theUser); 206 207 try { 208 setDataContext(RequestRegistry.getDataContext()); 209 } catch (IllegalStateException e) { 210 } 213 214 checkKeyLength(); 216 } 217 218 224 public RowSecuredDBObject(RequestContext request) throws DBException { 225 super(request); 226 227 checkKeyLength(); 229 } 230 231 236 public List getAdministrateGroups() throws DBException { 237 List allGroups = this.getGroups(); 238 239 if (allGroups.size() == 0) { 240 return allGroups; 241 } 242 243 List permGroups = new ArrayList (); 244 245 for (Iterator iterator = allGroups.iterator(); iterator.hasNext();) { 246 RowGroupPerms perms = (RowGroupPerms) iterator.next(); 247 248 if (perms.canGroupAdministrate()) { 249 permGroups.add(perms.group()); 250 } 251 } 252 253 return permGroups; 254 } 255 256 264 public List getGroups() throws DBException { 265 String primeKey = getKey(); 266 267 RowGroupPerms rowGroupPerms = new RowGroupPerms(getJDBCMetaData() 268 .getTargetTable(), 269 primeKey); 270 rowGroupPerms.setDataContext(getDataContext()); 271 272 if (getLocalConnection() != null) { 273 rowGroupPerms.setConnection(getLocalConnection()); 274 } 275 276 return rowGroupPerms.searchAndRetrieveList(); 277 } 278 279 288 public void setPermissions(String group, int perm) 289 throws DBException { 290 if (!canRequesterAdministrate()) { 291 throw new SecurityException ("cannot set permissions by user_id: " + 292 getRequestingUid() + " for db object in table: " + 293 getJDBCMetaData().getTargetTable() + " with primary key: " + 294 getKey()); 295 } 296 297 setPermissions(getRequestingUid(), group, perm); 298 } 299 300 308 public void setPermissions(int perm) throws DBException { 309 setPermissions(getRequestingUid(), null, perm); 310 } 311 312 321 public RowPermissions getPermissions() throws DBException { 322 if (!this.haveAllKeys()) { 323 throw new DBException("cannot get/set permissions on item before item's primary key(s) are available."); 324 } 325 326 String primeKey = getKey(); 327 RowPermissions rowPermissions = new 328 RowPermissions(getJDBCMetaData().getTargetTable(), primeKey); 329 rowPermissions.setDataContext(getDataContext()); 330 if (getLocalConnection() != null) { 331 rowPermissions.setConnection(getLocalConnection()); 332 } 333 334 boolean found = rowPermissions.find(); 335 336 if (!found) { 338 rowPermissions.setAttribute(CREATED_NEW, "1"); 339 rowPermissions.owner(getRequestingUid()); 340 } 341 342 return rowPermissions; 343 } 344 345 349 public List getReadGroups() throws DBException { 350 List allGroups = this.getGroups(); 351 352 if (allGroups.size() == 0) { 353 return allGroups; 354 } 355 356 List readGroups = new ArrayList (); 357 358 for (Iterator iterator = allGroups.iterator(); iterator.hasNext();) { 359 RowGroupPerms perms = (RowGroupPerms) iterator.next(); 360 361 if (perms.canGroupRead()) { 362 readGroups.add(perms.group()); 363 } 364 } 365 366 return readGroups; 367 } 368 369 377 public boolean isRowAllowed(String requestedFunction) 378 throws DBException { 379 boolean result = false; 380 381 if (SEARCH.equals(requestedFunction)) { 382 result = canRequesterRead(); 383 } else if (UPDATE.equals(requestedFunction) || 384 DELETE.equals(requestedFunction)) { 385 result = canRequesterWrite(); 386 } else if (ADD.equals(requestedFunction)) { 387 result = true; 390 } 391 392 if (!result) { 393 throw new SecurityException ("User '" + 394 User.getLoginFromId(getRequestingUid(), getDataContext()) + 395 "' does not have permission to perform function '" + 396 requestedFunction + "' on database object in table '" + 397 getJDBCMetaData().getTargetTable() + "', row: '" + getKey() + 398 "' in db/context '" + getDataContext() + 399 "'. Please contact your system administrator if you feel this is incorrect."); 400 } 401 402 return result; 403 } 404 405 415 public boolean isRowAllowed(String requestedFunction, Collection items) 416 throws DBException { 417 if (items.size() == 0) { 418 return false; 419 } 420 421 boolean result = false; 422 List goodItems = new ArrayList (); 423 424 for (Iterator iterator = items.iterator(); iterator.hasNext();) { 425 RowSecuredDBObject rowSecureDBObject = (RowSecuredDBObject) iterator.next(); 426 427 rowSecureDBObject.setRequestingUid(getRequestingUid()); 429 430 try { 431 if (rowSecureDBObject.isRowAllowed(requestedFunction)) { 432 result = true; goodItems.add(rowSecureDBObject); 434 } 435 } catch (SecurityException e) { 436 if (getLogger().isDebugEnabled()) { 437 getLogger().debug( 438 "Security exception checking for isRowAllowed: denying a row with key: " + getKey() + " , removing it from list of approved."); 439 } 440 } 442 } 443 444 if (result) { 445 items.retainAll(goodItems); 447 448 451 } else { 452 items.clear(); 453 } 454 455 return result; 456 } 457 458 462 public List getWriteGroups() throws DBException { 463 List allGroups = this.getGroups(); 464 465 if (allGroups.size() == 0) { 466 return allGroups; 467 } 468 469 List writeGroups = new ArrayList (); 470 471 for (Iterator iterator = allGroups.iterator(); iterator.hasNext();) { 472 RowGroupPerms perms = (RowGroupPerms) iterator.next(); 473 474 if (perms.canGroupWrite()) { 475 writeGroups.add(perms.group()); 476 } 477 } 478 479 return writeGroups; 480 } 481 482 490 public void add() throws DBException { 491 super.add(); 492 setDefaultPermissions(); 493 } 494 495 496 504 public synchronized void add(String group, int permissions) 505 throws DBException { 506 super.add(); 507 setPermissions(getRequestingUid(), group, permissions); 508 } 509 510 511 519 public void addGroupPerm(String group, int perm) throws DBException { 520 if (group == null) { 521 throw new DBException("null group name"); 522 } 523 524 UserGroup grp = new UserGroup(); 526 grp.setDataContext(getDataContext()); 527 grp.setField("GroupName", group); 528 529 if (!grp.find()) { 530 throw new DBException("cannot find group: " + group); 531 } 532 533 RowGroupPerms rowGroupPerms = new RowGroupPerms(getJDBCMetaData() 534 .getTargetTable(), 535 getKey()); 536 rowGroupPerms.setDataContext(getDataContext()); 537 if (getLocalConnection() != null) { 538 rowGroupPerms.setConnection(getLocalConnection()); 539 } 540 rowGroupPerms.group(group); 541 542 if (rowGroupPerms.find()) { 543 int addperm = perm | rowGroupPerms.permissions(); 545 546 if (addperm != rowGroupPerms.permissions()) { 547 rowGroupPerms.permissions(addperm); 548 rowGroupPerms.update(); 549 } 550 } else { 551 rowGroupPerms.permissions(perm); 552 rowGroupPerms.add(); 553 } 554 } 555 556 561 public boolean canRequesterAdministrate() throws DBException { 562 boolean result = false; 563 564 int userId = this.getRequestingUid(); 565 566 if (userId == SYSTEM_ACCOUNT) { 567 return true; 568 } 569 570 User user = new User(); 571 user.setUid(userId); 572 user.setDataContext(this.getDataContext()); 573 574 if (user.isAdmin()) { 575 return true; 576 } 577 578 if (!user.find()) { 579 throw new DBException("cannot find requesting user."); 580 } 581 582 RowPermissions rowPermissions = this.getPermissions(); 583 584 result = rowPermissions.canOthersAdministrate() || 588 ((userId == rowPermissions.owner()) && 589 rowPermissions.canOwnerAdministrate()); 590 591 if (!result) { 592 result = doUsersGroupsIntersect(user, getAdministrateGroups()); 594 } 595 596 return result; 597 } 598 599 605 public boolean canRequesterRead() throws DBException { 606 boolean result = false; 607 608 int userId = this.getRequestingUid(); 609 610 if (userId == SYSTEM_ACCOUNT) { 611 return true; 612 } 613 614 User user = new User(); 615 user.setUid(userId); 616 user.setDataContext(this.getDataContext()); 617 618 if (user.isAdmin()) { 619 return true; 620 } 621 622 if (!user.find()) { 623 throw new DBException("cannot find requesting user."); 624 } 625 626 RowPermissions rowPermissions = this.getPermissions(); 627 628 result = rowPermissions.canOthersRead() || 632 ((userId == rowPermissions.owner()) && 633 rowPermissions.canOwnerRead()); 634 635 if (!result) { 636 640 result = doUsersGroupsIntersect(user, getReadGroups()); 641 } 642 643 return result; 644 } 645 646 private boolean doUsersGroupsIntersect(User user, List groups) throws DBException { 647 boolean result = false; 648 for (Iterator iterator = user.getGroups().iterator(); iterator.hasNext();) { 649 String userGrpname = (String ) iterator.next(); 650 for (Iterator iterator1 = groups.iterator(); iterator1.hasNext();) { 651 String readGrpName = (String ) iterator1.next(); 652 result = readGrpName.equals(userGrpname); 653 if ( result ) return true; 654 } 655 } 656 return false; 657 } 658 659 663 public boolean canRequesterWrite() throws DBException { 664 boolean result = false; 665 666 int userId = this.getRequestingUid(); 667 668 if (userId == SYSTEM_ACCOUNT) { 669 return true; 670 } 671 672 User user = new User(); 673 user.setUid(userId); 674 user.setDataContext(this.getDataContext()); 675 676 if (user.isAdmin()) { 677 return true; 678 } 679 680 if (!user.find()) { 681 throw new DBException("cannot find requesting user."); 682 } 683 684 RowPermissions rowPermissions = this.getPermissions(); 685 686 result = rowPermissions.canOthersWrite() || 690 ((userId == rowPermissions.owner()) && 691 rowPermissions.canOwnerWrite()); 692 693 if (!result) { 694 result = doUsersGroupsIntersect(user, getWriteGroups()); 696 } 697 698 return result; 699 } 700 701 706 public String defaultGroup() throws DBException { 707 User user = new User(); 708 user.setUid(getRequestingUid()); 709 user.setDataContext(getDataContext()); 710 711 if (!user.find()) { 712 return null; 713 } else { 714 return user.getPrimaryGroup(); 715 } 716 } 717 718 724 public int defaultPermissions() { 725 return RowPermissions.DEFAULT_PERMISSIONS; 726 } 727 728 734 public synchronized void delete(boolean deleteDetails) 735 throws DBException { 736 isRowAllowed(DELETE); 737 super.delete(deleteDetails); 738 739 RowPermissions rowPermissions = getPermissions(); 741 742 if (rowPermissions.find()) { 743 rowPermissions.delete(); 744 } 745 746 List groups = getGroups(); 747 748 for (Iterator iterator = groups.iterator(); iterator.hasNext();) { 749 RowGroupPerms rowGroupPerms = (RowGroupPerms) iterator.next(); 750 rowGroupPerms.delete(); 751 } 752 } 753 754 755 761 public synchronized void deleteAll() throws DBException { 762 List list = searchAndRetrieveList(); 763 764 for (Iterator iter = list.iterator(); iter.hasNext();) { 765 RowSecuredDBObject object = (RowSecuredDBObject) iter.next(); 766 object.isRowAllowed(DELETE); 767 } 768 769 super.deleteAll(); 770 } 771 772 773 779 public boolean find() throws DBException { 780 boolean result = super.find(); 781 782 if (result == true) { 785 isRowAllowed(SEARCH); 786 } 787 788 return result; 789 } 790 791 795 public int ownerID() throws DBException { 796 RowPermissions perms = this.getPermissions(); 798 799 return perms.getFieldInt(RowPermissions.OWNER_ID); 800 } 801 802 808 public void removeGroup(String group) throws DBException { 809 if (group == null) { 810 throw new DBException("null group name"); 811 } 812 813 RowGroupPerms rowGroupPerms = new RowGroupPerms(getJDBCMetaData() 814 .getTargetTable(), 815 getKey()); 816 rowGroupPerms.setDataContext(getDataContext()); 817 rowGroupPerms.group(group); 818 819 if (getLocalConnection() != null) { 820 rowGroupPerms.setConnection(getLocalConnection()); 821 } 822 823 if (rowGroupPerms.find()) { 824 rowGroupPerms.delete(); 825 } 826 } 827 828 833 public void retrieve() throws DBException { 834 super.retrieve(); 835 this.isRowAllowed(SEARCH); } 837 838 847 public synchronized ArrayList searchAndRetrieveList() 848 throws DBException { 849 ArrayList result = super.searchAndRetrieveList(); 851 this.isRowAllowed(SEARCH, result); 852 853 return result; 854 } 855 856 866 public synchronized ArrayList searchAndRetrieveList(String sortKeys) 867 throws DBException { 868 ArrayList result = super.searchAndRetrieveList(sortKeys); 869 this.isRowAllowed(SEARCH, result); 870 871 return result; 872 } 873 874 875 880 public void update() throws DBException { 881 isRowAllowed(UPDATE); 882 super.update(); 883 } 884 885 886 893 protected void checkDeleteDetailPerm(DBObject obj) 894 throws DBException { 895 if (obj instanceof RowSecuredDBObject) { 897 RowSecuredDBObject row = (RowSecuredDBObject) obj; 898 row.setRequestingUid(getRequestingUid()); 899 } 900 901 super.checkDeleteDetailPerm(obj); 902 } 903 904 910 protected void checkKeyLength() throws DBException { 911 } 926 927 934 private void setDefaultPermissions() throws DBException { 935 RowPermissions rowPermissions = getPermissions(); 936 rowPermissions.owner(getRequestingUid()); 937 rowPermissions.permissions(defaultPermissions()); 938 rowPermissions.addOrUpdate(); 939 940 String group = defaultGroup(); 942 943 if ((group != null) && (group.length() > 0)) { 944 addGroupPerm(group, defaultPermissions()); 945 } 946 } 947 948 956 private void setPermissions(int owner, String group, int perm) 957 throws DBException { 958 RowPermissions rowPermissions = getPermissions(); 959 rowPermissions.owner(owner); 960 rowPermissions.permissions(perm); 961 if (rowPermissions.getAttribute(CREATED_NEW) != null) { 962 rowPermissions.add(); 963 rowPermissions.removeAttribute(CREATED_NEW); 964 } else { 965 rowPermissions.update(); 966 } 967 968 if (group != null) { 969 addGroupPerm(group, perm); 970 } 971 } 972 } 973 | Popular Tags |