001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.serializer.objects;
018:
019: import java.security.Principal;
020: import java.util.ArrayList;
021: import java.sql.Date;
022: import java.util.Iterator;
023: import java.util.prefs.Preferences;
024:
025: import javolution.xml.XMLFormat;
026: import javolution.xml.stream.XMLStreamException;
027:
028: import org.apache.commons.lang.StringEscapeUtils;
029:
030: /**
031: * Jetspeed Serialized (JS) User
032: *
033: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
034: * @version $Id: $
035: */
036: public class JSUser {
037:
038: private String name;
039:
040: private char[] password;
041:
042: private JSPWAttributes pwData = null;
043:
044: private ArrayList roles = null;
045:
046: private ArrayList groups = null;
047:
048: private JSUserAttributes userInfo = null;
049:
050: private JSNVPElements preferences = null;
051:
052: private ArrayList publicCredentials = null;
053:
054: private ArrayList privateCredentials = null;
055:
056: private JSUserRoles roleString;
057:
058: private JSUserGroups groupString;
059:
060: private JSPrincipalRules rules = new JSPrincipalRules();
061:
062: private transient Principal principal;
063:
064: public JSUser() {
065: }
066:
067: public void addPublicCredential(Object o) {
068: if (publicCredentials == null)
069: publicCredentials = new ArrayList();
070: publicCredentials.add(o);
071: }
072:
073: public void addPrivateCredential(Object o) {
074: if (privateCredentials == null)
075: privateCredentials = new ArrayList();
076: privateCredentials.add(o);
077: }
078:
079: public void addGroup(JSGroup group) {
080: if (groups == null)
081: groups = new ArrayList();
082: groups.add(group);
083: }
084:
085: public void addRole(JSRole role) {
086: if (roles == null)
087: roles = new ArrayList();
088: roles.add(role);
089: }
090:
091: public ArrayList getGroups() {
092: return groups;
093: }
094:
095: public void setGroups(ArrayList groups) {
096: this .groups = groups;
097: }
098:
099: public char[] getPassword() {
100: return password;
101: }
102:
103: public void setUserCredential(String name, char[] password,
104: Date expirationDate, boolean isEnabled, boolean isExpired,
105: boolean requireUpdate) {
106: setName(name);
107: setPassword(password);
108: pwData = new JSPWAttributes();
109: if (password != null) {
110: pwData.getMyMap().put("password", this .getPasswordString());
111: if (expirationDate != null) {
112: pwData.getMyMap().put("expirationDate",
113: expirationDate.toString());
114: }
115: pwData.getMyMap().put("enabled",
116: (isEnabled ? "TRUE" : "FALSE"));
117: pwData.getMyMap().put("requiresUpdate",
118: (requireUpdate ? "TRUE" : "FALSE"));
119: }
120: }
121:
122: protected void resetPassword() {
123: try {
124: if (pwData != null) {
125: Object o = pwData.getMyMap().get("password");
126:
127: String pw = StringEscapeUtils.unescapeHtml((String) o);
128: if ((pw != null) && (pw.length() > 0))
129: password = pw.toCharArray();
130: else
131: password = null;
132: }
133: } catch (Exception e) {
134: password = null;
135: }
136: }
137:
138: public boolean getPwEnabled() {
139: return getPWBoolean("enabled", false);
140: }
141:
142: public boolean getPwRequiredUpdate() {
143: return getPWBoolean("requiresUpdate", false);
144: }
145:
146: public Date getPwExpirationDate() {
147: if (pwData != null) {
148: Object o = pwData.getMyMap().get("expirationDate");
149: if (o == null)
150: return null;
151: if (o instanceof Date)
152: return (Date) o;
153:
154: Date d = Date.valueOf((String) o);
155: return d;
156:
157: }
158: return null;
159: }
160:
161: private boolean getPWBoolean(String property, boolean defaultSetting) {
162: if (pwData == null)
163: return defaultSetting;
164: try {
165: Object o = pwData.getMyMap().get(property);
166: if (o == null)
167: return defaultSetting;
168: return ((String) o).equalsIgnoreCase("TRUE");
169: } catch (Exception e) {
170: return defaultSetting;
171: }
172: }
173:
174: public void setPassword(char[] password) {
175: this .password = password;
176: }
177:
178: public void setName(String name) {
179: this .name = name;
180: }
181:
182: public ArrayList getRoles() {
183: return roles;
184: }
185:
186: public void setRoles(ArrayList roles) {
187: this .roles = roles;
188: }
189:
190: public String getName() {
191: return name;
192: }
193:
194: /*
195: * private void initUser() throws Exception { User user = null; try {
196: * ums.addUser("test", "password01"); user = ums.getUser("test"); } catch
197: * (SecurityException sex) { assertTrue("user exists. should not have thrown
198: * an exception.", false); }
199: *
200: * Preferences userInfoPrefs = user.getPreferences().node("userinfo");
201: * userInfoPrefs.put("user.name.given", "Test Dude");
202: * userInfoPrefs.put("user.name.family", "Dudley"); }
203: *
204: */
205:
206: /**
207: * @return Returns the preferences.
208: */
209: public JSNVPElements getPreferences() {
210: return preferences;
211: }
212:
213: /**
214: * @param preferences
215: * The preferences to set.
216: */
217: public void setPreferences(Preferences preferences) {
218: this .preferences = new JSNVPElements(preferences);
219: }
220:
221: /**
222: * @return Returns the privateCredentials.
223: */
224: public ArrayList getPrivateCredentials() {
225: return privateCredentials;
226: }
227:
228: /**
229: * @param privateCredentials
230: * The privateCredentials to set.
231: */
232: public void setPrivateCredentials(ArrayList privateCredentials) {
233: this .privateCredentials = privateCredentials;
234: }
235:
236: /**
237: * @return Returns the publicCredentials.
238: */
239: public ArrayList getPublicCredentials() {
240: return publicCredentials;
241: }
242:
243: /**
244: * @param publicCredentials
245: * The publicCredentials to set.
246: */
247: public void setPublicCredentials(ArrayList publicCredentials) {
248: this .publicCredentials = publicCredentials;
249: }
250:
251: /**
252: * @param userInfo
253: * The userInfo to set.
254: */
255: public void setUserInfo(Preferences userInfo) {
256: this .userInfo = new JSUserAttributes(userInfo);
257: }
258:
259: /**
260: * @return Returns the userInfo.
261: */
262: public JSUserAttributes getUserInfo() {
263: return userInfo;
264: }
265:
266: /***************************************************************************
267: * SERIALIZER
268: */
269: private static final XMLFormat XML = new XMLFormat(JSUser.class) {
270:
271: public void write(Object o, OutputElement xml)
272: throws XMLStreamException {
273: try {
274: JSUser g = (JSUser) o;
275: String s = g.getName();
276: if ((s == null) || (s.length() == 0))
277: s = "guest";
278: xml.setAttribute("name", s);
279:
280: xml.add(g.getPwData());
281:
282: /** named fields HERE */
283:
284: /** implicitly named (through binding) fields here */
285: g.groupString = new JSUserGroups(g.putTokens(g
286: .getGroups()));
287: g.roleString = new JSUserRoles(g
288: .putTokens(g.getRoles()));
289:
290: xml.add(g.roleString);
291: xml.add(g.groupString);
292: xml.add(g.preferences);
293: xml.add(g.userInfo);
294: xml.add(g.rules);
295:
296: } catch (Exception e) {
297: e.printStackTrace();
298: }
299: }
300:
301: public void read(InputElement xml, Object o) {
302: try {
303: JSUser g = (JSUser) o;
304: g.name = StringEscapeUtils.unescapeHtml(xml
305: .getAttribute("name", "unknown"));
306:
307: Object o1 = null;
308:
309: while (xml.hasNext()) {
310: o1 = xml.getNext(); // mime
311:
312: if (o1 instanceof JSPWAttributes) {
313: g.pwData = (JSPWAttributes) o1;
314: g.resetPassword();
315: } else if (o1 instanceof JSUserGroups)
316: g.groupString = (JSUserGroups) o1;
317: else if (o1 instanceof JSUserRoles)
318: g.roleString = (JSUserRoles) o1;
319: else if (o1 instanceof JSUserAttributes)
320: g.userInfo = (JSUserAttributes) o1;
321: else if (o1 instanceof JSNVPElements)
322: g.preferences = (JSNVPElements) o1;
323: else if (o1 instanceof JSPrincipalRules)
324: g.rules = (JSPrincipalRules) o1;
325: }
326:
327: } catch (Exception e) {
328: e.printStackTrace();
329: }
330: }
331:
332: };
333:
334: private String append(JSRole rule) {
335: return rule.getName();
336: }
337:
338: private String append(JSGroup group) {
339: return group.getName();
340: }
341:
342: private String append(Object s) {
343: if (s instanceof JSRole)
344: return append((JSRole) s);
345: if (s instanceof JSGroup)
346: return append((JSGroup) s);
347:
348: return s.toString();
349: }
350:
351: private String putTokens(ArrayList _list) {
352: if ((_list == null) || (_list.size() == 0))
353: return "";
354: boolean _start = true;
355: Iterator _it = _list.iterator();
356: StringBuffer _sb = new StringBuffer();
357: while (_it.hasNext()) {
358: if (!_start)
359: _sb.append(',');
360: else
361: _start = false;
362:
363: _sb.append(append(_it.next()));
364: }
365: return _sb.toString();
366: }
367:
368: private String getPasswordString() {
369: if ((this .getPassword() == null)
370: || (this .getPassword().length == 0))
371: return "";
372: else
373: return new String(this .getPassword());
374: }
375:
376: /**
377: * @return Returns the rules.
378: */
379: public JSPrincipalRules getRules() {
380: return rules;
381: }
382:
383: /**
384: * @param rules
385: * The rules to set.
386: */
387: public void setRules(JSPrincipalRules rules) {
388: this .rules = rules;
389: }
390:
391: /**
392: * @return Returns the principal.
393: */
394: public Principal getPrincipal() {
395: return principal;
396: }
397:
398: /**
399: * @param principal
400: * The principal to set.
401: */
402: public void setPrincipal(Principal principal) {
403: this .principal = principal;
404: }
405:
406: public JSUserGroups getGroupString() {
407: return groupString;
408: }
409:
410: public JSUserRoles getRoleString() {
411: return roleString;
412: }
413:
414: public JSPWAttributes getPwData() {
415: return pwData;
416: }
417:
418: public void setPwData(JSPWAttributes pwData) {
419: this.pwData = pwData;
420: }
421:
422: }
|