1 16 package org.jmanage.core.auth; 17 18 import java.util.List ; 19 import java.util.LinkedList ; 20 import java.util.Iterator ; 21 22 27 public class ACL { 28 29 private final String name; 30 31 private List authorizedList; 32 private List contextList = new LinkedList (); 33 34 public ACL(String name){ 35 this.name = name; 36 } 37 38 public String getName(){ 39 return this.name; 40 } 41 42 public void setAuthorizedList(List authorizedList){ 43 assert authorizedList != null; 44 assert this.authorizedList == null:"authorized list is already specified"; 45 this.authorizedList = authorizedList; 46 } 47 48 public List getAuthorizedList(){ 49 return authorizedList; 50 } 51 52 public List getAuthorizedList(ACLContext context){ 53 54 if(context != null){ 55 for(Iterator it=contextList.iterator(); it.hasNext(); ){ 56 ACLContextWrapper wrapper = (ACLContextWrapper)it.next(); 57 if(wrapper.context.equals(context)){ 58 return wrapper.authorizedList; 59 } 60 } 61 } 62 63 return getAuthorizedList(); 64 } 65 66 public void add(ACLContext context, List authorizedList){ 67 assert context != null; 68 assert authorizedList != null; 69 contextList.add(new ACLContextWrapper(context, authorizedList)); 70 } 71 72 81 public boolean isAuthorized(ACLContext context, User user) { 82 List authorizedList = getAuthorizedList(context); 83 for(Iterator it=authorizedList.iterator(); it.hasNext(); ){ 84 String authorized = (String )it.next(); 85 if(user.getName().equals(authorized) || user.hasRole(authorized)){ 86 return true; 87 } 88 } 89 return false; 90 } 91 92 private class ACLContextWrapper { 93 ACLContext context; 94 List authorizedList; 95 96 ACLContextWrapper(ACLContext context, List authorizedList){ 97 this.context = context; 98 this.authorizedList = authorizedList; 99 } 100 } 101 } 102 | Popular Tags |