diff --git a/Models/DataBase/Config.cs b/Models/DataBase/Config.cs index f18c5c1..60ebf8b 100644 --- a/Models/DataBase/Config.cs +++ b/Models/DataBase/Config.cs @@ -14,4 +14,5 @@ public class Config : BaseModel [StringLength(255)] public string Description { get; set; } = string.Empty; + public bool IsSecret { get; set; } = false; } \ No newline at end of file diff --git a/Models/Request/Config/SetConfigRequest.cs b/Models/Request/Config/SetConfigRequest.cs index 1fdf2fa..033705a 100644 --- a/Models/Request/Config/SetConfigRequest.cs +++ b/Models/Request/Config/SetConfigRequest.cs @@ -12,4 +12,6 @@ public record SetConfigRequest [StringLength(255, ErrorMessage = "描述长度不能超过255个字符")] public string? Description { get; set; } + + public bool? IsSecret { get; set; } } diff --git a/Services/Configuration/ConfigService.cs b/Services/Configuration/ConfigService.cs index 556d636..c865a66 100644 --- a/Services/Configuration/ConfigService.cs +++ b/Services/Configuration/ConfigService.cs @@ -70,13 +70,39 @@ public class ConfigService( public async Task GetConfigAsync(string key) { await using var context = await contextFactory.CreateDbContextAsync(); - return await context.Configs.FirstOrDefaultAsync(c => c.Key == key); + var config = await context.Configs.FirstOrDefaultAsync(c => c.Key == key); + + // 如果配置是私密的,返回值设为空字符串 + if (config?.IsSecret == true) + { + // 创建一个新的实例,避免修改数据库中的值 + var displayConfig = new Config + { + Id = config.Id, + Key = config.Key, + Value = string.Empty, // 私密配置的值设为空 + Description = config.Description, + IsSecret = config.IsSecret, + CreatedAt = config.CreatedAt, + UpdatedAt = config.UpdatedAt + }; + return displayConfig; + } + + return config; } public async Task> GetAllConfigsAsync() { await using var context = await contextFactory.CreateDbContextAsync(); - return await context.Configs.OrderBy(c => c.Key).ToListAsync(); + var configs = await context.Configs.OrderBy(c => c.Key).ToListAsync(); + + foreach (var config in configs.Where(c => c.IsSecret)) + { + config.Value = string.Empty; + } + + return configs; } public async Task SetConfigAsync(string key, string value, string? description = null) @@ -90,12 +116,12 @@ public class ConfigService( if (config == null) { - // 创建新配置 config = new Config { Key = key, Value = value, Description = description ?? string.Empty, + IsSecret = false, CreatedAt = DateTime.UtcNow, UpdatedAt = DateTime.UtcNow }; @@ -104,8 +130,11 @@ public class ConfigService( } else { - // 更新现有配置 - config.Value = value; + if (!(config.IsSecret && string.IsNullOrEmpty(value))) + { + config.Value = value; + } + if (description != null) { config.Description = description; @@ -113,7 +142,10 @@ public class ConfigService( config.UpdatedAt = DateTime.UtcNow; } await context.SaveChangesAsync(); - memoryCache.Set($"config:{key}", value, _cacheExpiration); + if (!config.IsSecret) + { + memoryCache.Set($"config:{key}", value, _cacheExpiration); + } return config; } diff --git a/Services/Configuration/IConfigService.cs b/Services/Configuration/IConfigService.cs index 8fcd560..52f3e91 100644 --- a/Services/Configuration/IConfigService.cs +++ b/Services/Configuration/IConfigService.cs @@ -5,19 +5,13 @@ namespace Foxel.Services.Configuration; public interface IConfigService { string this[string key] { get; } - Task GetValueAsync(string key); Task GetValueAsync(string key, T? defaultValue = default); Task GetConfigAsync(string key); Task> GetAllConfigsAsync(); - Task SetConfigAsync(string key, string value, string? description = null); - Task DeleteConfigAsync(string key); - Task ExistsAsync(string key); - Task> BackupConfigsAsync(); - Task RestoreConfigsAsync(Dictionary configBackup); }