Skip to content

Commit

Permalink
Merge pull request #104 from EasyAbp/hide-encrypted-value
Browse files Browse the repository at this point in the history
Hide default/global value for tenants if the setting item is encrypted
  • Loading branch information
gdlcf88 authored Jan 19, 2024
2 parents bc6084e + 2ae6d44 commit 1f933a9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
9 changes: 6 additions & 3 deletions src/EasyAbp.Abp.SettingUi.Application/SettingUiAppService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
using Volo.Abp.Localization;
using Volo.Abp.SettingManagement;
using Volo.Abp.Settings;
using Volo.Abp.Threading;
using Volo.Abp.VirtualFileSystem;

namespace EasyAbp.Abp.SettingUi
Expand Down Expand Up @@ -130,7 +129,7 @@ public virtual async Task SetSettingValuesAsync(Dictionary<string, string> setti
continue;
}

await SetSettingAsync(setting, kv.Value);
await SetSettingAsync(setting, kv.Value); // todo: needs permission check?
}
}

Expand Down Expand Up @@ -168,6 +167,7 @@ protected virtual IDictionary<string, IDictionary<string, string>> GetMergedSett
{
return _fileProvider
.GetDirectoryContents(SettingUiConst.SettingPropertiesFileFolder)
.Where(x => x.Name.EndsWith(".json"))
.Select(content =>
_jsonSerializer.Deserialize<IDictionary<string, IDictionary<string, string>>>(content.ReadAsString()))
.SelectMany(dict => dict)
Expand Down Expand Up @@ -260,7 +260,10 @@ protected virtual async Task<SettingInfo> CreateSettingInfoAsync(SettingDefiniti
description = settingDefinition.Description.Localize(_factory);
}

string value = await SettingProvider.GetOrNullAsync(name);
/* 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 si = new SettingInfo
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
using Shouldly;
using Volo.Abp.MultiTenancy;
using Volo.Abp.SettingManagement;
using Volo.Abp.Settings;
using Xunit;
Expand Down Expand Up @@ -34,7 +36,7 @@ protected override void AfterAddApplication(IServiceCollection services)
.WithProperty(SettingUiConst.Group2, "TestGroup2")
.WithProperty(SettingUiConst.Type, "number");
var setting2 = new SettingDefinition("Test.Setting2", "2");
var setting3 = new SettingDefinition("Test.Setting3", "3");
var setting3 = new SettingDefinition("Test.Setting3", "3", isEncrypted: true);
settingDefinitionManager.GetAllAsync().Returns(new List<SettingDefinition>
{
setting1,
Expand Down Expand Up @@ -121,5 +123,27 @@ public async Task SettingValues_Should_Be_Set()
await _settingManager.Received().SetForCurrentTenantAsync("Test.Setting2", "value2");
await _settingManager.DidNotReceive().SetForCurrentTenantAsync("RequestToken", "value3");
}

[Fact]
public async Task Should_Hide_Encrypted_Default_Value_For_Tenants()
{
var currentTenant = GetRequiredService<ICurrentTenant>();

var result = await _service.GroupSettingDefinitionsAsync();

var setting3 = result.First(x => x.GroupName == SettingUiConst.DefaultGroup).SettingInfos
.First(x => x.Name == "Test.Setting3");

setting3.Value.ShouldBe("3");

using var changeTenant = currentTenant.Change(Guid.NewGuid());

result = await _service.GroupSettingDefinitionsAsync();

setting3 = result.First(x => x.GroupName == SettingUiConst.DefaultGroup).SettingInfos
.First(x => x.Name == "Test.Setting3");

setting3.Value.ShouldBeNullOrEmpty();
}
}
}

0 comments on commit 1f933a9

Please sign in to comment.