%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.turbine.services.security.ldap.LDAPUser |
|
|
1 | package org.apache.turbine.services.security.ldap; |
|
2 | ||
3 | /* |
|
4 | * Copyright 2001-2004 The Apache Software Foundation. |
|
5 | * |
|
6 | * Licensed under the Apache License, Version 2.0 (the "License") |
|
7 | * you may not use this file except in compliance with the License. |
|
8 | * You may obtain a copy of the License at |
|
9 | * |
|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
11 | * |
|
12 | * Unless required by applicable law or agreed to in writing, software |
|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15 | * See the License for the specific language governing permissions and |
|
16 | * limitations under the License. |
|
17 | */ |
|
18 | ||
19 | import java.io.ByteArrayOutputStream; |
|
20 | import java.io.PrintWriter; |
|
21 | import java.sql.Connection; |
|
22 | import java.util.Hashtable; |
|
23 | import javax.naming.NamingException; |
|
24 | import javax.naming.directory.Attribute; |
|
25 | import javax.naming.directory.Attributes; |
|
26 | import javax.naming.directory.BasicAttribute; |
|
27 | import javax.naming.directory.BasicAttributes; |
|
28 | import javax.servlet.http.HttpSessionBindingEvent; |
|
29 | ||
30 | import org.apache.commons.logging.Log; |
|
31 | import org.apache.commons.logging.LogFactory; |
|
32 | import org.apache.torque.om.BaseObject; |
|
33 | import org.apache.torque.om.StringKey; |
|
34 | import org.apache.turbine.om.security.User; |
|
35 | import org.apache.turbine.services.security.TurbineSecurity; |
|
36 | ||
37 | /** |
|
38 | * LDAPUser implements User and provides access to a user who accesses the |
|
39 | * system via LDAP. |
|
40 | * |
|
41 | * @author <a href="mailto:cberry@gluecode.com">Craig D. Berry</a> |
|
42 | * @author <a href="mailto:tadewunmi@gluecode.com">Tracy M. Adewunmi</a> |
|
43 | * @author <a href="mailto:lflournoy@gluecode.com">Leonard J. Flournoy </a> |
|
44 | * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a> |
|
45 | * @author <a href="mailto:hhernandez@itweb.com.mx">Humberto Hernandez</a> |
|
46 | * @version $Id: LDAPUser.java,v 1.12.2.2 2004/05/20 03:06:48 seade Exp $ |
|
47 | */ |
|
48 | public class LDAPUser extends BaseObject implements User |
|
49 | { |
|
50 | ||
51 | /** Logging */ |
|
52 | 0 | private static Log log = LogFactory.getLog(LDAPUser.class); |
53 | ||
54 | /* A few attributes common to a User. */ |
|
55 | ||
56 | /** Date when the user was created */ |
|
57 | 0 | private java.util.Date createDate = null; |
58 | ||
59 | /** Date when the user was last accessed */ |
|
60 | 0 | private java.util.Date lastAccessDate = null; |
61 | ||
62 | /** timeout */ |
|
63 | 0 | private int timeout = 15; |
64 | ||
65 | /** This is data that will survive a servlet engine restart. */ |
|
66 | 0 | private Hashtable permStorage = null; |
67 | ||
68 | /** This is data that will not survive a servlet engine restart. */ |
|
69 | 0 | private Hashtable tempStorage = null; |
70 | ||
71 | /** |
|
72 | * Constructor. |
|
73 | * Create a new User and set the createDate. |
|
74 | */ |
|
75 | public LDAPUser() |
|
76 | 0 | { |
77 | 0 | createDate = new java.util.Date(); |
78 | 0 | tempStorage = new Hashtable(10); |
79 | 0 | permStorage = new Hashtable(10); |
80 | 0 | setHasLoggedIn(new Boolean(false)); |
81 | 0 | } |
82 | ||
83 | /** |
|
84 | * Populates the user with values obtained from the LDAP Service. |
|
85 | * This method could be redefined in subclasses. |
|
86 | * @param attribs The attributes obtained from LDAP. |
|
87 | * @throws NamingException if there was an error with JNDI. |
|
88 | */ |
|
89 | public void setLDAPAttributes(Attributes attribs) |
|
90 | throws NamingException |
|
91 | { |
|
92 | ||
93 | Attribute attr; |
|
94 | String attrName; |
|
95 | ||
96 | // Set the User id. |
|
97 | 0 | attrName = LDAPSecurityConstants.getUserIdAttribute(); |
98 | 0 | if (attrName != null) |
99 | { |
|
100 | 0 | attr = attribs.get(attrName); |
101 | 0 | if (attr != null && attr.get() != class="keyword">null) |
102 | { |
|
103 | try |
|
104 | { |
|
105 | 0 | setPrimaryKey(new StringKey(attr.get().toString())); |
106 | } |
|
107 | 0 | catch (Exception ex) |
108 | { |
|
109 | 0 | log.error("Exception caught:", ex); |
110 | 0 | } |
111 | } |
|
112 | } |
|
113 | ||
114 | // Set the Username. |
|
115 | 0 | attrName = LDAPSecurityConstants.getNameAttribute(); |
116 | 0 | if (attrName != null) |
117 | { |
|
118 | 0 | attr = attribs.get(attrName); |
119 | 0 | if (attr != null && attr.get() != class="keyword">null) |
120 | { |
|
121 | 0 | setUserName(attr.get().toString()); |
122 | } |
|
123 | } |
|
124 | else |
|
125 | { |
|
126 | 0 | log.error("There is no LDAP attribute for the username."); |
127 | } |
|
128 | ||
129 | // Set the Firstname. |
|
130 | 0 | attrName = LDAPSecurityConstants.getFirstNameAttribute(); |
131 | 0 | if (attrName != null) |
132 | { |
|
133 | 0 | attr = attribs.get(attrName); |
134 | 0 | if (attr != null && attr.get() != class="keyword">null) |
135 | { |
|
136 | 0 | setFirstName(attr.get().toString()); |
137 | } |
|
138 | } |
|
139 | ||
140 | // Set the Lastname. |
|
141 | 0 | attrName = LDAPSecurityConstants.getLastNameAttribute(); |
142 | 0 | if (attrName != null) |
143 | { |
|
144 | 0 | attr = attribs.get(attrName); |
145 | 0 | if (attr != null && attr.get() != class="keyword">null) |
146 | { |
|
147 | 0 | setLastName(attr.get().toString()); |
148 | } |
|
149 | } |
|
150 | ||
151 | // Set the E-Mail |
|
152 | 0 | attrName = LDAPSecurityConstants.getEmailAttribute(); |
153 | 0 | if (attrName != null) |
154 | { |
|
155 | 0 | attr = attribs.get(attrName); |
156 | 0 | if (attr != null && attr.get() != class="keyword">null) |
157 | { |
|
158 | 0 | setEmail(attr.get().toString()); |
159 | } |
|
160 | } |
|
161 | 0 | } |
162 | ||
163 | /** |
|
164 | * Get the JNDI Attributes used to store the user in LDAP. |
|
165 | * This method could be redefined in a subclass. |
|
166 | * |
|
167 | * @throws NamingException if there is a JNDI error. |
|
168 | * @return The JNDI attributes of the user. |
|
169 | */ |
|
170 | public Attributes getLDAPAttributes() |
|
171 | throws NamingException |
|
172 | { |
|
173 | 0 | Attributes attribs = new BasicAttributes(); |
174 | String attrName; |
|
175 | ||
176 | // Set the objectClass |
|
177 | 0 | attrName = "objectClass"; |
178 | 0 | if (attrName != null) |
179 | { |
|
180 | 0 | Object value = "turbineUser"; |
181 | ||
182 | 0 | if (value != null) |
183 | { |
|
184 | 0 | Attribute attr = new BasicAttribute(attrName, value); |
185 | ||
186 | 0 | attribs.put(attr); |
187 | } |
|
188 | } |
|
189 | ||
190 | // Set the User id. |
|
191 | 0 | attrName = LDAPSecurityConstants.getUserIdAttribute(); |
192 | 0 | if (attrName != null) |
193 | { |
|
194 | 0 | Object value = getPrimaryKey(); |
195 | ||
196 | 0 | if (value != null) |
197 | { |
|
198 | 0 | Attribute attr = new BasicAttribute(attrName, value); |
199 | ||
200 | 0 | attribs.put(attr); |
201 | } |
|
202 | } |
|
203 | ||
204 | // Set the Username. |
|
205 | 0 | attrName = LDAPSecurityConstants.getNameAttribute(); |
206 | 0 | if (attrName != null) |
207 | { |
|
208 | 0 | Object value = getName(); |
209 | ||
210 | 0 | if (value != null) |
211 | { |
|
212 | 0 | Attribute attr = new BasicAttribute(attrName, value); |
213 | ||
214 | 0 | attribs.put(attr); |
215 | } |
|
216 | } |
|
217 | ||
218 | // Set the Firstname. |
|
219 | 0 | attrName = LDAPSecurityConstants.getFirstNameAttribute(); |
220 | 0 | if (attrName != null) |
221 | { |
|
222 | 0 | Object value = getFirstName(); |
223 | ||
224 | 0 | if (value != null) |
225 | { |
|
226 | 0 | Attribute attr = new BasicAttribute(attrName, value); |
227 | ||
228 | 0 | attribs.put(attr); |
229 | } |
|
230 | } |
|
231 | ||
232 | // Set the Lastname. |
|
233 | 0 | attrName = LDAPSecurityConstants.getLastNameAttribute(); |
234 | 0 | if (attrName != null) |
235 | { |
|
236 | 0 | Object value = getLastName(); |
237 | ||
238 | 0 | if (value != null) |
239 | { |
|
240 | 0 | Attribute attr = new BasicAttribute(attrName, value); |
241 | ||
242 | 0 | attribs.put(attr); |
243 | } |
|
244 | } |
|
245 | ||
246 | // Set the E-Mail. |
|
247 | 0 | attrName = LDAPSecurityConstants.getEmailAttribute(); |
248 | 0 | if (attrName != null) |
249 | { |
|
250 | 0 | Object value = getEmail(); |
251 | ||
252 | 0 | if (value != null) |
253 | { |
|
254 | 0 | Attribute attr = new BasicAttribute(attrName, value); |
255 | ||
256 | 0 | attribs.put(attr); |
257 | } |
|
258 | } |
|
259 | ||
260 | // Set the Password |
|
261 | 0 | attrName = LDAPSecurityConstants.getPasswordAttribute(); |
262 | 0 | if (attrName != null) |
263 | { |
|
264 | 0 | Object value = getPassword(); |
265 | ||
266 | 0 | if (value != null) |
267 | { |
|
268 | 0 | Attribute attr = new BasicAttribute(attrName, value); |
269 | ||
270 | 0 | attribs.put(attr); |
271 | } |
|
272 | } |
|
273 | ||
274 | 0 | return attribs; |
275 | } |
|
276 | ||
277 | /** |
|
278 | * Gets the distinguished name (DN) of the User. |
|
279 | * This method could be redefined in a subclass. |
|
280 | * @return The Distinguished Name of the user. |
|
281 | */ |
|
282 | public String getDN() |
|
283 | { |
|
284 | 0 | String filterAttribute = LDAPSecurityConstants.getNameAttribute(); |
285 | 0 | String userBaseSearch = LDAPSecurityConstants.getBaseSearch(); |
286 | 0 | String userName = getName(); |
287 | ||
288 | 0 | String dn = filterAttribute + "=" + userName + "," + userBaseSearch; |
289 | ||
290 | 0 | return dn; |
291 | } |
|
292 | ||
293 | /** |
|
294 | * Gets the access counter for a user during a session. |
|
295 | * |
|
296 | * @return The access counter for the user for the session. |
|
297 | */ |
|
298 | public int getAccessCounterForSession() |
|
299 | { |
|
300 | try |
|
301 | { |
|
302 | 0 | return ((Integer) getTemp(User.SESSION_ACCESS_COUNTER)).intValue(); |
303 | } |
|
304 | 0 | catch (Exception e) |
305 | { |
|
306 | 0 | return 0; |
307 | } |
|
308 | } |
|
309 | ||
310 | /** |
|
311 | * Gets the access counter for a user from perm storage. |
|
312 | * |
|
313 | * @return The access counter for the user. |
|
314 | */ |
|
315 | public int getAccessCounter() |
|
316 | { |
|
317 | try |
|
318 | { |
|
319 | 0 | return ((Integer) getPerm(User.ACCESS_COUNTER)).intValue(); |
320 | } |
|
321 | 0 | catch (Exception e) |
322 | { |
|
323 | 0 | return 0; |
324 | } |
|
325 | } |
|
326 | ||
327 | /** |
|
328 | * Gets the create date for this User. This is the time at which |
|
329 | * the user object was created. |
|
330 | * |
|
331 | * @return A Java Date with the date of creation for the user. |
|
332 | */ |
|
333 | public java.util.Date getCreateDate() |
|
334 | { |
|
335 | 0 | return createDate; |
336 | } |
|
337 | ||
338 | /** |
|
339 | * Returns the value of Confirmed variable |
|
340 | * @return the confirm value. |
|
341 | */ |
|
342 | public String getConfirmed() |
|
343 | { |
|
344 | 0 | String tmp = null; |
345 | ||
346 | 0 | tmp = (String) getPerm(User.CONFIRM_VALUE); |
347 | 0 | if (tmp != null && tmp.length() == 0) |
348 | { |
|
349 | 0 | tmp = null; |
350 | } |
|
351 | 0 | return tmp; |
352 | } |
|
353 | ||
354 | /** |
|
355 | * Returns the Email for this user. If this is defined, then |
|
356 | * the user is considered logged in. |
|
357 | * |
|
358 | * @return A String with the user's Email. |
|
359 | */ |
|
360 | public String getEmail() |
|
361 | { |
|
362 | 0 | String tmp = null; |
363 | ||
364 | 0 | tmp = (String) getPerm(User.EMAIL); |
365 | 0 | if (tmp != null && tmp.length() == 0) |
366 | { |
|
367 | 0 | tmp = null; |
368 | } |
|
369 | 0 | return tmp; |
370 | } |
|
371 | ||
372 | /** |
|
373 | * Gets the last access date for this User. This is the last time |
|
374 | * that the user object was referenced. |
|
375 | * |
|
376 | * @return A Java Date with the last access date for the user. |
|
377 | */ |
|
378 | public java.util.Date getLastAccessDate() |
|
379 | { |
|
380 | 0 | if (lastAccessDate == null) |
381 | { |
|
382 | 0 | setLastAccessDate(); |
383 | } |
|
384 | 0 | return lastAccessDate; |
385 | } |
|
386 | ||
387 | /** |
|
388 | * Get last login date/time for this user. |
|
389 | * |
|
390 | * @return A Java Date with the last login date for the user. |
|
391 | */ |
|
392 | public java.util.Date getLastLogin() |
|
393 | { |
|
394 | 0 | return (java.util.Date) getPerm(User.LAST_LOGIN); |
395 | } |
|
396 | ||
397 | /** |
|
398 | * Get password for this user. |
|
399 | * |
|
400 | * @return A String with the password for the user. |
|
401 | */ |
|
402 | public String getPassword() |
|
403 | { |
|
404 | 0 | return (String) getPerm(User.PASSWORD); |
405 | } |
|
406 | ||
407 | /** |
|
408 | * Get an object from permanent storage. |
|
409 | * @param name The object's name. |
|
410 | * @return An Object with the given name. |
|
411 | */ |
|
412 | public Object getPerm(String name) |
|
413 | { |
|
414 | 0 | return permStorage.get(name); |
415 | } |
|
416 | ||
417 | /** |
|
418 | * Get an object from permanent storage; return default if value |
|
419 | * is null. |
|
420 | * |
|
421 | * @param name The object's name. |
|
422 | * @param def A default value to return. |
|
423 | * @return An Object with the given name. |
|
424 | */ |
|
425 | public Object getPerm(String name, Object def) |
|
426 | { |
|
427 | try |
|
428 | { |
|
429 | 0 | Object val = permStorage.get(name); |
430 | ||
431 | 0 | if (val == null) |
432 | { |
|
433 | 0 | return def; |
434 | } |
|
435 | 0 | return val; |
436 | } |
|
437 | 0 | catch (Exception e) |
438 | { |
|
439 | 0 | return def; |
440 | } |
|
441 | } |
|
442 | ||
443 | /** |
|
444 | * This should only be used in the case where we want to save the |
|
445 | * data to the database. |
|
446 | * |
|
447 | * @return A Hashtable. |
|
448 | */ |
|
449 | public Hashtable getPermStorage() |
|
450 | { |
|
451 | 0 | if (this.permStorage == null) |
452 | { |
|
453 | 0 | this.permStorage = new Hashtable(); |
454 | } |
|
455 | 0 | return this.permStorage; |
456 | } |
|
457 | ||
458 | /** |
|
459 | * Get an object from temporary storage. |
|
460 | * |
|
461 | * @param name The object's name. |
|
462 | * @return An Object with the given name. |
|
463 | */ |
|
464 | public Object getTemp(String name) |
|
465 | { |
|
466 | 0 | return tempStorage.get(name); |
467 | } |
|
468 | ||
469 | /** |
|
470 | * Get an object from temporary storage; return default if value |
|
471 | * is null. |
|
472 | * |
|
473 | * @param name The object's name. |
|
474 | * @param def A default value to return. |
|
475 | * @return An Object with the given name. |
|
476 | */ |
|
477 | public Object getTemp(String name, Object def) |
|
478 | { |
|
479 | Object val; |
|
480 | ||
481 | try |
|
482 | { |
|
483 | 0 | val = tempStorage.get(name); |
484 | 0 | if (val == null) |
485 | { |
|
486 | 0 | val = def; |
487 | } |
|
488 | } |
|
489 | 0 | catch (Exception e) |
490 | { |
|
491 | 0 | val = def; |
492 | 0 | } |
493 | 0 | return val; |
494 | } |
|
495 | ||
496 | /** |
|
497 | * A User object can have a variable Timeout, which is defined in |
|
498 | * minutes. If the user has been timed out, then the |
|
499 | * hasLoggedIn() value will return false. |
|
500 | * |
|
501 | * @return An int specifying the timeout. |
|
502 | */ |
|
503 | public int getTimeout() |
|
504 | { |
|
505 | 0 | return this.timeout; |
506 | } |
|
507 | ||
508 | /** |
|
509 | * Returns the username for this user. If this is defined, then |
|
510 | * the user is considered logged in. |
|
511 | * |
|
512 | * @return A String with the username. |
|
513 | * @deprecated Use getName() instead |
|
514 | */ |
|
515 | public String getUserName() |
|
516 | { |
|
517 | 0 | return getName(); |
518 | } |
|
519 | ||
520 | /** |
|
521 | * Returns the first name for this user. If this is defined, then |
|
522 | * the user is considered logged in. |
|
523 | * |
|
524 | * @return A String with the user's first name. |
|
525 | */ |
|
526 | public String getFirstName() |
|
527 | { |
|
528 | 0 | String tmp = null; |
529 | ||
530 | 0 | tmp = (String) getPerm(User.FIRST_NAME); |
531 | 0 | if (tmp != null && tmp.length() == 0) |
532 | { |
|
533 | 0 | tmp = null; |
534 | } |
|
535 | 0 | return tmp; |
536 | } |
|
537 | ||
538 | /** |
|
539 | * Returns the last name for this user. If this is defined, then |
|
540 | * the user is considered logged in. |
|
541 | * |
|
542 | * @return A String with the user's last name. |
|
543 | */ |
|
544 | public String getLastName() |
|
545 | { |
|
546 | 0 | String tmp = null; |
547 | ||
548 | 0 | tmp = (String) getPerm(User.LAST_NAME); |
549 | 0 | if (tmp != null && tmp.length() == 0) |
550 | { |
|
551 | 0 | tmp = null; |
552 | } |
|
553 | 0 | return tmp; |
554 | } |
|
555 | ||
556 | /** |
|
557 | * The user is considered logged in if they have not timed out. |
|
558 | * |
|
559 | * @return True if the user has logged in. |
|
560 | */ |
|
561 | public boolean hasLoggedIn() |
|
562 | { |
|
563 | 0 | Boolean tmp = getHasLoggedIn(); |
564 | ||
565 | 0 | if (tmp != null && tmp.booleanValue()) |
566 | { |
|
567 | 0 | return true; |
568 | } |
|
569 | else |
|
570 | { |
|
571 | 0 | return false; |
572 | } |
|
573 | } |
|
574 | ||
575 | /** |
|
576 | * This method reports whether or not the user has been confirmed |
|
577 | * in the system by checking the <code>CONFIRM_VALUE</code> |
|
578 | * column to see if it is equal to <code>CONFIRM_DATA</code>. |
|
579 | * |
|
580 | * @return True if the user has been confirmed. |
|
581 | */ |
|
582 | public boolean isConfirmed() |
|
583 | { |
|
584 | 0 | return ((String) getTemp(CONFIRM_VALUE, "")).equals(CONFIRM_DATA); |
585 | } |
|
586 | ||
587 | /** |
|
588 | * Increments the permanent hit counter for the user. |
|
589 | */ |
|
590 | public void incrementAccessCounter() |
|
591 | { |
|
592 | 0 | setAccessCounter(getAccessCounter() + 1); |
593 | 0 | } |
594 | ||
595 | /** |
|
596 | * Increments the session hit counter for the user. |
|
597 | */ |
|
598 | public void incrementAccessCounterForSession() |
|
599 | { |
|
600 | 0 | setAccessCounterForSession(getAccessCounterForSession() + 1); |
601 | 0 | } |
602 | ||
603 | /** |
|
604 | * Remove an object from temporary storage and return the object. |
|
605 | * |
|
606 | * @param name The name of the object to remove. |
|
607 | * @return An Object. |
|
608 | */ |
|
609 | public Object removeTemp(String name) |
|
610 | { |
|
611 | 0 | return tempStorage.remove(name); |
612 | } |
|
613 | ||
614 | /** |
|
615 | * Sets the access counter for a user, saved in perm storage. |
|
616 | * |
|
617 | * @param cnt The new count. |
|
618 | */ |
|
619 | public void setAccessCounter(int cnt) |
|
620 | { |
|
621 | 0 | setPerm(User.ACCESS_COUNTER, new Integer(cnt)); |
622 | 0 | } |
623 | ||
624 | /** |
|
625 | * Sets the session access counter for a user, saved in temp |
|
626 | * storage. |
|
627 | * |
|
628 | * @param cnt The new count. |
|
629 | */ |
|
630 | public void setAccessCounterForSession(int cnt) |
|
631 | { |
|
632 | 0 | setTemp(User.SESSION_ACCESS_COUNTER, new Integer(cnt)); |
633 | 0 | } |
634 | ||
635 | /** |
|
636 | * Set the users confirmed variable |
|
637 | * |
|
638 | * @param confirm The new confim value. |
|
639 | */ |
|
640 | public void setConfirmed(String confirm) |
|
641 | { |
|
642 | 0 | getPerm(User.CONFIRM_VALUE, confirm); |
643 | 0 | } |
644 | ||
645 | /** |
|
646 | * Sets the last access date for this User. This is the last time |
|
647 | * that the user object was referenced. |
|
648 | */ |
|
649 | public void setLastAccessDate() |
|
650 | { |
|
651 | 0 | lastAccessDate = new java.util.Date(); |
652 | 0 | } |
653 | ||
654 | /** |
|
655 | * Sets the create date for this User. This is the time at which |
|
656 | * the user object was created. |
|
657 | * |
|
658 | * @param date The create date. |
|
659 | */ |
|
660 | public void setCreateDate(java.util.Date date) |
|
661 | { |
|
662 | 0 | createDate = date; |
663 | 0 | } |
664 | ||
665 | /** |
|
666 | * Set the users Email |
|
667 | * |
|
668 | * @param email The new email. |
|
669 | */ |
|
670 | public void setEmail(String email) |
|
671 | { |
|
672 | 0 | setPerm(User.EMAIL, email); |
673 | 0 | } |
674 | ||
675 | /** |
|
676 | * Set the users First Name |
|
677 | * |
|
678 | * @param fname The new firstname. |
|
679 | */ |
|
680 | public void setFirstName(String fname) |
|
681 | { |
|
682 | 0 | setPerm(User.FIRST_NAME, fname); |
683 | 0 | } |
684 | ||
685 | /** |
|
686 | * Set last login date/time. |
|
687 | * |
|
688 | * @param date The last login date. |
|
689 | */ |
|
690 | public void setLastLogin(java.util.Date date) |
|
691 | { |
|
692 | 0 | setPerm(User.LAST_LOGIN, date); |
693 | 0 | } |
694 | ||
695 | /** |
|
696 | * Set the users Last Name |
|
697 | * Sets the last name for this user. |
|
698 | * |
|
699 | * @param lname The new lastname. |
|
700 | */ |
|
701 | public void setLastName(String lname) |
|
702 | { |
|
703 | 0 | setPerm(User.LAST_NAME, lname); |
704 | 0 | } |
705 | ||
706 | /** |
|
707 | * Set password. |
|
708 | * |
|
709 | * @param password The new password. |
|
710 | */ |
|
711 | public void setPassword(String password) |
|
712 | { |
|
713 | 0 | setPerm(User.PASSWORD, password); |
714 | 0 | } |
715 | ||
716 | /** |
|
717 | * Put an object into permanent storage. |
|
718 | * |
|
719 | * @param name The object's name. |
|
720 | * @param value The object. |
|
721 | */ |
|
722 | public void setPerm(String name, Object value) |
|
723 | { |
|
724 | 0 | permStorage.put(name, value); |
725 | 0 | } |
726 | ||
727 | /** |
|
728 | * This should only be used in the case where we want to save the |
|
729 | * data to the database. |
|
730 | * |
|
731 | * @param stuff A Hashtable. |
|
732 | */ |
|
733 | public void setPermStorage(Hashtable stuff) |
|
734 | { |
|
735 | 0 | this.permStorage = stuff; |
736 | 0 | } |
737 | ||
738 | /** |
|
739 | * This should only be used in the case where we want to save the |
|
740 | * data to the database. |
|
741 | * |
|
742 | * @return A Hashtable. |
|
743 | */ |
|
744 | public Hashtable getTempStorage() |
|
745 | { |
|
746 | 0 | if (this.tempStorage == null) |
747 | { |
|
748 | 0 | this.tempStorage = new Hashtable(); |
749 | } |
|
750 | 0 | return this.tempStorage; |
751 | } |
|
752 | ||
753 | /** |
|
754 | * This should only be used in the case where we want to save the |
|
755 | * data to the database. |
|
756 | * |
|
757 | * @param storage A Hashtable. |
|
758 | */ |
|
759 | public void setTempStorage(Hashtable storage) |
|
760 | { |
|
761 | 0 | this.tempStorage = storage; |
762 | 0 | } |
763 | ||
764 | /** |
|
765 | * This gets whether or not someone has logged in. hasLoggedIn() |
|
766 | * returns this value as a boolean. This is private because you |
|
767 | * should use hasLoggedIn() instead. |
|
768 | * |
|
769 | * @return True if someone has logged in. |
|
770 | */ |
|
771 | private Boolean getHasLoggedIn() |
|
772 | { |
|
773 | 0 | return (Boolean) getTemp(User.HAS_LOGGED_IN); |
774 | } |
|
775 | ||
776 | /** |
|
777 | * This sets whether or not someone has logged in. hasLoggedIn() |
|
778 | * returns this value. |
|
779 | * |
|
780 | * @param value Whether someone has logged in or not. |
|
781 | */ |
|
782 | public void setHasLoggedIn(Boolean value) |
|
783 | { |
|
784 | 0 | setTemp(User.HAS_LOGGED_IN, value); |
785 | 0 | } |
786 | ||
787 | /** |
|
788 | * Put an object into temporary storage. |
|
789 | * |
|
790 | * @param name The object's name. |
|
791 | * @param value The object. |
|
792 | */ |
|
793 | public void setTemp(String name, Object value) |
|
794 | { |
|
795 | 0 | tempStorage.put(name, value); |
796 | 0 | } |
797 | ||
798 | /** |
|
799 | * A User object can have a variable Timeout which is defined in |
|
800 | * minutes. If the user has been timed out, then the |
|
801 | * hasLoggedIn() value will return false. |
|
802 | * |
|
803 | * @param time The user's timeout. |
|
804 | */ |
|
805 | public void setTimeout(int time) |
|
806 | { |
|
807 | 0 | this.timeout = time; |
808 | 0 | } |
809 | ||
810 | /** |
|
811 | * Sets the username for this user. |
|
812 | * |
|
813 | * @param username The user's username. |
|
814 | */ |
|
815 | public void setUserName(String username) |
|
816 | { |
|
817 | 0 | setPerm(User.USERNAME, username); |
818 | 0 | } |
819 | ||
820 | /** |
|
821 | * Updates the last login date in the database. |
|
822 | * |
|
823 | * @exception Exception a generic exception. |
|
824 | */ |
|
825 | public void updateLastLogin() throws Exception |
|
826 | { |
|
827 | 0 | setPerm(User.LAST_LOGIN, new java.util.Date()); |
828 | 0 | } |
829 | ||
830 | /** |
|
831 | * Implement this method if you wish to be notified when the User |
|
832 | * has been Bound to the session. |
|
833 | * |
|
834 | * @param hsbe The HttpSessionBindingEvent. |
|
835 | */ |
|
836 | public void valueBound(HttpSessionBindingEvent hsbe) |
|
837 | { |
|
838 | // Do not currently need this method. |
|
839 | 0 | } |
840 | ||
841 | /** |
|
842 | * Implement this method if you wish to be notified when the User |
|
843 | * has been Unbound from the session. |
|
844 | * |
|
845 | * @param hsbe The HttpSessionBindingEvent. |
|
846 | */ |
|
847 | public void valueUnbound(HttpSessionBindingEvent hsbe) |
|
848 | { |
|
849 | try |
|
850 | { |
|
851 | 0 | if (hasLoggedIn()) |
852 | { |
|
853 | 0 | TurbineSecurity.saveUser(this); |
854 | } |
|
855 | } |
|
856 | 0 | catch (Exception e) |
857 | { |
|
858 | 0 | log.error("BaseUser.valueUnbobund(): " |
859 | + e.getMessage()); |
|
860 | 0 | log.error(e); |
861 | ||
862 | // To prevent messages being lost in case the logging system |
|
863 | // goes away before sessions get unbound on servlet container |
|
864 | // shutdown, print the stcktrace to the container's console. |
|
865 | 0 | ByteArrayOutputStream ostr = new ByteArrayOutputStream(); |
866 | ||
867 | 0 | e.printStackTrace(new PrintWriter(ostr, true)); |
868 | 0 | String stackTrace = ostr.toString(); |
869 | ||
870 | 0 | System.out.println(stackTrace); |
871 | 0 | } |
872 | 0 | } |
873 | ||
874 | /** |
|
875 | * Returns the username for this user. If this is defined, then |
|
876 | * the user is considered logged in. |
|
877 | * |
|
878 | * @return A String with the username. |
|
879 | */ |
|
880 | public String getName() |
|
881 | { |
|
882 | 0 | String tmp = null; |
883 | ||
884 | 0 | tmp = (String) getPerm(User.USERNAME); |
885 | 0 | if (tmp != null && tmp.length() == 0) |
886 | { |
|
887 | 0 | tmp = null; |
888 | } |
|
889 | 0 | return tmp; |
890 | } |
|
891 | ||
892 | /** |
|
893 | * Not implemented. |
|
894 | * @param name the name of the User. |
|
895 | */ |
|
896 | public void setName(String name) |
|
897 | { |
|
898 | 0 | } |
899 | ||
900 | /** |
|
901 | * Not implemented. |
|
902 | * @return 0 |
|
903 | */ |
|
904 | public int getId() |
|
905 | { |
|
906 | 0 | return 0; |
907 | } |
|
908 | ||
909 | /** |
|
910 | * Not implemented. |
|
911 | * @return null |
|
912 | */ |
|
913 | public Integer getIdAsObj() |
|
914 | { |
|
915 | 0 | return new Integer(0); |
916 | } |
|
917 | ||
918 | /** |
|
919 | * Not implemented. |
|
920 | * |
|
921 | * @param id The id of the User. |
|
922 | */ |
|
923 | public void setId(int id) |
|
924 | { |
|
925 | 0 | } |
926 | ||
927 | /** |
|
928 | * Saves this object to the data store. |
|
929 | * @throws Exception if it cannot be saved |
|
930 | */ |
|
931 | public void save() |
|
932 | throws Exception |
|
933 | { |
|
934 | 0 | if (TurbineSecurity.accountExists(this)) |
935 | { |
|
936 | 0 | TurbineSecurity.saveUser(this); |
937 | } |
|
938 | else |
|
939 | { |
|
940 | 0 | TurbineSecurity.addUser(this, getPassword()); |
941 | } |
|
942 | 0 | } |
943 | ||
944 | /** |
|
945 | * not implemented |
|
946 | * |
|
947 | * @param conn the database connection |
|
948 | * @throws Exception if there is an error |
|
949 | */ |
|
950 | public void save(Connection conn) throws Exception |
|
951 | { |
|
952 | 0 | throw new Exception("not implemented"); |
953 | } |
|
954 | ||
955 | /** |
|
956 | * not implemented |
|
957 | * |
|
958 | * @param dbname the database name |
|
959 | * @throws Exception if there is an error |
|
960 | */ |
|
961 | public void save(String dbname) throws Exception |
|
962 | { |
|
963 | 0 | throw new Exception("not implemented"); |
964 | } |
|
965 | ||
966 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |