1 15 package org.jahia.services.usermanager; 16 17 import org.jahia.exceptions.JahiaException; 18 import org.jahia.exceptions.database.JahiaDatabaseConnectionException; 19 import org.jahia.exceptions.database.JahiaDatabaseException; 20 import org.jahia.registries.ServicesRegistry; 21 22 import java.security.Principal ; 23 import java.util.Enumeration ; 24 import java.util.Properties ; 25 import java.util.StringTokenizer ; 26 import java.util.Vector ; 27 import java.io.Serializable ; 28 import java.util.Iterator ; 29 30 31 41 public class JahiaDBUser implements JahiaUser, Serializable { 42 private static org.apache.log4j.Logger logger = 43 org.apache.log4j.Logger.getLogger (JahiaDBUser.class); 44 45 private int mID; 46 47 48 private String mUsername; 49 50 51 private String mPassword; 52 53 54 private String mUserKey; 55 56 57 private int mSiteID = -1; 58 59 60 61 private static final String mHOMEPAGE_PROP = "user_homepage"; 62 63 private static final String mLANGUAGES_ORDER_PROP = "language_codes"; 65 private static final String mLANGUAGES_ORDER_PROP_SEPARATOR = ","; 66 private static final String mLANGUAGES_MIX_PROP = "langage_mix"; 67 private static final String mLANGUAGES_ONLYUSER_PROP = "language_onlyuser"; 68 69 70 private UserProperties mProperties = new UserProperties (); 71 72 73 90 protected JahiaDBUser (int id, String name, String password, String userKey, int siteID, 91 UserProperties properties) { 92 mID = id; 93 mUsername = name; 94 mPassword = password; 95 mUserKey = userKey; 96 mSiteID = siteID; 97 98 if (properties != null) { 99 mProperties = properties; 100 } 101 } 102 103 104 public boolean equals (Object another) { 109 if (another instanceof Principal ) { 110 if (another != null) { 111 return (getName ().equals (((Principal ) another).getName ())); 112 } 113 } 114 return false; 115 } 116 117 118 127 public int getID () { 128 return mID; 129 } 130 131 132 public String getName () { 137 return getUserKey (); 138 } 139 140 public String getUsername () { 145 return mUsername; 146 } 147 148 public String getUserKey () { 153 return mUserKey; 154 } 155 156 public int getSiteID () { 161 return mSiteID; 162 } 163 164 public void setSiteID (int siteID) { 165 mSiteID = siteID; 166 } 167 168 175 public int getHomepageID () { 176 177 if (mProperties != null) { 178 179 try { 180 String value = mProperties.getProperty (mHOMEPAGE_PROP); 181 if (value == null) 182 return -1; 183 return Integer.parseInt (value); 184 } catch (Throwable t) { 185 t.printStackTrace (); 186 } 187 } 188 return -1; 189 } 190 191 199 public boolean setHomepageID (int id) { 200 201 205 return setProperty (mHOMEPAGE_PROP, String.valueOf (id)); 206 } 207 208 217 public Properties getProperties () { 218 if (mProperties != null) { 219 return mProperties.getProperties(); 220 } else { 221 return null; 222 } 223 } 224 225 231 public UserProperties getUserProperties() { 232 return mProperties; 233 } 234 235 public String getProperty (String key) { 240 241 if ((mProperties != null) && (key != null)) { 242 return mProperties.getProperty (key); 243 } 244 return null; 245 } 246 247 public UserProperty getUserProperty(String key) { 248 if ((mProperties != null) && (key != null)) { 249 return mProperties.getUserProperty (key); 250 } 251 return null; 252 } 253 254 264 public int hashCode () { 265 return ("user" + mID).hashCode (); 266 } 267 268 269 280 public synchronized boolean removeProperty (String key) { 281 boolean result = false; 282 283 if (mProperties == null) { 284 return result; 285 } 286 287 if ((key != null) && (key.length () > 0) && (!mProperties.isReadOnly(key))) { 288 JahiaUserDBUtils utils = JahiaUserDBUtils.getInstance (); 289 if (utils != null) { 290 try { 291 result = utils.removeProperty (key, mID, getProviderName (), getUserKey ()); 292 } 293 catch (JahiaDatabaseException ex) { 295 } 297 298 catch (JahiaDatabaseConnectionException ex) { 300 } 302 303 catch (JahiaException ex) { 305 } 307 } 308 } 309 310 if (result) { 311 mProperties.removeUserProperty (key); 312 } 313 314 return result; 315 } 316 317 public synchronized boolean isPasswordReadOnly() { 318 return false; 319 } 320 321 333 public synchronized boolean setPassword (String password) { 334 boolean result = false; 335 336 if (password != null) { 338 if (password.length () > 0) { 340 String tmp = JahiaUserManagerService. 342 encryptPassword (password); 343 JahiaUserDBUtils utils = JahiaUserDBUtils.getInstance (); 344 if (utils != null) { 345 try { 346 result = utils.setPassword (password, mID); 347 if (result) { 348 mPassword = tmp; 349 ServicesRegistry.getInstance().getJahiaUserManagerService().updateCache(this); 350 } 351 } 352 catch (JahiaDatabaseException ex) { 354 } 356 357 catch (JahiaDatabaseConnectionException ex) { 359 } 361 362 catch (JahiaException ex) { 364 } 366 } 367 } 368 } 369 return result; 370 } 371 372 public String getPassword() { 373 return mPassword; 374 } 375 376 389 public synchronized boolean setProperty (String key, String value) { 390 boolean result = false; 391 392 if (mProperties == null) { 393 return result; 394 } 395 396 if ((key != null) && (value != null) && (!mProperties.isReadOnly(key))) { 397 398 JahiaUserDBUtils utils = JahiaUserDBUtils.getInstance (); 399 if (utils != null) { 400 try { 401 if (getProperty (key) == null) { 402 result = 403 utils.addProperty (key, value, mID, getProviderName (), 404 getUserKey ()); 405 } else { 406 result = 407 utils.updateProperty (key, value, mID, getProviderName (), 408 getUserKey ()); 409 } 410 } 411 catch (JahiaDatabaseException ex) { 413 } 415 416 catch (JahiaDatabaseConnectionException ex) { 418 } 420 421 catch (JahiaException ex) { 423 } 425 } 426 427 if (result) { 428 try { 429 mProperties.setProperty(key, value); 430 } catch (UserPropertyReadOnlyException uproe) { 431 logger.warn("Cannot set read-only property " + key); 432 } 433 ServicesRegistry.getInstance().getJahiaUserManagerService().updateCache(this); 434 } 435 } 436 return result; 437 } 438 439 440 public boolean verifyPassword (String password) { 445 446 if (password != null) { 447 String test = JahiaUserManagerService.encryptPassword (password); 448 return mPassword.equals (test); 449 } 450 return false; 451 } 452 453 454 463 public String toString () { 464 StringBuffer output = new StringBuffer ("Detail of user [" + mUsername + "]\n"); 465 output.append (" - ID [" + Integer.toString (mID) + "]"); 466 output.append (" - password [" + mPassword + "]\n"); 467 468 if (mProperties != null) { 469 output.append(" - properties :"); 470 471 Iterator nameIter = mProperties.propertyNameIterator(); 472 String name; 473 if (nameIter.hasNext()) { 474 output.append("\n"); 475 while (nameIter.hasNext()) { 476 name = (String ) nameIter.next(); 477 output.append( 478 " " + name + " -> [" + 479 (String ) mProperties.getProperty(name) + "]\n"); 480 } 481 } else { 482 output.append(" -no properties-\n"); 483 } 484 } 485 return output.toString (); 486 } 487 488 489 497 public boolean isAdminMember (int siteID) { 498 499 return isMemberOfGroup (siteID, JahiaGroupManagerService.ADMINISTRATORS_GROUPNAME); 500 } 501 502 508 public boolean isRoot () { 509 512 if (getID () == JahiaUserManagerDBProvider.ROOT_USER_ID) { 513 return true; 514 } else { 515 return false; 516 } 517 } 518 519 520 public boolean isMemberOfGroup (int siteID, String name) { 522 ServicesRegistry servicesRegistry = ServicesRegistry.getInstance (); 524 if (servicesRegistry != null) { 525 526 JahiaGroupManagerService groupService = 528 servicesRegistry.getJahiaGroupManagerService (); 529 530 JahiaGroup group = groupService.lookupGroup (siteID, name); 532 if (group != null) { 533 return group.isMember (this); 534 } 535 } 536 return false; 537 } 538 539 547 public Vector getLanguageCodes () { 548 String encodedLanguagesCodes = getProperty (mLANGUAGES_ORDER_PROP); 549 Vector result = new Vector (); 550 if (encodedLanguagesCodes != null) { 551 StringTokenizer strTokens = new StringTokenizer (encodedLanguagesCodes, 552 mLANGUAGES_ORDER_PROP_SEPARATOR); 553 while (strTokens.hasMoreTokens ()) { 554 String curLanguageCode = strTokens.nextToken (); 555 result.add (curLanguageCode); 556 } 557 } 558 return result; 559 } 560 561 570 public void setLanguageCodes (Vector userLanguages) { 571 StringBuffer encodedLanguageCodes = new StringBuffer (); 572 Enumeration userLanguagesEnum = userLanguages.elements (); 573 while (userLanguagesEnum.hasMoreElements ()) { 574 String curLanguage = (String ) userLanguagesEnum.nextElement (); 575 if (curLanguage.indexOf (mLANGUAGES_ORDER_PROP_SEPARATOR) != 0) { 576 logger.debug ("Invalid " + mLANGUAGES_ORDER_PROP_SEPARATOR + 578 " character in language code : " + curLanguage + 579 ". Not storing this language code."); 580 } else { 581 encodedLanguageCodes.append (curLanguage); 582 encodedLanguageCodes.append (mLANGUAGES_ORDER_PROP_SEPARATOR); 583 } 584 } 585 String encodedLanguageCodeStr = encodedLanguageCodes.toString (); 586 encodedLanguageCodeStr = 588 encodedLanguageCodeStr.substring (0, encodedLanguageCodeStr.length () - 1); 589 setProperty (mLANGUAGES_ORDER_PROP, encodedLanguageCodeStr); 590 } 591 592 599 public boolean isMixLanguagesActive () { 600 String mixActiveStr = getProperty (mLANGUAGES_MIX_PROP); 601 if (mixActiveStr != null) { 602 Boolean mixActiveBool = Boolean.valueOf (mixActiveStr); 603 return mixActiveBool.booleanValue (); 604 } else { 605 logger.debug (mLANGUAGES_MIX_PROP + 606 " property not found for user " + 607 this.getUsername () + 608 ". Defaulting to false."); 609 return false; 610 } 611 } 612 613 619 public void setMixLanguagesActive (boolean mixLanguagesActive) { 620 setProperty (mLANGUAGES_MIX_PROP, 621 new Boolean (mixLanguagesActive).toString ()); 622 } 623 624 633 public boolean isUserLanguagesOnlyActive () { 634 String userLanguagesOnlyStr = getProperty (mLANGUAGES_ONLYUSER_PROP); 635 if (userLanguagesOnlyStr != null) { 636 Boolean userLanguagesOnlyBool = Boolean.valueOf (userLanguagesOnlyStr); 637 return userLanguagesOnlyBool.booleanValue (); 638 } else { 639 logger.debug (mLANGUAGES_ONLYUSER_PROP + 640 " property not found for user " + 641 this.getUsername () + 642 ". Defaulting to false."); 643 return false; 644 } 645 646 } 647 648 656 public void setUserLanguagesOnlyActive (boolean userLanguagesOnlyActive) { 657 setProperty (mLANGUAGES_ONLYUSER_PROP, 658 new Boolean (userLanguagesOnlyActive).toString ()); 659 660 } 661 662 667 public String getProviderName () { 668 return JahiaUserManagerDBProvider.PROVIDER_NAME; 669 } 670 671 672 } 673 | Popular Tags |