197 lines
5.3 KiB
C#
197 lines
5.3 KiB
C#
using System.IO;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
|
|
namespace UtopiaCanteenSystem.Services;
|
|
|
|
/// <summary>
|
|
/// Simple JSON-backed configuration stored alongside the app binaries.
|
|
/// </summary>
|
|
public class ConfigService : IConfigService
|
|
{
|
|
private readonly string _configPath;
|
|
private AppConfig _config;
|
|
|
|
public ConfigService()
|
|
{
|
|
_configPath = Path.Combine(AppContext.BaseDirectory, "appsettings.json");
|
|
_config = LoadConfig();
|
|
}
|
|
|
|
public string GetSyncApiEndpoint() => _config.SyncApiEndpoint ?? string.Empty;
|
|
|
|
public void SetSyncApiEndpoint(string endpoint)
|
|
{
|
|
_config.SyncApiEndpoint = endpoint ?? string.Empty;
|
|
SaveConfig();
|
|
}
|
|
|
|
public bool GetScannerConnected() => _config.ScannerConnected;
|
|
|
|
public void SetScannerConnected(bool connected)
|
|
{
|
|
_config.ScannerConnected = connected;
|
|
SaveConfig();
|
|
}
|
|
|
|
public int GetScanTimeoutSeconds() => _config.ScanTimeoutSeconds;
|
|
|
|
public void SetScanTimeoutSeconds(int seconds)
|
|
{
|
|
_config.ScanTimeoutSeconds = seconds;
|
|
SaveConfig();
|
|
}
|
|
|
|
public string GetAdminCardId() => _config.AdminCardId ?? string.Empty;
|
|
|
|
public void SetAdminCardId(string cardId)
|
|
{
|
|
_config.AdminCardId = cardId ?? string.Empty;
|
|
SaveConfig();
|
|
}
|
|
|
|
public string GetSiteId() => _config.SiteId ?? "SITE : 1";
|
|
|
|
public void SetSiteId(string siteId)
|
|
{
|
|
_config.SiteId = string.IsNullOrWhiteSpace(siteId) ? "SITE : 1" : (siteId ?? string.Empty);
|
|
SaveConfig();
|
|
}
|
|
|
|
public string GetDeviceId()
|
|
{
|
|
var id = _config.DeviceId ?? string.Empty;
|
|
if (string.IsNullOrWhiteSpace(id))
|
|
{
|
|
id = Guid.NewGuid().ToString("N");
|
|
_config.DeviceId = id;
|
|
SaveConfig();
|
|
}
|
|
return id;
|
|
}
|
|
|
|
public void SetDeviceId(string deviceId)
|
|
{
|
|
_config.DeviceId = deviceId ?? string.Empty;
|
|
SaveConfig();
|
|
}
|
|
|
|
public bool GetRememberAdminCredentials() => _config.RememberAdminCredentials;
|
|
|
|
public void SetRememberAdminCredentials(bool remember)
|
|
{
|
|
_config.RememberAdminCredentials = remember;
|
|
SaveConfig();
|
|
}
|
|
|
|
public string GetSavedAdminUsername() => _config.SavedAdminUsername ?? string.Empty;
|
|
|
|
public void SetSavedAdminUsername(string username)
|
|
{
|
|
_config.SavedAdminUsername = username ?? string.Empty;
|
|
SaveConfig();
|
|
}
|
|
|
|
public string GetSavedAdminPassword()
|
|
{
|
|
if (!_config.RememberAdminCredentials)
|
|
return string.Empty;
|
|
|
|
var protectedValue = _config.SavedAdminPasswordProtected ?? string.Empty;
|
|
if (string.IsNullOrWhiteSpace(protectedValue))
|
|
return string.Empty;
|
|
|
|
try
|
|
{
|
|
var bytes = Convert.FromBase64String(protectedValue);
|
|
var clear = ProtectedData.Unprotect(bytes, null, DataProtectionScope.CurrentUser);
|
|
return Encoding.UTF8.GetString(clear);
|
|
}
|
|
catch
|
|
{
|
|
return string.Empty;
|
|
}
|
|
}
|
|
|
|
public void SetSavedAdminPassword(string password)
|
|
{
|
|
if (!_config.RememberAdminCredentials)
|
|
{
|
|
_config.SavedAdminPasswordProtected = string.Empty;
|
|
SaveConfig();
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
var clear = Encoding.UTF8.GetBytes(password ?? string.Empty);
|
|
var protectedBytes = ProtectedData.Protect(clear, null, DataProtectionScope.CurrentUser);
|
|
_config.SavedAdminPasswordProtected = Convert.ToBase64String(protectedBytes);
|
|
}
|
|
catch
|
|
{
|
|
_config.SavedAdminPasswordProtected = string.Empty;
|
|
}
|
|
|
|
SaveConfig();
|
|
}
|
|
|
|
private AppConfig LoadConfig()
|
|
{
|
|
try
|
|
{
|
|
if (!File.Exists(_configPath))
|
|
{
|
|
var defaults = new AppConfig();
|
|
SaveConfig(defaults);
|
|
return defaults;
|
|
}
|
|
|
|
var json = File.ReadAllText(_configPath);
|
|
var config = JsonSerializer.Deserialize<AppConfig>(json);
|
|
return config ?? new AppConfig();
|
|
}
|
|
catch
|
|
{
|
|
return new AppConfig();
|
|
}
|
|
}
|
|
|
|
private void SaveConfig()
|
|
{
|
|
SaveConfig(_config);
|
|
}
|
|
|
|
private void SaveConfig(AppConfig config)
|
|
{
|
|
try
|
|
{
|
|
var json = JsonSerializer.Serialize(config, new JsonSerializerOptions
|
|
{
|
|
WriteIndented = true
|
|
});
|
|
File.WriteAllText(_configPath, json);
|
|
}
|
|
catch
|
|
{
|
|
// Ignore write failures (e.g., read-only location).
|
|
}
|
|
}
|
|
|
|
private sealed class AppConfig
|
|
{
|
|
public string SyncApiEndpoint { get; set; } = "https://api.example.com/uind/sync";
|
|
public bool ScannerConnected { get; set; } = false;
|
|
public int ScanTimeoutSeconds { get; set; } = 60;
|
|
public string AdminCardId { get; set; } = "ADMIN";
|
|
public string SiteId { get; set; } = "SITE : 1";
|
|
public string DeviceId { get; set; } = string.Empty;
|
|
|
|
// Admin credential persistence (optional).
|
|
public bool RememberAdminCredentials { get; set; } = false;
|
|
public string SavedAdminUsername { get; set; } = string.Empty;
|
|
public string SavedAdminPasswordProtected { get; set; } = string.Empty;
|
|
}
|
|
}
|