001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: RoleUserAttributes.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.authentication.credentialsmanagers;
009:
010: import java.util.Arrays;
011: import java.util.Collection;
012: import java.util.HashSet;
013:
014: public class RoleUserAttributes implements Cloneable {
015: private long mUserId = -1;
016: private boolean mAutomaticUserId = false;
017: private String mPassword = null;
018: private HashSet<String> mRoles = null;
019:
020: public RoleUserAttributes() {
021: }
022:
023: public RoleUserAttributes(long userId, String password) {
024: setUserId(userId);
025: setPassword(password);
026: }
027:
028: public RoleUserAttributes(long userId, String password,
029: String[] roles) {
030: setUserId(userId);
031: setPassword(password);
032: setRoles(roles);
033: }
034:
035: public RoleUserAttributes(long userId, String password,
036: Collection<String> roles) {
037: setUserId(userId);
038: setPassword(password);
039: setRoles(roles);
040: }
041:
042: public RoleUserAttributes(String password) {
043: setPassword(password);
044: }
045:
046: public RoleUserAttributes(String password, String[] roles) {
047: setPassword(password);
048: setRoles(roles);
049: }
050:
051: public RoleUserAttributes(String password, Collection<String> roles) {
052: setPassword(password);
053: setRoles(roles);
054: }
055:
056: public RoleUserAttributes(long userId) {
057: setUserId(userId);
058: }
059:
060: public RoleUserAttributes(long userId, String[] roles) {
061: setUserId(userId);
062: setRoles(roles);
063: }
064:
065: public RoleUserAttributes(long userId, Collection<String> roles) {
066: setUserId(userId);
067: setRoles(roles);
068: }
069:
070: public RoleUserAttributes(String[] roles) {
071: setRoles(roles);
072: }
073:
074: public RoleUserAttributes(Collection<String> roles) {
075: setRoles(roles);
076: }
077:
078: public void setUserId(long userId) {
079: if (userId < 0)
080: throw new IllegalArgumentException(
081: "userId can't be negative.");
082:
083: mUserId = userId;
084: }
085:
086: public long getUserId() {
087: return mUserId;
088: }
089:
090: void setAutomaticUserId(boolean automatic) {
091: mAutomaticUserId = automatic;
092: }
093:
094: boolean isAutomaticUserId() {
095: return mAutomaticUserId;
096: }
097:
098: public void setPassword(String password) {
099: if (password != null && 0 == password.length())
100: throw new IllegalArgumentException(
101: "password can't be empty.");
102:
103: mPassword = password;
104: }
105:
106: public String getPassword() {
107: return mPassword;
108: }
109:
110: public void setRoles(Collection<String> roles) {
111: if (null == roles) {
112: mRoles = null;
113: return;
114: }
115:
116: mRoles = new HashSet<String>(roles);
117: }
118:
119: public void setRoles(String[] roles) {
120: if (roles != null && roles.length > 0) {
121: setRoles(new HashSet<String>(Arrays.asList(roles)));
122: }
123: }
124:
125: public void addRole(String role) {
126: if (null == mRoles) {
127: mRoles = new HashSet<String>();
128: }
129: mRoles.add(role);
130: }
131:
132: public void removeRole(String role) {
133: if (null == mRoles) {
134: return;
135: }
136: mRoles.remove(role);
137: }
138:
139: public Collection<String> getRoles() {
140: if (null == mRoles) {
141: mRoles = new HashSet<String>();
142: }
143:
144: return mRoles;
145: }
146:
147: public boolean isInRole(String role) {
148: if (null == role)
149: throw new IllegalArgumentException("role can't be null.");
150: if (0 == role.length())
151: throw new IllegalArgumentException("role can't be empty.");
152:
153: if (null == mRoles) {
154: return false;
155: }
156:
157: return mRoles.contains(role);
158: }
159:
160: public boolean isValid(String password) {
161: if (null == password)
162: throw new IllegalArgumentException(
163: "password can't be null.");
164: if (0 == password.length())
165: throw new IllegalArgumentException(
166: "password can't be empty.");
167:
168: return mPassword != null && password.equals(mPassword);
169: }
170:
171: public boolean isValid(String password, String role) {
172: if (isValid(password) && isInRole(role)) {
173:
174: return true;
175: }
176:
177: return false;
178: }
179:
180: public synchronized RoleUserAttributes clone() {
181: RoleUserAttributes new_attributes = null;
182: try {
183: new_attributes = (RoleUserAttributes) super .clone();
184:
185: if (mRoles != null) {
186: new_attributes.mRoles = new HashSet<String>(mRoles);
187: }
188: } catch (CloneNotSupportedException e) {
189: new_attributes = null;
190: }
191:
192: return new_attributes;
193: }
194:
195: public boolean equals(Object other) {
196: if (null == other) {
197: return false;
198: }
199:
200: if (this == other) {
201: return true;
202: }
203:
204: if (!(other instanceof RoleUserAttributes)) {
205: return false;
206: }
207:
208: RoleUserAttributes other_attributes = (RoleUserAttributes) other;
209: if (getUserId() != other_attributes.getUserId()) {
210: return false;
211: }
212: if (!getPassword().equals(other_attributes.getPassword())) {
213: return false;
214: }
215: Collection<String> roles = getRoles();
216: Collection<String> other_roles = other_attributes.getRoles();
217: if ((roles != null || other_roles != null)) {
218: if (null == roles || null == other_roles) {
219: return false;
220: }
221: if (roles.size() != other_roles.size()) {
222: return false;
223: }
224:
225: for (String role : roles) {
226: if (!other_roles.contains(role)) {
227: return false;
228: }
229: }
230: }
231:
232: return true;
233: }
234: }
|