1
2
3
4
5
6
7
8
9
10
11
12
13
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
31
32 private GrantedAuthority[] authorities;
33 private int keyHash;
34
35
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
57
58 /***
59 * Setting is ignored. Always considered authenticated.
60 *
61 * @param ignored DOCUMENT ME!
62 */
63 public void setAuthenticated(boolean ignored) {
64
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 }