Skip to content

Commit

Permalink
Resolved #203: Encrypt option to settings
Browse files Browse the repository at this point in the history
  • Loading branch information
hikalkan committed Dec 13, 2018
1 parent 66fcaf9 commit ac3d7c7
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 50 deletions.
1 change: 1 addition & 0 deletions framework/src/Volo.Abp.Settings/Volo.Abp.Settings.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,28 @@ public ILocalizableString DisplayName
[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;
IsVisibleToClients = isVisibleToClients;
DisplayName = displayName ?? new FixedLocalizableString(name);
Description = description;
IsInherited = isInherited;
IsEncrypted = isEncrypted;

Properties = new Dictionary<string, object>();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 changes: 82 additions & 48 deletions framework/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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>>(
Expand All @@ -48,34 +49,6 @@ public virtual Task<string> GetOrNullAsync(string name, string providerName, str
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>();
Expand All @@ -88,6 +61,11 @@ public virtual async Task<List<SettingValue>> GetAllAsync()
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);
}
}
Expand All @@ -100,7 +78,6 @@ public virtual async Task<List<SettingValue>> GetAllAsync(string providerName, s
{
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);
Expand All @@ -112,29 +89,39 @@ public virtual async Task<List<SettingValue>> GetAllAsync(string providerName, s

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();
Expand All @@ -157,6 +144,11 @@ public virtual async Task SetAsync(string name, string value, string providerNam
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
Expand Down Expand Up @@ -186,5 +178,47 @@ public virtual async Task SetAsync(string name, string value, string providerNam
}
}
}

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;
}
}
}

0 comments on commit ac3d7c7

Please sign in to comment.