Skip to content

Commit

Permalink
refactor: system and user settings as value objects [DHIS2-18061] (#1…
Browse files Browse the repository at this point in the history
…8649)

* refactor: user and system settings as immutable value objects [DHIS2-18061]

* refactor: UserSettingKey gone

* fix: compiles again

* chore: code format

* refactor: unify naming

* chore: code format

* chore: delete unused class

* refactor: implement settings and tests

* fix: value conversions

* fix: empty settings init and translations without user context

* fix: dependencies and user settings access

* fix: mock test setup

* fix: mock test setup

* fix: mock test setup

* fix: mock test setup

* fix: mock test setup

* fix: hibernate mapping requirements

* fix: system settings persistence layer

* fix: maven dependencies

* fix: flyway script - decryption

* fix: settings persistence and serialisation

* fix: tests - user setting value persistence

* fix: system settings API

* fix: ignore empty settings in LazySettings set

* chore: rename service methods to reflect map character

* refactor: UserSettings no longer in UserDetails

* fix: settings to JSON enums and session event listener

* fix: AnalyticsServiceTest asserts

* fix: H2 integration tests

* fix: failing tests

* fix: revert exception change

* fixed: analytics system settings usage null cases

* fix: mock tests

* fix: settings vs settings translations API and e2e tests

* chore: rename

* chore: extracted ThreadUserSettings to manage the per thread state

* fix: imports

* fix: mvn dependencies

* fix: use different classloader?

* fix: run events tests first

* fix: remaining integration tests

* fix: CodeQL issues

* chore: sonar issues
  • Loading branch information
jbee authored Oct 7, 2024
1 parent 84c0520 commit 6e7d208
Show file tree
Hide file tree
Showing 283 changed files with 5,054 additions and 5,314 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,8 @@ public interface CacheProvider {

<V> Cache<V> createCurrentUserGroupInfoCache();

<V> Cache<V> createUserSettingCache();

<V> Cache<V> createAttrOptionComboIdCache();

<V> Cache<V> createSystemSettingCache();

<V> Cache<V> createGoogleAccessTokenCache();

<V> Cache<V> createDataItemsPaginationCache();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@
import org.hisp.dhis.schema.annotation.PropertyTransformer;
import org.hisp.dhis.schema.transformer.UserPropertyTransformer;
import org.hisp.dhis.security.acl.Access;
import org.hisp.dhis.setting.UserSettings;
import org.hisp.dhis.translation.Translatable;
import org.hisp.dhis.translation.Translation;
import org.hisp.dhis.user.CurrentUserUtil;
import org.hisp.dhis.user.User;
import org.hisp.dhis.user.UserDetails;
import org.hisp.dhis.user.UserSettingKey;
import org.hisp.dhis.user.sharing.Sharing;
import org.hisp.dhis.user.sharing.UserAccess;
import org.hisp.dhis.user.sharing.UserGroupAccess;
Expand Down Expand Up @@ -334,7 +334,7 @@ public void setTranslations(Set<Translation> translations) {
* @return a translated value.
*/
protected String getTranslation(String translationKey, String defaultValue) {
Locale locale = CurrentUserUtil.getUserSetting(UserSettingKey.DB_LOCALE);
Locale locale = UserSettings.getCurrentSettings().getUserDbLocale();

final String defaultTranslation = defaultValue != null ? defaultValue.trim() : null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.hisp.dhis.schema.annotation.Gist;
import org.hisp.dhis.setting.UserSettings;
import org.hisp.dhis.translation.Translation;
import org.hisp.dhis.user.CurrentUserUtil;
import org.hisp.dhis.user.UserSettingKey;

/**
* Base class for translatable object.
Expand Down Expand Up @@ -95,7 +94,7 @@ public void setTranslations(Set<Translation> translations) {
* @return a translated value.
*/
protected String getTranslation(String translationKey, String defaultValue) {
Locale locale = CurrentUserUtil.getUserSetting(UserSettingKey.DB_LOCALE);
Locale locale = UserSettings.getCurrentSettings().getUserDbLocale();

final String defaultTranslation = defaultValue != null ? defaultValue.trim() : null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ public DatastoreEntry(String namespace, String key) {
this(namespace, key, null, false);
}

public DatastoreEntry(String namespace, String key, String value) {
this(namespace, key, value, false);
}

public DatastoreEntry(String namespace, String key, String value, boolean encrypted) {
this.namespace = namespace;
this.key = key;
Expand All @@ -83,14 +87,19 @@ public DatastoreEntry(String namespace, String key, String value, boolean encryp
@ToString.Include
@EqualsAndHashCode.Include
public String getValue() {
return encrypted ? getEncryptedValue() : getJbPlainValue();
return isEncryptedInternal() ? getEncryptedValue() : getJbPlainValue();
}

public String getJbPlainValue() {
return !encrypted && value != null ? value : jbPlainValue;
return !isEncryptedInternal() && value != null ? value : jbPlainValue;
}

public String getEncryptedValue() {
return encrypted && value != null ? value : encryptedValue;
return isEncryptedInternal() && value != null ? value : encryptedValue;
}

/** Note: this must use a name that is not also a hibernate mapped property */
private boolean isEncryptedInternal() {
return encrypted != null && encrypted;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import org.hisp.dhis.feedback.ForbiddenException;
import org.hisp.dhis.user.UserDetails;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.transaction.annotation.Transactional;

/**
* Datastore is a key-value store with namespaces to isolate different collections of key-value
Expand All @@ -56,7 +55,7 @@ public interface DatastoreService {

/**
* @param namespace to check
* @return the protection used or {@null} when unprotected
* @return the protection used or {@code null} when unprotected
*/
@CheckForNull
DatastoreNamespaceProtection getProtection(@Nonnull String namespace);
Expand Down Expand Up @@ -143,9 +142,10 @@ <T> T getEntries(DatastoreQuery query, Function<Stream<DatastoreFields>, T> tran
* @return the KeyJsonValue matching the key and namespace.
* @throws AccessDeniedException when user lacks authority for namespace
*/
@CheckForNull
DatastoreEntry getEntry(String namespace, String key) throws ForbiddenException;

@Transactional(readOnly = true)
@CheckForNull
DatastoreEntry getEntry(String namespace, String key, UserDetails user) throws ForbiddenException;

/**
Expand Down Expand Up @@ -191,14 +191,19 @@ void updateEntry(
*/
void deleteEntry(DatastoreEntry entry);

void deleteEntry(DatastoreEntry entry, UserDetails user);

/**
* Adds a new KeyJsonValue entry or updates the entry if the namespace and key already exists.
*
* @param entry the KeyJsonValue entry to be saved or updated.
* @param entry to be saved or updated
* @throws IllegalArgumentException when the entry value is not valid JSON
*/
void saveOrUpdateEntry(DatastoreEntry entry) throws BadRequestException, ForbiddenException;

void saveOrUpdateEntry(DatastoreEntry entry, UserDetails user)
throws BadRequestException, ForbiddenException;

/**
* Deletes all entries associated with a given namespace.
*
Expand Down
Loading

0 comments on commit 6e7d208

Please sign in to comment.