diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2021-08-18 11:08:54 -0500 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2021-08-19 20:08:16 +0000 |
commit | e6ee756424fd84eeb58af3407e604cd9d9b289ce (patch) | |
tree | 7f93b47d5892440e741adcbc5016c4d61cf35645 /server/sonar-db-dao/src/main/java/org/sonar/db/property | |
parent | 11a4c829eb81f895f3b19b892e002071b8608316 (diff) | |
download | sonarqube-e6ee756424fd84eeb58af3407e604cd9d9b289ce.tar.gz sonarqube-e6ee756424fd84eeb58af3407e604cd9d9b289ce.zip |
SONAR-15142 Audit internal properties
Diffstat (limited to 'server/sonar-db-dao/src/main/java/org/sonar/db/property')
3 files changed, 39 insertions, 7 deletions
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/property/InternalPropertiesDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/property/InternalPropertiesDao.java index bbb58c35a3c..5c79ec60cad 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/property/InternalPropertiesDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/property/InternalPropertiesDao.java @@ -36,6 +36,8 @@ import org.sonar.api.utils.System2; import org.sonar.api.utils.log.Loggers; import org.sonar.db.Dao; import org.sonar.db.DbSession; +import org.sonar.db.audit.AuditPersister; +import org.sonar.db.audit.model.PropertyNewValue; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkState; @@ -55,17 +57,23 @@ public class InternalPropertiesDao implements Dao { private static final Optional<String> OPTIONAL_OF_EMPTY_STRING = Optional.of(""); private final System2 system2; + private AuditPersister auditPersister; public InternalPropertiesDao(System2 system2) { this.system2 = system2; } + public InternalPropertiesDao(System2 system2, AuditPersister auditPersister) { + this.system2 = system2; + this.auditPersister = auditPersister; + } + /** * Save a property which value is not empty. * <p>Value can't be {@code null} but can have any size except 0.</p> - * + * * @throws IllegalArgumentException if {@code key} or {@code value} is {@code null} or empty. - * + * * @see #saveAsEmpty(DbSession, String) */ public void save(DbSession dbSession, String key, String value) { @@ -73,13 +81,21 @@ public class InternalPropertiesDao implements Dao { checkArgument(value != null && !value.isEmpty(), "value can't be null nor empty"); InternalPropertiesMapper mapper = getMapper(dbSession); - mapper.deleteByKey(key); + int deletedRows = mapper.deleteByKey(key); long now = system2.now(); if (mustsBeStoredInClob(value)) { mapper.insertAsClob(key, value, now); } else { mapper.insertAsText(key, value, now); } + + if (auditPersister != null && auditPersister.isTrackedProperty(key)) { + if (deletedRows > 0) { + auditPersister.updateProperty(dbSession, new PropertyNewValue(key, value), false); + } else { + auditPersister.addProperty(dbSession, new PropertyNewValue(key, value), false); + } + } } private static boolean mustsBeStoredInClob(String value) { @@ -93,12 +109,24 @@ public class InternalPropertiesDao implements Dao { checkKey(key); InternalPropertiesMapper mapper = getMapper(dbSession); - mapper.deleteByKey(key); + int deletedRows = mapper.deleteByKey(key); mapper.insertAsEmpty(key, system2.now()); + + if (auditPersister != null && auditPersister.isTrackedProperty(key)) { + if (deletedRows > 0) { + auditPersister.updateProperty(dbSession, new PropertyNewValue(key, ""), false); + } else { + auditPersister.addProperty(dbSession, new PropertyNewValue(key, ""), false); + } + } } public void delete(DbSession dbSession, String key) { - getMapper(dbSession).deleteByKey(key); + int deletedRows = getMapper(dbSession).deleteByKey(key); + + if (auditPersister != null && deletedRows > 0 && auditPersister.isTrackedProperty(key)) { + auditPersister.deleteProperty(dbSession, new PropertyNewValue(key), false); + } } /** diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/property/InternalPropertiesMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/property/InternalPropertiesMapper.java index 6116bcf129f..f4992ee2d2c 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/property/InternalPropertiesMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/property/InternalPropertiesMapper.java @@ -33,7 +33,7 @@ public interface InternalPropertiesMapper { void insertAsClob(@Param("key") String key, @Param("value") String value, @Param("createdAt") long createdAt); - void deleteByKey(@Param("key") String key); + int deleteByKey(@Param("key") String key); /** * Replace the value of the specified key, only if the existing value matches the expected old value. diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/property/PropertiesDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/property/PropertiesDao.java index 138c402fd47..522d7cdbf33 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/property/PropertiesDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/property/PropertiesDao.java @@ -210,6 +210,10 @@ public class PropertiesDao implements Dao { * * @throws IllegalArgumentException if {@link PropertyDto#getKey()} is {@code null} or empty */ + public void saveProperty(DbSession session, PropertyDto property) { + saveProperty(session, property, null, null, null, null); + } + public void saveProperty(DbSession session, PropertyDto property, @Nullable String userLogin, @Nullable String projectKey, @Nullable String projectName, @Nullable String qualifier) { int affectedRows = save(getMapper(session), property.getKey(), property.getUserUuid(), property.getComponentUuid(), property.getValue()); @@ -253,7 +257,7 @@ public class PropertiesDao implements Dao { public void saveProperty(PropertyDto property) { try (DbSession session = mybatis.openSession(false)) { - saveProperty(session, property, null, null, null, null); + saveProperty(session, property); session.commit(); } } |