diff --git a/src/Volo.Abp.Session/Volo/Abp/Settings/UserSettingManagerExtensions.cs b/src/Volo.Abp.Session/Volo/Abp/Settings/UserSettingManagerExtensions.cs index 019cf7eadf..569bde8395 100644 --- a/src/Volo.Abp.Session/Volo/Abp/Settings/UserSettingManagerExtensions.cs +++ b/src/Volo.Abp.Session/Volo/Abp/Settings/UserSettingManagerExtensions.cs @@ -28,14 +28,14 @@ namespace Volo.Abp.Settings return settingManager.GetAllAsync(UserSettingValueProvider.DefaultEntityType, null, fallback); } - public static Task SetForUserAsync(this ISettingManager settingManager, Guid userId, [NotNull] string name, [CanBeNull] string value) + public static Task SetForUserAsync(this ISettingManager settingManager, Guid userId, [NotNull] string name, [CanBeNull] string value, bool forceToSet = false) { - return settingManager.SetAsync(name, value, UserSettingValueProvider.DefaultEntityType, userId.ToString()); + return settingManager.SetAsync(name, value, UserSettingValueProvider.DefaultEntityType, userId.ToString(), forceToSet); } - public static Task SetForCurrentUserAsync(this ISettingManager settingManager, [NotNull] string name, [CanBeNull] string value) + public static Task SetForCurrentUserAsync(this ISettingManager settingManager, [NotNull] string name, [CanBeNull] string value, bool forceToSet = false) { - return settingManager.SetAsync(name, value, UserSettingValueProvider.DefaultEntityType, null); + return settingManager.SetAsync(name, value, UserSettingValueProvider.DefaultEntityType, null, forceToSet); } } } diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs index d426661417..ca4108f554 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs @@ -138,15 +138,6 @@ namespace Volo.Abp.Settings var setting = SettingDefinitionManager.Get(name); - if (!forceToSet) - { - var currentValue = await GetOrNullInternalAsync(name, entityType, entityId); - if (currentValue == value) - { - return; - } - } - var providers = Enumerable .Reverse(Providers.Value) .SkipWhile(p => p.EntityType != entityType) @@ -157,6 +148,20 @@ namespace Volo.Abp.Settings return; } + //Clear the value if it's same as it's fallback value + if (providers.Count > 1 && !forceToSet && value != null) + { + var fallbackValue = await GetOrNullInternalAsync(name, providers[1].EntityType, entityId); + if (fallbackValue == value) + { + value = null; + } + } + + providers = providers + .TakeWhile(p => p.EntityType == entityType) + .ToList(); + if (value == null) { foreach (var provider in providers) diff --git a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityApplicationTestBase.cs b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityApplicationTestBase.cs index c8e3d391b9..5077ad8e3c 100644 --- a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityApplicationTestBase.cs +++ b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityApplicationTestBase.cs @@ -1,4 +1,7 @@ using System; +using System.Collections.Generic; +using System.Linq; +using Volo.Abp.Session; using Volo.Abp.Settings.EntityFrameworkCore; using Volo.Abp.TestBase; @@ -27,5 +30,16 @@ namespace Volo.Abp.Settings } } + protected List GetSettingsFromDbContext(string entityType, string entityId, string name) + { + return UsingDbContext(context => + context.Settings.Where( + s => + s.EntityType == UserSettingValueProvider.DefaultEntityType && + s.EntityId == AbpIdentityTestDataBuilder.User1Id.ToString() && + s.Name == "MySetting2" + ).ToList() + ); + } } } diff --git a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_User_Tests.cs b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_User_Tests.cs index a570fbbab7..49e5201562 100644 --- a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_User_Tests.cs +++ b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_User_Tests.cs @@ -109,15 +109,11 @@ namespace Volo.Abp.Settings { await _settingManager.SetForUserAsync(AbpIdentityTestDataBuilder.User1Id, "MySetting2", null); - UsingDbContext(context => - { - context.Settings.Count( - s => - s.EntityType == UserSettingValueProvider.DefaultEntityType && - s.EntityId == AbpIdentityTestDataBuilder.User1Id.ToString() && - s.Name == "MySetting2" - ).ShouldBe(0); - }); + GetSettingsFromDbContext( + UserSettingValueProvider.DefaultEntityType, + AbpIdentityTestDataBuilder.User1Id.ToString(), + "MySetting2" + ).Count.ShouldBe(0); } [Fact] @@ -131,17 +127,50 @@ namespace Volo.Abp.Settings (await _settingManager.GetOrNullForUserAsync("MySetting2", AbpIdentityTestDataBuilder.User1Id)) .ShouldBe("user1-new-store-value"); - UsingDbContext(context => - { - var setting = context.Settings.Single( - s => - s.EntityType == UserSettingValueProvider.DefaultEntityType && - s.EntityId == AbpIdentityTestDataBuilder.User1Id.ToString() && - s.Name == "MySetting2" - ); - - setting.Value.ShouldBe("user1-new-store-value"); - }); + GetSettingsFromDbContext( + UserSettingValueProvider.DefaultEntityType, + AbpIdentityTestDataBuilder.User1Id.ToString(), + "MySetting2" + ).Single().Value.ShouldBe("user1-new-store-value"); + } + + [Fact] + public async Task Should_Delete_Setting_Record_When_Set_To_Fallback_Value() + { + await _settingManager.SetForUserAsync( + AbpIdentityTestDataBuilder.User1Id, + "MySetting2", + "default-store-value" + ); + + GetSettingsFromDbContext( + UserSettingValueProvider.DefaultEntityType, + AbpIdentityTestDataBuilder.User1Id.ToString(), + "MySetting2" + ).Count.ShouldBe(0); + + (await _settingManager.GetOrNullForUserAsync("MySetting2", AbpIdentityTestDataBuilder.User1Id)) + .ShouldBe("default-store-value"); + } + + [Fact] + public async Task Should_Not_Delete_Setting_Record_When_Set_To_Fallback_Value_If_Forced() + { + await _settingManager.SetForUserAsync( + AbpIdentityTestDataBuilder.User1Id, + "MySetting2", + "default-store-value", + forceToSet: true + ); + + GetSettingsFromDbContext( + UserSettingValueProvider.DefaultEntityType, + AbpIdentityTestDataBuilder.User1Id.ToString(), + "MySetting2" + ).Single().Value.ShouldBe("default-store-value"); + + (await _settingManager.GetOrNullForUserAsync("MySetting2", AbpIdentityTestDataBuilder.User1Id)) + .ShouldBe("default-store-value"); } } } \ No newline at end of file