001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.security;
031:
032: import com.caucho.log.Log;
033: import com.caucho.util.L10N;
034:
035: import java.security.*;
036: import java.util.logging.Logger;
037:
038: /**
039: * Defines a proxy for the current security context.
040: */
041: public class SecurityContext {
042: static final Logger log = Logger.getLogger(SecurityContext.class
043: .getName());
044: static final L10N L = new L10N(SecurityContext.class);
045:
046: /**
047: * Mapping from threads to providers.
048: */
049: private static ThreadLocal<SecurityContextProvider> _providers = new ThreadLocal<SecurityContextProvider>();
050:
051: /**
052: * The context cannot be instantiated.
053: */
054: private SecurityContext() {
055: }
056:
057: /**
058: * Returns the principal for this security context.
059: *
060: * @return the principal or null of no provider for the thread.
061: */
062: public static Principal getUserPrincipal()
063: throws SecurityContextException {
064: SecurityContextProvider provider = getProvider();
065:
066: if (provider != null)
067: return provider.getUserPrincipal();
068: else
069: return null;
070: }
071:
072: /**
073: * Returns true if the user principal is in the specified role.
074: *
075: * @param roleName the name of the role to test.
076: */
077: public static boolean isUserInRole(String roleName) {
078: SecurityContextProvider provider = getProvider();
079:
080: if (provider != null)
081: return provider.isUserInRole(roleName);
082: else
083: return false;
084: }
085:
086: /**
087: * Returns true if the user principal is in the specified role.
088: *
089: * @param roleSet a set of roles to test.
090: */
091: public static boolean isUserInRole(String[] roleSet) {
092: SecurityContextProvider provider = getProvider();
093:
094: if (provider != null && roleSet != null) {
095: for (int i = 0; i < roleSet.length; i++) {
096: if (provider.isUserInRole(roleSet[i]))
097: return true;
098: }
099: }
100:
101: return false;
102: }
103:
104: /**
105: * Returns true if the user principal is in the specified role.
106: *
107: * @param roleSet a set of roles to test.
108: */
109: public static void checkUserInRole(String[] roleSet) {
110: SecurityContextProvider provider = getProvider();
111:
112: if (provider != null && roleSet != null) {
113: for (int i = 0; i < roleSet.length; i++) {
114: if (provider.isUserInRole(roleSet[i]))
115: return;
116: }
117:
118: throw new AccessControlException(L.l("permission denied"));
119: }
120: }
121:
122: /**
123: * Returns true if the user principal is in the specified role.
124: *
125: * @param roleSet a set of roles to test.
126: */
127: public static String runAs(String role) {
128: SecurityContextProvider provider = getProvider();
129:
130: if (provider != null)
131: return provider.runAs(role);
132: else
133: return null;
134: }
135:
136: /**
137: * Returns true if the context is secure (SSL).
138: */
139: public static boolean isTransportSecure()
140: throws SecurityContextException {
141: SecurityContextProvider provider = getProvider();
142:
143: if (provider != null)
144: return provider.isTransportSecure();
145: else
146: return false;
147: }
148:
149: /**
150: * Logs the principal out.
151: */
152: public static void logout() throws SecurityContextException {
153: SecurityContextProvider provider = getProvider();
154:
155: if (provider != null)
156: provider.logout();
157: }
158:
159: /**
160: * Gets the provider for the current thread.
161: *
162: * @return the provider for the thread
163: */
164: public static SecurityContextProvider getProvider() {
165: return _providers.get();
166: }
167:
168: /**
169: * Sets the provider for the current thread.
170: *
171: * @param provider the new provider
172: */
173: public static SecurityContextProvider setProvider(
174: SecurityContextProvider provider) {
175: SecurityContextProvider oldProvider = _providers.get();
176:
177: _providers.set(provider);
178:
179: return oldProvider;
180: }
181: }
|