001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/file/CmsUser.java,v $
003: * Date : $Date: 2008-02-27 12:05:38 $
004: * Version: $Revision: 1.40 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.file;
033:
034: import org.opencms.db.CmsUserSettings;
035: import org.opencms.main.CmsException;
036: import org.opencms.main.CmsIllegalArgumentException;
037: import org.opencms.main.OpenCms;
038: import org.opencms.security.CmsPrincipal;
039: import org.opencms.security.CmsSecurityException;
040: import org.opencms.security.I_CmsPrincipal;
041: import org.opencms.util.CmsMacroResolver;
042: import org.opencms.util.CmsStringUtil;
043: import org.opencms.util.CmsUUID;
044:
045: import java.util.Collections;
046: import java.util.HashMap;
047: import java.util.Locale;
048: import java.util.Map;
049:
050: /**
051: * A user principal in the OpenCms permission system.<p>
052: *
053: * A user in OpenCms is uniquely defined by its user named returned by
054: * <code>{@link #getName()}</code>.<p>
055: *
056: * Basic users in OpenCms are users that can access the OpenCms Workplace.
057: * Moreover, the user must be created by another user with the
058: * <code>{@link org.opencms.security.CmsRole#ACCOUNT_MANAGER}</code> role.
059: * These users are "content managers" that actually have write permissions in
060: * at last some parts of the VFS.<p>
061: *
062: * Another possibility is to have users in a 'Guests' group.
063: * These users do not have access to the OpenCms Workplace.
064: * However, an user in a 'Guests' group can be created by every user, for example
065: * the "Guest" user. The main use case is that these users are used for users of
066: * the website that can generate their own accounts, in a "please register your
067: * account..." scenario.
068: * These user accounts can then be used to build personalized web sites.<p>
069: *
070: * @author Alexander Kandzior
071: * @author Michael Emmerich
072: * @author Michael Moossen
073: *
074: * @version $Revision: 1.40 $
075: *
076: * @since 6.0.0
077: *
078: * @see CmsGroup
079: */
080: public class CmsUser extends CmsPrincipal implements I_CmsPrincipal,
081: Cloneable {
082:
083: /** Storage for additional user information. */
084: private Map m_additionalInfo;
085:
086: /** The creation date. */
087: private long m_dateCreated;
088:
089: /** The email of the user. */
090: private String m_email;
091:
092: /** The first name of this user. */
093: private String m_firstname;
094:
095: /** Boolean flag whether the last-login time stamp of this user was modified. */
096: private boolean m_isTouched;
097:
098: /** The last login date of this user. */
099: private long m_lastlogin;
100:
101: /** The last name of this user. */
102: private String m_lastname;
103:
104: /** The password of this user. */
105: private String m_password;
106:
107: /**
108: * Creates a new, empty OpenCms user principal.<p>
109: *
110: * Mostly intended to be used with the <code>org.opencms.workplace.tools.accounts.A_CmsEditUserDialog</code>.<p>
111: */
112: public CmsUser() {
113:
114: this (
115: null,
116: "",
117: "",
118: "",
119: "",
120: "",
121: 0,
122: I_CmsPrincipal.FLAG_ENABLED,
123: System.currentTimeMillis(),
124: Collections
125: .singletonMap(
126: CmsUserSettings.ADDITIONAL_INFO_DESCRIPTION,
127: ""));
128: }
129:
130: /**
131: * Creates a new OpenCms user principal.<p>
132: *
133: * @param id the unique id of the new user
134: * @param name the fully qualified name of the new user
135: * @param password the password of the user
136: * @param firstname the first name
137: * @param lastname the last name
138: * @param email the email address
139: * @param lastlogin time stamp
140: * @param flags flags
141: * @param dateCreated the creation date
142: * @param additionalInfo user related information
143: */
144: public CmsUser(CmsUUID id, String name, String password,
145: String firstname, String lastname, String email,
146: long lastlogin, int flags, long dateCreated,
147: Map additionalInfo) {
148:
149: m_id = id;
150: m_name = name;
151: m_password = password;
152: m_firstname = firstname;
153: m_lastname = lastname;
154: m_email = email;
155: m_lastlogin = lastlogin;
156: m_flags = flags;
157: m_dateCreated = dateCreated;
158: if (additionalInfo != null) {
159: m_additionalInfo = new HashMap(additionalInfo);
160: } else {
161: m_additionalInfo = new HashMap();
162: }
163: if (m_additionalInfo
164: .get(CmsUserSettings.ADDITIONAL_INFO_ADDRESS) == null) {
165: m_additionalInfo.put(
166: CmsUserSettings.ADDITIONAL_INFO_ADDRESS, "");
167: }
168: if (m_additionalInfo
169: .get(CmsUserSettings.ADDITIONAL_INFO_DESCRIPTION) == null) {
170: m_additionalInfo.put(
171: CmsUserSettings.ADDITIONAL_INFO_DESCRIPTION, "");
172: }
173: }
174:
175: /**
176: * Validates an email address.<p>
177: *
178: * That means, the parameter should only be composed by digits and standard english letters, points, underscores and exact one "At" symbol.<p>
179: *
180: * @param email the email to validate
181: */
182: public static void checkEmail(String email) {
183:
184: OpenCms.getValidationHandler().checkEmail(email);
185: }
186:
187: /**
188: * Validates a zip code.<p>
189: *
190: * That means, the parameter should only be composed by digits and standard english letters.<p>
191: *
192: * @param zipcode the zip code to validate
193: */
194: public static void checkZipCode(String zipcode) {
195:
196: OpenCms.getValidationHandler().checkZipCode(zipcode);
197: }
198:
199: /**
200: * Returns the "full" name of the given user in the format <code>"{firstname} {lastname} ({username})"</code>,
201: * or the empty String <code>""</code> if the user is null.<p>
202: *
203: * @param user the user to get the full name from
204: * @return the "full" name the user
205: *
206: * @see #getFullName()
207: */
208: public static String getFullName(CmsUser user) {
209:
210: if (user == null) {
211: return "";
212: } else {
213: return user.getFullName();
214: }
215: }
216:
217: /**
218: * Returns <code>true</code> if the provided user type indicates a system user.<p>
219: *
220: * @param type the user type to check
221: *
222: * @return true if the provided user type indicates a system user
223: *
224: * @deprecated the user type concept has been abandoned
225: */
226: public static boolean isSystemUser(int type) {
227:
228: return (type & 1) > 0;
229: }
230:
231: /**
232: * Checks if the provided user name is a valid user name and can be used as an argument value
233: * for {@link #setName(String)}.<p>
234: *
235: * @param name the user name to check
236: *
237: * @throws CmsIllegalArgumentException if the check fails
238: */
239: public void checkName(String name)
240: throws CmsIllegalArgumentException {
241:
242: OpenCms.getValidationHandler().checkUserName(name);
243: }
244:
245: /**
246: * @see java.lang.Object#clone()
247: */
248: public Object clone() {
249:
250: return new CmsUser(m_id, m_name, m_password, m_firstname,
251: m_lastname, m_email, m_lastlogin, m_flags,
252: m_dateCreated, m_additionalInfo);
253: }
254:
255: /**
256: * Deletes a value from this users "additional information" storage map.<p>
257: *
258: * @param key the additional user information to delete
259: *
260: * @see #getAdditionalInfo()
261: */
262: public void deleteAdditionalInfo(String key) {
263:
264: m_additionalInfo.remove(key);
265: }
266:
267: /**
268: * Returns this users complete "additional information" storage map.<p>
269: *
270: * The "additional information" storage map is a simple {@link java.util.Map}
271: * that can be used to store any key / value pairs for the user.
272: * Some information parts of the users address are stored in this map
273: * by default.<p>
274: *
275: * @return this users complete "additional information" storage map
276: */
277: public Map getAdditionalInfo() {
278:
279: return m_additionalInfo;
280: }
281:
282: /**
283: * Returns a value from this users "additional information" storage map,
284: * or <code>null</code> if no value for the given key is available.<p>
285: *
286: * @param key selects the value to return from the "additional information" storage map
287: *
288: * @return the selected value from this users "additional information" storage map
289: *
290: * @see #getAdditionalInfo()
291: */
292: public Object getAdditionalInfo(String key) {
293:
294: return m_additionalInfo.get(key);
295: }
296:
297: /**
298: * Returns the address line of this user.<p>
299: *
300: * @return the address line of this user
301: */
302: public String getAddress() {
303:
304: return (String) getAdditionalInfo(CmsUserSettings.ADDITIONAL_INFO_ADDRESS);
305: }
306:
307: /**
308: * Returns the city information of this user.<p>
309: *
310: * This information is stored in the "additional information" storage map
311: * using the key <code>{@link CmsUserSettings#ADDITIONAL_INFO_CITY}</code>.<p>
312: *
313: * @return the city information of this user
314: */
315: public String getCity() {
316:
317: return (String) getAdditionalInfo(CmsUserSettings.ADDITIONAL_INFO_CITY);
318: }
319:
320: /**
321: * Returns the country information of this user.<p>
322: *
323: * This information is stored in the "additional information" storage map
324: * using the key <code>{@link CmsUserSettings#ADDITIONAL_INFO_COUNTRY}</code>.<p>
325: *
326: * @return the country information of this user
327: */
328: public String getCountry() {
329:
330: return (String) getAdditionalInfo(CmsUserSettings.ADDITIONAL_INFO_COUNTRY);
331: }
332:
333: /**
334: * Returns the creation date.<p>
335: *
336: * @return the creation date
337: */
338: public long getDateCreated() {
339:
340: return m_dateCreated;
341: }
342:
343: /**
344: * @see org.opencms.security.CmsPrincipal#getDescription()
345: */
346: public String getDescription() {
347:
348: return (String) getAdditionalInfo(CmsUserSettings.ADDITIONAL_INFO_DESCRIPTION);
349: }
350:
351: /**
352: * Returns the description of this organizational unit.<p>
353: *
354: * @param locale the locale
355: *
356: * @return the description of this organizational unit
357: */
358: public String getDescription(Locale locale) {
359:
360: CmsMacroResolver macroResolver = new CmsMacroResolver();
361: macroResolver.setMessages(org.opencms.db.generic.Messages.get()
362: .getBundle(locale));
363: return macroResolver
364: .resolveMacros((String) getAdditionalInfo(CmsUserSettings.ADDITIONAL_INFO_DESCRIPTION));
365: }
366:
367: /**
368: * Returns <code>true</code> if this user is disabled.<p>
369: *
370: * @return <code>true</code> if this user is disabled
371: *
372: * @deprecated use {@link CmsPrincipal#isEnabled()} instead
373: */
374: public boolean getDisabled() {
375:
376: return !isEnabled();
377: }
378:
379: /**
380: * @see org.opencms.security.CmsPrincipal#getDisplayName(org.opencms.file.CmsObject, java.util.Locale)
381: */
382: public String getDisplayName(CmsObject cms, Locale locale)
383: throws CmsException {
384:
385: if (OpenCms.getOrgUnitManager().getOrganizationalUnits(cms, "",
386: true).size() > 0) {
387: return org.opencms.security.Messages
388: .get()
389: .getBundle(locale)
390: .key(
391: org.opencms.security.Messages.GUI_PRINCIPAL_DISPLAY_NAME_2,
392: getFullName(),
393: OpenCms.getOrgUnitManager()
394: .readOrganizationalUnit(cms,
395: getOuFqn()).getDisplayName(
396: locale));
397: } else {
398: return getFullName();
399: }
400: }
401:
402: /**
403: * Returns the email address of this user.<p>
404: *
405: * @return the email address of this user
406: */
407: public String getEmail() {
408:
409: return m_email;
410: }
411:
412: /**
413: * Returns the first name of this user.<p>
414: *
415: * @return the first name of this user
416: */
417: public String getFirstname() {
418:
419: return m_firstname;
420: }
421:
422: /**
423: * Returns the "full" name of the this user in the format <code>"{firstname} {lastname} ({username})"</code>.<p>
424: *
425: * @return the "full" name this user
426: */
427: public String getFullName() {
428:
429: StringBuffer buf = new StringBuffer();
430: String first = getFirstname();
431: if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(first)) {
432: buf.append(first);
433: buf.append(" ");
434: }
435: String last = getLastname();
436: if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(last)) {
437: buf.append(last);
438: buf.append(" ");
439: }
440: buf.append("(");
441: buf.append(getSimpleName());
442: buf.append(")");
443: return buf.toString();
444: }
445:
446: /**
447: * Returns the time of the last login of this user.<p>
448: *
449: * @return the time of the last login of this user
450: */
451: public long getLastlogin() {
452:
453: return m_lastlogin;
454: }
455:
456: /**
457: * Returns the last name of this user.<p>
458: *
459: * @return the last name of this user
460: */
461: public String getLastname() {
462:
463: return m_lastname;
464: }
465:
466: /**
467: * Returns the encrypted user password.<p>
468: *
469: * @return the encrypted user password
470: */
471: public String getPassword() {
472:
473: return m_password;
474: }
475:
476: /**
477: * Returns the zip code information of this user.<p>
478: *
479: * This information is stored in the "additional information" storage map
480: * using the key <code>{@link CmsUserSettings#ADDITIONAL_INFO_ZIPCODE}</code>.<p>
481: *
482: * @return the zip code information of this user
483: */
484: public String getZipcode() {
485:
486: return (String) getAdditionalInfo(CmsUserSettings.ADDITIONAL_INFO_ZIPCODE);
487: }
488:
489: /**
490: * @see org.opencms.security.I_CmsPrincipal#isGroup()
491: */
492: public boolean isGroup() {
493:
494: return false;
495: }
496:
497: /**
498: * Returns <code>true</code> if this user is the default guest user.<p>
499: *
500: * @return true if this user is the default guest user
501: */
502: public boolean isGuestUser() {
503:
504: return OpenCms.getDefaultUsers().isUserGuest(getName());
505: }
506:
507: /**
508: * Returns <code>true</code> if this user is not able to manage itself.<p>
509: *
510: * @return <code>true</code> if this user is not able to manage itself
511: */
512: public boolean isManaged() {
513:
514: return (getFlags() & I_CmsPrincipal.FLAG_USER_MANAGED) == I_CmsPrincipal.FLAG_USER_MANAGED;
515: }
516:
517: /**
518: * Returns <code>true</code> if this user was touched.<p>
519: *
520: * @return boolean true if this user was touched
521: */
522: public boolean isTouched() {
523:
524: return m_isTouched;
525: }
526:
527: /**
528: * @see org.opencms.security.I_CmsPrincipal#isUser()
529: */
530: public boolean isUser() {
531:
532: return true;
533: }
534:
535: /**
536: * Checks if the user is marked as webuser.<p>
537: *
538: * @return <code>true</code> if the user is marked as webuser
539: */
540: public boolean isWebuser() {
541:
542: return (getFlags() & FLAG_USER_WEBUSER) == FLAG_USER_WEBUSER;
543: }
544:
545: /**
546: * Sets this users complete "additional information" storage map to the given value.<p>
547: *
548: * @param additionalInfo the complete "additional information" map to set
549: *
550: * @see #getAdditionalInfo()
551: */
552: public void setAdditionalInfo(Map additionalInfo) {
553:
554: m_additionalInfo = additionalInfo;
555: }
556:
557: /**
558: * Stores a value in this users "additional information" storage map with the given access key.<p>
559: *
560: * @param key the key to store the value under
561: * @param value the value to store in the users "additional information" storage map
562: *
563: * @see #getAdditionalInfo()
564: */
565: public void setAdditionalInfo(String key, Object value) {
566:
567: m_additionalInfo.put(key, value);
568: }
569:
570: /**
571: * Sets the address line of this user.<p>
572: *
573: * @param address the address line to set
574: */
575: public void setAddress(String address) {
576:
577: setAdditionalInfo(CmsUserSettings.ADDITIONAL_INFO_ADDRESS,
578: address);
579: }
580:
581: /**
582: * Sets the city information of this user.<p>
583: *
584: * @param city the city information to set
585: */
586: public void setCity(String city) {
587:
588: setAdditionalInfo(CmsUserSettings.ADDITIONAL_INFO_CITY, city);
589: }
590:
591: /**
592: * Sets the country information of this user.<p>
593: *
594: * @param country the city information to set
595: */
596: public void setCountry(String country) {
597:
598: setAdditionalInfo(CmsUserSettings.ADDITIONAL_INFO_COUNTRY,
599: country);
600: }
601:
602: /**
603: * @see org.opencms.security.CmsPrincipal#setDescription(java.lang.String)
604: */
605: public void setDescription(String description) {
606:
607: setAdditionalInfo(CmsUserSettings.ADDITIONAL_INFO_DESCRIPTION,
608: description);
609: }
610:
611: /**
612: * Disables this user.<p>
613: *
614: * @deprecated use {@link CmsPrincipal#setEnabled(boolean)} instead
615: */
616: public void setDisabled() {
617:
618: setEnabled(false);
619: }
620:
621: /**
622: * Sets the email address of this user.<p>
623: *
624: * @param email the email address to set
625: */
626: public void setEmail(String email) {
627:
628: checkEmail(email);
629: if (email != null) {
630: email = email.trim();
631: }
632: m_email = email;
633: }
634:
635: /**
636: * Enables this user.<p>
637: *
638: * @deprecated use {@link CmsPrincipal#setEnabled(boolean)} instead
639: */
640: public void setEnabled() {
641:
642: setEnabled(true);
643: }
644:
645: /**
646: * Sets the first name of this user.<p>
647: *
648: * @param firstname the name to set
649: */
650: public void setFirstname(String firstname) {
651:
652: OpenCms.getValidationHandler().checkFirstname(firstname);
653: if (firstname != null) {
654: firstname = firstname.trim();
655: }
656: m_firstname = firstname;
657: }
658:
659: /**
660: * Sets the last login time stamp of this user.<p>
661: *
662: * @param value the last login time stamp to set
663: */
664: public void setLastlogin(long value) {
665:
666: m_isTouched = true;
667: m_lastlogin = value;
668: }
669:
670: /**
671: * Sets the last name of this user.<p>
672: *
673: * @param lastname the name to set
674: */
675: public void setLastname(String lastname) {
676:
677: OpenCms.getValidationHandler().checkLastname(lastname);
678: if (lastname != null) {
679: lastname = lastname.trim();
680: }
681: m_lastname = lastname;
682: }
683:
684: /**
685: * Sets the managed flag for this user to the given value.<p>
686: *
687: * @param value the value to set
688: */
689: public void setManaged(boolean value) {
690:
691: if (isManaged() != value) {
692: setFlags(getFlags() ^ I_CmsPrincipal.FLAG_USER_MANAGED);
693: }
694: }
695:
696: /**
697: * Sets the password of this user.<p>
698: *
699: * @param value the password to set
700: */
701: public void setPassword(String value) {
702:
703: try {
704: OpenCms.getPasswordHandler().validatePassword(value);
705: } catch (CmsSecurityException e) {
706: throw new CmsIllegalArgumentException(e
707: .getMessageContainer());
708: }
709: m_password = value;
710: }
711:
712: /**
713: * Sets the zip code information of this user.<p>
714: *
715: * @param zipcode the zip code information to set
716: */
717: public void setZipcode(String zipcode) {
718:
719: checkZipCode(zipcode);
720: if (zipcode != null) {
721: zipcode = zipcode.toUpperCase();
722: }
723: setAdditionalInfo(CmsUserSettings.ADDITIONAL_INFO_ZIPCODE,
724: zipcode);
725: }
726:
727: /**
728: * @see java.lang.Object#toString()
729: */
730: public String toString() {
731:
732: StringBuffer result = new StringBuffer();
733: result.append("[User]");
734: result.append(" name:");
735: result.append(getName());
736: result.append(" id:");
737: result.append(m_id);
738: result.append(" flags:");
739: result.append(getFlags());
740: result.append(" description:");
741: result.append(getDescription());
742: return result.toString();
743: }
744:
745: /**
746: * Sets the "touched" status of this user to <code>true</code>.<p>
747: */
748: public void touch() {
749:
750: m_isTouched = true;
751: }
752: }
|