1 16 package org.jmanage.core.services; 17 18 import org.jmanage.core.services.ServiceContext; 19 import org.jmanage.core.auth.ACL; 20 import org.jmanage.core.auth.ACLStore; 21 import org.jmanage.core.auth.ACLContext; 22 import org.jmanage.core.auth.UnAuthorizedAccessException; 23 24 31 public class AccessController { 32 33 40 public static boolean canAccess(ServiceContext context, 41 String aclName, 42 String targetName){ 43 44 ACL acl = ACLStore.getInstance().getACL(aclName); 45 if(acl == null){ 46 47 return true; 48 } 49 50 51 ACLContext aclContext = getACLContext(context, targetName); 52 if(acl.isAuthorized(aclContext, context.getUser())){ 53 return true; 54 } 55 return false; 56 } 57 58 public static boolean canAccess(ServiceContext context, 59 String aclName){ 60 return canAccess(context, aclName, null); 61 } 62 63 public static void checkAccess(ServiceContext context, 64 String aclName, 65 String targetName) 66 throws UnAuthorizedAccessException { 67 68 if(!canAccess(context, aclName, targetName)){ 69 throw new UnAuthorizedAccessException("Insufficient Privileges"); 70 } 71 } 72 73 public static void checkAccess(ServiceContext context, 74 String aclName) 75 throws UnAuthorizedAccessException { 76 77 checkAccess(context, aclName, null); 78 } 79 80 private static ACLContext getACLContext(ServiceContext context, 81 String targetName){ 82 String appName = null; 83 String mbeanName = null; 84 if(context.getApplicationConfig() != null){ 85 appName = context.getApplicationConfig().getName(); 86 } 87 if(context.getObjectName() != null){ 88 mbeanName = context.getObjectName().getCanonicalName(); 89 } 90 return new ACLContext(appName, mbeanName, targetName); 91 } 92 } | Popular Tags |