diff --git a/src/Volo.Abp.Session/Volo.Abp.Session.csproj b/src/Volo.Abp.Session/Volo.Abp.Session.csproj index dde6a11a58..6aef78ea80 100644 --- a/src/Volo.Abp.Session/Volo.Abp.Session.csproj +++ b/src/Volo.Abp.Session/Volo.Abp.Session.csproj @@ -16,6 +16,7 @@ + diff --git a/src/Volo.Abp.Session/Volo/Abp/Session/AbpSessionModule.cs b/src/Volo.Abp.Session/Volo/Abp/Session/AbpSessionModule.cs index b547f74663..2f9a3e7cf4 100644 --- a/src/Volo.Abp.Session/Volo/Abp/Session/AbpSessionModule.cs +++ b/src/Volo.Abp.Session/Volo/Abp/Session/AbpSessionModule.cs @@ -1,14 +1,21 @@ using Microsoft.Extensions.DependencyInjection; using Volo.Abp.Modularity; using Volo.Abp.Security; +using Volo.Abp.Settings; namespace Volo.Abp.Session { [DependsOn(typeof(AbpSecurityModule))] + [DependsOn(typeof(AbpSettingsModule))] public class AbpSessionModule : AbpModule { public override void ConfigureServices(IServiceCollection services) { + services.Configure(options => + { + options.Contributors.Add(); + }); + services.AddAssemblyOf(); } } diff --git a/src/Volo.Abp.Session/Volo/Abp/Session/UserSettingContributor.cs b/src/Volo.Abp.Session/Volo/Abp/Session/UserSettingContributor.cs new file mode 100644 index 0000000000..009b59bd81 --- /dev/null +++ b/src/Volo.Abp.Session/Volo/Abp/Session/UserSettingContributor.cs @@ -0,0 +1,37 @@ +using System.Threading.Tasks; +using Volo.Abp.Settings; + +namespace Volo.Abp.Session +{ + //TODO: Optimization: Get all settings and cache it! + + public class UserSettingContributor : SettingContributor + { + public const string DefaultEntityType = "User"; + + public override string EntityType => DefaultEntityType; + + protected ICurrentUser CurrentUser { get; } + + public UserSettingContributor(ISettingStore settingStore, ICurrentUser currentUser) + : base(settingStore) + { + CurrentUser = currentUser; + } + + public override async Task GetOrNullAsync(string name, bool fallback) + { + if (CurrentUser.Id == null) + { + return null; + } + + return await SettingStore.GetOrNullAsync(name, EntityType, CurrentUser.Id.Value.ToString()); + } + + public override async Task GetOrNullAsync(string name, string entityId, bool fallback = true) + { + return await SettingStore.GetOrNullAsync(name, EntityType, entityId); + } + } +} \ No newline at end of file diff --git a/src/Volo.Abp.Session/Volo/Abp/Settings/UserSettingManagerExtensions.cs b/src/Volo.Abp.Session/Volo/Abp/Settings/UserSettingManagerExtensions.cs new file mode 100644 index 0000000000..62eafb94c3 --- /dev/null +++ b/src/Volo.Abp.Session/Volo/Abp/Settings/UserSettingManagerExtensions.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using JetBrains.Annotations; +using Volo.Abp.Session; + +namespace Volo.Abp.Settings +{ + public static class UserSettingManagerExtensions + { + public static Task GetOrNullForUserAsync(this ISettingManager settingManager, [NotNull] string name, Guid userId, bool fallback = true) + { + return settingManager.GetOrNullAsync(name, UserSettingContributor.DefaultEntityType, userId.ToString(), fallback); + } + + public static Task> GetAllForUserAsync(this ISettingManager settingManager, Guid userId, bool fallback = true) + { + return settingManager.GetAllAsync(UserSettingContributor.DefaultEntityType, userId.ToString(), fallback); + } + + public static Task SetForUserAsync(this ISettingManager settingManager, Guid userId, [NotNull] string name, [CanBeNull] string value) + { + return settingManager.SetAsync(name, value, UserSettingContributor.DefaultEntityType, userId.ToString()); + } + } +} diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultSettingContributor.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultSettingContributor.cs deleted file mode 100644 index 66c1d50f5f..0000000000 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultSettingContributor.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System.Threading.Tasks; -using Volo.Abp.DependencyInjection; - -namespace Volo.Abp.Settings -{ - public class DefaultSettingContributor : ISettingContributor, ISingletonDependency - { - private readonly ISettingStore _settingStore; - - public DefaultSettingContributor(ISettingStore settingStore) - { - _settingStore = settingStore; - } - - public async Task GetOrNullAsync(string name, string entityType, string entityId, bool fallback = true) - { - //TODO: Optimization: Get all settings and cache it! - return await _settingStore.GetOrNullAsync(name, null, null); - } - } -} \ No newline at end of file diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingContributor.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingContributor.cs index 1fdaa206f3..0b48f06839 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingContributor.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingContributor.cs @@ -4,6 +4,10 @@ namespace Volo.Abp.Settings { public interface ISettingContributor { - Task GetOrNullAsync(string name, string entityType, string entityId, bool fallback = true); + string EntityType { get; } + + Task GetOrNullAsync(string name, bool fallback); + + Task GetOrNullAsync(string name, string entityId, bool fallback = true); } } \ No newline at end of file diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingDefinitionManager.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingDefinitionManager.cs index 28ec16e185..e8795e283b 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingDefinitionManager.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingDefinitionManager.cs @@ -1,7 +1,10 @@ -namespace Volo.Abp.Settings +using JetBrains.Annotations; + +namespace Volo.Abp.Settings { public interface ISettingDefinitionManager { - SettingDefinition Get(string name); + [NotNull] + SettingDefinition Get([NotNull] string name); } } \ No newline at end of file diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingManager.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingManager.cs index b768648e9d..d51da5b1f2 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingManager.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingManager.cs @@ -5,7 +5,7 @@ namespace Volo.Abp.Settings { public interface ISettingManager { - Task GetOrNullAsync(string name); + Task GetOrNullAsync(string name, bool fallback = true); Task GetOrNullAsync(string name, string entityType, string entityId, bool fallback = true); diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingContributor.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingContributor.cs new file mode 100644 index 0000000000..f3c7a226d5 --- /dev/null +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingContributor.cs @@ -0,0 +1,21 @@ +using System.Threading.Tasks; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.Settings +{ + public abstract class SettingContributor : ISettingContributor, ISingletonDependency + { + public abstract string EntityType { get; } + + protected ISettingStore SettingStore { get; } + + protected SettingContributor(ISettingStore settingStore) + { + SettingStore = settingStore; + } + + public abstract Task GetOrNullAsync(string name, bool fallback); + + public abstract Task GetOrNullAsync(string name, string entityId, bool fallback = true); + } +} \ No newline at end of file diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingDefinitionManager.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingDefinitionManager.cs index 14bb230b91..a80ee47ab2 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingDefinitionManager.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingDefinitionManager.cs @@ -30,6 +30,8 @@ namespace Volo.Abp.Settings public virtual SettingDefinition Get(string name) { + Check.NotNull(name, nameof(name)); + var settingDefinition = GetOrNull(name); if (settingDefinition == null) diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs index ca042a7108..b059bfe18a 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs @@ -16,11 +16,15 @@ namespace Volo.Abp.Settings protected SettingOptions Options { get; } + protected ISettingStore SettingStore { get; } + public SettingManager( - IOptions options, - IServiceProvider serviceProvider, - ISettingDefinitionManager settingDefinitionManager) + IOptions options, + IServiceProvider serviceProvider, + ISettingDefinitionManager settingDefinitionManager, + ISettingStore settingStore) { + SettingStore = settingStore; SettingDefinitionManager = settingDefinitionManager; Options = options.Value; @@ -33,27 +37,38 @@ namespace Volo.Abp.Settings ); } - public Task GetOrNullAsync(string name) + public Task GetOrNullAsync(string name, bool fallback = true) { - return GetOrNullAsync(name, null, null); + return GetOrNullAsync(name, null, null, fallback); } public async Task GetOrNullAsync(string name, string entityType, string entityId, bool fallback = true) { var settingDefinition = SettingDefinitionManager.Get(name); - foreach (var contributor in Contributors.Value) + foreach (var contributor in GetContributors(entityType, fallback)) { - var value = await contributor.GetOrNullAsync(name, entityType, entityId, fallback); + var value = await GetContributorValue(contributor, name, entityId, fallback); if (value != null) { return value; } } + var defaultStoreValue = await SettingStore.GetOrNullAsync(name, null, null); + if (defaultStoreValue != null) + { + return defaultStoreValue; + } + + if (!fallback) + { + return null; + } + return settingDefinition.DefaultValue; } - + public Task> GetAllAsync() { throw new System.NotImplementedException(); @@ -73,5 +88,34 @@ namespace Volo.Abp.Settings { throw new System.NotImplementedException(); } + + private static async Task GetContributorValue(ISettingContributor contributor, string name, string entityId, bool fallback) + { + if (entityId != null) + { + return await contributor.GetOrNullAsync(name, entityId, fallback); + } + else + { + return await contributor.GetOrNullAsync(name, fallback); + } + } + + private IEnumerable GetContributors(string entityType, bool fallback) + { + var contributors = Enumerable.Reverse(Contributors.Value); + + if (entityType != null) + { + contributors = contributors.SkipWhile(c => c.EntityType != entityType); + } + + if (!fallback) + { + contributors = contributors.TakeWhile(c => c.EntityType == entityType); + } + + return contributors; + } } } \ No newline at end of file diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingOptions.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingOptions.cs index 3be5cc2e8d..eb4b65c44a 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingOptions.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingOptions.cs @@ -10,11 +10,7 @@ namespace Volo.Abp.Settings public SettingOptions() { - Contributors = new TypeList - { - typeof(DefaultSettingContributor) - }; - + Contributors = new TypeList(); Providers = new TypeList(); } }