1 14 15 package org.jahia.security.accounts; 16 17 import java.util.Date ; 18 19 import org.jahia.exceptions.database.JahiaDatabaseException; 20 21 45 public class Account { 46 47 48 private String mUserKey = null; 49 50 51 private String mSiteKey = null; 52 53 54 private Date mCreationDate; 55 56 57 private Date mExpirationDate; 58 59 60 private Date mPasswordExpirationDate; 61 62 63 private Date mLastLoginDate; 64 65 67 private boolean mActivated; 68 69 70 71 private static AccountDBUtils mDBUtils = null; 72 73 74 92 protected Account (String userKey, String siteKey, Date creationDate, 93 Date expirationDate, Date passwordExpirationDate, 94 Date lastLoginDate, boolean activated) 95 { 96 initialize (userKey, siteKey, creationDate, expirationDate, 97 passwordExpirationDate, lastLoginDate, activated); 98 } 99 100 101 107 public final String getUserKey () { 108 return new String (mUserKey); 109 } 110 111 117 public final String getSiteKey () { 118 return new String (mSiteKey); 119 } 120 121 122 128 public final Date getCreationDate () { 129 return (Date )mCreationDate.clone(); 130 } 131 132 133 139 public final Date getExpirationDate () { 140 return mExpirationDate; 141 } 142 143 144 150 public final Date getPasswordExpirationDate () { 151 return mPasswordExpirationDate; 152 } 153 154 155 161 public final Date getLastLoginDate () { 162 return mLastLoginDate; 163 } 164 165 166 187 private void initialize (String userKey, String siteKey, Date creationDate, 188 Date expirationDate, Date passwordExpirationDate, 189 Date lastLoginDate, boolean activated) 190 { 191 mUserKey = userKey; 192 mSiteKey = siteKey; 193 mCreationDate = creationDate; 194 mExpirationDate = expirationDate; 195 mPasswordExpirationDate = passwordExpirationDate; 196 mLastLoginDate = lastLoginDate; 197 mActivated = activated; 198 199 mDBUtils = AccountDBUtils.getInstance(); 200 } 201 202 203 210 public final boolean isActivated () { 211 return mActivated; 212 } 213 214 215 222 public boolean isExpired () { 223 224 if (mExpirationDate != null) { 225 Date currentDate = new Date (); 227 228 return (currentDate.after (mExpirationDate)); 229 } 230 231 return false; 233 } 234 235 236 243 public final void setExpirationDate (Date value) { 244 if (value != null) { 245 mExpirationDate = value; 246 } 247 } 248 249 250 257 public final void setPasswordExpirationDate (Date value) { 258 if (value != null) { 259 mExpirationDate = value; 260 } 261 } 262 263 264 271 public final void setLastLoginDate (Date value) { 272 if (value != null) { 273 mLastLoginDate = value; 274 } 275 } 276 277 278 285 public final void setActivation (boolean value) { 286 mActivated = value; 287 } 288 289 296 public String toString() 297 { 298 StringBuffer result = new StringBuffer (); 299 result.append ("Account internal data :"); 300 result.append (" - user key = ["); 301 result.append (mUserKey); 302 result.append ("]"); 303 304 result.append (" - site key = ["); 305 result.append (mSiteKey); 306 result.append ("]"); 307 308 309 result.append (" - Creation date = "); 310 result.append (dateString (mCreationDate)); 311 result.append ("\n"); 312 313 result.append (" - Expiration date = "); 314 result.append (dateString (mExpirationDate)); 315 result.append ("\n"); 316 317 result.append (" - Password Exp. date = "); 318 result.append (dateString (mPasswordExpirationDate)); 319 result.append ("\n"); 320 321 result.append (" - Last login date = "); 322 result.append (dateString (mLastLoginDate)); 323 result.append ("\n"); 324 325 result.append (" - activation = "); 326 result.append (mActivated ? "ACTIVATED" : "DISABLED"); 327 result.append ("\n"); 328 return result.toString(); 329 } 330 331 338 private String dateString (Date date) 339 { 340 String result = "-null-"; 341 if (date != null) { 342 result = (date.toString() + " ("+Long.toString(date.getTime())+")"); 343 } 344 return result; 345 } 346 347 348 374 static public synchronized Account createAccount 375 (String userKey, String siteKey, Date expDate, Date pwdExpDate, 376 boolean activated) 377 throws JahiaDatabaseException, 378 AccountExistException 379 { 380 Account account = mDBUtils.loadAccount (userKey, siteKey); 381 if (account != null) { 382 throw new AccountExistException (userKey, siteKey); 383 } 384 385 Date creationDate = new Date (); 387 388 account = new Account (userKey, siteKey, creationDate, expDate, 390 pwdExpDate, null, activated); 391 392 if (account != null) { 394 if (!mDBUtils.insertAccount (account)) { 395 account = null; 396 } 397 } 398 399 return account; 400 } 401 402 403 424 static public Account getAccount (String userKey, String siteKey) 425 throws AccountNotFoundException, 426 JahiaDatabaseException 427 { 428 Account account = mDBUtils.loadAccount (userKey, siteKey); 429 if (account == null) { 430 throw new AccountNotFoundException (userKey, siteKey); 431 } 432 return account; 433 } 434 435 436 451 static public synchronized boolean deleteAccount (Account account) 452 throws JahiaDatabaseException 453 { 454 if (account != null) { 455 return mDBUtils.deleteAccount (account); 456 } 457 return false; 458 } 459 460 461 478 public synchronized boolean updatdeAccount () 479 throws JahiaDatabaseException 480 { 481 return mDBUtils.updateAccount (this); 482 } 483 } 484 | Popular Tags |