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.
68 lines
2.0 KiB
68 lines
2.0 KiB
using System.IO;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace Housing.Services;
|
|
|
|
public sealed class LoginSettings
|
|
{
|
|
private const string DefaultProcedure = "dbo.CheckOperator";
|
|
|
|
public string Procedure { get; set; } = DefaultProcedure;
|
|
public bool OfflinePreview { get; set; }
|
|
|
|
public static LoginSettings Load(string filePath)
|
|
{
|
|
if (!File.Exists(filePath))
|
|
{
|
|
throw new FileNotFoundException("Database.ini file was not found.", filePath);
|
|
}
|
|
|
|
var values = IniFile.LoadSection(filePath, "Login");
|
|
return new LoginSettings
|
|
{
|
|
Procedure = GetString(values, "Procedure", DefaultProcedure),
|
|
OfflinePreview = GetBool(values, "OfflinePreview", false)
|
|
};
|
|
}
|
|
|
|
public string GetProcedureName()
|
|
{
|
|
var parts = Procedure
|
|
.Split('.', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
|
|
.ToArray();
|
|
|
|
if (parts.Length is 0 or > 3 || parts.Any(part => !IsSqlIdentifier(part)))
|
|
{
|
|
throw new InvalidOperationException("Check Database.ini [Login] Procedure value.");
|
|
}
|
|
|
|
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
|
|
};
|
|
}
|
|
|
|
private static bool IsSqlIdentifier(string value)
|
|
{
|
|
return Regex.IsMatch(value, @"^[A-Za-z_][A-Za-z0-9_]*$");
|
|
}
|
|
}
|
|
|