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.adapters;
017:
018: import org.acegisecurity.GrantedAuthority;
019:
020: import org.acegisecurity.providers.AbstractAuthenticationToken;
021:
022: /**
023: * Convenience superclass for {@link AuthByAdapter} implementations.
024: *
025: * @author Ben Alex
026: * @version $Id: AbstractAdapterAuthenticationToken.java 1496 2006-05-23 13:38:33Z benalex $
027: */
028: public abstract class AbstractAdapterAuthenticationToken extends
029: AbstractAuthenticationToken implements AuthByAdapter {
030: //~ Instance fields ================================================================================================
031:
032: private int keyHash;
033:
034: //~ Constructors ===================================================================================================
035:
036: protected AbstractAdapterAuthenticationToken() {
037: super (null);
038: }
039:
040: /**
041: * The only way an <code>AbstractAdapterAuthentication</code> should be
042: * constructed.
043: *
044: * @param key the key that is hashed and made available via {@link
045: * #getKeyHash()}
046: * @param authorities the authorities granted to this principal
047: */
048: protected AbstractAdapterAuthenticationToken(String key,
049: GrantedAuthority[] authorities) {
050: super (authorities);
051: this .keyHash = key.hashCode();
052: }
053:
054: //~ Methods ========================================================================================================
055:
056: public boolean equals(Object obj) {
057: if (obj instanceof AbstractAdapterAuthenticationToken) {
058: if (!super .equals(obj)) {
059: return false;
060: }
061:
062: AbstractAdapterAuthenticationToken test = (AbstractAdapterAuthenticationToken) obj;
063:
064: return (this .getKeyHash() == test.getKeyHash());
065: }
066:
067: return false;
068: }
069:
070: public int getKeyHash() {
071: return this .keyHash;
072: }
073:
074: /**
075: * Always returns <code>true</code>.
076: *
077: * @return DOCUMENT ME!
078: */
079: public boolean isAuthenticated() {
080: return true;
081: }
082:
083: /**
084: * Iterates the granted authorities and indicates whether or not the specified role is held.<p>Comparison
085: * is based on the <code>String</code> returned by {@link GrantedAuthority#getAuthority}.</p>
086: *
087: * @param role the role being searched for in this object's granted authorities list
088: *
089: * @return <code>true</code> if the granted authority is held, or <code>false</code> otherwise
090: */
091: public boolean isUserInRole(String role) {
092: GrantedAuthority[] authorities = super .getAuthorities();
093:
094: for (int i = 0; i < authorities.length; i++) {
095: if (role.equals(authorities[i].getAuthority())) {
096: return true;
097: }
098: }
099:
100: return false;
101: }
102:
103: /**
104: * Setting is ignored. Always considered authenticated.
105: *
106: * @param ignored DOCUMENT ME!
107: */
108: public void setAuthenticated(boolean ignored) {
109: // ignored
110: }
111: }
|