KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > file > CmsUser


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/file/CmsUser.java,v $
3  * Date : $Date: 2006/05/18 11:15:40 $
4  * Version: $Revision: 1.34 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.file;
33
34 import org.opencms.db.CmsDbUtil;
35 import org.opencms.db.CmsUserSettings;
36 import org.opencms.main.CmsIllegalArgumentException;
37 import org.opencms.main.OpenCms;
38 import org.opencms.security.CmsPrincipal;
39 import org.opencms.security.CmsSecurityException;
40 import org.opencms.security.I_CmsPrincipal;
41 import org.opencms.util.CmsStringUtil;
42 import org.opencms.util.CmsUUID;
43
44 import java.util.HashMap JavaDoc;
45 import java.util.Map JavaDoc;
46
47 /**
48  * A user principal in the OpenCms permission system.<p>
49  *
50  * A user in OpenCms is uniquely definded by its user named returned by
51  * <code>{@link #getName()}</code>.<p>
52  *
53  * Basic users in OpenCms are of type <code>{@link #USER_TYPE_SYSTEMUSER}</code>.
54  * This means that the user can access the OpenCms Workplace.
55  * Moreover, the user must be created by another user with the
56  * <code>{@link org.opencms.security.CmsRole#ACCOUNT_MANAGER}</code>.
57  * This user type is for "content managers" that actually have write permissions in
58  * at last some parts of the VFS.<p>
59  *
60  * Another possible type of users is <code>{@link #USER_TYPE_WEBUSER}</code>.
61  * These users do not have access to the OpenCms Workplace.
62  * However, a web user can be created by every user, for example the "Guest" user.
63  * The main use case is that web users are used for users of the website that
64  * can generate their own accounts, in a "please register your account..." scenario.
65  * These web user accounts can then be used to
66  * build personalized web sites. A web user is created using
67  * <code>{@link org.opencms.file.CmsObject#addWebUser(String, String, String, String, Map)}</code>.<p>
68  *
69  * @author Alexander Kandzior
70  * @author Michael Emmerich
71  *
72  * @version $Revision: 1.34 $
73  *
74  * @since 6.0.0
75  *
76  * @see CmsGroup
77  */

78 public class CmsUser extends CmsPrincipal implements I_CmsPrincipal, Cloneable JavaDoc {
79
80     /** Identifies the system user type. */
81     public static final int USER_TYPE_SYSTEMUSER = 0;
82
83     /** Identifies the web user type. */
84     public static final int USER_TYPE_WEBUSER = 2;
85
86     /** Storage for additional user information. */
87     private Map JavaDoc m_additionalInfo;
88
89     /** The address of this user. */
90     private String JavaDoc m_address;
91
92     /** The email of the user. */
93     private String JavaDoc m_email;
94
95     /** The first name of this user. */
96     private String JavaDoc m_firstname;
97
98     /** Boolean flag whether the last-login timestamp of this user was modified. */
99     private boolean m_isTouched;
100
101     /** The last login date of this user. */
102     private long m_lastlogin;
103
104     /** The last name of this user. */
105     private String JavaDoc m_lastname;
106
107     /** The password of this user. */
108     private String JavaDoc m_password;
109
110     /**
111      * Defines if the user is of type "syetem user" or a "web user".<p>
112      *
113      * Use {@link #USER_TYPE_SYSTEMUSER} for system users, or
114      * {@link #USER_TYPE_WEBUSER} for web usera.
115      */

116     private int m_type;
117
118     /**
119      * Creates a new, empty OpenCms user principal.<p>
120      *
121      * Mostly intented to be used with the {@link org.opencms.workplace.tools.accounts.A_CmsEditUserDialog}.<p>
122      */

123     public CmsUser() {
124
125         this(null, "", "");
126         setAdditionalInfo(new HashMap JavaDoc());
127     }
128
129     /**
130      * Creates a new OpenCms user principal.<p>
131      *
132      * @param id the unique id of the new user
133      * @param name the unique name of the new user
134      * @param description the description of the new user
135      */

136     public CmsUser(CmsUUID id, String JavaDoc name, String JavaDoc description) {
137
138         this(
139             id,
140             name,
141             "",
142             description,
143             "",
144             "",
145             "",
146             CmsDbUtil.UNKNOWN_ID,
147             I_CmsPrincipal.FLAG_ENABLED,
148             null,
149             "",
150             CmsDbUtil.UNKNOWN_ID);
151     }
152
153     /**
154      * Creates a new OpenCms user principal.<p>
155      *
156      * @param id the unique id of the new user
157      * @param name the unique name of the new user
158      * @param password the password of the user
159      * @param description the description of the new user
160      * @param firstname the first name
161      * @param lastname the last name
162      * @param email the email address
163      * @param lastlogin time stamp
164      * @param flags flags
165      * @param additionalInfo user related information
166      * @param address the address
167      * @param type the type of this user
168      */

169     public CmsUser(
170         CmsUUID id,
171         String JavaDoc name,
172         String JavaDoc password,
173         String JavaDoc description,
174         String JavaDoc firstname,
175         String JavaDoc lastname,
176         String JavaDoc email,
177         long lastlogin,
178         int flags,
179         Map JavaDoc additionalInfo,
180         String JavaDoc address,
181         int type) {
182
183         m_id = id;
184         m_name = name;
185         m_password = password;
186         m_description = description;
187         m_firstname = firstname;
188         m_lastname = lastname;
189         m_email = email;
190         m_lastlogin = lastlogin;
191         m_flags = flags;
192         m_additionalInfo = additionalInfo;
193         m_address = address;
194         m_type = type;
195     }
196
197     /**
198      * Validates an email address.<p>
199      *
200      * That means, the parameter should only be composed by digits and standard english letters, points, underscores and exact one "At" symbol.<p>
201      *
202      * @param email the email to validate
203      */

204     public static void checkEmail(String JavaDoc email) {
205
206         OpenCms.getValidationHandler().checkEmail(email);
207     }
208
209     /**
210      * Validates a zip code.<p>
211      *
212      * That means, the parameter should only be composed by digits and standard english letters.<p>
213      *
214      * @param zipcode the zipcode to validate
215      */

216     public static void checkZipCode(String JavaDoc zipcode) {
217
218         OpenCms.getValidationHandler().checkZipCode(zipcode);
219     }
220
221     /**
222      * Returns the "full" name of the given user in the format <code>"{firstname} {lastname} ({username})"</code>,
223      * or the empty String <code>""</code> if the user is null.<p>
224      *
225      * @param user the user to get the full name from
226      * @return the "full" name the user
227      *
228      * @see #getFullName()
229      */

230     public static String JavaDoc getFullName(CmsUser user) {
231
232         if (user == null) {
233             return "";
234         } else {
235             return user.getFullName();
236         }
237     }
238
239     /**
240      * Returns <code>true</code> if the provided user type indicates a system user.<p>
241      *
242      * @param type the user type to check
243      *
244      * @return true if the provided user type indicates a system user
245      */

246     public static boolean isSystemUser(int type) {
247
248         return (type & 1) > 0;
249     }
250
251     /**
252      * Returns <code>true</code> if the provided user type indicates a web user.<p>
253      *
254      * @param type the user type to check
255      *
256      * @return true if the provided user type indicates a web user
257      */

258     public static boolean isWebUser(int type) {
259
260         return (type & 2) > 0;
261     }
262
263     /**
264      * Checks if the provided user name is a valid user name and can be used as an argument value
265      * for {@link #setName(String)}.<p>
266      *
267      * @param name the user name to check
268      *
269      * @throws CmsIllegalArgumentException if the check fails
270      */

271     public void checkName(String JavaDoc name) throws CmsIllegalArgumentException {
272
273         OpenCms.getValidationHandler().checkUserName(name);
274     }
275
276     /**
277      * @see java.lang.Object#clone()
278      */

279     public Object JavaDoc clone() {
280
281         return new CmsUser(
282             m_id,
283             m_name,
284             m_password,
285             m_description,
286             m_firstname,
287             m_lastname,
288             m_email,
289             m_lastlogin,
290             m_flags,
291             m_additionalInfo != null ? new HashMap JavaDoc(m_additionalInfo) : null,
292             m_address,
293             m_type);
294     }
295
296     /**
297      * Deletes a value from this users "additional information" storage map.<p>
298      *
299      * @param key the additional user information to delete
300      *
301      * @see #getAdditionalInfo()
302      */

303     public void deleteAdditionalInfo(String JavaDoc key) {
304
305         m_additionalInfo.remove(key);
306     }
307
308     /**
309      * Returns this users complete "additional information" storage map.<p>
310      *
311      * The "additional information" storage map is a simple {@link java.util#Map}
312      * that can be used to store any key / value pairs for the user.
313      * Some information parts of the users address are stored in this map
314      * by default. The map is serialized when the user is stored in the database.<p>
315      *
316      * @return this users complete "additional information" storage map
317      */

318     public Map JavaDoc getAdditionalInfo() {
319
320         return m_additionalInfo;
321     }
322
323     /**
324      * Returns a value from this users "additional information" storage map,
325      * or <code>null</code> if no value for the given key is available.<p>
326      *
327      * @param key selects the value to return from the "additional information" storage map
328      *
329      * @return the selected value from this users "additional information" storage map
330      *
331      * @see #getAdditionalInfo()
332      */

333     public Object JavaDoc getAdditionalInfo(String JavaDoc key) {
334
335         return m_additionalInfo.get(key);
336     }
337
338     /**
339      * Returns the address line of this user.<p>
340      *
341      * @return the address line of this user
342      */

343     public String JavaDoc getAddress() {
344
345         return m_address;
346     }
347
348     /**
349      * Returns the city information of this user.<p>
350      *
351      * This informaion is stored in the "additional information" storage map
352      * using the key <code>{@link CmsUserSettings#ADDITIONAL_INFO_CITY}</code>.<p>
353      *
354      * @return the city information of this user
355      */

356     public String JavaDoc getCity() {
357
358         return (String JavaDoc)getAdditionalInfo(CmsUserSettings.ADDITIONAL_INFO_CITY);
359     }
360
361     /**
362      * Returns the country information of this user.<p>
363      *
364      * This informaion is stored in the "additional information" storage map
365      * using the key <code>{@link CmsUserSettings#ADDITIONAL_INFO_COUNTRY}</code>.<p>
366      *
367      * @return the country information of this user
368      */

369     public String JavaDoc getCountry() {
370
371         return (String JavaDoc)getAdditionalInfo(CmsUserSettings.ADDITIONAL_INFO_COUNTRY);
372     }
373
374     /**
375      * Returns <code>true</code> if this user is disabled.<p>
376      *
377      * @return <code>true</code> if this user is disabled
378      *
379      * @deprecated use {@link CmsPrincipal#isEnabled()} instead
380      */

381     public boolean getDisabled() {
382
383         return !isEnabled();
384     }
385
386     /**
387      * Returns the email address of this user.<p>
388      *
389      * @return the email address of this user
390      */

391     public String JavaDoc getEmail() {
392
393         return m_email;
394     }
395
396     /**
397      * Returns the firstname of this user.<p>
398      *
399      * @return the firstname of this user
400      */

401     public String JavaDoc getFirstname() {
402
403         return m_firstname;
404     }
405
406     /**
407      * Returns the "full" name of the this user in the format <code>"{firstname} {lastname} ({username})"</code>.<p>
408      *
409      * @return the "full" name this user
410      */

411     public String JavaDoc getFullName() {
412
413         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
414         String JavaDoc first = getFirstname();
415         if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(first)) {
416             buf.append(first);
417             buf.append(" ");
418         }
419         String JavaDoc last = getLastname();
420         if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(last)) {
421             buf.append(last);
422             buf.append(" ");
423         }
424         buf.append("(");
425         buf.append(getName());
426         buf.append(")");
427         return buf.toString();
428     }
429
430     /**
431      * Returns the time of the last login of this user.<p>
432      *
433      * @return the time of the last login of this user
434      */

435     public long getLastlogin() {
436
437         return m_lastlogin;
438     }
439
440     /**
441      * Returns the lastname of this user.<p>
442      *
443      * @return the lastname of this user
444      */

445     public String JavaDoc getLastname() {
446
447         return m_lastname;
448     }
449
450     /**
451      * Returns the encrypted user password.<p>
452      *
453      * @return the encrypted user password
454      */

455     public String JavaDoc getPassword() {
456
457         return m_password;
458     }
459
460     /**
461      * Returns the type of this user.<p>
462      *
463      * Possible options are
464      * <code>{@link #USER_TYPE_SYSTEMUSER}</code> for a "system user",
465      * or <code>{@link #USER_TYPE_WEBUSER}</code> for a "web user".<p>
466      *
467      * @return the type of this user
468      */

469     public int getType() {
470
471         return m_type;
472     }
473
474     /**
475      * Returns the zip code information of this user.<p>
476      *
477      * This informaion is stored in the "additional information" storage map
478      * using the key <code>{@link CmsUserSettings#ADDITIONAL_INFO_ZIPCODE}</code>.<p>
479      *
480      * @return the zip code information of this user
481      */

482     public String JavaDoc getZipcode() {
483
484         return (String JavaDoc)getAdditionalInfo(CmsUserSettings.ADDITIONAL_INFO_ZIPCODE);
485     }
486
487     /**
488      * @see org.opencms.security.I_CmsPrincipal#isGroup()
489      */

490     public boolean isGroup() {
491
492         return false;
493     }
494
495     /**
496      * Returns <code>true</code> if this user is the default guest user.<p>
497      *
498      * @return true if this user is the default guest user
499      */

500     public boolean isGuestUser() {
501
502         return OpenCms.getDefaultUsers().getUserGuest().equals(getName());
503     }
504
505     /**
506      * Returns <code>true</code> if this user is a "system user".<p>
507      *
508      * @return true if this user is a "system user"
509      */

510     public boolean isSystemUser() {
511
512         return isSystemUser(m_type);
513     }
514
515     /**
516      * Returns <code>true</code> if this user was touched.<p>
517      *
518      * @return boolean true if this user was touched
519      */

520     public boolean isTouched() {
521
522         return m_isTouched;
523     }
524
525     /**
526      * @see org.opencms.security.I_CmsPrincipal#isUser()
527      */

528     public boolean isUser() {
529
530         return true;
531     }
532
533     /**
534      * Returns <code>true</code> if this user is a "web user".<p>
535      *
536      * @return true if this user is a "web user"
537      */

538     public boolean isWebUser() {
539
540         return isWebUser(m_type);
541     }
542
543     /**
544      * Sets this users complete "additional information" storage map to the given value.<p>
545      *
546      * @param additionalInfo the complete "additional information" map to set
547      *
548      * @see #getAdditionalInfo()
549      */

550     public void setAdditionalInfo(Map JavaDoc additionalInfo) {
551
552         m_additionalInfo = additionalInfo;
553     }
554
555     /**
556      * Stores a value in this users "additional information" storage map with the gicen access key.<p>
557      *
558      * @param key the key to store the value under
559      * @param value the value to store in the users "additional information" storage map
560      *
561      * @see #getAdditionalInfo()
562      */

563     public void setAdditionalInfo(String JavaDoc key, Object JavaDoc value) {
564
565         m_additionalInfo.put(key, value);
566     }
567
568     /**
569      * Sets the address line of this user.<p>
570      *
571      * @param address the address line to set
572      */

573     public void setAddress(String JavaDoc address) {
574
575         m_address = address;
576     }
577
578     /**
579      * Sets the city information of this user.<p>
580      *
581      * @param city the city information to set
582      */

583     public void setCity(String JavaDoc city) {
584
585         setAdditionalInfo(CmsUserSettings.ADDITIONAL_INFO_CITY, city);
586     }
587
588     /**
589      * Sets the country information of this user.<p>
590      *
591      * @param country the city information to set
592      */

593     public void setCountry(String JavaDoc country) {
594
595         setAdditionalInfo(CmsUserSettings.ADDITIONAL_INFO_COUNTRY, country);
596     }
597
598     /**
599      * Disables this user.<p>
600      *
601      * @deprecated use {@link CmsPrincipal#setEnabled(boolean)} instead
602      */

603     public void setDisabled() {
604
605         setEnabled(false);
606     }
607
608     /**
609      * Sets the email address of this user.<p>
610      *
611      * @param email the email address to set
612      */

613     public void setEmail(String JavaDoc email) {
614
615         checkEmail(email);
616         m_email = email;
617     }
618
619     /**
620      * Enables this user.<p>
621      *
622      * @deprecated use {@link CmsPrincipal#setEnabled(boolean)} instead
623      */

624     public void setEnabled() {
625
626         setEnabled(true);
627     }
628
629     /**
630      * Sets the first name of this user.<p>
631      *
632      * @param firstname the name to set
633      */

634     public void setFirstname(String JavaDoc firstname) {
635
636         if (CmsStringUtil.isEmptyOrWhitespaceOnly(firstname)) {
637             throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_FIRSTNAME_EMPTY_0));
638         }
639         m_firstname = firstname;
640     }
641
642     /**
643      * Sets the last login timestamp of this user.<p>
644      *
645      * @param value the last login timestamp to set
646      */

647     public void setLastlogin(long value) {
648
649         m_isTouched = true;
650         m_lastlogin = value;
651     }
652
653     /**
654      * Sets the last name of this user.<p>
655      *
656      * @param lastname the name to set
657      */

658     public void setLastname(String JavaDoc lastname) {
659
660         if (CmsStringUtil.isEmptyOrWhitespaceOnly(lastname)) {
661             throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_LASTNAME_EMPTY_0));
662         }
663         m_lastname = lastname;
664     }
665
666     /**
667      * Sets the password of this user.<p>
668      *
669      * @param value the password to set
670      */

671     public void setPassword(String JavaDoc value) {
672
673         try {
674             OpenCms.getPasswordHandler().validatePassword(value);
675         } catch (CmsSecurityException e) {
676             throw new CmsIllegalArgumentException(e.getMessageContainer());
677         }
678         m_password = value;
679     }
680
681     /**
682      * Sets the zip code information of this user.<p>
683      *
684      * @param zipcode the zip code information to set
685      */

686     public void setZipcode(String JavaDoc zipcode) {
687
688         checkZipCode(zipcode);
689         zipcode = zipcode.toUpperCase();
690         setAdditionalInfo(CmsUserSettings.ADDITIONAL_INFO_ZIPCODE, zipcode);
691     }
692
693     /**
694      * @see java.lang.Object#toString()
695      */

696     public String JavaDoc toString() {
697
698         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
699         result.append("[User]");
700         result.append(" name:");
701         result.append(m_name);
702         result.append(" id:");
703         result.append(m_id);
704         result.append(" flags:");
705         result.append(getFlags());
706         result.append(" type:");
707         result.append(getType());
708         result.append(" description:");
709         result.append(m_description);
710         return result.toString();
711     }
712
713     /**
714      * Sets the "touched" status of this user to <code>true</code>.<p>
715      */

716     public void touch() {
717
718         m_isTouched = true;
719     }
720
721     /**
722      * Sets the type of this user.<p>
723      *
724      * Possible options are
725      * <code>{@link #USER_TYPE_SYSTEMUSER}</code> for a "system user",
726      * or <code>{@link #USER_TYPE_WEBUSER}</code> for a "web user".<p>
727      *
728      * @param value the type to set
729      */

730     protected void setType(int value) {
731
732         m_type = value;
733     }
734 }
Popular Tags