1 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 ; 45 import java.util.Map ; 46 47 78 public class CmsUser extends CmsPrincipal implements I_CmsPrincipal, Cloneable { 79 80 81 public static final int USER_TYPE_SYSTEMUSER = 0; 82 83 84 public static final int USER_TYPE_WEBUSER = 2; 85 86 87 private Map m_additionalInfo; 88 89 90 private String m_address; 91 92 93 private String m_email; 94 95 96 private String m_firstname; 97 98 99 private boolean m_isTouched; 100 101 102 private long m_lastlogin; 103 104 105 private String m_lastname; 106 107 108 private String m_password; 109 110 116 private int m_type; 117 118 123 public CmsUser() { 124 125 this(null, "", ""); 126 setAdditionalInfo(new HashMap ()); 127 } 128 129 136 public CmsUser(CmsUUID id, String name, String 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 169 public CmsUser( 170 CmsUUID id, 171 String name, 172 String password, 173 String description, 174 String firstname, 175 String lastname, 176 String email, 177 long lastlogin, 178 int flags, 179 Map additionalInfo, 180 String 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 204 public static void checkEmail(String email) { 205 206 OpenCms.getValidationHandler().checkEmail(email); 207 } 208 209 216 public static void checkZipCode(String zipcode) { 217 218 OpenCms.getValidationHandler().checkZipCode(zipcode); 219 } 220 221 230 public static String getFullName(CmsUser user) { 231 232 if (user == null) { 233 return ""; 234 } else { 235 return user.getFullName(); 236 } 237 } 238 239 246 public static boolean isSystemUser(int type) { 247 248 return (type & 1) > 0; 249 } 250 251 258 public static boolean isWebUser(int type) { 259 260 return (type & 2) > 0; 261 } 262 263 271 public void checkName(String name) throws CmsIllegalArgumentException { 272 273 OpenCms.getValidationHandler().checkUserName(name); 274 } 275 276 279 public Object 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 (m_additionalInfo) : null, 292 m_address, 293 m_type); 294 } 295 296 303 public void deleteAdditionalInfo(String key) { 304 305 m_additionalInfo.remove(key); 306 } 307 308 318 public Map getAdditionalInfo() { 319 320 return m_additionalInfo; 321 } 322 323 333 public Object getAdditionalInfo(String key) { 334 335 return m_additionalInfo.get(key); 336 } 337 338 343 public String getAddress() { 344 345 return m_address; 346 } 347 348 356 public String getCity() { 357 358 return (String )getAdditionalInfo(CmsUserSettings.ADDITIONAL_INFO_CITY); 359 } 360 361 369 public String getCountry() { 370 371 return (String )getAdditionalInfo(CmsUserSettings.ADDITIONAL_INFO_COUNTRY); 372 } 373 374 381 public boolean getDisabled() { 382 383 return !isEnabled(); 384 } 385 386 391 public String getEmail() { 392 393 return m_email; 394 } 395 396 401 public String getFirstname() { 402 403 return m_firstname; 404 } 405 406 411 public String getFullName() { 412 413 StringBuffer buf = new StringBuffer (); 414 String first = getFirstname(); 415 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(first)) { 416 buf.append(first); 417 buf.append(" "); 418 } 419 String 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 435 public long getLastlogin() { 436 437 return m_lastlogin; 438 } 439 440 445 public String getLastname() { 446 447 return m_lastname; 448 } 449 450 455 public String getPassword() { 456 457 return m_password; 458 } 459 460 469 public int getType() { 470 471 return m_type; 472 } 473 474 482 public String getZipcode() { 483 484 return (String )getAdditionalInfo(CmsUserSettings.ADDITIONAL_INFO_ZIPCODE); 485 } 486 487 490 public boolean isGroup() { 491 492 return false; 493 } 494 495 500 public boolean isGuestUser() { 501 502 return OpenCms.getDefaultUsers().getUserGuest().equals(getName()); 503 } 504 505 510 public boolean isSystemUser() { 511 512 return isSystemUser(m_type); 513 } 514 515 520 public boolean isTouched() { 521 522 return m_isTouched; 523 } 524 525 528 public boolean isUser() { 529 530 return true; 531 } 532 533 538 public boolean isWebUser() { 539 540 return isWebUser(m_type); 541 } 542 543 550 public void setAdditionalInfo(Map additionalInfo) { 551 552 m_additionalInfo = additionalInfo; 553 } 554 555 563 public void setAdditionalInfo(String key, Object value) { 564 565 m_additionalInfo.put(key, value); 566 } 567 568 573 public void setAddress(String address) { 574 575 m_address = address; 576 } 577 578 583 public void setCity(String city) { 584 585 setAdditionalInfo(CmsUserSettings.ADDITIONAL_INFO_CITY, city); 586 } 587 588 593 public void setCountry(String country) { 594 595 setAdditionalInfo(CmsUserSettings.ADDITIONAL_INFO_COUNTRY, country); 596 } 597 598 603 public void setDisabled() { 604 605 setEnabled(false); 606 } 607 608 613 public void setEmail(String email) { 614 615 checkEmail(email); 616 m_email = email; 617 } 618 619 624 public void setEnabled() { 625 626 setEnabled(true); 627 } 628 629 634 public void setFirstname(String 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 647 public void setLastlogin(long value) { 648 649 m_isTouched = true; 650 m_lastlogin = value; 651 } 652 653 658 public void setLastname(String 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 671 public void setPassword(String 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 686 public void setZipcode(String zipcode) { 687 688 checkZipCode(zipcode); 689 zipcode = zipcode.toUpperCase(); 690 setAdditionalInfo(CmsUserSettings.ADDITIONAL_INFO_ZIPCODE, zipcode); 691 } 692 693 696 public String toString() { 697 698 StringBuffer result = new StringBuffer (); 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 716 public void touch() { 717 718 m_isTouched = true; 719 } 720 721 730 protected void setType(int value) { 731 732 m_type = value; 733 } 734 } | Popular Tags |