Browse Source

Resolved #203: Encrypt option to settings

pull/652/head
Halil ibrahim Kalkan 8 years ago
parent
commit
ac3d7c7c16
  1. 1
      framework/src/Volo.Abp.Settings/Volo.Abp.Settings.csproj
  2. 4
      framework/src/Volo.Abp.Settings/Volo/Abp/Settings/AbpSettingsModule.cs
  3. 13
      framework/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingEncryptionService.cs
  4. 10
      framework/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingDefinition.cs
  5. 36
      framework/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingEncryptionService.cs
  6. 130
      framework/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs

1
framework/src/Volo.Abp.Settings/Volo.Abp.Settings.csproj

@ -15,6 +15,7 @@
<ItemGroup>
<ProjectReference Include="..\Volo.Abp.Localization.Abstractions\Volo.Abp.Localization.Abstractions.csproj" />
<ProjectReference Include="..\Volo.Abp.Security\Volo.Abp.Security.csproj" />
</ItemGroup>
</Project>

4
framework/src/Volo.Abp.Settings/Volo/Abp/Settings/AbpSettingsModule.cs

@ -1,10 +1,12 @@
using Volo.Abp.Localization;
using Volo.Abp.Modularity;
using Volo.Abp.Security;
namespace Volo.Abp.Settings
{
[DependsOn(
typeof(AbpLocalizationAbstractionsModule)
typeof(AbpLocalizationAbstractionsModule),
typeof(AbpSecurityModule)
)]
public class AbpSettingsModule : AbpModule
{

13
framework/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingEncryptionService.cs

@ -0,0 +1,13 @@
using JetBrains.Annotations;
namespace Volo.Abp.Settings
{
public interface ISettingEncryptionService
{
[CanBeNull]
string Encrypt([NotNull]SettingDefinition settingDefinition, [CanBeNull] string plainValue);
[CanBeNull]
string Decrypt([NotNull]SettingDefinition settingDefinition, [CanBeNull] string encryptedValue);
}
}

10
framework/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingDefinition.cs

@ -48,13 +48,20 @@ namespace Volo.Abp.Settings
[NotNull]
public Dictionary<string, object> Properties { get; }
/// <summary>
/// Is this setting stored as encrypted in the data source.
/// Default: False.
/// </summary>
public bool IsEncrypted { get; set; }
public SettingDefinition(
string name,
string defaultValue = null,
ILocalizableString displayName = null,
ILocalizableString description = null,
bool isVisibleToClients = false,
bool isInherited = true)
bool isInherited = true,
bool isEncrypted = false)
{
Name = name;
DefaultValue = defaultValue;
@ -62,6 +69,7 @@ namespace Volo.Abp.Settings
DisplayName = displayName ?? new FixedLocalizableString(name);
Description = description;
IsInherited = isInherited;
IsEncrypted = isEncrypted;
Properties = new Dictionary<string, object>();
}

36
framework/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingEncryptionService.cs

@ -0,0 +1,36 @@
using System;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Security.Encryption;
namespace Volo.Abp.Settings
{
public class SettingEncryptionService : ISettingEncryptionService, ITransientDependency
{
protected IStringEncryptionService StringEncryptionService { get; }
public SettingEncryptionService(IStringEncryptionService stringEncryptionService)
{
StringEncryptionService = stringEncryptionService;
}
public virtual string Encrypt(SettingDefinition settingDefinition, string plainValue)
{
if (plainValue.IsNullOrEmpty())
{
return plainValue;
}
return StringEncryptionService.Encrypt(plainValue);
}
public virtual string Decrypt(SettingDefinition settingDefinition, string encryptedValue)
{
if (encryptedValue.IsNullOrEmpty())
{
return encryptedValue;
}
return StringEncryptionService.Decrypt(encryptedValue);
}
}
}

130
framework/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs

@ -11,17 +11,18 @@ namespace Volo.Abp.Settings
public class SettingManager : ISettingManager, ISingletonDependency
{
protected ISettingDefinitionManager SettingDefinitionManager { get; }
protected ISettingEncryptionService SettingEncryptionService { get; }
protected Lazy<List<ISettingValueProvider>> Providers { get; }
protected SettingOptions Options { get; }
public SettingManager(
IOptions<SettingOptions> options,
IServiceProvider serviceProvider,
ISettingDefinitionManager settingDefinitionManager)
ISettingDefinitionManager settingDefinitionManager,
ISettingEncryptionService settingEncryptionService)
{
SettingDefinitionManager = settingDefinitionManager;
SettingEncryptionService = settingEncryptionService;
Options = options.Value;
Providers = new Lazy<List<ISettingValueProvider>>(
@ -48,34 +49,6 @@ namespace Volo.Abp.Settings
return GetOrNullInternalAsync(name, providerName, providerKey, fallback);
}
public virtual async Task<string> GetOrNullInternalAsync(string name, string providerName, string providerKey, bool fallback = true)
{
var setting = SettingDefinitionManager.Get(name);
var providers = Enumerable
.Reverse(Providers.Value);
if (providerName != null)
{
providers = providers.SkipWhile(c => c.Name != providerName);
}
if (!fallback || !setting.IsInherited)
{
providers = providers.TakeWhile(c => c.Name == providerName);
}
foreach (var provider in providers)
{
var value = await provider.GetOrNullAsync(setting, providerKey);
if (value != null)
{
return value;
}
}
return null;
}
public virtual async Task<List<SettingValue>> GetAllAsync()
{
var settingValues = new Dictionary<string, SettingValue>();
@ -88,6 +61,11 @@ namespace Volo.Abp.Settings
var value = await provider.GetOrNullAsync(setting, null);
if (value != null)
{
if (setting.IsEncrypted)
{
value = SettingEncryptionService.Decrypt(setting, value);
}
settingValues[setting.Name] = new SettingValue(setting.Name, value);
}
}
@ -100,7 +78,6 @@ namespace Volo.Abp.Settings
{
Check.NotNull(providerName, nameof(providerName));
var settingValues = new Dictionary<string, SettingValue>();
var settingDefinitions = SettingDefinitionManager.GetAll();
var providers = Enumerable.Reverse(Providers.Value)
.SkipWhile(c => c.Name != providerName);
@ -112,29 +89,39 @@ namespace Volo.Abp.Settings
var providerList = providers.Reverse().ToList();
if (providerList.Any())
if (!providerList.Any())
{
foreach (var setting in settingDefinitions)
return new List<SettingValue>();
}
var settingValues = new Dictionary<string, SettingValue>();
foreach (var setting in settingDefinitions)
{
string value = null;
if (setting.IsInherited)
{
if (setting.IsInherited)
foreach (var provider in providerList)
{
foreach (var provider in providerList)
var providerValue = await provider.GetOrNullAsync(setting, providerKey);
if (providerValue != null)
{
var value = await provider.GetOrNullAsync(setting, providerKey);
if (value != null)
{
settingValues[setting.Name] = new SettingValue(setting.Name, value);
}
value = providerValue;
}
}
else
{
settingValues[setting.Name] = new SettingValue(
setting.Name,
await providerList[0].GetOrNullAsync(setting, providerKey)
);
}
}
else
{
value = await providerList[0].GetOrNullAsync(setting, providerKey);
}
if (setting.IsEncrypted)
{
value = SettingEncryptionService.Decrypt(setting, value);
}
settingValues[setting.Name] = new SettingValue(setting.Name, value);
}
return settingValues.Values.ToList();
@ -157,6 +144,11 @@ namespace Volo.Abp.Settings
return;
}
if (setting.IsEncrypted)
{
value = SettingEncryptionService.Encrypt(setting, value);
}
if (providers.Count > 1 && !forceToSet && setting.IsInherited && value != null)
{
//Clear the value if it's same as it's fallback value
@ -186,5 +178,47 @@ namespace Volo.Abp.Settings
}
}
}
protected virtual async Task<string> GetOrNullInternalAsync(string name, string providerName, string providerKey, bool fallback = true)
{
var setting = SettingDefinitionManager.Get(name);
var providers = Enumerable
.Reverse(Providers.Value);
if (providerName != null)
{
providers = providers.SkipWhile(c => c.Name != providerName);
}
if (!fallback || !setting.IsInherited)
{
providers = providers.TakeWhile(c => c.Name == providerName);
}
var value = await GetOrNullValueFromProvidersAsync(providerKey, providers, setting);
if (setting.IsEncrypted)
{
value = SettingEncryptionService.Decrypt(setting, value);
}
return value;
}
protected virtual async Task<string> GetOrNullValueFromProvidersAsync(
string providerKey,
IEnumerable<ISettingValueProvider> providers,
SettingDefinition setting)
{
foreach (var provider in providers)
{
var value = await provider.GetOrNullAsync(setting, providerKey);
if (value != null)
{
return value;
}
}
return null;
}
}
}
Loading…
Cancel
Save