KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > security > accounts > Account


1 // $Id: Account.java 3567 2003-06-30 22:32:03Z shuber $
2
//
3
// ____.
4
// __/\ ______| |__/\. _______
5
// __ .____| | \ | +----+ \
6
// _______| /--| | | - \ _ | : - \_________
7
// \\______: :---| : : | : | \________>
8
// |__\---\_____________:______: :____|____:_____\
9
// /_____|
10
//
11
// . . . i n j a h i a w e t r u s t . . .
12
//
13

14
15 package org.jahia.security.accounts;
16
17 import java.util.Date JavaDoc;
18
19 import org.jahia.exceptions.database.JahiaDatabaseException;
20
21 /**
22  * This class represent the user account for a specified jahia site. An account
23  * is bounded at creation time to one specific user and cannot be change
24  * afterwards, the same rule is applied for the site assignation. <b>An account
25  * cannot be shared between two sites</b>.</br></br>
26  *
27  * An account is made of an unique identification number, which is unique among
28  * a site. A creation date is associated to the account as well as an
29  * expiratin date and a password expiration date. The account expiration date
30  * is used to check if the account is still valid when a user tries to access
31  * the server, and the password expiration date is used to force the user to
32  * change his password after a laps of time. Another date is also stored inside
33  * the account, it's the last date the user logged in the server</br></br>
34  *
35  * An Account can be created but not yet available to the related user, for
36  * example, the account activation is waiting a billing confirmation. Therefore
37  * an account can be disactivated for a while and enabled later.</br></br>
38  *
39  * No security and priviledge information is stored inside the account, this is
40  * handled through access control lists (ACL).
41  *
42  * @author Fulco Houkes
43  * @version 1.0
44  */

45 public class Account {
46
47     /** User unique key */
48     private String JavaDoc mUserKey = null;
49
50     /** Site unique key */
51     private String JavaDoc mSiteKey = null;
52
53     /** Creation date */
54     private Date JavaDoc mCreationDate;
55
56     /** Expiration date */
57     private Date JavaDoc mExpirationDate;
58
59     /** Password expiration date */
60     private Date JavaDoc mPasswordExpirationDate;
61
62     /** Last login date */
63     private Date JavaDoc mLastLoginDate;
64
65     /** Account activation flag. True if the accound is active,
66      * otherwise false. */

67     private boolean mActivated;
68
69
70     /** reference to the DB tool methods */
71     private static AccountDBUtils mDBUtils = null;
72
73
74     //-------------------------------------------------------------------------
75
/** Default constructor
76      *
77      * @param userKey
78      * User unique identification key.
79      * @param siteKey
80      * Site unique identification key.
81      * @param creationDate
82      * Account creation date.
83      * @param expirationDate
84      * Account expiration date. Specify null in case the account never
85      * expire.
86      * @param lastLoginDate
87      * Last Date the user used his account.
88      * @param activated
89      * True if the user is active an can be used by the user. False if the
90      * account is not activated and not available to the user.
91      */

92     protected Account (String JavaDoc userKey, String JavaDoc siteKey, Date JavaDoc creationDate,
93             Date JavaDoc expirationDate, Date JavaDoc passwordExpirationDate,
94             Date JavaDoc lastLoginDate, boolean activated)
95     {
96         initialize (userKey, siteKey, creationDate, expirationDate,
97                     passwordExpirationDate, lastLoginDate, activated);
98     }
99
100
101     //-------------------------------------------------------------------------
102
/** Return the account unique identification number.
103      *
104      * @return
105      * Return the account unique identification number.
106      */

107     public final String JavaDoc getUserKey () {
108         return new String JavaDoc (mUserKey);
109     }
110
111     //-------------------------------------------------------------------------
112
/**
113      * Return the unique identification number.
114      * @return
115      * Return the site unique identification number.
116      */

117     public final String JavaDoc getSiteKey () {
118         return new String JavaDoc (mSiteKey);
119     }
120
121
122     //-------------------------------------------------------------------------
123
/** Return the creation date.
124      *
125      * @return
126      * Retrun the creation date.
127      */

128     public final Date JavaDoc getCreationDate () {
129         return (Date JavaDoc)mCreationDate.clone();
130     }
131
132
133     //-------------------------------------------------------------------------
134
/** Return the expiration date.
135      *
136      * @return
137      * Return the expiration date.
138      */

139     public final Date JavaDoc getExpirationDate () {
140         return mExpirationDate;
141     }
142
143
144     //-------------------------------------------------------------------------
145
/** Return the password expiration date.
146      *
147      * @return
148      * Return the expiration date.
149      */

150     public final Date JavaDoc getPasswordExpirationDate () {
151         return mPasswordExpirationDate;
152     }
153
154
155     //-------------------------------------------------------------------------
156
/** Return the last login date.
157      *
158      * @return
159      * Return the last login date.
160      */

161     public final Date JavaDoc getLastLoginDate () {
162         return mLastLoginDate;
163     }
164
165
166     //-------------------------------------------------------------------------
167
/** This method is called by the constructors in order to initialize a new
168      * object instanciation of this class.
169      *
170      * @param userKey
171      * User unique identification key.
172      * @param siteKey
173      * Site unique identification key.
174      * @param creationDate
175      * Account creation date.
176      * @param expirationDate
177      * Account expiration date. Specify null in case the account never
178      * expire.
179      * @param lastLoginDate
180      * Last Date the user used his account.
181      * @param activated
182      * True if the user is active an can be used by the user. False if the
183      * account is not activated and not available to the user.
184      * @param siteID
185      * Site unique identification number to which the account belongs.
186      */

187     private void initialize (String JavaDoc userKey, String JavaDoc siteKey, Date JavaDoc creationDate,
188             Date JavaDoc expirationDate, Date JavaDoc passwordExpirationDate,
189             Date JavaDoc 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     //-------------------------------------------------------------------------
204
/** Return the account's activation state.
205      *
206      * @return
207      * Retrun <code>true</code> if the account is activated (available),
208      * otherwise return <code>false</code>.
209      */

210     public final boolean isActivated () {
211         return mActivated;
212     }
213
214
215     //-------------------------------------------------------------------------
216
/** Test for account expiration.
217      *
218      * @return
219      * Return <code>true</code> if the account has expired, otherwise
220      * return <code>false</code>.
221      */

222     public boolean isExpired () {
223
224         if (mExpirationDate != null) {
225             // get the current date
226
Date JavaDoc currentDate = new Date JavaDoc ();
227
228             return (currentDate.after (mExpirationDate));
229         }
230
231         // if not expiration date is given, the account does not expire.
232
return false;
233     }
234
235
236     //-------------------------------------------------------------------------
237
/** Reset the expiration to a new date.
238      *
239      * @param value
240      * New expiration date. A <code>null</code> value will not reset the
241      * expiration date.
242      */

243     public final void setExpirationDate (Date JavaDoc value) {
244         if (value != null) {
245             mExpirationDate = value;
246         }
247     }
248
249
250     //-------------------------------------------------------------------------
251
/** Reset the password expiration to a new date.
252      *
253      * @param value
254      * New password expiration date. A <code>null</code> value will not
255      * reset the expiration date.
256      */

257     public final void setPasswordExpirationDate (Date JavaDoc value) {
258         if (value != null) {
259             mExpirationDate = value;
260         }
261     }
262
263
264     //-------------------------------------------------------------------------
265
/** Reset the last login date.
266      *
267      * @param value
268      * Last login date. A <code>null</code> value will not reset the login
269      * date.
270      */

271     public final void setLastLoginDate (Date JavaDoc value) {
272         if (value != null) {
273             mLastLoginDate = value;
274         }
275     }
276
277
278     //-------------------------------------------------------------------------
279
/** Enable or disable the account.
280      *
281      * @param activation
282      * <code>true</code> will activate the account and <code>false</code>
283      * will disactivate the account.
284      */

285     public final void setActivation (boolean value) {
286         mActivated = value;
287     }
288
289     //-------------------------------------------------------------------------
290
/**
291      * Return a string representation of the account's content.
292      *
293      * @return
294      * Account's string representation.
295      */

296     public String JavaDoc toString()
297     {
298         StringBuffer JavaDoc result = new StringBuffer JavaDoc ();
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     //-------------------------------------------------------------------------
332
/**
333      * Return the string representaiton of the specified date.
334      *
335      * @return
336      * String representing the date.
337      */

338     private String JavaDoc dateString (Date JavaDoc date)
339     {
340         String JavaDoc result = "-null-";
341         if (date != null) {
342             result = (date.toString() + " ("+Long.toString(date.getTime())+")");
343         }
344         return result;
345     }
346
347
348     //-------------------------------------------------------------------------
349
// Foux 17 Apr. 2001
350
/**
351      * Create a new account.
352      *
353      * @param userKey
354      * The user unique identification key.
355      * @param siteKey
356      * The site unique identification key.
357      * @param expDate
358      * User account expiration date. Specify <code>null</code> if the
359      * account does not expire.
360      * @param pwdExpDate
361      * User password expiration date. Specify <code>null</code> if the user
362      * has not password expiration applied on his account.
363      * @param activated
364      * Account activation. <code>true</code> if the account is activated or
365      * <code>false</code> if it's not.
366      *
367      * @return
368      * Return a valid Account reference if the account could be
369      * successfully created.
370      *
371      * @exception JahiaDatabaseException
372      * Throws this exception on any database failure.
373      */

374     static public synchronized Account createAccount
375             (String JavaDoc userKey, String JavaDoc siteKey, Date JavaDoc expDate, Date JavaDoc 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         // Get the current date.
386
Date JavaDoc creationDate = new Date JavaDoc ();
387
388         // Create the new account object
389
account = new Account (userKey, siteKey, creationDate, expDate,
390                                pwdExpDate, null, activated);
391
392         // insert the new account into the database.
393
if (account != null) {
394             if (!mDBUtils.insertAccount (account)) {
395                 account = null;
396             }
397         }
398
399         return account;
400     }
401
402
403     //-------------------------------------------------------------------------
404
// Foux 17 Apr. 2001
405
/**
406      * Lookup the account for the specified user related to the specified site
407      * identification number.
408      *
409      * @param userKey
410      * The user unique identification key.
411      * @param siteKey
412      * The site unique identification key.
413      *
414      * @return
415      * Return a reference to the account object.
416      *
417      * @exception AccountNotFoundException
418      * Throws this exception if the specified has no account for the
419      * specified site.
420      *
421      * @exception JahiaDatabaseException
422      * Throws this exception on any database failure.
423      */

424     static public Account getAccount (String JavaDoc userKey, String JavaDoc 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     //-------------------------------------------------------------------------
437
// Foux 17 Apr. 2001
438
/**
439      * Delete the specified account.
440      *
441      * @param account
442      * Reference to the account to be updated.
443      *
444      * @return
445      * Return <code>true</code> if the account could be successfully
446      * deleted, or false if the specified account was <code>null</code>.
447      *
448      * @exception JahiaDatabaseException
449      * Throws this exception on any database failure.
450      */

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     //-------------------------------------------------------------------------
462
// Foux 17 Apr. 2001
463
/**
464      * Update to the database the account's internal data. Only the expiration
465      * date, the last login date and the activation data will be updated, all
466      * the other data have no sens to be updated because they cannot change.
467      *
468      * @param account
469      * Reference to the account to be updated.
470      *
471      * @return
472      * Return <code>true</code> if the account could be successfully
473      * updated, or false if the specified account was <code>null</code>.
474      *
475      * @exception JahiaDatabaseException
476      * Throws this exception on any database failure.
477      */

478     public synchronized boolean updatdeAccount ()
479         throws JahiaDatabaseException
480     {
481         return mDBUtils.updateAccount (this);
482     }
483 }
484
Popular Tags