1 40 41 package org.jahia.services.acl; 42 43 import java.io.IOException ; 44 import java.io.Serializable ; 45 import java.util.Arrays ; 46 import java.util.Collections ; 47 import java.util.List ; 48 import java.util.Vector ; 49 50 import org.jahia.exceptions.JahiaException; 51 import org.jahia.exceptions.database.JahiaDatabaseException; 52 import org.jahia.registries.ServicesRegistry; 53 import org.jahia.services.usermanager.JahiaGroup; 54 import org.jahia.services.usermanager.JahiaUser; 55 56 69 public abstract class JahiaAbstractACL implements Cloneable , Serializable { 70 71 75 public static char SYMBOL_EMPTY = '-'; 76 77 78 protected JahiaACL mACL; 79 80 81 private JahiaACLManagerService mACLService; 82 83 protected static volatile char[] sharedActions = null; 84 85 protected static volatile List sharedActionsNames = null; 86 87 protected Boolean inheritance = null; 88 89 90 private final String INIT_ERROR_MSG = "ACL Object not initialized"; 91 92 public static List getActionNames() { 93 if (sharedActionsNames == null) 94 getSharedActions(); return sharedActionsNames; 96 } 97 98 107 public abstract Vector getBitdesc(); 108 109 123 public JahiaAbstractACL(int aclID) throws ACLNotFoundException, 124 JahiaException { 125 init(); 126 load(aclID); 127 } 128 129 protected static final char[] getSharedActions() { 130 if (sharedActions == null) { 131 sharedActionsNames = Collections.unmodifiableList(Arrays.asList(new String []{"Read", "Write", "Admin"})); 132 sharedActions = new char[]{'r', 'w', 'A'}; 133 } 134 135 return sharedActions; 136 } 137 138 145 protected JahiaAbstractACL() throws JahiaException { 146 init(); 147 } 148 149 154 protected JahiaACL getACL() { 155 return mACL; 156 } 157 158 173 public synchronized boolean create(int parentID) 174 throws ACLNotFoundException, JahiaDatabaseException { 175 JahiaACL parent = null; 177 if (parentID > 0) { 178 parent = getService().lookupACL(parentID); 179 } 180 181 mACL = getService().createACL(parent); 183 184 return (mACL != null); 185 } 186 187 199 public synchronized boolean delete() throws JahiaACLException { 200 testProxy(); 201 202 boolean result = false; 203 synchronized (mACL) { 204 if (getService().deleteACL(mACL)) { 205 mACL = null; 206 result = true; 207 } 208 } 209 return result; 210 } 211 212 224 public synchronized void load(int aclID) throws ACLNotFoundException, 225 JahiaDatabaseException { 226 mACL = getService().lookupACL(aclID); 227 } 228 229 private void init() throws JahiaException { 233 mACLService = JahiaACLManagerService.getInstance(); 234 if (mACLService == null) { 235 throw new JahiaException("JahiaAbstractACL", 236 "Abstract ACL could not get the ACL Manager Instance.", 237 JahiaException.SERVICE_ERROR, JahiaException.CRITICAL_SEVERITY); 238 } 239 } 240 241 251 public int getID() throws JahiaACLException { 252 testProxy(); 253 return mACL.getID(); 254 } 255 256 264 public int getParentID() throws JahiaACLException { 265 testProxy(); 266 return mACL.getParentID(); 267 } 268 269 276 public final int getInheritance() throws JahiaACLException { 277 testProxy(); 278 return mACL.getInheritance(); 279 } 280 281 291 public final boolean setInheritance(int inheritance) 292 throws JahiaACLException { 293 testProxy(); 294 synchronized (mACL) { 295 JahiaACL newACL = (JahiaACL) mACL.clone(); 296 boolean result = newACL.setInheritance(inheritance); 297 getService().updateCache(newACL); 298 mACL = newACL; 299 return result; 300 } 301 } 302 303 315 public JahiaACLEntry getUserEntry(JahiaUser user) throws JahiaACLException { 316 testProxy(); 317 return mACL.getUserEntry(user); 318 } 319 320 336 public boolean setUserEntry(JahiaUser user, JahiaACLEntry entry) 337 throws JahiaACLException { 338 testProxy(); 339 synchronized (mACL) { 340 JahiaACL newACL = (JahiaACL) mACL.clone(); 341 boolean result = newACL.setUserEntry(user, entry); 342 getService().updateCache(newACL); 343 mACL = newACL; 344 return result; 345 } 346 } 347 348 361 public boolean removeUserEntry(JahiaUser user) throws JahiaACLException { 362 testProxy(); 363 synchronized (mACL) { 364 JahiaACL newACL = (JahiaACL) mACL.clone(); 365 boolean result = newACL.removeUserEntry(user); 366 getService().updateCache(newACL); 367 mACL = newACL; 368 return result; 369 } 370 } 371 372 382 public boolean removeAllUserEntries() throws JahiaACLException { 383 testProxy(); 384 synchronized (mACL) { 385 JahiaACL newACL = (JahiaACL) mACL.clone(); 386 boolean result = newACL.clearEntries(JahiaACL.USER_TYPE_ENTRY); 387 getService().updateCache(newACL); 388 mACL = newACL; 389 return result; 390 } 391 } 392 393 413 414 public Vector getUsernameList(JahiaACLEntry entry) throws JahiaACLException { 415 testProxy(); 416 return mACL.getUsernameList(entry); 417 } 418 419 442 public Vector getUsernameList(ParentACLFinder parentACLFinder, 443 ACLResourceInterface aclResource, JahiaACLEntry entry) 444 throws JahiaACLException { 445 testProxy(); 446 return mACL.getUsernameList(parentACLFinder, aclResource, entry); 447 } 448 449 461 public JahiaACLEntry getGroupEntry(JahiaGroup group) 462 throws JahiaACLException { 463 testProxy(); 464 return mACL.getGroupEntry(group); 465 } 466 467 483 public boolean setGroupEntry(JahiaGroup group, JahiaACLEntry entry) 484 throws JahiaACLException { 485 testProxy(); 486 synchronized (mACL) { 487 JahiaACL newACL = (JahiaACL) mACL.clone(); 488 boolean result = newACL.setGroupEntry(group, entry); 489 getService().updateCache(newACL); 490 mACL = newACL; 491 return result; 492 } 493 } 494 495 508 public boolean removeGroupEntry(JahiaGroup group) throws JahiaACLException { 509 testProxy(); 510 synchronized (mACL) { 511 JahiaACL newACL = (JahiaACL) mACL.clone(); 512 boolean result = newACL.removeGroupEntry(group); 513 getService().updateCache(newACL); 514 mACL = newACL; 515 return result; 516 } 517 } 518 519 529 public boolean removeAllGroupEntries() throws JahiaACLException { 530 testProxy(); 531 synchronized (mACL) { 532 JahiaACL newACL = (JahiaACL) mACL.clone(); 533 boolean result = newACL.clearEntries(JahiaACL.GROUP_TYPE_ENTRY); 534 getService().updateCache(newACL); 535 mACL = newACL; 536 return result; 537 } 538 } 539 540 557 public Vector getGroupnameList(JahiaACLEntry entry) 558 throws JahiaACLException { 559 testProxy(); 560 return mACL.getGroupnameList(entry); 561 } 562 563 585 public Vector getGroupnameList(ParentACLFinder parentACLFinder, 586 ACLResourceInterface aclResource, JahiaACLEntry entry) 587 throws JahiaACLException { 588 testProxy(); 589 return mACL.getGroupnameList(parentACLFinder, aclResource, entry); 590 } 591 592 609 public Vector getGroupnameListNoAdmin(JahiaACLEntry entry) 610 throws JahiaACLException { 611 testProxy(); 612 return mACL.getGroupnameListNoAdmin(entry); 613 } 614 615 635 public Vector getGroupnameListNoAdmin(ParentACLFinder parentACLFinder, 636 ACLResourceInterface aclResource, JahiaACLEntry entry) 637 throws JahiaACLException { 638 testProxy(); 639 return mACL 640 .getGroupnameListNoAdmin(parentACLFinder, aclResource, entry); 641 } 642 643 664 public boolean getPermission(JahiaUser user, int permission) 665 throws JahiaACLException { 666 testProxy(); 667 return mACL.getPermission(user, permission); 668 } 669 670 692 public boolean getPermission(ParentACLFinder parentACLFinder, 693 ACLResourceInterface aclResource, JahiaUser user, int permission) 694 throws JahiaACLException { 695 testProxy(); 696 return mACL.getPermission(parentACLFinder, aclResource, user, 697 permission); 698 } 699 700 721 public boolean getPermission(JahiaUser user, int permission, 722 boolean checkLocaleEntry) throws JahiaACLException { 723 testProxy(); 724 boolean hasPermission = mACL.getPermission(user, permission); 725 if (!hasPermission) { 726 boolean foundAclEntry = false; 727 JahiaACLEntry aclEntry = getUserEntry(user); 728 if (aclEntry == null) { 729 731 737 aclEntry = new JahiaACLEntry(permission - 1, 0); 738 Vector v = getGroupnameList(aclEntry); 739 if (v.size() == 0) { 740 hasPermission = true; 746 } else { 747 int size = v.size(); 748 String grpName = null; 749 JahiaGroup grp = null; 750 for (int i = 0; i < size; i++) { 751 grpName = (String ) v.get(i); 752 grp = ServicesRegistry.getInstance() 753 .getJahiaGroupManagerService().lookupGroup(grpName); 754 if (grp != null) { 755 if (grp.isMember(user)) { 756 JahiaACLEntry grpACLEntry = getGroupEntry(grp); 757 if (grpACLEntry != null) { 758 foundAclEntry = true; 759 break; 760 } 761 } 762 } 763 } 764 } 765 } else { 766 foundAclEntry = true; 767 } 768 if (!foundAclEntry) { 769 hasPermission = true; 770 } 771 } 772 return hasPermission; 773 } 774 775 806 public boolean getPermission(ParentACLFinder parentACLFinder, 807 ACLResourceInterface aclResource, JahiaUser user, int permission, 808 boolean checkLocaleEntry) throws JahiaACLException { 809 testProxy(); 810 boolean hasPermission = mACL.getPermission(parentACLFinder, 811 aclResource, user, permission); 812 if (!hasPermission) { 813 boolean foundAclEntry = false; 814 JahiaACLEntry aclEntry = getUserEntry(user); 815 if (aclEntry == null) { 816 818 824 aclEntry = new JahiaACLEntry(permission - 1, 0); 825 Vector v = getGroupnameList(aclEntry); 826 if (v.size() == 0) { 827 hasPermission = true; 833 } else { 834 int size = v.size(); 835 String grpName = null; 836 JahiaGroup grp = null; 837 for (int i = 0; i < size; i++) { 838 grpName = (String ) v.get(i); 839 grp = ServicesRegistry.getInstance() 840 .getJahiaGroupManagerService().lookupGroup(grpName); 841 if (grp != null) { 842 if (grp.isMember(user)) { 843 JahiaACLEntry grpACLEntry = getGroupEntry(grp); 844 if (grpACLEntry != null) { 845 foundAclEntry = true; 846 break; 847 } 848 } 849 } 850 } 851 } 852 } else { 853 foundAclEntry = true; 854 } 855 if (!foundAclEntry) { 856 hasPermission = true; 857 } 858 } 859 return hasPermission; 860 } 861 862 882 public boolean getPermission(JahiaGroup group, int permission) 883 throws JahiaACLException { 884 testProxy(); 885 return mACL.getPermission(group, permission); 886 } 887 888 910 public boolean getPermission(ParentACLFinder parentACLFinder, 911 ACLResourceInterface aclResource, JahiaGroup group, int permission) 912 throws JahiaACLException { 913 testProxy(); 914 return mACL.getPermission(parentACLFinder, aclResource, group, 915 permission); 916 } 917 918 public String toString() { 920 if (mACL == null) { 921 return "-ACL proxy not initialized-"; 922 } 923 return mACL.toString(); 924 } 925 926 947 public Vector getUsernameListAlsoGroupUsers(JahiaACLEntry entry) 948 throws JahiaACLException { 949 testProxy(); 950 return mACL.getUsernameListAlsoGroupUsers(entry); 951 } 952 953 977 public Vector getUsernameListAlsoGroupUsers( 978 ParentACLFinder parentACLFinder, ACLResourceInterface aclResource, 979 JahiaACLEntry entry) throws JahiaACLException { 980 testProxy(); 981 return mACL.getUsernameListAlsoGroupUsers(parentACLFinder, aclResource, 982 entry); 983 } 984 985 private void testProxy() throws JahiaACLException { 987 if (mACL == null) { 988 throw new JahiaACLException(INIT_ERROR_MSG, 989 JahiaACLException.ACL_NOT_INITIALIZED); 990 } 991 } 992 993 1008 public void setParentID(int parentID) throws ACLNotFoundException, 1009 JahiaDatabaseException { 1010 JahiaACL parentACL = getService().lookupACL(parentID); 1011 synchronized (mACL) { 1012 JahiaACL newACL = (JahiaACL) mACL.clone(); 1013 newACL.setParentACL(parentACL); 1014 getService().updateCache(newACL); 1015 mACL = newACL; 1016 } 1017 } 1018 1019 1025 public Object clone() { 1026 return mACL.clone(); 1027 } 1028 1029 1040 public boolean isAclInParents(int aclId) { 1041 return this.getACL().isAclInParents(aclId); 1042 } 1043 1044 private void writeObject(java.io.ObjectOutputStream out) throws IOException { 1045 out.writeObject(mACL); 1046 } 1047 1048 private void readObject(java.io.ObjectInputStream in) throws IOException , 1049 ClassNotFoundException { 1050 mACL = (JahiaACL) in.readObject(); 1051 mACLService = null; 1052 } 1053 1054 1062 private JahiaACLManagerService getService() { 1063 if (mACLService == null) { 1064 mACLService = ServicesRegistry.getInstance() 1065 .getJahiaACLManagerService(); 1066 } 1067 return mACLService; 1068 } 1069} 1070 | Popular Tags |