|
|
|
@ -1,5 +1,4 @@ |
|
|
|
using JetBrains.Annotations; |
|
|
|
using Microsoft.AspNetCore.Authorization; |
|
|
|
using Microsoft.AspNetCore.Authorization; |
|
|
|
using System; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.ComponentModel.DataAnnotations; |
|
|
|
@ -8,9 +7,11 @@ using System.Threading.Tasks; |
|
|
|
using Volo.Abp.Application.Dtos; |
|
|
|
using Volo.Abp.Application.Services; |
|
|
|
using Volo.Abp.Caching; |
|
|
|
using Volo.Abp.MultiTenancy; |
|
|
|
using Volo.Abp.SettingManagement; |
|
|
|
using Volo.Abp.SettingManagement.Localization; |
|
|
|
using Volo.Abp.Settings; |
|
|
|
using Volo.Abp.Users; |
|
|
|
|
|
|
|
namespace LINGYUN.Abp.SettingManagement |
|
|
|
{ |
|
|
|
@ -32,109 +33,147 @@ namespace LINGYUN.Abp.SettingManagement |
|
|
|
LocalizationResource = typeof(AbpSettingManagementResource); |
|
|
|
} |
|
|
|
|
|
|
|
public virtual async Task<ListResultDto<SettingDto>> GetAsync([NotNull] string providerName, [NotNull] string providerKey) |
|
|
|
[Authorize(AbpSettingManagementPermissions.Settings.Manager)] |
|
|
|
public virtual async Task SetGlobalAsync(UpdateSettingsDto input) |
|
|
|
{ |
|
|
|
foreach (var setting in input.Settings) |
|
|
|
{ |
|
|
|
await SettingManager.SetGlobalAsync(setting.Name, setting.Value); |
|
|
|
var settingDefinition = SettingDefinitionManager.GetOrNull(setting.Name); |
|
|
|
if (settingDefinition != null) |
|
|
|
{ |
|
|
|
foreach (var provider in settingDefinition.Providers) |
|
|
|
{ |
|
|
|
// 同步变更缓存配置
|
|
|
|
await SetCacheItemAsync(setting.Name, setting.Value, provider, null); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
await CurrentUnitOfWork.SaveChangesAsync(); |
|
|
|
} |
|
|
|
|
|
|
|
[Authorize(AbpSettingManagementPermissions.Settings.Manager)] |
|
|
|
public virtual async Task SetCurrentTenantAsync(UpdateSettingsDto input) |
|
|
|
{ |
|
|
|
return await GetAllSettingAsync(providerName, providerKey); |
|
|
|
if (CurrentTenant.IsAvailable) |
|
|
|
{ |
|
|
|
foreach (var setting in input.Settings) |
|
|
|
{ |
|
|
|
await SettingManager.SetForTenantAsync(CurrentTenant.GetId(), setting.Name, setting.Value); |
|
|
|
// 同步变更缓存配置
|
|
|
|
await SetCacheItemAsync(setting.Name, setting.Value, TenantSettingValueProvider.ProviderName, CurrentTenant.GetId().ToString()); |
|
|
|
} |
|
|
|
|
|
|
|
await CurrentUnitOfWork.SaveChangesAsync(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
[Authorize(AbpSettingManagementPermissions.Settings.Manager)] |
|
|
|
public virtual async Task UpdateAsync([NotNull] string providerName, [NotNull] string providerKey, UpdateSettingsDto input) |
|
|
|
public virtual async Task SetForUserAsync([Required] Guid userId, UpdateSettingsDto input) |
|
|
|
{ |
|
|
|
foreach (var setting in input.Settings) |
|
|
|
{ |
|
|
|
await SettingManager.SetAsync(setting.Name, setting.Value, providerName, providerKey); |
|
|
|
await SettingManager.SetForUserAsync(userId, setting.Name, setting.Value); |
|
|
|
// 同步变更缓存配置
|
|
|
|
var settingCacheKey = CalculateCacheKey(setting.Name, providerName, providerKey); |
|
|
|
var settignCacheItem = new SettingCacheItem(setting.Value); |
|
|
|
await Cache.SetAsync(settingCacheKey, settignCacheItem); |
|
|
|
await SetCacheItemAsync(setting.Name, setting.Value, UserSettingValueProvider.ProviderName, userId.ToString()); |
|
|
|
} |
|
|
|
|
|
|
|
await CurrentUnitOfWork.SaveChangesAsync(); |
|
|
|
} |
|
|
|
|
|
|
|
[Authorize] |
|
|
|
public virtual async Task SetCurrentUserAsync(UpdateSettingsDto input) |
|
|
|
{ |
|
|
|
foreach (var setting in input.Settings) |
|
|
|
{ |
|
|
|
await SettingManager.SetForUserAsync(CurrentUser.GetId(), setting.Name, setting.Value); |
|
|
|
// 同步变更缓存配置
|
|
|
|
await SetCacheItemAsync(setting.Name, setting.Value, UserSettingValueProvider.ProviderName, CurrentUser.GetId().ToString()); |
|
|
|
} |
|
|
|
|
|
|
|
await CurrentUnitOfWork.SaveChangesAsync(); |
|
|
|
} |
|
|
|
|
|
|
|
[AllowAnonymous] |
|
|
|
public virtual async Task<ListResultDto<SettingDto>> GetAllGlobalAsync() |
|
|
|
{ |
|
|
|
var globalSettings = await SettingManager.GetAllGlobalAsync(); |
|
|
|
// return GetAllSetting(await SettingManager.GetAllGlobalAsync());
|
|
|
|
|
|
|
|
return GetAllSetting(globalSettings); |
|
|
|
return await GetAllSettingAsync(GlobalSettingValueProvider.ProviderName, null); |
|
|
|
} |
|
|
|
|
|
|
|
public virtual async Task<ListResultDto<SettingDto>> GetAllForTenantAsync() |
|
|
|
public virtual async Task<ListResultDto<SettingDto>> GetAllForCurrentTenantAsync() |
|
|
|
{ |
|
|
|
if (CurrentTenant.IsAvailable) |
|
|
|
{ |
|
|
|
var tenantSettings = await SettingManager.GetAllForTenantAsync(CurrentTenant.Id.Value); |
|
|
|
return GetAllSetting(tenantSettings); |
|
|
|
// return GetAllSetting(await SettingManager.GetAllForTenantAsync(CurrentTenant.GetId(), false));
|
|
|
|
|
|
|
|
return await GetAllSettingAsync(TenantSettingValueProvider.ProviderName, CurrentTenant.GetId().ToString()); |
|
|
|
} |
|
|
|
return new ListResultDto<SettingDto>(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public virtual async Task<ListResultDto<SettingDto>> GetAllForUserAsync([Required] Guid userId) |
|
|
|
{ |
|
|
|
var userSettings = await SettingManager.GetAllForUserAsync(userId); |
|
|
|
return GetAllSetting(userSettings); |
|
|
|
// return GetAllSetting(await SettingManager.GetAllForUserAsync(userId));
|
|
|
|
|
|
|
|
return await GetAllSettingAsync(UserSettingValueProvider.ProviderName, userId.ToString()); |
|
|
|
} |
|
|
|
|
|
|
|
[Authorize] |
|
|
|
public virtual async Task<ListResultDto<SettingDto>> GetAllForCurrentUserAsync() |
|
|
|
{ |
|
|
|
var userSettings = await SettingManager.GetAllForUserAsync(CurrentUser.Id.Value); |
|
|
|
return GetAllSetting(userSettings); |
|
|
|
// return GetAllSetting(await SettingManager.GetAllForUserAsync(CurrentUser.GetId()));
|
|
|
|
|
|
|
|
return await GetAllSettingAsync(UserSettingValueProvider.ProviderName, CurrentUser.GetId().ToString()); |
|
|
|
} |
|
|
|
|
|
|
|
protected virtual async Task<ListResultDto<SettingDto>> GetAllSettingAsync( |
|
|
|
string providerName, string providerKey) |
|
|
|
protected virtual ListResultDto<SettingDto> GetAllSetting(List<SettingValue> settings) |
|
|
|
{ |
|
|
|
var settingsDto = new List<SettingDto>(); |
|
|
|
var settingDefinitions = SettingDefinitionManager.GetAll(); |
|
|
|
foreach (var setting in settingDefinitions) |
|
|
|
foreach (var setting in settings) |
|
|
|
{ |
|
|
|
if (setting.Providers.Any() && !setting.Providers.Contains(providerName)) |
|
|
|
{ |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
// 既然是配置服务,那必须能管理所有配置才对
|
|
|
|
//if (!setting.IsVisibleToClients)
|
|
|
|
//{
|
|
|
|
// continue;
|
|
|
|
//}
|
|
|
|
|
|
|
|
var settingValue = await SettingManager.GetOrNullAsync(setting.Name, providerName, providerKey); |
|
|
|
var settingDefinition = SettingDefinitionManager.Get(setting.Name); |
|
|
|
var settingInfo = new SettingDto |
|
|
|
{ |
|
|
|
Name = setting.Name, |
|
|
|
Value = settingValue, |
|
|
|
DefaultValue = setting.DefaultValue, |
|
|
|
Description = setting.Description.Localize(StringLocalizerFactory), |
|
|
|
DisplayName = setting.DisplayName.Localize(StringLocalizerFactory) |
|
|
|
Value = setting.Value ?? settingDefinition.DefaultValue, |
|
|
|
DefaultValue = settingDefinition.DefaultValue, |
|
|
|
Description = settingDefinition.Description.Localize(StringLocalizerFactory), |
|
|
|
DisplayName = settingDefinition.DisplayName.Localize(StringLocalizerFactory) |
|
|
|
}; |
|
|
|
settingsDto.Add(settingInfo); |
|
|
|
} |
|
|
|
|
|
|
|
return new ListResultDto<SettingDto>(settingsDto); |
|
|
|
} |
|
|
|
|
|
|
|
protected virtual ListResultDto<SettingDto> GetAllSetting( |
|
|
|
List<SettingValue> settings) |
|
|
|
protected virtual async Task<ListResultDto<SettingDto>> GetAllSettingAsync(string providerName, string providerKey) |
|
|
|
{ |
|
|
|
var settingsDto = new List<SettingDto>(); |
|
|
|
|
|
|
|
var settings = await SettingManager.GetAllAsync(providerName, providerKey); |
|
|
|
foreach (var setting in settings) |
|
|
|
{ |
|
|
|
var settingDefinition = SettingDefinitionManager.Get(setting.Name); |
|
|
|
|
|
|
|
if (!settingDefinition.IsVisibleToClients) |
|
|
|
if (settingDefinition.Providers.Count > 0 && |
|
|
|
!settingDefinition.Providers.Any(p => p.Equals(providerName))) |
|
|
|
{ |
|
|
|
continue; |
|
|
|
} |
|
|
|
var settingInfo = new SettingDto |
|
|
|
var settingInfo = new SettingDto |
|
|
|
{ |
|
|
|
Name = setting.Name, |
|
|
|
Value = setting.Value, |
|
|
|
Value = setting.Value ?? settingDefinition.DefaultValue, |
|
|
|
DefaultValue = settingDefinition.DefaultValue, |
|
|
|
Description = settingDefinition.Description.Localize(StringLocalizerFactory), |
|
|
|
DisplayName = settingDefinition.DisplayName.Localize(StringLocalizerFactory) |
|
|
|
}; |
|
|
|
settingsDto.Add(settingInfo); |
|
|
|
} |
|
|
|
|
|
|
|
return new ListResultDto<SettingDto>(settingsDto); |
|
|
|
} |
|
|
|
|
|
|
|
@ -142,5 +181,12 @@ namespace LINGYUN.Abp.SettingManagement |
|
|
|
{ |
|
|
|
return SettingCacheItem.CalculateCacheKey(name, providerName, providerKey); |
|
|
|
} |
|
|
|
|
|
|
|
protected virtual async Task SetCacheItemAsync(string name, string value, string providerName, string providerKey) |
|
|
|
{ |
|
|
|
var settingCacheKey = CalculateCacheKey(name, providerName, providerKey); |
|
|
|
var settignCacheItem = new SettingCacheItem(value); |
|
|
|
await Cache.SetAsync(settingCacheKey, settignCacheItem); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|