1 package org.apache.turbine.om.security; 2 3 18 19 import java.sql.Connection ; 20 21 import java.util.Date ; 22 import java.util.Hashtable ; 23 24 import javax.servlet.http.HttpSessionBindingEvent ; 25 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 29 import org.apache.turbine.services.security.TurbineSecurity; 30 31 51 public class TurbineUser extends SecurityObject implements User 52 { 53 54 private static Log log = LogFactory.getLog(TurbineUser.class); 55 56 57 private Date createDate = null; 58 59 60 private Date lastAccessDate = null; 61 62 63 private Hashtable permStorage = null; 64 65 66 private Hashtable tempStorage = null; 67 68 73 public TurbineUser() 74 { 75 super(); 76 createDate = new Date (); 77 setHasLoggedIn(Boolean.FALSE); 78 } 79 80 85 public int getAccessCounterForSession() 86 { 87 try 88 { 89 return ((Integer ) getTemp(User.SESSION_ACCESS_COUNTER)).intValue(); 90 } 91 catch (Exception e) 92 { 93 return 0; 94 } 95 } 96 97 102 public int getAccessCounter() 103 { 104 try 105 { 106 return ((Integer ) getPerm(User.ACCESS_COUNTER)).intValue(); 107 } 108 catch (Exception e) 109 { 110 return 0; 111 } 112 } 113 114 120 public java.util.Date getCreateDate() 121 { 122 return createDate; 123 } 124 125 131 public java.util.Date getLastAccessDate() 132 { 133 if (lastAccessDate == null) 134 { 135 setLastAccessDate(); 136 } 137 return lastAccessDate; 138 } 139 140 145 public java.util.Date getLastLogin() 146 { 147 return (java.util.Date ) getPerm(User.LAST_LOGIN); 148 } 149 150 155 public String getPassword() 156 { 157 return (String ) getPerm(User.PASSWORD); 158 } 159 160 166 public Object getPerm(String name) 167 { 168 return getPerm(name,null); 169 } 170 171 179 public Object getPerm(String name, Object def) 180 { 181 Object val; 182 try 183 { 184 val = getPermStorage().get(name); 185 if (val == null) 186 { 187 val = def; 188 } 189 } 190 catch (Exception e) 191 { 192 val = def; 193 } 194 return val; 195 } 196 197 203 public Hashtable getPermStorage() 204 { 205 if (permStorage == null) 206 { 207 permStorage = new Hashtable (10); 208 } 209 return permStorage; 210 } 211 212 219 public Object getTemp(String name) 220 { 221 return getTemp(name, null); 222 } 223 224 232 public Object getTemp(String name, Object def) 233 { 234 Object val; 235 try 236 { 237 val = getTempStorage().get(name); 238 if (val == null) 239 { 240 val = def; 241 } 242 243 } 244 catch (Exception e) 245 { 246 val = def; 247 } 248 return val; 249 } 250 251 257 public String getUserName() 258 { 259 return getName(); 260 } 261 262 267 public String getFirstName() 268 { 269 String tmp = null; 270 try 271 { 272 tmp = (String ) getPerm(User.FIRST_NAME); 273 if(tmp.length() == 0) 274 { 275 tmp = null; 276 } 277 } 278 catch (Exception e) 279 { 280 } 281 return tmp; 282 } 283 284 289 public String getLastName() 290 { 291 String tmp = null; 292 try 293 { 294 tmp = (String ) getPerm(User.LAST_NAME); 295 if (tmp.length() == 0) 296 tmp = null; 297 } 298 catch (Exception e) 299 { 300 } 301 return tmp; 302 } 303 304 309 public boolean hasLoggedIn() 310 { 311 Boolean loggedIn = getHasLoggedIn(); 312 return (loggedIn != null && loggedIn.booleanValue()); 313 } 314 315 320 public String getEmail() 321 { 322 return (String ) getPerm(User.EMAIL); 323 } 324 325 328 public void incrementAccessCounter() 329 { 330 setAccessCounter(getAccessCounter() + 1); 331 } 332 333 336 public void incrementAccessCounterForSession() 337 { 338 setAccessCounterForSession(getAccessCounterForSession() + 1); 339 } 340 341 347 public Object removeTemp(String name) 348 { 349 return getTempStorage().remove(name); 350 } 351 352 357 public void setAccessCounter(int cnt) 358 { 359 setPerm(User.ACCESS_COUNTER, new Integer (cnt)); 360 } 361 362 368 public void setAccessCounterForSession(int cnt) 369 { 370 setTemp(User.SESSION_ACCESS_COUNTER, new Integer (cnt)); 371 } 372 373 377 public void setLastAccessDate() 378 { 379 lastAccessDate = new java.util.Date (); 380 } 381 382 388 public void setCreateDate(java.util.Date date) 389 { 390 createDate = date; 391 } 392 393 398 public void setLastLogin(java.util.Date date) 399 { 400 setPerm(User.LAST_LOGIN, date); 401 } 402 403 408 public void setPassword(String password) 409 { 410 setPerm(User.PASSWORD, password); 411 } 412 413 422 public void setPerm(String name, Object value) 423 { 424 getPermStorage().put(name, (value == null) ? "" : value); 425 } 426 427 433 public void setPermStorage(Hashtable permStorage) 434 { 435 this.permStorage = permStorage; 436 } 437 438 444 public Hashtable getTempStorage() 445 { 446 if (tempStorage == null) 447 { 448 tempStorage = new Hashtable (10); 449 } 450 return tempStorage; 451 } 452 453 459 public void setTempStorage(Hashtable tempStorage) 460 { 461 this.tempStorage = tempStorage; 462 } 463 464 471 private Boolean getHasLoggedIn() 472 { 473 return (Boolean ) getTemp(User.HAS_LOGGED_IN); 474 } 475 476 482 public void setHasLoggedIn(Boolean value) 483 { 484 setTemp(User.HAS_LOGGED_IN, value); 485 } 486 487 496 public void setTemp(String name, Object value) 497 { 498 getTempStorage().put(name, (value == null) ? "" : value); 499 } 500 501 507 public void setUserName(String username) 508 { 509 setName(username); 510 } 511 512 517 public void setFirstName(String firstName) 518 { 519 setPerm(User.FIRST_NAME, firstName); 520 } 521 522 527 public void setLastName(String lastName) 528 { 529 setPerm(User.LAST_NAME, lastName); 530 } 531 532 537 public void setEmail(String address) 538 { 539 setPerm(User.EMAIL, address); 540 } 541 542 550 public boolean isConfirmed() 551 { 552 String value = getConfirmed(); 553 return (value != null && value.equals(User.CONFIRM_DATA)); 554 } 555 556 562 public void setConfirmed(String value) 563 { 564 String val = ""; 565 if (value != null) 566 { 567 val = value; 568 } 569 setPerm(User.CONFIRM_VALUE, val); 570 } 571 572 577 public String getConfirmed() 578 { 579 return (String ) getPerm(User.CONFIRM_VALUE); 580 } 581 582 587 public void updateLastLogin() 588 throws Exception 589 { 590 setPerm(User.LAST_LOGIN, new java.util.Date ()); 591 } 592 593 599 public void valueBound(HttpSessionBindingEvent hsbe) 600 { 601 } 603 604 610 public void valueUnbound(HttpSessionBindingEvent hsbe) 611 { 612 try 613 { 614 if (hasLoggedIn()) 615 { 616 TurbineSecurity.saveOnSessionUnbind(this); 617 } 618 } 619 catch (Exception e) 620 { 621 log.error("TurbineUser.valueUnbound(): " + e.getMessage(), e); 622 } 623 } 624 625 628 public void save() 629 throws Exception 630 { 631 if (TurbineSecurity.accountExists(this)) 632 { 633 TurbineSecurity.saveUser(this); 634 } 635 else 636 { 637 TurbineSecurity.addUser(this, getPassword()); 638 } 639 } 640 641 647 public void save(Connection conn) throws Exception 648 { 649 throw new Exception ("not implemented"); 650 } 651 652 658 public void save(String dbname) throws Exception 659 { 660 throw new Exception ("not implemented"); 661 } 662 663 669 public String getName() 670 { 671 return (String ) getPerm(User.USERNAME); 672 } 673 674 680 public void setName(String name) 681 { 682 setPerm(User.USERNAME, name); 683 } 684 } 685 | Popular Tags |