155 lines
6 KiB
C#
155 lines
6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Kurs.Languages;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Volo.Abp.Application.Services;
|
|
using Volo.Abp.Domain.Repositories;
|
|
using Volo.Abp.SettingManagement;
|
|
using Volo.Abp.Settings;
|
|
using SettingDefinition = Kurs.Settings.Entities.SettingDefinition;
|
|
|
|
namespace Kurs.Settings;
|
|
|
|
[Authorize]
|
|
public class SettingUiAppService : ApplicationService, ISettingUiAppService
|
|
{
|
|
//private readonly IRepository<SettingDefinition, Guid> repositorySettingDefinition;
|
|
private readonly ILanguageKeyIntegrationService languageKeyIntegrationService;
|
|
private readonly ISettingDefinitionManager settingDefinitionManager;
|
|
private readonly ISettingManager settingManager;
|
|
private readonly KursSettingDefinitionManager kursSettingDefinitionManager;
|
|
|
|
public SettingUiAppService(
|
|
ILanguageKeyIntegrationService languageKeyIntegrationService,
|
|
ISettingDefinitionManager settingDefinitionManager,
|
|
ISettingManager settingManager,
|
|
KursSettingDefinitionManager kursSettingDefinitionManager)
|
|
{
|
|
this.languageKeyIntegrationService = languageKeyIntegrationService;
|
|
this.settingDefinitionManager = settingDefinitionManager;
|
|
this.settingManager = settingManager;
|
|
this.kursSettingDefinitionManager = kursSettingDefinitionManager;
|
|
}
|
|
|
|
public virtual async Task<List<MainGroupedSettingDto>> GetListAsync()
|
|
{
|
|
var groups = await kursSettingDefinitionManager.GetGroupedSettingDefinitionsAsync();
|
|
return groups.Select(group => new MainGroupedSettingDto
|
|
{
|
|
GroupName = group.GroupName,
|
|
SubGroups = group.GroupedSettingDefinitions.Select(subGroup => new SubGroupedSettingDto
|
|
{
|
|
SubGroupName = subGroup.Key,
|
|
Settings = subGroup.Value.Select(a =>
|
|
new SettingDefinitionWithGroupDto
|
|
{
|
|
Code = a.Definition.Code.Replace('.', '_'),
|
|
Name = a.Definition.NameKey,
|
|
Description = a.Definition.DescriptionKey.IsNullOrEmpty() ? "" : a.Definition.DescriptionKey,
|
|
Value = a.Value,
|
|
ValueBool = a.Value.ToLower() == "true",
|
|
MainGroup = group.GroupName,
|
|
SubGroup = subGroup.Key,
|
|
DataType = a.Definition.DataType,
|
|
SelectOptions = a.Definition.SelectOptions?.ToDictionary(x => x.Key, x => x.Value),
|
|
FormName = $"{SettingsConsts.FormNamePrefix}{a.Definition.Code}".Replace('.', '_'),
|
|
Providers = a.Definition.Providers ?? "U|T|G|C|D"
|
|
}).ToList()
|
|
}).ToList()
|
|
}).ToList();
|
|
}
|
|
|
|
public virtual async Task UpdateSettingValuesAsync(Dictionary<string, object> settingValues)
|
|
{
|
|
foreach (var kv in settingValues)
|
|
{
|
|
//// The key of the settingValues is in camel_Case, like "setting_Abp_Localization_DefaultLanguage",
|
|
//// change it to "Abp.Localization.DefaultLanguage" form
|
|
string pascalCaseName = kv.Key.ToPascalCase();
|
|
//if (!pascalCaseName.StartsWith(SettingsConsts.FormNamePrefix))
|
|
//{
|
|
// continue;
|
|
//}
|
|
|
|
string name = pascalCaseName.RemovePreFix(SettingsConsts.FormNamePrefix).Replace('_', '.');
|
|
string value = kv.Value.ToString();
|
|
|
|
var setting = await settingDefinitionManager.GetOrNullAsync(name);
|
|
if (setting == null)
|
|
{
|
|
continue;
|
|
}
|
|
var requiredPermission = setting.Properties.GetOrDefault(SettingsConsts.RequiredPermission);
|
|
if (requiredPermission != null)
|
|
{
|
|
await AuthorizationService.CheckAsync(requiredPermission.ToString());
|
|
}
|
|
|
|
await SetSettingAsync(setting, value);
|
|
}
|
|
}
|
|
|
|
protected virtual async Task SetSettingAsync(Volo.Abp.Settings.SettingDefinition setting, string value)
|
|
{
|
|
if (setting.Providers.IsNullOrEmpty())
|
|
{
|
|
await settingManager.SetForCurrentUserAsync(setting.Name, value);
|
|
}
|
|
else
|
|
{
|
|
if (setting.Providers.Any(p => p == UserSettingValueProvider.ProviderName))
|
|
{
|
|
await settingManager.SetForCurrentUserAsync(setting.Name, value);
|
|
}
|
|
else if (setting.Providers.Any(p => p == TenantSettingValueProvider.ProviderName))
|
|
{
|
|
await settingManager.SetForCurrentTenantAsync(setting.Name, value);
|
|
}
|
|
else if (setting.Providers.Any(p => p == GlobalSettingValueProvider.ProviderName))
|
|
{
|
|
await settingManager.SetGlobalAsync(setting.Name, value);
|
|
}
|
|
}
|
|
}
|
|
|
|
/*
|
|
public override async Task<SettingDefinitionDto> CreateAsync(SettingDefinitionDto input)
|
|
{
|
|
await languageKeyIntegrationService.CreateKeysForSettingsAsync(
|
|
input.NameKey,
|
|
input.DescriptionKey,
|
|
input.MainGroupKey,
|
|
input.SubGroupKey);
|
|
|
|
return await base.CreateAsync(input);
|
|
}
|
|
|
|
public override async Task<SettingDefinitionDto> UpdateAsync(Guid id, SettingDefinitionDto input)
|
|
{
|
|
await languageKeyIntegrationService.CreateKeysForSettingsAsync(
|
|
input.NameKey,
|
|
input.DescriptionKey,
|
|
input.MainGroupKey,
|
|
input.SubGroupKey);
|
|
|
|
return await base.UpdateAsync(id, input);
|
|
}
|
|
|
|
public override async Task DeleteAsync(Guid id)
|
|
{
|
|
var key = await _repositorySettingDefinition.GetAsync(id);
|
|
if (key != null)
|
|
{
|
|
await languageKeyIntegrationService.DeleteKeysForSettingsAsync(
|
|
key.NameKey,
|
|
key.DescriptionKey,
|
|
key.MainGroupKey,
|
|
key.SubGroupKey);
|
|
}
|
|
|
|
await base.DeleteAsync(id);
|
|
}
|
|
*/
|
|
}
|