Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to allow global setting value management #107

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions src/EasyAbp.Abp.SettingUi.Application/SettingUiAppService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,9 @@ protected virtual Task SetSettingAsync(SettingDefinition setting, [CanBeNull] st
return _settingManager.SetGlobalAsync(setting.Name, value);
}

//Default
return _settingManager.SetForCurrentTenantAsync(setting.Name, value);
return ShouldManageAsGlobal(setting)
? _settingManager.SetGlobalAsync(setting.Name, value)
: _settingManager.SetForCurrentTenantAsync(setting.Name, value);
}

protected virtual IDictionary<string, IDictionary<string, string>> GetMergedSettingPropertiesAsync()
Expand Down Expand Up @@ -277,10 +278,7 @@ protected virtual async Task<SettingInfo> CreateSettingInfoAsync(SettingDefiniti
description = settingDefinition.Description.Localize(_factory);
}

/* Hide default/global value for tenants if the setting item is encrypted. */
var value = settingDefinition.IsEncrypted && CurrentTenant.IsAvailable
? await _settingManager.GetOrNullForCurrentTenantAsync(name, false)
: await SettingProvider.GetOrNullAsync(name);
var value = await GetSettingValueAsync(settingDefinition);

var si = new SettingInfo
{
Expand All @@ -299,5 +297,26 @@ protected virtual async Task<SettingInfo> CreateSettingInfoAsync(SettingDefiniti

return si;
}

protected virtual async Task<string> GetSettingValueAsync(SettingDefinition settingDefinition)
{
/* Hide default/global value for tenants if the setting item is encrypted. */
if (settingDefinition.IsEncrypted && CurrentTenant.IsAvailable)
{
return await _settingManager.GetOrNullForCurrentTenantAsync(settingDefinition.Name, false);
}

return ShouldManageAsGlobal(settingDefinition)
? await _settingManager.GetOrNullGlobalAsync(settingDefinition.Name)
: await SettingProvider.GetOrNullAsync(settingDefinition.Name);
}

protected virtual bool ShouldManageAsGlobal(SettingDefinition settingDefinition)
{
// todo: settingDefinition.Providers.Count != 0 can be improved.
return !CurrentTenant.IsAvailable &&
_options.ManageGlobalSettingsOnHostSide &&
settingDefinition.Providers.Count == 0;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,11 @@ public class AbpSettingUiOptions
/// If true, the default "Others" setting group (group1) will be unavailable, only grouped items can be used.
/// </summary>
public bool DisableDefaultGroup { get; set; }

/// <summary>
/// If true:
/// 1. On the host side, global setting values will replace host-side setting values for management.
/// 2. If the host setting value is different from the global setting value, it shows and manages the global one.
/// </summary>
public bool ManageGlobalSettingsOnHostSide { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ public async Task SettingValues_Should_Be_Set()
await _settingManager.Received().SetForCurrentTenantAsync("Test.Setting1", "value1");
await _settingManager.Received().SetForCurrentTenantAsync("Test.Setting2", "value2");
await _settingManager.DidNotReceive().SetForCurrentTenantAsync("RequestToken", "value3");

// Act
_optionsValue.ManageGlobalSettingsOnHostSide = true;
await _service.SetSettingValuesAsync(settingValues);

// Assert
await _settingManager.Received().SetGlobalAsync("Test.Setting1", "value1");
await _settingManager.Received().SetGlobalAsync("Test.Setting2", "value2");
await _settingManager.DidNotReceive().SetGlobalAsync("RequestToken", "value3");
}

[Fact]
Expand Down