View Javadoc

    1   /* Copyright 2004 Acegi Technology Pty Limited
    2    *
    3    * Licensed under the Apache License, Version 2.0 (the "License");
    4    * you may not use this file except in compliance with the License.
    5    * You may obtain a copy of the License at
    6    *
    7    *     http://www.apache.org/licenses/LICENSE-2.0
    8    *
    9    * Unless required by applicable law or agreed to in writing, software
    10   * distributed under the License is distributed on an "AS IS" BASIS,
    11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12   * See the License for the specific language governing permissions and
    13   * limitations under the License.
    14   */
    15
    16  package org.acegisecurity.adapters;
    17
    18  import org.acegisecurity.GrantedAuthority;
    19  import org.acegisecurity.providers.AbstractAuthenticationToken;
    20
    21
    22  /***
    23   * Convenience superclass for {@link AuthByAdapter} implementations.
    24   *
    25   * @author Ben Alex
    26   * @version $Id: AbstractAdapterAuthenticationToken.java,v 1.5 2005/11/17 00:55:49 benalex Exp $
    27   */
    28  public abstract class AbstractAdapterAuthenticationToken
    29      extends AbstractAuthenticationToken implements AuthByAdapter {
    30      //~ Instance fields ========================================================
    31
    32      private GrantedAuthority[] authorities;
    33      private int keyHash;
    34
    35      //~ Constructors ===========================================================
    36
    37      protected AbstractAdapterAuthenticationToken() {
    38          super();
    39      }
    40
    41      /***
    42       * The only way an <code>AbstractAdapterAuthentication</code> should be
    43       * constructed.
    44       *
    45       * @param key the key that is hashed and made available via  {@link
    46       *        #getKeyHash()}
    47       * @param authorities the authorities granted to this principal
    48       */
    49      protected AbstractAdapterAuthenticationToken(String key,
    50          GrantedAuthority[] authorities) {
    51          super();
    52          this.keyHash = key.hashCode();
    53          this.authorities = authorities;
    54      }
    55
    56      //~ Methods ================================================================
    57
    58      /***
    59       * Setting is ignored. Always considered authenticated.
    60       *
    61       * @param ignored DOCUMENT ME!
    62       */
    63      public void setAuthenticated(boolean ignored) {
    64          // ignored
    65      }
    66
    67      /***
    68       * Always returns <code>true</code>.
    69       *
    70       * @return DOCUMENT ME!
    71       */
    72      public boolean isAuthenticated() {
    73          return true;
    74      }
    75
    76      public GrantedAuthority[] getAuthorities() {
    77          return authorities;
    78      }
    79
    80      public int getKeyHash() {
    81          return this.keyHash;
    82      }
    83
    84      /***
    85       * Iterates the granted authorities and indicates whether or not the
    86       * specified role is held.
    87       *
    88       * <p>
    89       * Comparison is based on the <code>String</code> returned by {@link
    90       * GrantedAuthority#getAuthority}.
    91       * </p>
    92       *
    93       * @param role the role being searched for in this object's granted
    94       *        authorities list
    95       *
    96       * @return <code>true</code> if the granted authority is held, or
    97       *         <code>false</code> otherwise
    98       */
    99      public boolean isUserInRole(String role) {
    100         for (int i = 0; i < this.authorities.length; i++) {
    101             if (role.equals(this.authorities[i].getAuthority())) {
    102                 return true;
    103             }
    104         }
    105
    106         return false;
    107     }
    108
    109     public boolean equals(Object obj) {
    110         if (obj instanceof AbstractAdapterAuthenticationToken) {
    111             if (!super.equals(obj)) {
    112                 return false;
    113             }
    114
    115             AbstractAdapterAuthenticationToken test = (AbstractAdapterAuthenticationToken) obj;
    116
    117             return (this.getKeyHash() == test.getKeyHash());
    118         }
    119
    120         return false;
    121     }
    122 }