001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.runas;
017:
018: import org.acegisecurity.Authentication;
019: import org.acegisecurity.ConfigAttribute;
020: import org.acegisecurity.ConfigAttributeDefinition;
021: import org.acegisecurity.GrantedAuthority;
022: import org.acegisecurity.GrantedAuthorityImpl;
023: import org.acegisecurity.RunAsManager;
024:
025: import org.springframework.beans.factory.InitializingBean;
026:
027: import org.springframework.util.Assert;
028:
029: import java.util.Iterator;
030: import java.util.List;
031: import java.util.Vector;
032:
033: /**
034: * Basic concrete implementation of a {@link RunAsManager}.<p>Is activated if any {@link
035: * ConfigAttribute#getAttribute()} is prefixed with <Code>RUN_AS_</code>. If found, it generates a new {@link
036: * RunAsUserToken} containing the same principal, credentials and granted authorities as the original {@link
037: * Authentication} object, along with {@link GrantedAuthorityImpl}s for each <code>RUN_AS_</code> indicated. The
038: * created <code>GrantedAuthorityImpl</code>s will be prefixed with a special prefix indicating that it is a role
039: * (default prefix value is <code>ROLE_</code>), and then the remainder of the <code>RUN_AS_</code> keyword. For
040: * example, <code>RUN_AS_FOO</code> will result in the creation of a granted authority of
041: * <code>ROLE_RUN_AS_FOO</code>.</p>
042: * <p>The role prefix may be overriden from the default, to match that used elsewhere, for example when using an
043: * existing role database with another prefix. An empty role prefix may also be specified. Note however that there are
044: * potential issues with using an empty role prefix since different categories of {@link
045: * org.acegisecurity.ConfigAttribute} can not be properly discerned based on the prefix, with possible consequences
046: * when performing voting and other actions. However, this option may be of some use when using preexisting role names
047: * without a prefix, and no ability exists to prefix them with a role prefix on reading them in, such as provided for
048: * example in {@link org.acegisecurity.userdetails.jdbc.JdbcDaoImpl}.</p>
049: *
050: * @author Ben Alex
051: * @author colin sampaleanu
052: * @version $Id: RunAsManagerImpl.java 1496 2006-05-23 13:38:33Z benalex $
053: */
054: public class RunAsManagerImpl implements RunAsManager, InitializingBean {
055: //~ Instance fields ================================================================================================
056:
057: private String key;
058: private String rolePrefix = "ROLE_";
059:
060: //~ Methods ========================================================================================================
061:
062: public void afterPropertiesSet() throws Exception {
063: Assert
064: .notNull(
065: key,
066: "A Key is required and should match that configured for the RunAsImplAuthenticationProvider");
067: }
068:
069: public Authentication buildRunAs(Authentication authentication,
070: Object object, ConfigAttributeDefinition config) {
071: List newAuthorities = new Vector();
072: Iterator iter = config.getConfigAttributes();
073:
074: while (iter.hasNext()) {
075: ConfigAttribute attribute = (ConfigAttribute) iter.next();
076:
077: if (this .supports(attribute)) {
078: GrantedAuthorityImpl extraAuthority = new GrantedAuthorityImpl(
079: getRolePrefix() + attribute.getAttribute());
080: newAuthorities.add(extraAuthority);
081: }
082: }
083:
084: if (newAuthorities.size() == 0) {
085: return null;
086: } else {
087: for (int i = 0; i < authentication.getAuthorities().length; i++) {
088: newAuthorities.add(authentication.getAuthorities()[i]);
089: }
090:
091: GrantedAuthority[] resultType = { new GrantedAuthorityImpl(
092: "holder") };
093: GrantedAuthority[] newAuthoritiesAsArray = (GrantedAuthority[]) newAuthorities
094: .toArray(resultType);
095:
096: return new RunAsUserToken(this .key, authentication
097: .getPrincipal(), authentication.getCredentials(),
098: newAuthoritiesAsArray, authentication.getClass());
099: }
100: }
101:
102: public String getKey() {
103: return key;
104: }
105:
106: public String getRolePrefix() {
107: return rolePrefix;
108: }
109:
110: public void setKey(String key) {
111: this .key = key;
112: }
113:
114: /**
115: * Allows the default role prefix of <code>ROLE_</code> to be overriden. May be set to an empty value,
116: * although this is usually not desireable.
117: *
118: * @param rolePrefix the new prefix
119: */
120: public void setRolePrefix(String rolePrefix) {
121: this .rolePrefix = rolePrefix;
122: }
123:
124: public boolean supports(ConfigAttribute attribute) {
125: if ((attribute.getAttribute() != null)
126: && attribute.getAttribute().startsWith("RUN_AS_")) {
127: return true;
128: } else {
129: return false;
130: }
131: }
132:
133: /**
134: * This implementation supports any type of class, because it does not query the presented secure object.
135: *
136: * @param clazz the secure object
137: *
138: * @return alwaus <code>true</code>
139: */
140: public boolean supports(Class clazz) {
141: return true;
142: }
143: }
|