001: package org.osbl.authorization;
002:
003: import org.osbl.ItemRegistry;
004: import org.osbl.identity.model.User;
005: import org.osbl.persistence.*;
006: import org.osbl.authorization.model.*;
007: import org.osbl.authorization.model.Pattern;
008: import org.osbl.authorization.model.PatternType;
009: import org.apache.commons.logging.LogFactory;
010:
011: import java.util.*;
012: import java.security.PermissionCollection;
013: import java.security.Permission;
014:
015: public class AuthorizationService extends PermissionCache implements
016: org.osbl.authorization.Authorization {
017: private static org.apache.commons.logging.Log LOG = LogFactory
018: .getLog(AuthorizationService.class);
019:
020: ItemRegistry authorizationItems;
021: Persistence identityPersistence;
022:
023: private Map<String, SortedSet<String>> permissionsByAction = new HashMap<String, SortedSet<String>>();
024: int size;
025:
026: public AuthorizationService() {
027: INSTANCE = this ;
028: }
029:
030: public void setAuthorizationItems(ItemRegistry authorizationItems) {
031: this .authorizationItems = authorizationItems;
032: }
033:
034: public void setIdentityPersistence(Persistence identityPersistence) {
035: this .identityPersistence = identityPersistence;
036: }
037:
038: public Map<String, SortedSet<String>> getPermissionsByAction() {
039: initializePermissionsByAction(authorizationItems.getItems());
040: return permissionsByAction;
041: }
042:
043: public void initializePermissionsByAction(Collection<String> items) {
044: if (size == items.size())
045: return;
046:
047: permissionsByAction.clear();
048: for (Iterator<String> iterator = items.iterator(); iterator
049: .hasNext();) {
050: String item = iterator.next();
051: int pos = item.lastIndexOf('[');
052: String permission = item.substring(0, pos);
053: String actions = item.substring(pos + 1, item.length() - 1);
054:
055: for (int i = 0; i < actions.length(); i++) {
056: String action = actions.substring(i, i + 1);
057: SortedSet<String> permissions = permissionsByAction
058: .get(action);
059: if (permissions == null) {
060: permissions = new TreeSet<String>();
061: permissionsByAction.put(action, permissions);
062: }
063: permissions.add(permission);
064: }
065: }
066: size = items.size();
067: }
068:
069: Map<String, PermissionCollection> cache = new HashMap<String, PermissionCollection>();
070:
071: private PermissionCollection initialize(String userid) {
072: SimpleQueryCommand command = (SimpleQueryCommand) identityPersistence
073: .createCommand("list");
074: command.setType(User.class);
075: command.addFilter("account", Operator.EQUAL, userid);
076: List<User> list = command.execute();
077: User user = list.size() == 1 ? list.get(0) : null;
078: List<Profile> profiles = user != null ? user.getProfiles()
079: : null;
080:
081: UnionPermissionCollection permissionCollection = new UnionPermissionCollection();
082:
083: if (profiles == null) {
084: profiles = new ArrayList<Profile>(1);
085: profiles.add(getDefaultProfile(userid));
086: }
087:
088: for (Profile profile : profiles) {
089: PatternPermissionCollection patternPermissionCollection = new PatternPermissionCollection();
090: for (Iterator<org.osbl.authorization.model.Pattern> iterator = profile
091: .getPatterns().iterator(); iterator.hasNext();) {
092: Pattern pattern = iterator.next();
093: if (pattern.getType() == PatternType.INCLUDE)
094: patternPermissionCollection.include(pattern
095: .getPattern(), pattern.getAction());
096: else
097: patternPermissionCollection.exclude(pattern
098: .getPattern(), pattern.getAction());
099: }
100: permissionCollection
101: .addPermissionCollection(patternPermissionCollection);
102: }
103:
104: return permissionCollection;
105: }
106:
107: private Profile getDefaultProfile(String userid) {
108: LOG.warn("Default Authorization Profile for " + userid);
109: Profile profile = new Profile();
110: List<Pattern> patterns = new ArrayList<Pattern>();
111: profile.setPatterns(patterns);
112:
113: Map<String, SortedSet<String>> permissionsByAction = getPermissionsByAction();
114: for (String action : permissionsByAction.keySet()) {
115: Pattern pattern = new Pattern();
116: pattern.setType(PatternType.INCLUDE);
117: pattern.setAction(action);
118: pattern.setPattern("(.*)");
119: patterns.add(pattern);
120: }
121:
122: return profile;
123: }
124:
125: public boolean checkPermission(String userid, String permission,
126: String action) {
127: return getPermissions(userid).implies(
128: new ActionPermission(permission, action));
129: }
130:
131: public PermissionCollection getPermissions(String userid) {
132: PermissionCollection permissionCollection = cache.get(userid);
133: if (permissionCollection == null) {
134: permissionCollection = initialize(userid);
135: cache.put(userid, permissionCollection);
136: }
137: return permissionCollection;
138: }
139:
140: public Permission getPermission(String userid, String permission) {
141: return null;
142: }
143:
144: public void clearCache(String userid) {
145: cache.remove(userid);
146: }
147: }
|