View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.serializer.objects;
18  
19  import java.security.Principal;
20  import java.util.ArrayList;
21  import java.sql.Date;
22  import java.util.Iterator;
23  import java.util.prefs.Preferences;
24  
25  import javolution.xml.XMLFormat;
26  import javolution.xml.stream.XMLStreamException;
27  
28  import org.apache.commons.lang.StringEscapeUtils;
29  
30  /***
31   * Jetspeed Serialized (JS) User
32   * 
33   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
34   * @version $Id: $
35   */
36  public class JSUser
37  {
38  
39      private String name;
40  
41      private char[] password;
42  
43      private JSPWAttributes pwData = null;
44  
45      private ArrayList roles = null;
46  
47      private ArrayList groups = null;
48  
49      private JSUserAttributes userInfo = null;
50  
51      private JSNVPElements preferences = null;
52  
53      private ArrayList publicCredentials = null;
54  
55      private ArrayList privateCredentials = null;
56  
57      private JSUserRoles roleString;
58  
59      private JSUserGroups groupString;
60  
61      private JSPrincipalRules rules = new JSPrincipalRules();
62  
63      private transient Principal principal;
64  
65      public JSUser()
66      {
67      }
68  
69      public void addPublicCredential(Object o)
70      {
71          if (publicCredentials == null) publicCredentials = new ArrayList();
72          publicCredentials.add(o);
73      }
74  
75      public void addPrivateCredential(Object o)
76      {
77          if (privateCredentials == null) privateCredentials = new ArrayList();
78          privateCredentials.add(o);
79      }
80  
81      public void addGroup(JSGroup group)
82      {
83          if (groups == null) groups = new ArrayList();
84          groups.add(group);
85      }
86  
87      public void addRole(JSRole role)
88      {
89          if (roles == null) roles = new ArrayList();
90          roles.add(role);
91      }
92  
93      public ArrayList getGroups()
94      {
95          return groups;
96      }
97  
98      public void setGroups(ArrayList groups)
99      {
100         this.groups = groups;
101     }
102 
103     public char[] getPassword()
104     {
105         return password;
106     }
107 
108     public void setUserCredential(String name, char[] password, Date expirationDate, boolean isEnabled, boolean isExpired, boolean requireUpdate)
109     {
110         setName(name);
111         setPassword(password);
112         pwData = new JSPWAttributes();
113         if (password != null)
114         {
115 	        pwData.getMyMap().put("password",this.getPasswordString());
116 	        if (expirationDate != null)
117 	        {
118 	        	pwData.getMyMap().put("expirationDate",expirationDate.toString());
119 	        }
120 	        pwData.getMyMap().put("enabled",(isEnabled?"TRUE":"FALSE"));
121 	        pwData.getMyMap().put("requiresUpdate",(requireUpdate?"TRUE":"FALSE"));
122         }
123     }
124 
125     protected void resetPassword()
126     {
127     	try
128     	{
129 	    	if (pwData != null)
130 	    	{
131 	    		Object o = pwData.getMyMap().get("password");
132     		
133 	    		String pw = StringEscapeUtils.unescapeHtml((String)o);
134 	    		if ((pw != null) && (pw.length()>0))
135 	    			password = pw.toCharArray();
136 	    		else
137 	    			password = null;
138 	    	}
139     	}
140     	catch (Exception e)
141     	{
142 			password = null;
143     	}
144     }
145     
146     public boolean getPwEnabled()
147     {
148        	return getPWBoolean("enabled",false);
149     }
150     public boolean getPwRequiredUpdate()
151     {
152        	return getPWBoolean("requiresUpdate",false);
153     }
154 
155     
156 
157     
158     
159     public Date getPwExpirationDate()
160     {
161     	if (pwData != null)
162     	{
163     		Object o = pwData.getMyMap().get("expirationDate");
164     		if (o == null)
165     			return null;
166     		if ( o instanceof Date)
167     			return (Date)o;
168     		
169     		Date d = Date.valueOf((String)o);
170     		return d;
171     		
172     	}
173     	return null;
174     }
175     
176     
177    private boolean getPWBoolean(String property, boolean defaultSetting)
178     {
179        	if (pwData == null)
180        		return defaultSetting;
181        	try
182        	{
183 	   		Object o = pwData.getMyMap().get(property);
184 			if (o == null)
185 				return defaultSetting;
186 			return ((String)o).equalsIgnoreCase("TRUE");
187        	}
188        	catch (Exception e)
189        	{
190        		return defaultSetting;
191        	}
192     }
193    
194     public void setPassword(char[] password)
195     {
196         this.password = password;
197     }
198 
199     public void setName(String name)
200     {
201         this.name = name;
202     }
203 
204     public ArrayList getRoles()
205     {
206         return roles;
207     }
208 
209     public void setRoles(ArrayList roles)
210     {
211         this.roles = roles;
212     }
213 
214     public String getName()
215     {
216         return name;
217     }
218 
219     /*
220      * private void initUser() throws Exception { User user = null; try {
221      * ums.addUser("test", "password01"); user = ums.getUser("test"); } catch
222      * (SecurityException sex) { assertTrue("user exists. should not have thrown
223      * an exception.", false); }
224      * 
225      * Preferences userInfoPrefs = user.getPreferences().node("userinfo");
226      * userInfoPrefs.put("user.name.given", "Test Dude");
227      * userInfoPrefs.put("user.name.family", "Dudley"); }
228      * 
229      */
230 
231     /***
232      * @return Returns the preferences.
233      */
234     public JSNVPElements getPreferences()
235     {
236         return preferences;
237     }
238 
239     /***
240      * @param preferences
241      *            The preferences to set.
242      */
243     public void setPreferences(Preferences preferences)
244     {
245         this.preferences = new JSNVPElements(preferences);
246     }
247 
248     /***
249      * @return Returns the privateCredentials.
250      */
251     public ArrayList getPrivateCredentials()
252     {
253         return privateCredentials;
254     }
255 
256     /***
257      * @param privateCredentials
258      *            The privateCredentials to set.
259      */
260     public void setPrivateCredentials(ArrayList privateCredentials)
261     {
262         this.privateCredentials = privateCredentials;
263     }
264 
265     /***
266      * @return Returns the publicCredentials.
267      */
268     public ArrayList getPublicCredentials()
269     {
270         return publicCredentials;
271     }
272 
273     /***
274      * @param publicCredentials
275      *            The publicCredentials to set.
276      */
277     public void setPublicCredentials(ArrayList publicCredentials)
278     {
279         this.publicCredentials = publicCredentials;
280     }
281 
282     /***
283      * @param userInfo
284      *            The userInfo to set.
285      */
286     public void setUserInfo(Preferences userInfo)
287     {
288         this.userInfo = new JSUserAttributes(userInfo);
289     }
290 
291     /***
292      * @return Returns the userInfo.
293      */
294     public JSUserAttributes getUserInfo()
295     {
296         return userInfo;
297     }
298 
299     /****************************************************************************
300      * SERIALIZER
301      */
302     private static final XMLFormat XML = new XMLFormat(JSUser.class)
303     {
304 
305         public void write(Object o, OutputElement xml)
306                 throws XMLStreamException
307         {
308             try
309             {
310                 JSUser g = (JSUser) o;
311                 String s = g.getName();
312                 if ((s == null) || (s.length() == 0)) s = "guest";
313                 xml.setAttribute("name", s);
314 
315                 
316                 xml.add(g.getPwData());
317 
318                 /*** named fields HERE */
319  
320                 /*** implicitly named (through binding) fields here */
321                 g.groupString = new JSUserGroups(g.putTokens(g.getGroups()));
322                 g.roleString = new JSUserRoles(g.putTokens(g.getRoles()));
323 
324                 xml.add(g.roleString);
325                 xml.add(g.groupString);
326                 xml.add(g.preferences);
327                 xml.add(g.userInfo);
328                 xml.add(g.rules);
329 
330             } catch (Exception e)
331             {
332                 e.printStackTrace();
333             }
334         }
335 
336         public void read(InputElement xml, Object o)
337         {
338             try
339             {
340                 JSUser g = (JSUser) o;
341                 g.name = StringEscapeUtils.unescapeHtml(xml.getAttribute("name", "unknown"));
342                 
343                 
344                 Object o1 = null;
345  
346 
347 				while (xml.hasNext())
348 				{
349 					o1 = xml.getNext(); // mime
350 					
351 					
352 					if (o1 instanceof JSPWAttributes)
353 					{
354 						g.pwData = (JSPWAttributes) o1;
355 						g.resetPassword();
356 					}
357 					else
358 					if (o1 instanceof JSUserGroups)
359 						g.groupString = (JSUserGroups) o1;
360 					else
361 	                    if (o1 instanceof JSUserRoles)
362 	                        g.roleString = (JSUserRoles) o1;
363 	                    else
364                             if (o1 instanceof JSUserAttributes)
365 	                            g.userInfo  = (JSUserAttributes) o1;
366 	                            else
367 		                        if (o1 instanceof JSNVPElements)
368 		                        	g.preferences  = (JSNVPElements) o1;
369 		                        else
370 	                                if (o1 instanceof JSPrincipalRules)
371 	                                g.rules  = (JSPrincipalRules) o1;
372                 }
373                 
374  
375             } catch (Exception e)
376             {
377                 e.printStackTrace();
378             }
379         }
380 
381     };
382 
383 
384     private String append(JSRole rule)
385     {
386         return rule.getName();
387     }
388 
389     private String append(JSGroup group)
390     {
391         return group.getName();
392     }
393 
394     private String append(Object s)
395     {
396         if (s instanceof JSRole) return append((JSRole) s);
397         if (s instanceof JSGroup) return append((JSGroup) s);
398 
399         return s.toString();
400     }
401 
402     private String putTokens(ArrayList _list)
403     {
404         if ((_list == null) || (_list.size() == 0)) return "";
405         boolean _start = true;
406         Iterator _it = _list.iterator();
407         StringBuffer _sb = new StringBuffer();
408         while (_it.hasNext())
409         {
410             if (!_start)
411                 _sb.append(',');
412             else
413                 _start = false;
414 
415             _sb.append(append(_it.next()));
416         }
417         return _sb.toString();
418     }
419 
420     private String getPasswordString()
421     {
422         if ((this.getPassword() == null) || (this.getPassword().length == 0))
423             return "";
424         else
425             return new String(this.getPassword());
426     }
427 
428     /***
429      * @return Returns the rules.
430      */
431     public JSPrincipalRules getRules()
432     {
433         return rules;
434     }
435 
436     /***
437      * @param rules
438      *            The rules to set.
439      */
440     public void setRules(JSPrincipalRules rules)
441     {
442         this.rules = rules;
443     }
444 
445     /***
446      * @return Returns the principal.
447      */
448     public Principal getPrincipal()
449     {
450         return principal;
451     }
452 
453     /***
454      * @param principal
455      *            The principal to set.
456      */
457     public void setPrincipal(Principal principal)
458     {
459         this.principal = principal;
460     }
461 
462 	public JSUserGroups getGroupString()
463 	{
464 		return groupString;
465 	}
466 
467 	public JSUserRoles getRoleString()
468 	{
469 		return roleString;
470 	}
471 
472 	public JSPWAttributes getPwData()
473 	{
474 		return pwData;
475 	}
476 
477 	public void setPwData(JSPWAttributes pwData)
478 	{
479 		this.pwData = pwData;
480 	}
481 
482 }