1 19 package org.openharmonise.rm.resources.users; 20 21 import java.sql.*; 22 import java.util.Vector ; 23 import java.util.logging.*; 24 25 import org.openharmonise.commons.dsi.*; 26 import org.openharmonise.commons.dsi.dml.*; 27 import org.openharmonise.rm.*; 28 import org.openharmonise.rm.config.*; 29 import org.openharmonise.rm.dsi.DataStoreObject; 30 import org.openharmonise.rm.metadata.Profile; 31 import org.openharmonise.rm.publishing.*; 32 import org.openharmonise.rm.resources.*; 33 import org.openharmonise.rm.resources.lifecycle.*; 34 import org.openharmonise.rm.security.authentication.*; 35 import org.w3c.dom.*; 36 37 45 public class User 46 extends AbstractChildObject 47 implements DataStoreObject, Publishable, Editable, Cloneable , Comparable { 48 49 public static final String TAG_USER = "User"; 51 public static final String TAG_PASSWORD = "Password"; 52 53 public static final String TBL_USER = "users"; 55 protected static final String CLMN_PASSWORD = "password"; 56 protected static final String CLMN_IS_SUPER = "isSuper"; 57 protected static final String CLMN_SALT = "salt"; 58 59 protected String m_sPassword = ""; 61 62 66 protected String m_sSalt = ""; 67 protected boolean m_bIsSuper = false; 68 69 72 private final static Logger m_logger = Logger.getLogger(User.class.getName()); 73 74 78 public User() { 79 super(); 80 String crypto = "NONE"; 81 try { 82 crypto = ConfigSettings.getProperty("PWD_ENCRYPTION", "NONE"); 84 m_logger.log(Level.INFO, "Got crypto property: " + crypto); 85 } 86 catch (ConfigException e) { 87 m_logger.log(Level.SEVERE, "config exception", e); 88 } 89 90 if (crypto.equals("MD5")) { 91 m_sSalt = PasswordCryptUtil.getNewSalt(32); 92 m_logger.log(Level.FINE, "Got 32 character salt for user " + m_sName + " " + m_sSalt); 93 } 94 else if (crypto.equals("SHA-1")) { 95 m_sSalt = PasswordCryptUtil.getNewSalt(40); 96 m_logger.log(Level.FINE, "Got 40 character salt for user " + m_sName + " " + m_sSalt); 97 } 98 else { 99 m_sSalt = ""; 100 } 101 } 102 103 110 public User(AbstractDataStoreInterface dbinterf, boolean bHist) { 111 super(dbinterf); 112 113 } 114 115 121 public User(AbstractDataStoreInterface dbinterf, int nId) { 122 super(dbinterf, nId); 123 124 } 125 126 133 public User(AbstractDataStoreInterface dbinterf, int nId, int nKey, boolean bHist) { 134 super(dbinterf, nId, nKey, bHist); 135 136 } 137 138 144 public User(AbstractDataStoreInterface dbinterf) { 145 super(dbinterf); 146 147 } 148 149 155 public String getPassword() throws DataAccessException { 156 if ((m_sPassword == null || m_sPassword.length() == 0) 157 && isPopulated() == false) { 158 try { 159 populateFromDatabase(); 160 } catch (PopulateException e) { 161 throw new DataAccessException( 162 "Error occured populating user:" + e.getLocalizedMessage()); 163 } 164 } 165 166 return m_sPassword; 167 } 168 169 175 public void setPassword(String sPassword) { 176 if (isPopulated() == true) { 177 if (m_sPassword.equals(sPassword) == false) { 178 m_bIsChanged = true; 179 } 180 } 181 m_sPassword = sPassword; 182 } 183 184 190 public boolean isSuper() throws DataAccessException { 191 if(isPopulated() == false) { 192 try { 193 populateFromDatabase(); 194 } catch (PopulateException e) { 195 throw new DataAccessException(e.getLocalizedMessage(),e); 196 } 197 } 198 199 return m_bIsSuper; 200 } 201 202 207 public void setIsSuper(boolean bIsSuper) { 208 if (isPopulated() == true) { 209 if (m_bIsSuper != bIsSuper) { 210 m_bIsChanged = true; 211 } 212 } 213 214 m_bIsSuper = bIsSuper; 215 } 216 217 220 public Element publish(Element topEl, HarmoniseOutput xmlDoc, State state) 221 throws PublishException { 222 223 Element docEl = null; 224 String sTagName = topEl.getTagName(); 225 Text txt = null; 226 227 if (sTagName.equals(TAG_PASSWORD)) { 228 docEl = xmlDoc.createElement(TAG_PASSWORD); 229 230 try { 231 txt = xmlDoc.createTextNode(getPassword()); 232 } catch (Exception e) { 233 throw new PublishException( 234 "Error occured getting password for user:" 235 + e.getLocalizedMessage()); 236 } 237 docEl.appendChild(txt); 238 239 xmlDoc.copyChildren(docEl, topEl, new Vector ()); 240 } else { 241 docEl = super.publish(topEl, xmlDoc, state); 242 } 243 244 return docEl; 245 } 246 247 250 public void populate(Element xmlElement, State state) 251 throws PopulateException { 252 String sTagName = xmlElement.getTagName(); 253 Text txt = null; 254 255 if (sTagName.equals(TAG_PASSWORD)) { 256 txt = (Text) xmlElement.getFirstChild(); 257 setPassword(txt.getNodeValue()); 258 } else { 259 super.populate(xmlElement, state); 260 } 261 } 262 263 266 public String toString() { 267 StringBuffer sReturn = new StringBuffer (); 268 269 try { 270 sReturn.append(" User_id=[") 271 .append(Integer.toString(m_nId)) 272 .append("]"); 273 sReturn.append(" UserName=[") 274 .append(m_sName) 275 .append("]"); 276 sReturn.append(" Password=[") 277 .append(m_sPassword) 278 .append("]"); 279 280 Profile prof = getProfile(); 281 282 if(prof != null) { 283 sReturn.append(" Profile=[") 284 .append(prof.toString()) 285 .append("]"); 286 } 287 288 } catch (Exception e) { 289 m_logger.log(Level.INFO, e.getLocalizedMessage(), e); 290 } 291 292 return sReturn.toString(); 293 } 294 295 298 public ColumnRef getInstanceColumnRef(String columnRef, boolean bIsHist) 299 throws DataStoreException { 300 ColumnRef colRef = null; 301 302 String sTable = getTableName(bIsHist); 303 304 if (columnRef.equals(TAG_PASSWORD) == true 305 || columnRef.equals(CLMN_PASSWORD)) { 306 colRef = new ColumnRef(sTable, CLMN_PASSWORD, ColumnRef.TEXT); 307 } else if (columnRef.equals(CLMN_IS_SUPER)) { 308 colRef = new ColumnRef(sTable, CLMN_IS_SUPER, ColumnRef.NUMBER); 309 } else if (columnRef.equals(CLMN_SALT)) { 310 colRef = new ColumnRef(sTable, CLMN_SALT, ColumnRef.TEXT); 311 } 312 else { 313 colRef = super.getInstanceColumnRef(columnRef, bIsHist); 314 } 315 316 return colRef; 317 } 318 319 322 public String getParentObjectClassName() { 323 return UserGroup.class.getName(); 324 } 325 326 329 public String getDBTableName() { 330 return TBL_USER; 331 } 332 333 336 public JoinConditions getInstanceJoinConditions( 337 String sObjectTag, 338 boolean bIsOuter) 339 throws DataStoreException { 340 return null; 342 } 343 344 347 public String getTagName() { 348 return TAG_USER; 349 } 350 351 354 public String getSalt() { 355 return this.m_sSalt; 356 } 357 358 361 362 365 protected void addDataToSave(InsertStatement insert) 366 throws DataStoreException { 367 368 insert.addColumnValue( 369 getInstanceColumnRef(CLMN_PASSWORD, isHistorical()), 370 this.m_sPassword); 371 372 int nSuper = 0; 373 374 if(m_bIsSuper == true) { 375 nSuper = 1; 376 } 377 378 insert.addColumnValue( 379 getInstanceColumnRef(CLMN_IS_SUPER, isHistorical()), 380 nSuper); 381 382 insert.addColumnValue(getInstanceColumnRef(CLMN_SALT, isHistorical()), 383 m_sSalt); 384 m_logger.log(Level.FINE, "Saving user " + m_sName + " with salt " + m_sSalt); 385 386 super.addDataToSave(insert); 387 } 388 389 392 protected void populateFromResultSetRow( 393 ResultSet rs, 394 SelectStatement select) 395 throws PopulateException { 396 397 if (isPopulated() == false) { 398 try { 399 ColumnRef colref = 400 getInstanceColumnRef(CLMN_PASSWORD, isHistorical()); 401 402 if (select.containsSelectColumn(colref) == true) { 403 String sTemp = null; 404 405 sTemp = rs.getString(select.getResultSetIndex(colref)); 406 407 if ((sTemp != null) && (sTemp.length() > 0)) { 408 if ((m_sPassword == null) 409 || (m_sPassword.length() == 0)) { 410 m_sPassword = sTemp; 411 } else if (m_sPassword.equals(sTemp) == false) { 412 setIsChanged(true); 413 } 414 } 415 416 } 417 418 colref = 419 getInstanceColumnRef(CLMN_IS_SUPER, isHistorical()); 420 421 if (select.containsSelectColumn(colref) == true) { 422 String sTemp = null; 423 424 m_bIsSuper = rs.getBoolean(select.getResultSetIndex(colref)); 425 426 } 427 428 colref = getInstanceColumnRef(CLMN_SALT, isHistorical()); 430 431 if (select.containsSelectColumn(colref) == true) { 432 m_sSalt = rs.getString(select.getResultSetIndex(colref)); 433 } 434 435 super.populateFromResultSetRow(rs, select); 436 } catch (SQLException e) { 437 throw new PopulateException( 438 "Error occured accessing password", 439 e); 440 } catch (DataStoreException e) { 441 throw new PopulateException( 442 "Error occured getting column ref", 443 e); 444 445 } 446 } 447 } 448 449 452 protected void saveNonCoreData() throws EditException { 453 } 455 456 459 460 469 private void populateIdFromUsernamePassword( 470 String sUserName, 471 String sPassword) 472 throws DataStoreException, ObjectNotFoundException { 473 ResultSet rs = null; 474 boolean bFound = false; 475 476 try { 477 SelectStatement newQuery = new SelectStatement(); 478 479 newQuery.addSelectColumn( 480 this.getInstanceColumnRef(ATTRIB_KEY, false)); 481 newQuery.addSelectColumn( 482 this.getInstanceColumnRef(ATTRIB_ID, false)); 483 484 newQuery.addWhereCondition( 485 this.getInstanceColumnRef(TAG_NAME, false), 486 "=", 487 sUserName); 488 newQuery.addWhereCondition( 489 this.getInstanceColumnRef(CLMN_PASSWORD, false), 490 "=", 491 sPassword); 492 newQuery.addWhereCondition( 493 this.getInstanceColumnRef(TAG_STATUS, false), 494 "=", 495 0); 496 497 rs = m_dsi.executeQuery(newQuery); 498 499 while (rs.next()) { 500 m_nObjectKey = rs.getInt(1); 501 m_nId = rs.getInt(2); 502 bFound = true; 503 } 504 505 if (bFound == false) { 506 rs.close(); 507 508 newQuery.clear(); 509 510 newQuery.addSelectColumn( 511 getInstanceColumnRef(ATTRIB_KEY, false)); 512 newQuery.addSelectColumn( 513 getInstanceColumnRef(ATTRIB_ID, false)); 514 515 newQuery.addWhereCondition( 516 getInstanceColumnRef(TAG_NAME, false), 517 "=", 518 sUserName); 519 newQuery.addWhereCondition( 520 getInstanceColumnRef(CLMN_PASSWORD, false), 521 "=", 522 sPassword); 523 524 rs = m_dsi.executeQuery(newQuery); 525 526 while (rs.next()) { 527 m_nObjectKey = rs.getInt(1); 528 m_nId = rs.getInt(2); 529 bFound = true; 530 } 531 } 532 533 if (bFound == false) { 534 throw new ObjectNotFoundException(); 535 } 536 } catch (SQLException e) { 537 throw new DataStoreException(e); 538 } finally { 539 if (rs != null) { 540 try { 541 rs.close(); 542 } catch (SQLException e) { 543 throw new DataStoreException(e); 544 } 545 } 546 } 547 } 548 549 552 protected void addColumnsToPopulateQuery( 553 SelectStatement select, 554 boolean bIsHist) 555 throws DataStoreException { 556 super.addColumnsToPopulateQuery(select, bIsHist); 557 select.addSelectColumn(getInstanceColumnRef(CLMN_PASSWORD, bIsHist)); 558 select.addSelectColumn(getInstanceColumnRef(CLMN_IS_SUPER, bIsHist)); 559 select.addSelectColumn(getInstanceColumnRef(CLMN_SALT, bIsHist)); 560 } 561 562 565 public void setName(String sName) throws InvalidNameException { 566 try { 567 if((m_sName != null && m_sName.equals(sName) == false && isNameValid(sName) == true) || isNameValid(sName) == true) { 568 super.setName(sName); 569 } else { 570 throw new InvalidNameException(sName + " is an invalid name for this user"); 571 } 572 } catch (DataStoreException e) { 573 throw new InvalidNameException("Error occured checking name validity", e); 574 } catch (SQLException e) { 575 throw new InvalidNameException("Error occured checking name validity", e); 576 } 577 578 } 579 580 584 private boolean isNameValid(String sName) throws DataStoreException, SQLException { 585 boolean bIsNameValid = true; 586 587 SelectStatement select = new SelectStatement(); 588 ResultSet rs = null; 589 590 try { 591 ColumnRef nameCol = getInstanceColumnRef(TAG_NAME,isHistorical()); 592 593 select.addSelectColumn(nameCol); 594 595 select.addWhereCondition(nameCol, "=", sName); 596 597 if(m_nId > 0) { 598 select.addWhereCondition(getInstanceColumnRef(ATTRIB_ID,isHistorical()), "!=", m_nId); 599 600 select.addWhereCondition(getInstanceColumnRef(TAG_LIVE_VERSION,isHistorical()), "!=", m_nId); 601 } 602 603 rs = m_dsi.execute(select); 604 605 if(rs.next()) { 606 bIsNameValid = false; 607 } 608 } finally { 609 if(rs != null) { 610 try { 611 rs.close(); 612 } catch (SQLException e) { 613 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e); 614 } 615 } 616 } 617 return bIsNameValid; 618 } 619 620 621 } | Popular Tags |