You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
2.0 KiB

2 months ago
using System.IO;
using System.Text.RegularExpressions;
namespace Housing.Services;
public sealed class LoginSettings
{
2 months ago
private const string DefaultProcedure = "dbo.CheckOperator";
public string Procedure { get; set; } = DefaultProcedure;
2 months ago
public bool OfflinePreview { get; set; }
public static LoginSettings Load(string filePath)
{
if (!File.Exists(filePath))
{
2 months ago
throw new FileNotFoundException("Database.ini file was not found.", filePath);
2 months ago
}
var values = IniFile.LoadSection(filePath, "Login");
return new LoginSettings
{
2 months ago
Procedure = GetString(values, "Procedure", DefaultProcedure),
2 months ago
OfflinePreview = GetBool(values, "OfflinePreview", false)
};
}
2 months ago
public string GetProcedureName()
2 months ago
{
2 months ago
var parts = Procedure
2 months ago
.Split('.', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
.ToArray();
2 months ago
if (parts.Length is 0 or > 3 || parts.Any(part => !IsSqlIdentifier(part)))
2 months ago
{
2 months ago
throw new InvalidOperationException("Check Database.ini [Login] Procedure value.");
2 months ago
}
return string.Join(".", parts);
}
private static string GetString(Dictionary<string, string> values, string key, string defaultValue)
{
return values.TryGetValue(key, out var value) && !string.IsNullOrWhiteSpace(value)
? value.Trim()
: defaultValue;
}
private static bool GetBool(Dictionary<string, string> values, string key, bool defaultValue)
{
if (!values.TryGetValue(key, out var value) || string.IsNullOrWhiteSpace(value))
{
return defaultValue;
}
return value.Trim().ToUpperInvariant() switch
{
"1" or "TRUE" or "YES" or "ON" => true,
"0" or "FALSE" or "NO" or "OFF" => false,
_ => defaultValue
};
}
2 months ago
private static bool IsSqlIdentifier(string value)
2 months ago
{
2 months ago
return Regex.IsMatch(value, @"^[A-Za-z_][A-Za-z0-9_]*$");
2 months ago
}
}