1 14 package org.jahia.services.usermanager; 15 16 import org.jahia.exceptions.JahiaException; 17 import org.jahia.exceptions.database.JahiaDatabaseConnectionException; 18 import org.jahia.exceptions.database.JahiaDatabaseException; 19 import org.jahia.registries.ServicesRegistry; 20 21 import java.security.Principal ; 22 import java.util.Enumeration ; 23 import java.util.Properties ; 24 import java.util.StringTokenizer ; 25 import java.util.Vector ; 26 import java.io.Serializable ; 27 import java.util.Iterator ; 28 29 39 public class JahiaLDAPUser implements JahiaUser, Serializable { 40 private static org.apache.log4j.Logger logger = 41 org.apache.log4j.Logger.getLogger (JahiaLDAPUser.class); 42 43 static final public String USERKEY_LDAP_PREFIX = "{ldap}"; 44 45 46 private int mID; 47 48 49 private String mUsername; 50 51 52 private String mPassword; 53 54 55 private String mUserKey; 56 57 58 private int mSiteID = -1; 59 60 61 private String mDn; 62 63 64 private Vector mGroups; 65 66 67 private UserProperties mProperties = new UserProperties (); 68 69 70 private static final String mHOMEPAGE_PROP = "user_homepage"; 71 72 private static final String mLANGUAGES_ORDER_PROP = "language_codes"; 74 private static final String mLANGUAGES_ORDER_PROP_SEPARATOR = ","; 75 private static final String mLANGUAGES_MIX_PROP = "langage_mix"; 76 private static final String mLANGUAGES_ONLYUSER_PROP = "language_onlyuser"; 77 78 91 protected JahiaLDAPUser (int id, String name, String password, 92 String userKey, int siteID, 93 UserProperties properties, String dn) 94 { 95 mID = id; 96 mUsername = name; 97 mPassword = password; 98 mUserKey = USERKEY_LDAP_PREFIX + userKey; 99 mSiteID = siteID; 100 mDn = dn; 101 if (properties != null) { 102 mProperties = properties; 103 } 104 } 105 106 107 public boolean equals (Object another) { 108 if (another instanceof Principal ) { 109 if (another != null) { 110 if (mUsername == null) { 111 logger.debug ( 112 "Username for user key [" + mUserKey + "] is null , please check your users.ldap.properties mapping file !"); 113 if (((Principal ) another).getName () == null) { 114 return true; 115 } else { 116 return false; 117 } 118 } 119 return (getName ().equals (((Principal ) another).getName ())); 120 } 121 } 122 return false; 123 } 124 125 126 131 public int getID () { 132 return mID; 133 } 134 135 136 public String getName () { 137 return getUserKey (); 138 } 139 140 public String getUsername () { 141 return mUsername; 142 } 143 144 public String getUserKey () { 145 return mUserKey; 146 } 147 148 public int getSiteID () { 149 return mSiteID; 150 } 151 152 public void setSiteID (int siteID) { 153 mSiteID = siteID; 154 } 155 156 public String getDN() { 157 return mDn; 158 } 159 160 165 public Properties getProperties () { 166 if (mProperties != null) { 167 return mProperties.getProperties(); 168 } else { 169 return null; 170 } 171 } 172 173 179 public UserProperties getUserProperties() { 180 return mProperties; 181 } 182 183 public String getProperty (String key) { 185 186 if ((mProperties != null) && (key != null)) { 187 return mProperties.getProperty (key); 188 } 189 return null; 190 } 191 192 public UserProperty getUserProperty(String key) { 193 if ((mProperties != null) && (key != null)) { 194 return mProperties.getUserProperty (key); 195 } 196 return null; 197 } 198 199 200 206 public int hashCode () { 207 return mID; 208 } 209 210 211 220 public synchronized boolean removeProperty (String key) { 221 boolean result = false; 222 if (mProperties == null) { 223 return result; 224 } 225 226 if ((key != null) && (key.length () > 0) && (!mProperties.isReadOnly(key))) { 227 JahiaUserDBUtils utils = JahiaUserDBUtils.getInstance (); 229 if (utils != null) { 230 try { 231 result = utils.removeProperty (key, -1, getProviderName (), getUserKey ()); 232 } 233 catch (JahiaDatabaseException ex) { 235 } 237 238 catch (JahiaDatabaseConnectionException ex) { 240 } 242 243 catch (JahiaException ex) { 245 } 247 } 248 } 250 if (result) { 252 mProperties.removeUserProperty (key); 253 } 254 return result; 256 } 257 258 public synchronized boolean isPasswordReadOnly() { 259 return true; 260 } 261 262 272 public synchronized boolean setPassword (String password) { 273 boolean result = false; 274 275 return result; 276 } 277 278 279 294 public synchronized boolean setProperty (String key, String value) { 295 boolean result = false; 296 if (mProperties == null) { 297 return result; 298 } 299 300 if ((key != null) && (value != null) && (!mProperties.isReadOnly(key))) { 301 JahiaUserDBUtils utils = JahiaUserDBUtils.getInstance (); 303 if (utils != null) { 304 try { 305 if (getProperty (key) == null) { 306 result = 307 utils.addProperty (key, value, -1, getProviderName (), 308 getUserKey ()); 309 } else { 310 result = 311 utils.updateProperty (key, value, -1, getProviderName (), 312 getUserKey ()); 313 } 314 } 315 catch (JahiaDatabaseException ex) { 317 logger.error("Error setting property " + key + " with value " + value + " for user " + getUserKey(), ex); 319 } 320 321 catch (JahiaDatabaseConnectionException ex) { 323 logger.error("Error setting property " + key + " with value " + value + " for user " + getUserKey(), ex); 325 } 326 327 catch (JahiaException ex) { 329 } 331 } 332 if (result) { 334 try { 335 mProperties.setProperty(key, value); 336 } catch (UserPropertyReadOnlyException uproe) { 337 logger.warn("Cannot set read-only property " + key); 338 } 339 ServicesRegistry.getInstance().getJahiaUserManagerService().updateCache(this); 340 } 341 } 342 return result; 343 } 344 345 352 public int getHomepageID () { 353 354 if (mProperties != null) { 355 356 try { 357 String value = mProperties.getProperty (mHOMEPAGE_PROP); 360 if (value == null) { 361 return -1; 362 } 363 return Integer.parseInt (value); 364 } catch (Throwable t) { 365 t.printStackTrace (); 366 } 367 } 368 return -1; 369 } 370 371 379 public boolean setHomepageID (int id) { 380 381 if (!removeProperty (mHOMEPAGE_PROP)) 382 return false; 383 return setProperty (mHOMEPAGE_PROP, String.valueOf (id)); 386 } 387 388 public Vector getGroups() { 389 return mGroups; 390 } 391 392 public void setGroups(Vector mGroups) { 393 this.mGroups = mGroups; 394 } 395 396 public boolean verifyPassword(String password) { 397 398 if (password != null) { 399 boolean localLoginResult = false; 400 401 if (mPassword.length() > 0) { 402 String test = JahiaUserManagerService.encryptPassword(password); 403 localLoginResult = mPassword.equals(test); 404 } 405 if (localLoginResult) { 408 return true; 410 } else { 411 boolean loginResult = JahiaUserManagerLDAPProvider.getInstance().login(mUserKey, password); 416 if (loginResult) { 417 422 mPassword = JahiaUserManagerService.encryptPassword(password); 423 return true; 424 } 425 426 427 } 428 } 429 return false; 430 } 431 432 433 438 public String toString () { 439 StringBuffer output = new StringBuffer ("Detail of user [" + mUsername + "]\n"); 440 output.append (" - ID [" + Integer.toString (mID) + "]"); 441 output.append (" - password [" + mPassword + "]\n"); 442 443 if (mProperties != null) { 444 output.append(" - properties :"); 445 446 Iterator nameIter = mProperties.propertyNameIterator(); 447 String name; 448 if (nameIter.hasNext()) { 449 output.append("\n"); 450 while (nameIter.hasNext()) { 451 name = (String ) nameIter.next(); 452 output.append( 453 " " + name + " -> [" + 454 (String ) mProperties.getProperty(name) + "]\n"); 455 } 456 } else { 457 output.append(" -no properties-\n"); 458 } 459 } 460 return output.toString (); 461 } 462 463 464 470 public boolean isAdminMember (int siteID) { 471 472 return isMemberOfGroup (siteID, JahiaGroupManagerService.ADMINISTRATORS_GROUPNAME); 473 } 474 475 481 public boolean isRoot () { 482 485 return false; 486 } 487 488 489 public boolean isMemberOfGroup (int siteID, String name) { 491 ServicesRegistry servicesRegistry = ServicesRegistry.getInstance (); 493 if (servicesRegistry != null) { 494 495 JahiaGroupManagerService groupService = 497 servicesRegistry.getJahiaGroupManagerService (); 498 499 JahiaGroup group = groupService.lookupGroup (siteID, name); 501 if (group != null) { 502 return group.isMember (this); 503 } 504 } 505 return false; 506 } 507 508 516 public Vector getLanguageCodes () { 517 String encodedLanguagesCodes = getProperty (mLANGUAGES_ORDER_PROP); 518 Vector result = new Vector (); 519 if (encodedLanguagesCodes != null) { 520 StringTokenizer strTokens = new StringTokenizer (encodedLanguagesCodes, 521 mLANGUAGES_ORDER_PROP_SEPARATOR); 522 while (strTokens.hasMoreTokens ()) { 523 String curLanguageCode = strTokens.nextToken (); 524 result.add (curLanguageCode); 525 } 526 } 527 return result; 528 } 529 530 539 public void setLanguageCodes (Vector userLanguages) { 540 StringBuffer encodedLanguageCodes = new StringBuffer (); 541 Enumeration userLanguagesEnum = userLanguages.elements (); 542 while (userLanguagesEnum.hasMoreElements ()) { 543 String curLanguage = (String ) userLanguagesEnum.nextElement (); 544 if (curLanguage.indexOf (mLANGUAGES_ORDER_PROP_SEPARATOR) != 0) { 545 logger.debug ("Invalid " + mLANGUAGES_ORDER_PROP_SEPARATOR + 547 " character in language code : " + curLanguage + 548 ". Not storing this language code."); 549 } else { 550 encodedLanguageCodes.append (curLanguage); 551 encodedLanguageCodes.append (mLANGUAGES_ORDER_PROP_SEPARATOR); 552 } 553 } 554 String encodedLanguageCodeStr = encodedLanguageCodes.toString (); 555 encodedLanguageCodeStr = 557 encodedLanguageCodeStr.substring (0, encodedLanguageCodeStr.length () - 1); 558 setProperty (mLANGUAGES_ORDER_PROP, encodedLanguageCodeStr); 559 } 560 561 568 public boolean isMixLanguagesActive () { 569 String mixActiveStr = getProperty (mLANGUAGES_MIX_PROP); 570 if (mixActiveStr != null) { 571 Boolean mixActiveBool = Boolean.valueOf (mixActiveStr); 572 return mixActiveBool.booleanValue (); 573 } else { 574 logger.debug (mLANGUAGES_MIX_PROP + 575 " property not found for user " + 576 this.getUsername () + 577 ". Defaulting to false."); 578 return false; 579 } 580 } 581 582 588 public void setMixLanguagesActive (boolean mixLanguagesActive) { 589 setProperty (mLANGUAGES_MIX_PROP, 590 new Boolean (mixLanguagesActive).toString ()); 591 } 592 593 602 public boolean isUserLanguagesOnlyActive () { 603 String userLanguagesOnlyStr = getProperty (mLANGUAGES_ONLYUSER_PROP); 604 if (userLanguagesOnlyStr != null) { 605 Boolean userLanguagesOnlyBool = Boolean.valueOf (userLanguagesOnlyStr); 606 return userLanguagesOnlyBool.booleanValue (); 607 } else { 608 logger.debug (mLANGUAGES_ONLYUSER_PROP + 609 " property not found for user " + 610 this.getUsername () + 611 ". Defaulting to false."); 612 return false; 613 } 614 615 } 616 617 625 public void setUserLanguagesOnlyActive (boolean userLanguagesOnlyActive) { 626 setProperty (mLANGUAGES_ONLYUSER_PROP, 627 new Boolean (userLanguagesOnlyActive).toString ()); 628 629 } 630 631 636 public String getProviderName () { 637 return JahiaUserManagerLDAPProvider.PROVIDER_NAME; 638 } 639 640 } 641 | Popular Tags |