001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or 1any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * Initial developer: Florent BENOIT
022: * --------------------------------------------------------------------------
023: * $Id: User.java 4804 2004-05-25 15:13:29Z benoitf $
024: * --------------------------------------------------------------------------
025: */package org.objectweb.jonas.security.realm.principals;
026:
027: import java.io.Serializable;
028: import java.security.Principal;
029: import java.util.ArrayList;
030: import java.util.Enumeration;
031: import java.util.StringTokenizer;
032: import java.util.Vector;
033:
034: import org.objectweb.jonas.security.realm.lib.HashHelper;
035: import org.objectweb.jonas.security.realm.lib.HashPassword;
036: import org.objectweb.jonas.security.realm.lib.XML;
037:
038: /**
039: * This class define the User class which represent a user by its name,
040: * password, etc.
041: * @author Florent Benoit (initial developer)
042: * @author Alexandre Thaveau (add DN for the certificates in method setName)
043: * @author Marc-Antoine Bourgeot (add DN for the certificates in method setName)
044: */
045: public class User implements Principal, Serializable, UserMBean {
046:
047: /**
048: * Separator of the groups/roles
049: */
050: protected static final String SEPARATOR = ",";
051:
052: /**
053: * Name of the user
054: */
055: private String name = null;
056:
057: /**
058: * Password of the user
059: */
060: private String password = null;
061:
062: /**
063: * Hash password of the user
064: */
065: private HashPassword hashPassword = null;
066:
067: /**
068: * Groups
069: */
070: private Vector groups = new Vector();
071:
072: /**
073: * Roles
074: */
075: private Vector roles = new Vector();
076:
077: /**
078: * Combined roles
079: */
080: private ArrayList combinedRoles = new ArrayList();
081:
082: /**
083: * Constructor
084: */
085: public User() {
086: }
087:
088: /**
089: * Constructor with a given login / password
090: * @param name the given name
091: * @param password the given password
092: */
093: public User(String name, String password) {
094: setName(name);
095: setPassword(password);
096: }
097:
098: /**
099: * Set the name of this user
100: * @param name Name of the user
101: */
102: public void setName(String name) {
103: if (name.startsWith("##DN##")) {
104: //replace problematic caracters(for mbeans) by special caracters
105: this .name = name.replace('=', '#').replace(',', '%')
106: .replace(' ', '$');
107: } else {
108: this .name = name;
109: }
110: }
111:
112: /**
113: * Get the name of this user
114: * @return the name of this user
115: */
116: public String getName() {
117: return name;
118: }
119:
120: /**
121: * Get the password of this user
122: * @return the password of this user
123: */
124: public String getPassword() {
125: return password;
126: }
127:
128: /**
129: * Set the password of this user
130: * @param password password of the user
131: */
132: public void setPassword(String password) {
133: this .password = password;
134: setHashPassword(HashHelper.getHashPassword(password));
135: }
136:
137: /**
138: * Set the hashed password of this user
139: * @param hashPassword hashed password of this user
140: */
141: protected void setHashPassword(HashPassword hashPassword) {
142: this .hashPassword = hashPassword;
143: }
144:
145: /**
146: * Set the hashed password of this user
147: * @return hashPassword hashed password of this user
148: */
149: public HashPassword getHashPassword() {
150: return hashPassword;
151: }
152:
153: /**
154: * Set the groups of the user
155: * @param groups the comma separated list of the groups of the user
156: */
157: public void setGroups(String groups) {
158: StringTokenizer st = new StringTokenizer(groups, SEPARATOR);
159: String group = null;
160: while (st.hasMoreTokens()) {
161: group = st.nextToken().trim();
162: addGroup(group);
163: }
164: }
165:
166: /**
167: * Get the groups
168: * @return the comma separated list of groups
169: */
170: public String getGroups() {
171: String groupsList = "";
172: Enumeration g = groups.elements();
173: int nb = 0;
174: String group = null;
175:
176: while (g.hasMoreElements()) {
177: if (nb > 0) {
178: groupsList += ", ";
179: }
180: group = (String) g.nextElement();
181: groupsList += group;
182: }
183: return groupsList;
184:
185: }
186:
187: /**
188: * Get the groups
189: * @return the array of the groups
190: */
191: public String[] getArrayGroups() {
192: return ((String[]) groups.toArray(new String[groups.size()]));
193: }
194:
195: /**
196: * Set the roles of the user
197: * @param roles the comma separated list of the roles of the user
198: */
199: public void setRoles(String roles) {
200: if (roles != null) {
201: StringTokenizer st = new StringTokenizer(roles, SEPARATOR);
202: String role = null;
203: while (st.hasMoreTokens()) {
204: role = st.nextToken().trim();
205: addRole(role);
206: }
207: }
208: }
209:
210: /**
211: * Add the specified group to this user
212: * @param group the group to add
213: */
214: public void addGroup(String group) {
215: if (!groups.contains(group)) {
216: this .groups.addElement(group);
217: }
218: }
219:
220: /**
221: * Add a role to this user
222: * @param role the given role
223: */
224: public void addRole(String role) {
225: if (!roles.contains(role)) {
226: this .roles.addElement(role);
227: }
228: }
229:
230: /**
231: * Remove a group from this user
232: * @param group the given group
233: */
234: public void removeGroup(String group) {
235: if (groups.contains(group)) {
236: this .groups.removeElement(group);
237: }
238: }
239:
240: /**
241: * Remove a role from this user
242: * @param role the given role
243: */
244: public void removeRole(String role) {
245: if (roles.contains(role)) {
246: this .roles.removeElement(role);
247: }
248: }
249:
250: /**
251: * Get the roles
252: * @return the array of the roles
253: */
254: public String getRoles() {
255: String rolesList = "";
256: Enumeration r = roles.elements();
257: int nb = 0;
258: String role = null;
259:
260: while (r.hasMoreElements()) {
261: if (nb > 0) {
262: rolesList += ", ";
263: }
264: role = (String) r.nextElement();
265: rolesList += role;
266: }
267: return rolesList;
268: }
269:
270: /**
271: * Set the combined roles of this user
272: * @param combinedRoles combined of the user
273: */
274: public void setCombinedRoles(ArrayList combinedRoles) {
275: this .combinedRoles = combinedRoles;
276: }
277:
278: /**
279: * Get the combined roles of this user
280: * @return the combined of the user
281: */
282: public ArrayList getCombinedRoles() {
283: return combinedRoles;
284: }
285:
286: /**
287: * Get the roles
288: * @return the array of the roles
289: */
290: public String[] getArrayRoles() {
291: return ((String[]) roles.toArray(new String[roles.size()]));
292: }
293:
294: /**
295: * String representation of the user
296: * @return the xml representation of the user
297: */
298: public String toXML() {
299: StringBuffer xml = new StringBuffer("<user name=\"");
300: xml.append(name);
301: xml.append("\" password=\"");
302: if (password != null) {
303: xml.append(password);
304: }
305: xml.append("\"");
306: XML.appendVectorToBuffer("groups=", xml, groups);
307: XML.appendVectorToBuffer("roles=", xml, roles);
308: xml.append(" />");
309: return xml.toString();
310: }
311:
312: /**
313: * Use the XML representation of this object
314: * @return the XML representation of this object
315: */
316: public String toString() {
317: return this.toXML();
318: }
319:
320: }
|