Skip to content

Commit

Permalink
Merge pull request #107 from EasyAbp/manage-global-values
Browse files Browse the repository at this point in the history
Add an option to allow global setting value management
  • Loading branch information
gdlcf88 authored Feb 8, 2024
2 parents cfc2b52 + bd53003 commit c0c0551
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
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

0 comments on commit c0c0551

Please sign in to comment.