litesoft
@ 151
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
package org.litesoft.datt.client.boviews; import java.util.*; import org.litesoft.core.simpletypes.*; import org.litesoft.datt.client.*; import org.litesoft.security.*; import org.litesoft.uispecification.*; public class UserViewAccessControlFactory { private static final Map<AccessKey, ViewAccessControlManager> CACHE = new HashMap<AccessKey, ViewAccessControlManager>(); public static synchronized ViewAccessControlManager get( UserView pUser ) { AccessKey zKey = new AccessKey( pUser ); ViewAccessControlManager zManager = CACHE.get( zKey ); if ( zManager == null ) { CACHE.put( zKey, zManager = createManager( zKey ) ); } return zManager; } private static class AccessKey { private int mFlags; private AccessKey( UserView pUser ) { mFlags = bit( Role.BITS.sAdminFlag, pUser.getCanAdministrate() ) + // bit( Role.BITS.sRestrictedResourceMgrFlag, pUser.getRestrictedResourceManage() ) + // bit( Role.BITS.sSprAdminFlag, pUser.getSuperAdmin() ) + // bit( Role.BITS.sHelpDeskFlag, pUser.getHelpDeskMember() ) + // bit( Role.BITS.sNonGuestFlag, !pUser.isGuest() ) + // Role.BITS.BIT.none(); TextLines zAllowedRoles = pUser.getCurrentRestrictedResourceAllowedRoles(); for ( TextLine zRole : zAllowedRoles ) { mFlags += Role.getBitFor( zRole.getLine() ); } } private int bit( int zBit, Boolean zFlag ) { return Boolean.TRUE.equals( zFlag ) ? zBit : 0; } private boolean isBit( int zBit ) { return (0 != (mFlags & zBit)); } public boolean isRole( Role pRole ) { return isBit( pRole.getBit() ); } public boolean isAdminFlag() { return isBit( Role.BITS.sAdminFlag ); } public boolean isRestrictedResourceMgrFlag() { return isBit( Role.BITS.sRestrictedResourceMgrFlag ); } public boolean isSprAdminFlag() { return isBit( Role.BITS.sSprAdminFlag ); } public boolean isHelpDeskFlag() { return isBit( Role.BITS.sHelpDeskFlag ); } public boolean isNonGuest() { return isBit( Role.BITS.sNonGuestFlag ); } @Override public boolean equals( Object o ) { if ( this == o ) { return true; } if ( o == null || getClass() != o.getClass() ) { return false; } AccessKey that = (AccessKey) o; return this.mFlags == that.mFlags; } @Override public int hashCode() { return mFlags; } } private static void addSection( List<ViewDef[]> pSections, boolean pFlag, ViewDef[] pViewDefs ) { if ( pFlag && (pViewDefs != null) ) { pSections.add( pViewDefs ); } } private static ViewAccessControlManager createManager( AccessKey pKey ) { List<ViewDef[]> zSections = new ArrayList<ViewDef[]>(); zSections.add( ViewDefs.ANY_USER ); addSection( zSections, pKey.isNonGuest(), ViewDefs.ANY_NON_GUEST ); addSection( zSections, pKey.isAdminFlag(), ViewDefs.CAN_RESTRICTED_RESOURCE_ADMIN ); addSection( zSections, pKey.isHelpDeskFlag(), ViewDefs.CAN_HELP_DESK ); addSection( zSections, pKey.isRestrictedResourceMgrFlag(), ViewDefs.CAN_MANAGE_RESTRICTED_RESOURCES ); addSection( zSections, pKey.isSprAdminFlag(), ViewDefs.CAN_SUPER_ADMIN ); for ( Role zRole : Role.values() ) { addSection( zSections, pKey.isRole( zRole ), ViewDefs.ROLES_MAP.get( zRole ) ); } return new ViewAccessControlManager.Only( zSections ); } } |