001: /*
002: * argun 1.0
003: * Web 2.0 delivery framework
004: * Copyright (C) 2007 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.web.security;
024:
025: import java.security.AccessControlException;
026: import java.util.Collection;
027: import java.util.Map;
028:
029: import org.w3c.dom.Node;
030:
031: import biz.hammurapi.authorization.AuthorizationProvider;
032: import biz.hammurapi.util.ClassHierarchyVisitable;
033: import biz.hammurapi.util.Visitor;
034:
035: /**
036: * Retrieves authorization information from User.
037: * @author Daniel
038: */
039: public class UserAuthorizationProvider implements AuthorizationProvider {
040: private final Collection permissions;
041: private final Map classPermissions;
042: private final User user;
043:
044: public UserAuthorizationProvider(User user, Collection permissions,
045: Map classPermissions) {
046: this .permissions = permissions;
047: this .user = user;
048: this .classPermissions = classPermissions;
049: }
050:
051: public void checkClassPermission(Class clazz, String action)
052: throws AccessControlException {
053: if (!hasClassPermission(clazz, action)) {
054: throw new AccessControlException("User "
055: + user.getLoginName() + " doesn't have "
056: + clazz.getName() + ":" + action + " permission");
057: }
058: }
059:
060: public void checkInstancePermission(Object obj, String action)
061: throws AccessControlException {
062: checkClassPermission(obj.getClass(), action);
063: }
064:
065: public Collection getPermissions() {
066: return permissions;
067: }
068:
069: public String getUserName() {
070: return user.getLoginName();
071: }
072:
073: public boolean hasClassPermission(Class clazz, final String action) {
074: synchronized (classPermissions) {
075: Boolean ret = (Boolean) classPermissions.get(clazz
076: .getName());
077: if (ret == null) {
078: final Boolean[] resolution = { null };
079: ClassHierarchyVisitable chv = new ClassHierarchyVisitable(
080: clazz);
081: chv.accept(new Visitor() {
082:
083: public boolean visit(Object obj) {
084: if (resolution[0] == null) {
085: resolution[0] = user.hasPermission(
086: ((Class) obj).getName(), action);
087: }
088: return resolution[0] == null;
089: }
090:
091: });
092: ret = resolution[0] == null ? Boolean.FALSE
093: : resolution[0];
094: classPermissions.put(clazz.getName(), ret);
095: }
096: return ret.booleanValue();
097: }
098: }
099:
100: public boolean hasClassPermission(String className, String action) {
101: Boolean ret = user.hasPermission(className, action);
102: return ret == null ? false : ret.booleanValue();
103: }
104:
105: public boolean hasInstancePermission(Object obj, String action) {
106: if (hasClassPermission(obj.getClass(), action)) {
107: return true;
108: }
109:
110: if (obj instanceof Protected) {
111: return ((Protected) obj).hasPermission(user.getLoginName(),
112: action);
113: }
114:
115: return false;
116: }
117:
118: public boolean hasInstancePermission(Node node, String arg1,
119: String arg2) {
120: throw new UnsupportedOperationException("Not implemented");
121: }
122:
123: public boolean isUserInRole(String roleName) {
124: return user.isInRole(roleName);
125: }
126:
127: /**
128: * Checks access to menu
129: * @param menuId
130: * @return Boolean.TRUE if access is granted, Boolean.FALSE if denied,
131: * null if it should be inherited.
132: */
133: public Boolean hasAccess(int menuId) {
134: return user.hasAccess(menuId);
135: }
136: }
|