001: /*
002: * hgcommons 7
003: * Hammurapi Group Common Library
004: * Copyright (C) 2003 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/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.authorization;
024:
025: import java.security.AccessControlException;
026: import java.util.Collection;
027:
028: import org.w3c.dom.Node;
029:
030: /**
031: * "Hub" class for authorization checks.
032: * @author Pavel Vlasov
033: * @revision $Revision$
034: */
035: public class AuthorizationManager {
036:
037: public static boolean hasClassPermission(Class clazz, String action) {
038: AuthorizationProvider provider = (AuthorizationProvider) threadProvider
039: .get();
040: return provider == null ? true : provider.hasClassPermission(
041: clazz, action);
042: }
043:
044: public static void checkClassPermission(Class clazz, String action)
045: throws AccessControlException {
046: AuthorizationProvider provider = (AuthorizationProvider) threadProvider
047: .get();
048: if (provider != null) {
049: provider.checkClassPermission(clazz, action);
050: }
051: }
052:
053: /**
054: * Authorization provider determines permission type from subject type
055: * @param subject
056: * @param action
057: * @return
058: */
059: public static boolean hasObjectPermission(Object subject,
060: String action) {
061: AuthorizationProvider provider = (AuthorizationProvider) threadProvider
062: .get();
063: return provider == null ? true : provider
064: .hasInstancePermission(subject, action);
065: }
066:
067: /**
068: * Authorization provider determines permission type from subject.
069: * @param subject
070: * @param action
071: * @return
072: */
073: public static void checkInstancePermission(Object subject,
074: String action) throws AccessControlException {
075: AuthorizationProvider provider = (AuthorizationProvider) threadProvider
076: .get();
077: if (provider != null) {
078: provider.checkInstancePermission(subject, action);
079: }
080: }
081:
082: /**
083: * Authorization provider determines permission type from className.
084: * This method is to be used from XSL stylesheets.
085: * @param subject
086: * @param action
087: * @return
088: */
089: public static boolean hasClassPermission(String className,
090: String action) {
091: AuthorizationProvider provider = (AuthorizationProvider) threadProvider
092: .get();
093: return provider == null ? true : provider.hasClassPermission(
094: className, action);
095: }
096:
097: /**
098: * Authorization provider determines permission type from Node where
099: * subject was serialized to.
100: * This method is to be used from XSL stylesheets.
101: * @param subject
102: * @param action
103: * @return
104: */
105: public static boolean hasInstancePermission(Node subjectNode,
106: String className, String action) {
107: AuthorizationProvider provider = (AuthorizationProvider) threadProvider
108: .get();
109: return provider == null ? true : provider
110: .hasInstancePermission(subjectNode, className, action);
111: }
112:
113: private static InheritableThreadLocal threadProvider = new InheritableThreadLocal();
114:
115: public static void setThreadProvider(AuthorizationProvider provider) {
116: threadProvider.set(provider);
117: }
118:
119: public static boolean isUserInRole(String role) {
120: AuthorizationProvider provider = (AuthorizationProvider) threadProvider
121: .get();
122: return provider == null ? true : provider.isUserInRole(role);
123: }
124:
125: public static String getUserName() {
126: AuthorizationProvider provider = (AuthorizationProvider) threadProvider
127: .get();
128: return provider == null ? null : provider.getUserName();
129: }
130:
131: /**
132: * @return Collection of permissions
133: */
134: public static Collection getPermissions() {
135: AuthorizationProvider provider = (AuthorizationProvider) threadProvider
136: .get();
137: return provider == null ? null : provider.getPermissions();
138: }
139:
140: }
|