001/* 002 * (C) Copyright 2006-2015 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * George Lefter 018 * Florent Guillaume 019 */ 020package org.nuxeo.ecm.directory.sql; 021 022import static org.nuxeo.ecm.directory.sql.SQLDirectory.TENANT_ID_FIELD; 023 024import java.io.Serializable; 025import java.sql.Connection; 026import java.sql.PreparedStatement; 027import java.sql.ResultSet; 028import java.sql.SQLException; 029import java.sql.Statement; 030import java.util.ArrayList; 031import java.util.Collections; 032import java.util.HashMap; 033import java.util.Iterator; 034import java.util.LinkedHashMap; 035import java.util.LinkedList; 036import java.util.List; 037import java.util.Map; 038import java.util.Map.Entry; 039import java.util.stream.Collectors; 040import java.util.Set; 041 042import org.apache.commons.lang.StringUtils; 043import org.apache.commons.logging.Log; 044import org.apache.commons.logging.LogFactory; 045import org.nuxeo.ecm.core.api.ConcurrentUpdateException; 046import org.nuxeo.ecm.core.api.DocumentModel; 047import org.nuxeo.ecm.core.api.DocumentModelList; 048import org.nuxeo.ecm.core.api.NuxeoPrincipal; 049import org.nuxeo.ecm.core.api.PropertyException; 050import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl; 051import org.nuxeo.ecm.core.api.local.ClientLoginModule; 052import org.nuxeo.ecm.core.api.model.Property; 053import org.nuxeo.ecm.core.api.security.SecurityConstants; 054import org.nuxeo.ecm.core.schema.types.Field; 055import org.nuxeo.ecm.core.storage.sql.ColumnSpec; 056import org.nuxeo.ecm.core.storage.sql.jdbc.JDBCLogger; 057import org.nuxeo.ecm.core.storage.sql.jdbc.db.Column; 058import org.nuxeo.ecm.core.storage.sql.jdbc.db.Delete; 059import org.nuxeo.ecm.core.storage.sql.jdbc.db.Insert; 060import org.nuxeo.ecm.core.storage.sql.jdbc.db.Select; 061import org.nuxeo.ecm.core.storage.sql.jdbc.db.Table; 062import org.nuxeo.ecm.core.storage.sql.jdbc.db.Update; 063import org.nuxeo.ecm.core.storage.sql.jdbc.dialect.Dialect; 064import org.nuxeo.ecm.core.utils.SIDGenerator; 065import org.nuxeo.ecm.directory.BaseSession; 066import org.nuxeo.ecm.directory.DirectoryException; 067import org.nuxeo.ecm.directory.EntrySource; 068import org.nuxeo.ecm.directory.OperationNotAllowedException; 069import org.nuxeo.ecm.directory.PasswordHelper; 070import org.nuxeo.ecm.directory.Reference; 071import org.nuxeo.ecm.directory.sql.filter.SQLComplexFilter; 072 073/** 074 * This class represents a session against an SQLDirectory. 075 */ 076public class SQLSession extends BaseSession { 077 078 private static final Log log = LogFactory.getLog(SQLSession.class); 079 080 // set to false for debugging 081 private static final boolean HIDE_PASSWORD_IN_LOGS = true; 082 083 final Table table; 084 085 private final boolean computeMultiTenantId; 086 087 protected SQLStaticFilter[] staticFilters; 088 089 String sid; 090 091 Connection sqlConnection; 092 093 private final Dialect dialect; 094 095 protected JDBCLogger logger = new JDBCLogger("SQLDirectory"); 096 097 public SQLSession(SQLDirectory directory, SQLDirectoryDescriptor config) throws DirectoryException { 098 super(directory, TableReference.class); 099 table = directory.getTable(); 100 dialect = directory.getDialect(); 101 sid = String.valueOf(SIDGenerator.next()); 102 staticFilters = config.getStaticFilters(); 103 computeMultiTenantId = config.isComputeMultiTenantId(); 104 acquireConnection(); 105 } 106 107 @Override 108 public SQLDirectory getDirectory() { 109 return (SQLDirectory) directory; 110 } 111 112 @Override 113 public DocumentModel getEntryFromSource(String id, boolean fetchReferences) throws DirectoryException { 114 acquireConnection(); 115 // String sql = String.format("SELECT * FROM %s WHERE %s = ?", 116 // tableName, idField); 117 Select select = new Select(table); 118 select.setFrom(table.getQuotedName()); 119 select.setWhat(getReadColumnsSQL()); 120 121 String whereClause = table.getPrimaryColumn().getQuotedName() + " = ?"; 122 whereClause = addFilterWhereClause(whereClause); 123 124 select.setWhere(whereClause); 125 String sql = select.getStatement(); 126 127 if (logger.isLogEnabled()) { 128 List<Serializable> values = new ArrayList<>(); 129 values.add(id); 130 addFilterValuesForLog(values); 131 logger.logSQL(sql, values); 132 } 133 134 PreparedStatement ps = null; 135 try { 136 ps = sqlConnection.prepareStatement(sql); 137 setFieldValue(ps, 1, table.getPrimaryColumn(), id); 138 addFilterValues(ps, 2); 139 140 Map<String, Object> fieldMap = new HashMap<>(); 141 try (ResultSet rs = ps.executeQuery()) { 142 if (!rs.next()) { 143 return null; 144 } 145 for (Column column : getReadColumns()) { 146 Object value = getFieldValue(rs, column); 147 fieldMap.put(column.getKey(), value); 148 } 149 if (logger.isLogEnabled()) { 150 logger.logResultSet(rs, getReadColumns()); 151 } 152 } 153 154 if (isMultiTenant()) { 155 // check that the entry is from the current tenant, or no tenant 156 // at all 157 String tenantId = getCurrentTenantId(); 158 if (!StringUtils.isBlank(tenantId)) { 159 String entryTenantId = (String) fieldMap.get(TENANT_ID_FIELD); 160 if (!StringUtils.isBlank(entryTenantId)) { 161 if (!entryTenantId.equals(tenantId)) { 162 return null; 163 } 164 } 165 } 166 } 167 168 DocumentModel entry = fieldMapToDocumentModel(fieldMap); 169 170 // fetch the reference fields 171 if (fetchReferences) { 172 Map<String, List<String>> targetIdsMap = new HashMap<>(); 173 for (Reference reference : directory.getReferences()) { 174 List<String> targetIds = reference.getTargetIdsForSource(entry.getId()); 175 targetIds = new ArrayList<>(targetIds); 176 Collections.sort(targetIds); 177 String fieldName = reference.getFieldName(); 178 if (targetIdsMap.containsKey(fieldName)) { 179 targetIdsMap.get(fieldName).addAll(targetIds); 180 } else { 181 targetIdsMap.put(fieldName, targetIds); 182 } 183 } 184 for (Entry<String, List<String>> en : targetIdsMap.entrySet()) { 185 String fieldName = en.getKey(); 186 List<String> targetIds = en.getValue(); 187 try { 188 entry.setProperty(schemaName, fieldName, targetIds); 189 } catch (PropertyException e) { 190 throw new DirectoryException(e); 191 } 192 } 193 } 194 return entry; 195 } catch (SQLException e) { 196 throw new DirectoryException("getEntry failed", e); 197 } finally { 198 try { 199 if (ps != null) { 200 ps.close(); 201 } 202 } catch (SQLException sqle) { 203 throw new DirectoryException(sqle); 204 } 205 } 206 } 207 208 protected List<Column> getReadColumns() { 209 return readAllColumns ? getDirectory().readColumnsAll : getDirectory().readColumns; 210 } 211 212 protected String getReadColumnsSQL() { 213 return readAllColumns ? getDirectory().readColumnsAllSQL : getDirectory().readColumnsSQL; 214 } 215 216 protected DocumentModel fieldMapToDocumentModel(Map<String, Object> fieldMap) { 217 String idFieldName = schemaFieldMap.get(getIdField()).getName().getPrefixedName(); 218 // If the prefixed id is not here, try to get without prefix 219 // It may happen when we gentry from sql 220 if (!fieldMap.containsKey(idFieldName)) { 221 idFieldName = getIdField(); 222 } 223 224 String id = String.valueOf(fieldMap.get(idFieldName)); 225 try { 226 DocumentModel docModel = BaseSession.createEntryModel(sid, schemaName, id, fieldMap, isReadOnly()); 227 return docModel; 228 } catch (PropertyException e) { 229 log.error(e, e); 230 return null; 231 } 232 } 233 234 private void acquireConnection() throws DirectoryException { 235 try { 236 if (sqlConnection == null || sqlConnection.isClosed()) { 237 sqlConnection = getDirectory().getConnection(); 238 } 239 } catch (SQLException e) { 240 throw new DirectoryException("Cannot connect to SQL directory '" + directory.getName() + "': " 241 + e.getMessage(), e); 242 } 243 } 244 245 /** 246 * Checks the SQL error we got and determine if a concurrent update happened. Throws if that's the case. 247 * 248 * @param e the exception 249 * @since 7.10-HF04, 8.2 250 */ 251 protected void checkConcurrentUpdate(Throwable e) throws ConcurrentUpdateException { 252 if (dialect.isConcurrentUpdateException(e)) { 253 throw new ConcurrentUpdateException(e); 254 } 255 } 256 257 protected String addFilterWhereClause(String whereClause) throws DirectoryException { 258 if (staticFilters.length == 0) { 259 return whereClause; 260 } 261 if (whereClause != null && whereClause.trim().length() > 0) { 262 whereClause = whereClause + " AND "; 263 } else { 264 whereClause = ""; 265 } 266 for (int i = 0; i < staticFilters.length; i++) { 267 SQLStaticFilter filter = staticFilters[i]; 268 whereClause += filter.getDirectoryColumn(table, getDirectory().useNativeCase()).getQuotedName(); 269 whereClause += " " + filter.getOperator() + " "; 270 whereClause += "? "; 271 272 if (i < staticFilters.length - 1) { 273 whereClause = whereClause + " AND "; 274 } 275 } 276 return whereClause; 277 } 278 279 protected void addFilterValues(PreparedStatement ps, int startIdx) throws DirectoryException { 280 for (int i = 0; i < staticFilters.length; i++) { 281 SQLStaticFilter filter = staticFilters[i]; 282 setFieldValue(ps, startIdx + i, filter.getDirectoryColumn(table, getDirectory().useNativeCase()), 283 filter.getValue()); 284 } 285 } 286 287 protected void addFilterValuesForLog(List<Serializable> values) { 288 for (int i = 0; i < staticFilters.length; i++) { 289 values.add(staticFilters[i].getValue()); 290 } 291 } 292 293 /** 294 * Internal method to read the hashed password for authentication. 295 * 296 * @since 9.1 297 */ 298 protected String getPassword(String id) { 299 acquireConnection(); 300 301 Select select = new Select(table); 302 select.setFrom(table.getQuotedName()); 303 List<Column> whatColumns = new ArrayList<>(2); 304 whatColumns.add(table.getColumn(getPasswordField())); 305 if (isMultiTenant()) { 306 whatColumns.add(table.getColumn(TENANT_ID_FIELD)); 307 } 308 String what = whatColumns.stream().map(Column::getQuotedName).collect(Collectors.joining(", ")); 309 select.setWhat(what); 310 String whereClause = table.getPrimaryColumn().getQuotedName() + " = ?"; 311 whereClause = addFilterWhereClause(whereClause); 312 select.setWhere(whereClause); 313 String sql = select.getStatement(); 314 315 if (logger.isLogEnabled()) { 316 List<Serializable> values = new ArrayList<>(); 317 values.add(id); 318 addFilterValuesForLog(values); 319 logger.logSQL(sql, values); 320 } 321 322 try (PreparedStatement ps = sqlConnection.prepareStatement(sql)) { 323 setFieldValue(ps, 1, table.getPrimaryColumn(), id); 324 addFilterValues(ps, 2); 325 try (ResultSet rs = ps.executeQuery()) { 326 if (!rs.next()) { 327 return null; 328 } 329 if (isMultiTenant()) { 330 // check that the entry is from the current tenant, or no tenant at all 331 String tenantId = getCurrentTenantId(); 332 if (!StringUtils.isBlank(tenantId)) { 333 String entryTenantId = (String) getFieldValue(rs, table.getColumn(TENANT_ID_FIELD)); 334 if (!StringUtils.isBlank(entryTenantId)) { 335 if (!entryTenantId.equals(tenantId)) { 336 return null; 337 } 338 } 339 } 340 } 341 String password = (String) getFieldValue(rs, table.getColumn(getPasswordField())); 342 if (logger.isLogEnabled()) { 343 String value = HIDE_PASSWORD_IN_LOGS ? "********" : password; 344 logger.logMap(Collections.singletonMap(getPasswordField(), value)); 345 } 346 return password; 347 } 348 } catch (SQLException e) { 349 throw new DirectoryException("getPassword failed", e); 350 } 351 } 352 353 @Override 354 public void deleteEntry(String id) { 355 acquireConnection(); 356 if (!canDeleteMultiTenantEntry(id)) { 357 throw new OperationNotAllowedException("Operation not allowed in the current tenant context", 358 "label.directory.error.multi.tenant.operationNotAllowed", null); 359 } 360 super.deleteEntry(id); 361 } 362 363 protected boolean canDeleteMultiTenantEntry(String entryId) throws DirectoryException { 364 if (isMultiTenant()) { 365 // can only delete entry from the current tenant 366 String tenantId = getCurrentTenantId(); 367 if (!StringUtils.isBlank(tenantId)) { 368 DocumentModel entry = getEntry(entryId); 369 String entryTenantId = (String) entry.getProperty(schemaName, TENANT_ID_FIELD); 370 if (StringUtils.isBlank(entryTenantId) || !entryTenantId.equals(tenantId)) { 371 if (log.isDebugEnabled()) { 372 log.debug(String.format("Trying to delete entry '%s' not part of current tenant '%s'", entryId, 373 tenantId)); 374 } 375 return false; 376 } 377 } 378 } 379 return true; 380 } 381 382 @Override 383 public void deleteEntry(String id, Map<String, String> map) throws DirectoryException { 384 checkPermission(SecurityConstants.WRITE); 385 acquireConnection(); 386 387 if (!canDeleteMultiTenantEntry(id)) { 388 throw new DirectoryException("Operation not allowed in the current tenant context"); 389 } 390 391 // Assume in this case that there are no References to this entry. 392 PreparedStatement ps = null; 393 try { 394 Delete delete = new Delete(table); 395 StringBuilder whereClause = new StringBuilder(); 396 List<Serializable> values = new ArrayList<>(1 + map.size()); 397 398 whereClause.append(table.getPrimaryColumn().getQuotedName()); 399 whereClause.append(" = ?"); 400 values.add(id); 401 for (Entry<String, String> e : map.entrySet()) { 402 String key = e.getKey(); 403 String value = e.getValue(); 404 whereClause.append(" AND "); 405 Column col = table.getColumn(key); 406 if (col == null) { 407 throw new IllegalArgumentException("Unknown column " + key); 408 } 409 whereClause.append(col.getQuotedName()); 410 if (value == null) { 411 whereClause.append(" IS NULL"); 412 } else { 413 whereClause.append(" = ?"); 414 values.add(value); 415 } 416 } 417 delete.setWhere(whereClause.toString()); 418 String sql = delete.getStatement(); 419 420 if (logger.isLogEnabled()) { 421 logger.logSQL(sql, values); 422 } 423 424 ps = sqlConnection.prepareStatement(sql); 425 for (int i = 0; i < values.size(); i++) { 426 if (i == 0) { 427 setFieldValue(ps, 1, table.getPrimaryColumn(), values.get(i)); 428 } else { 429 ps.setString(1 + i, (String) values.get(i)); 430 } 431 } 432 ps.execute(); 433 } catch (SQLException e) { 434 checkConcurrentUpdate(e); 435 throw new DirectoryException("deleteEntry failed", e); 436 } finally { 437 try { 438 if (ps != null) { 439 ps.close(); 440 } 441 } catch (SQLException sqle) { 442 throw new DirectoryException(sqle); 443 } 444 } 445 getDirectory().invalidateCaches(); 446 } 447 448 @Override 449 public DocumentModelList query(Map<String, Serializable> filter, Set<String> fulltext, Map<String, String> orderBy, 450 boolean fetchReferences) { 451 return query(filter, fulltext, orderBy, fetchReferences, -1, -1); 452 } 453 454 @Override 455 public DocumentModelList query(Map<String, Serializable> filter, Set<String> fulltext, Map<String, String> orderBy, 456 boolean fetchReferences, int limit, int offset) throws DirectoryException { 457 if (!hasPermission(SecurityConstants.READ)) { 458 return new DocumentModelListImpl(); 459 } 460 acquireConnection(); 461 Map<String, Object> filterMap = new LinkedHashMap<>(filter); 462 filterMap.remove(getPasswordField()); // cannot filter on password 463 464 if (isMultiTenant()) { 465 // filter entries on the tenantId field also 466 String tenantId = getCurrentTenantId(); 467 if (!StringUtils.isBlank(tenantId)) { 468 filterMap.put(TENANT_ID_FIELD, tenantId); 469 } 470 } 471 472 try { 473 // build count query statement 474 StringBuilder whereClause = new StringBuilder(); 475 String separator = ""; 476 List<Column> orderedColumns = new LinkedList<>(); 477 for (String columnName : filterMap.keySet()) { 478 479 if (getDirectory().isReference(columnName)) { 480 log.warn(columnName + " is a reference and will be ignored" + " as a query criterion"); 481 continue; 482 } 483 484 Object value = filterMap.get(columnName); 485 Column column = table.getColumn(columnName); 486 if (null == column) { 487 // this might happen if we have a case like a chain 488 // selection and a directory without parent column 489 throw new DirectoryException("cannot find column '" + columnName + "' for table: " + table); 490 } 491 String leftSide = column.getQuotedName(); 492 String rightSide = "?"; 493 String operator; 494 boolean substring = fulltext != null && fulltext.contains(columnName); 495 if ("".equals(value) && dialect.hasNullEmptyString() && !substring) { 496 // see NXP-6172, empty values are Null in Oracle 497 value = null; 498 } 499 if (value != null) { 500 if (value instanceof SQLComplexFilter) { 501 SQLComplexFilter complexFilter = (SQLComplexFilter) value; 502 operator = complexFilter.getOperator(); 503 rightSide = complexFilter.getRightSide(); 504 } else if (substring) { 505 // NB : remove double % in like query NXGED-833 506 String searchedValue = null; 507 switch (substringMatchType) { 508 case subany: 509 searchedValue = '%' + String.valueOf(value).toLowerCase() + '%'; 510 break; 511 case subinitial: 512 searchedValue = String.valueOf(value).toLowerCase() + '%'; 513 break; 514 case subfinal: 515 searchedValue = '%' + String.valueOf(value).toLowerCase(); 516 break; 517 } 518 filterMap.put(columnName, searchedValue); 519 if (dialect.supportsIlike()) { 520 operator = " ILIKE "; // postgresql rules 521 } else { 522 leftSide = "LOWER(" + leftSide + ')'; 523 operator = " LIKE "; 524 } 525 } else { 526 operator = " = "; 527 } 528 } else { 529 operator = " IS NULL"; 530 } 531 whereClause.append(separator).append(leftSide).append(operator); 532 if (value != null) { 533 whereClause.append(rightSide); 534 orderedColumns.add(column); 535 } 536 separator = " AND "; 537 } 538 539 int queryLimitSize = getDirectory().getDescriptor().getQuerySizeLimit(); 540 boolean trucatedResults = false; 541 if (queryLimitSize != 0 && (limit <= 0 || limit > queryLimitSize)) { 542 PreparedStatement ps = null; 543 try { 544 // create a preparedStatement for counting and bind the 545 // values 546 // String countQuery = new StringBuilder("SELECT count(*) 547 // FROM ") 548 // .append(table.getQuotedName(dialect)).append(whereClause).toString(); 549 Select select = new Select(table); 550 select.setWhat("count(*)"); 551 select.setFrom(table.getQuotedName()); 552 553 String where = whereClause.toString(); 554 where = addFilterWhereClause(where); 555 select.setWhere(where); 556 557 String countQuery = select.getStatement(); 558 if (logger.isLogEnabled()) { 559 List<Serializable> values = new ArrayList<>(orderedColumns.size()); 560 for (Column column : orderedColumns) { 561 Object value = filterMap.get(column.getKey()); 562 values.add((Serializable) value); 563 } 564 addFilterValuesForLog(values); 565 logger.logSQL(countQuery, values); 566 } 567 ps = sqlConnection.prepareStatement(countQuery); 568 fillPreparedStatementFields(filterMap, orderedColumns, ps); 569 570 int count; 571 try (ResultSet rs = ps.executeQuery()) { 572 rs.next(); 573 count = rs.getInt(1); 574 } 575 if (logger.isLogEnabled()) { 576 logger.logCount(count); 577 } 578 if (count > queryLimitSize) { 579 trucatedResults = true; 580 limit = queryLimitSize; 581 log.error("Displayed results will be truncated because too many rows in result: " + count); 582 // throw new SizeLimitExceededException("too many rows in result: " + count); 583 } 584 } finally { 585 if (ps != null) { 586 ps.close(); 587 } 588 } 589 } 590 591 // create a preparedStatement and bind the values 592 // String query = new StringBuilder("SELECT * FROM 593 // ").append(tableName).append( 594 // whereClause).toString(); 595 596 Select select = new Select(table); 597 select.setWhat(getReadColumnsSQL()); 598 select.setFrom(table.getQuotedName()); 599 600 String where = whereClause.toString(); 601 where = addFilterWhereClause(where); 602 select.setWhere(where); 603 604 StringBuilder orderby = new StringBuilder(128); 605 if (orderBy != null) { 606 for (Iterator<Map.Entry<String, String>> it = orderBy.entrySet().iterator(); it.hasNext();) { 607 Entry<String, String> entry = it.next(); 608 orderby.append(dialect.openQuote()) 609 .append(entry.getKey()) 610 .append(dialect.closeQuote()) 611 .append(' ') 612 .append(entry.getValue()); 613 if (it.hasNext()) { 614 orderby.append(','); 615 } 616 } 617 } 618 select.setOrderBy(orderby.toString()); 619 String query = select.getStatement(); 620 boolean manualLimitOffset; 621 if (limit <= 0) { 622 manualLimitOffset = false; 623 } else { 624 if (offset < 0) { 625 offset = 0; 626 } 627 if (dialect.supportsPaging()) { 628 query = dialect.addPagingClause(query, limit, offset); 629 manualLimitOffset = false; 630 } else { 631 manualLimitOffset = true; 632 } 633 } 634 635 if (logger.isLogEnabled()) { 636 List<Serializable> values = new ArrayList<>(orderedColumns.size()); 637 for (Column column : orderedColumns) { 638 Object value = filterMap.get(column.getKey()); 639 values.add((Serializable) value); 640 } 641 addFilterValuesForLog(values); 642 logger.logSQL(query, values); 643 } 644 645 try (PreparedStatement ps = sqlConnection.prepareStatement(query)) { 646 fillPreparedStatementFields(filterMap, orderedColumns, ps); 647 648 // execute the query and create a documentModel list 649 ResultSet rs = ps.executeQuery(); 650 DocumentModelList list = new DocumentModelListImpl(); 651 while (rs.next()) { 652 653 // fetch values for stored fields 654 Map<String, Object> map = new HashMap<>(); 655 for (Column column : getReadColumns()) { 656 Object o = getFieldValue(rs, column); 657 map.put(column.getKey(), o); 658 } 659 660 DocumentModel docModel = fieldMapToDocumentModel(map); 661 662 // fetch the reference fields 663 if (fetchReferences) { 664 Map<String, List<String>> targetIdsMap = new HashMap<>(); 665 for (Reference reference : directory.getReferences()) { 666 List<String> targetIds = reference.getTargetIdsForSource(docModel.getId()); 667 String fieldName = reference.getFieldName(); 668 if (targetIdsMap.containsKey(fieldName)) { 669 targetIdsMap.get(fieldName).addAll(targetIds); 670 } else { 671 targetIdsMap.put(fieldName, targetIds); 672 } 673 } 674 for (Entry<String, List<String>> en : targetIdsMap.entrySet()) { 675 String fieldName = en.getKey(); 676 List<String> targetIds = en.getValue(); 677 docModel.setProperty(schemaName, fieldName, targetIds); 678 } 679 } 680 list.add(docModel); 681 } 682 rs.close(); 683 if (manualLimitOffset) { 684 int totalSize = list.size(); 685 if (offset > 0) { 686 if (offset >= totalSize) { 687 list = new DocumentModelListImpl(); 688 } else { 689 list = new DocumentModelListImpl(list.subList(offset, totalSize)); 690 } 691 } 692 if (list.size() > limit) { // list.size() not totalSize, we may have an offset already 693 list = new DocumentModelListImpl(list.subList(0, limit)); 694 } 695 ((DocumentModelListImpl) list).setTotalSize(totalSize); 696 } 697 if (trucatedResults) { 698 ((DocumentModelListImpl) list).setTotalSize(-2); 699 } 700 return list; 701 } 702 703 } catch (SQLException e) { 704 try { 705 sqlConnection.close(); 706 } catch (SQLException e1) { 707 } 708 throw new DirectoryException("query failed", e); 709 } 710 } 711 712 @Override 713 protected DocumentModel createEntryWithoutReferences(Map<String, Object> fieldMap) { 714 Field schemaIdField = schemaFieldMap.get(getIdField()); 715 716 String idFieldName = schemaIdField.getName().getPrefixedName(); 717 718 acquireConnection(); 719 if (autoincrementId) { 720 fieldMap.remove(idFieldName); 721 } else { 722 // check id that was given 723 Object rawId = fieldMap.get(idFieldName); 724 if (rawId == null) { 725 throw new DirectoryException("Missing id"); 726 } 727 String id = String.valueOf(rawId); 728 if (hasEntry(id)) { 729 throw new DirectoryException(String.format("Entry with id %s already exists", id)); 730 } 731 732 if (isMultiTenant()) { 733 String tenantId = getCurrentTenantId(); 734 if (!StringUtils.isBlank(tenantId)) { 735 fieldMap.put(TENANT_ID_FIELD, tenantId); 736 if (computeMultiTenantId) { 737 fieldMap.put(idFieldName, computeMultiTenantDirectoryId(tenantId, id)); 738 } 739 } 740 } 741 } 742 743 List<Column> columnList = new ArrayList<>(table.getColumns()); 744 Column idColumn = null; 745 for (Iterator<Column> i = columnList.iterator(); i.hasNext();) { 746 Column column = i.next(); 747 if (column.isIdentity()) { 748 idColumn = column; 749 } 750 String prefixedName = schemaFieldMap.get(column.getKey()).getName().getPrefixedName(); 751 752 if (!fieldMap.containsKey(prefixedName)) { 753 Field prefixedField = schemaFieldMap.get(prefixedName); 754 if (prefixedField != null && prefixedField.getDefaultValue() != null) { 755 fieldMap.put(prefixedName, prefixedField.getDefaultValue()); 756 } else { 757 i.remove(); 758 } 759 } 760 } 761 Insert insert = new Insert(table); 762 for (Column column : columnList) { 763 insert.addColumn(column); 764 } 765 // needed for Oracle for empty map insert 766 insert.addIdentityColumn(idColumn); 767 String sql = insert.getStatement(); 768 769 if (logger.isLogEnabled()) { 770 List<Serializable> values = new ArrayList<>(columnList.size()); 771 for (Column column : columnList) { 772 String prefixField = schemaFieldMap.get(column.getKey()).getName().getPrefixedName(); 773 Object value = fieldMap.get(prefixField); 774 Serializable v; 775 if (HIDE_PASSWORD_IN_LOGS && column.getKey().equals(getPasswordField())) { 776 v = "********"; // hide password in logs 777 } else { 778 v = fieldValueForWrite(value, column); 779 } 780 values.add(v); 781 } 782 logger.logSQL(sql, values); 783 } 784 785 DocumentModel entry; 786 PreparedStatement ps = null; 787 Statement st = null; 788 try { 789 if (autoincrementId && dialect.hasIdentityGeneratedKey()) { 790 ps = sqlConnection.prepareStatement(sql, new String[] { getIdField() }); 791 } else { 792 ps = sqlConnection.prepareStatement(sql); 793 } 794 int index = 1; 795 for (Column column : columnList) { 796 String prefixField = schemaFieldMap.get(column.getKey()).getName().getPrefixedName(); 797 Object value = fieldMap.get(prefixField); 798 setFieldValue(ps, index, column, value); 799 index++; 800 } 801 ps.execute(); 802 if (autoincrementId) { 803 Column column = table.getColumn(getIdField()); 804 ResultSet rs; 805 if (dialect.hasIdentityGeneratedKey()) { 806 rs = ps.getGeneratedKeys(); 807 } else { 808 // needs specific statements 809 sql = dialect.getIdentityGeneratedKeySql(column); 810 st = sqlConnection.createStatement(); 811 rs = st.executeQuery(sql); 812 } 813 if (!rs.next()) { 814 throw new DirectoryException("Cannot get generated key"); 815 } 816 if (logger.isLogEnabled()) { 817 logger.logResultSet(rs, Collections.singletonList(column)); 818 } 819 Serializable rawId = column.getFromResultSet(rs, 1); 820 fieldMap.put(idFieldName, rawId); 821 rs.close(); 822 } 823 entry = fieldMapToDocumentModel(fieldMap); 824 } catch (SQLException e) { 825 checkConcurrentUpdate(e); 826 throw new DirectoryException("createEntry failed", e); 827 } finally { 828 try { 829 if (ps != null) { 830 ps.close(); 831 } 832 if (st != null) { 833 st.close(); 834 } 835 } catch (SQLException sqle) { 836 throw new DirectoryException(sqle); 837 } 838 } 839 840 return entry; 841 } 842 843 @Override 844 protected List<String> updateEntryWithoutReferences(DocumentModel docModel) throws DirectoryException { 845 acquireConnection(); 846 List<Column> storedColumnList = new LinkedList<>(); 847 List<String> referenceFieldList = new LinkedList<>(); 848 849 if (isMultiTenant()) { 850 // can only update entry from the current tenant 851 String tenantId = getCurrentTenantId(); 852 if (!StringUtils.isBlank(tenantId)) { 853 String entryTenantId = (String) docModel.getProperty(schemaName, TENANT_ID_FIELD); 854 if (StringUtils.isBlank(entryTenantId) || !entryTenantId.equals(tenantId)) { 855 if (log.isDebugEnabled()) { 856 log.debug(String.format("Trying to update entry '%s' not part of current tenant '%s'", 857 docModel.getId(), tenantId)); 858 } 859 throw new OperationNotAllowedException("Operation not allowed in the current tenant context", 860 "label.directory.error.multi.tenant.operationNotAllowed", null); 861 } 862 } 863 } 864 865 // collect fields to update 866 for (String fieldName : schemaFieldMap.keySet()) { 867 if (fieldName.equals(getIdField())) { 868 continue; 869 } 870 Property prop = docModel.getPropertyObject(schemaName, fieldName); 871 if (!prop.isDirty()) { 872 continue; 873 } 874 if (fieldName.equals(getPasswordField()) && StringUtils.isEmpty((String) prop.getValue())) { 875 // null/empty password means unchanged 876 continue; 877 } 878 if (getDirectory().isReference(fieldName)) { 879 referenceFieldList.add(fieldName); 880 } else { 881 storedColumnList.add(table.getColumn(fieldName)); 882 } 883 } 884 885 if (!storedColumnList.isEmpty()) { 886 // update stored fields 887 // String whereString = StringUtils.join( 888 // storedFieldPredicateList.iterator(), ", "); 889 // String sql = String.format("UPDATE %s SET %s WHERE %s = ?", 890 // tableName, whereString, 891 // primaryColumn); 892 893 Update update = new Update(table); 894 update.setUpdatedColumns(storedColumnList); 895 String whereString = table.getPrimaryColumn().getQuotedName() + " = ?"; 896 update.setWhere(whereString); 897 String sql = update.getStatement(); 898 899 if (logger.isLogEnabled()) { 900 List<Serializable> values = new ArrayList<>(storedColumnList.size()); 901 for (Column column : storedColumnList) { 902 Object value = docModel.getProperty(schemaName, column.getKey()); 903 if (HIDE_PASSWORD_IN_LOGS && column.getKey().equals(getPasswordField())) { 904 value = "********"; // hide password in logs 905 } 906 values.add((Serializable) value); 907 } 908 values.add(docModel.getId()); 909 logger.logSQL(sql, values); 910 } 911 912 PreparedStatement ps = null; 913 try { 914 ps = sqlConnection.prepareStatement(sql); 915 916 int index = 1; 917 // TODO: how can I reset dirty fields? 918 for (Column column : storedColumnList) { 919 Object value = docModel.getProperty(schemaName, column.getKey()); 920 setFieldValue(ps, index, column, value); 921 index++; 922 } 923 setFieldValue(ps, index, table.getPrimaryColumn(), docModel.getId()); 924 ps.execute(); 925 } catch (SQLException e) { 926 checkConcurrentUpdate(e); 927 throw new DirectoryException("updateEntry failed for " + docModel.getId(), e); 928 } finally { 929 try { 930 if (ps != null) { 931 ps.close(); 932 } 933 } catch (SQLException sqle) { 934 throw new DirectoryException(sqle); 935 } 936 } 937 } 938 939 return referenceFieldList; 940 } 941 942 @Override 943 public void deleteEntryWithoutReferences(String id) throws DirectoryException { 944 // second step: clean stored fields 945 PreparedStatement ps = null; 946 try { 947 Delete delete = new Delete(table); 948 String whereString = table.getPrimaryColumn().getQuotedName() + " = ?"; 949 delete.setWhere(whereString); 950 String sql = delete.getStatement(); 951 if (logger.isLogEnabled()) { 952 logger.logSQL(sql, Collections.singleton(id)); 953 } 954 ps = sqlConnection.prepareStatement(sql); 955 setFieldValue(ps, 1, table.getPrimaryColumn(), id); 956 ps.execute(); 957 } catch (SQLException e) { 958 checkConcurrentUpdate(e); 959 throw new DirectoryException("deleteEntry failed", e); 960 } finally { 961 try { 962 if (ps != null) { 963 ps.close(); 964 } 965 } catch (SQLException sqle) { 966 throw new DirectoryException(sqle); 967 } 968 } 969 } 970 971 protected void fillPreparedStatementFields(Map<String, Object> filterMap, List<Column> orderedColumns, 972 PreparedStatement ps) throws DirectoryException { 973 int index = 1; 974 for (Column column : orderedColumns) { 975 Object value = filterMap.get(column.getKey()); 976 977 if (value instanceof SQLComplexFilter) { 978 index = ((SQLComplexFilter) value).setFieldValue(ps, index, column); 979 } else { 980 setFieldValue(ps, index, column, value); 981 index++; 982 } 983 } 984 addFilterValues(ps, index); 985 } 986 987 private Object getFieldValue(ResultSet rs, Column column) throws DirectoryException { 988 try { 989 int index = rs.findColumn(column.getPhysicalName()); 990 return column.getFromResultSet(rs, index); 991 } catch (SQLException e) { 992 throw new DirectoryException("getFieldValue failed", e); 993 } 994 } 995 996 private void setFieldValue(PreparedStatement ps, int index, Column column, Object value) throws DirectoryException { 997 try { 998 column.setToPreparedStatement(ps, index, fieldValueForWrite(value, column)); 999 } catch (SQLException e) { 1000 throw new DirectoryException("setFieldValue failed", e); 1001 } 1002 } 1003 1004 protected Serializable fieldValueForWrite(Object value, Column column) { 1005 ColumnSpec spec = column.getType().spec; 1006 if (value instanceof String) { 1007 if (spec == ColumnSpec.LONG || spec == ColumnSpec.AUTOINC) { 1008 // allow storing string into integer/long key 1009 return Long.valueOf((String) value); 1010 } 1011 if (column.getKey().equals(getPasswordField())) { 1012 // hash password if not already hashed 1013 String password = (String) value; 1014 if (!PasswordHelper.isHashed(password)) { 1015 password = PasswordHelper.hashPassword(password, passwordHashAlgorithm); 1016 } 1017 return password; 1018 } 1019 } else if (value instanceof Number) { 1020 if (spec == ColumnSpec.LONG || spec == ColumnSpec.AUTOINC) { 1021 // canonicalize to Long 1022 if (value instanceof Integer) { 1023 return Long.valueOf(((Integer) value).longValue()); 1024 } 1025 } else if (spec == ColumnSpec.STRING) { 1026 // allow storing number in string field 1027 return value.toString(); 1028 } 1029 } 1030 return (Serializable) value; 1031 } 1032 1033 @Override 1034 public void close() throws DirectoryException { 1035 try { 1036 if (!sqlConnection.isClosed()) { 1037 sqlConnection.close(); 1038 } 1039 } catch (SQLException e) { 1040 throw new DirectoryException("close failed", e); 1041 } finally { 1042 getDirectory().removeSession(this); 1043 } 1044 } 1045 1046 /** 1047 * Enable connection status checking on SQL directory connections 1048 * 1049 * @since 5.7.2 1050 */ 1051 public boolean isLive() throws DirectoryException { 1052 try { 1053 return !sqlConnection.isClosed(); 1054 } catch (SQLException e) { 1055 throw new DirectoryException("Cannot check connection status of " + this, e); 1056 } 1057 } 1058 1059 @Override 1060 public boolean authenticate(String username, String password) { 1061 String storedPassword = getPassword(username); 1062 return PasswordHelper.verifyPassword(password, storedPassword); 1063 } 1064 1065 @Override 1066 public boolean isAuthenticating() { 1067 return schemaFieldMap.containsKey(getPasswordField()); 1068 } 1069 1070 @Override 1071 public boolean hasEntry(String id) { 1072 acquireConnection(); 1073 Select select = new Select(table); 1074 select.setFrom(table.getQuotedName()); 1075 select.setWhat("1"); 1076 select.setWhere(table.getPrimaryColumn().getQuotedName() + " = ?"); 1077 String sql = select.getStatement(); 1078 1079 if (logger.isLogEnabled()) { 1080 logger.logSQL(sql, Collections.singleton(id)); 1081 } 1082 1083 PreparedStatement ps = null; 1084 ResultSet rs = null; 1085 try { 1086 ps = sqlConnection.prepareStatement(sql); 1087 setFieldValue(ps, 1, table.getPrimaryColumn(), id); 1088 rs = ps.executeQuery(); 1089 boolean has = rs.next(); 1090 if (logger.isLogEnabled()) { 1091 logger.logCount(has ? 1 : 0); 1092 } 1093 return has; 1094 } catch (SQLException e) { 1095 throw new DirectoryException("hasEntry failed", e); 1096 } finally { 1097 try { 1098 if (rs != null) { 1099 rs.close(); 1100 } 1101 if (ps != null) { 1102 ps.close(); 1103 } 1104 } catch (SQLException sqle) { 1105 throw new DirectoryException(sqle); 1106 } 1107 } 1108 } 1109 1110 /** 1111 * Returns {@code true} if this directory supports multi tenancy, {@code false} otherwise. 1112 */ 1113 protected boolean isMultiTenant() { 1114 return directory.isMultiTenant(); 1115 } 1116 1117 /** 1118 * Returns the tenant id of the logged user if any, {@code null} otherwise. 1119 */ 1120 protected String getCurrentTenantId() { 1121 NuxeoPrincipal principal = ClientLoginModule.getCurrentPrincipal(); 1122 return principal != null ? principal.getTenantId() : null; 1123 } 1124 1125 @Override 1126 public String toString() { 1127 return "SQLSession [directory=" + directory.getName() + ", sid=" + sid + "]"; 1128 } 1129 1130}