2024-10-20 15:32:30 -04:00
|
|
|
using HermesSocketLibrary.db;
|
2024-10-21 16:44:20 -04:00
|
|
|
using HermesSocketServer.Models;
|
2024-10-20 15:32:30 -04:00
|
|
|
|
|
|
|
namespace HermesSocketServer.Store
|
|
|
|
{
|
2024-10-21 16:44:20 -04:00
|
|
|
public class PolicyStore : GroupSaveStore<string, PolicyMessage>
|
2024-10-20 15:32:30 -04:00
|
|
|
{
|
|
|
|
private readonly string _userId;
|
|
|
|
private readonly Database _database;
|
|
|
|
private readonly Serilog.ILogger _logger;
|
2024-10-21 16:44:20 -04:00
|
|
|
private readonly GroupSaveSqlGenerator<PolicyMessage> _generator;
|
2024-10-20 15:32:30 -04:00
|
|
|
|
|
|
|
|
|
|
|
public PolicyStore(string userId, Database database, Serilog.ILogger logger) : base(logger)
|
|
|
|
{
|
|
|
|
_userId = userId;
|
|
|
|
_database = database;
|
|
|
|
_logger = logger;
|
|
|
|
|
|
|
|
var ctp = new Dictionary<string, string>
|
|
|
|
{
|
|
|
|
{ "id", "Id" },
|
|
|
|
{ "userId", "UserId" },
|
|
|
|
{ "groupId", "GroupId" },
|
|
|
|
{ "path", "Path" },
|
|
|
|
{ "count", "Usage" },
|
|
|
|
{ "timespan", "Span" },
|
|
|
|
};
|
2024-10-21 16:44:20 -04:00
|
|
|
_generator = new GroupSaveSqlGenerator<PolicyMessage>(ctp);
|
2024-10-20 15:32:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public override async Task Load()
|
|
|
|
{
|
|
|
|
var data = new Dictionary<string, object>() { { "user", _userId } };
|
|
|
|
string sql = $"SELECT id, \"groupId\", path, count, timespan FROM \"GroupPermissionPolicy\" WHERE \"userId\" = @user";
|
|
|
|
await _database.Execute(sql, data, (reader) =>
|
|
|
|
{
|
2024-10-21 16:44:20 -04:00
|
|
|
var id = reader.GetGuid(0);
|
|
|
|
_store.Add(id.ToString(), new PolicyMessage()
|
2024-10-20 15:32:30 -04:00
|
|
|
{
|
|
|
|
Id = id,
|
|
|
|
UserId = _userId,
|
2024-10-21 16:44:20 -04:00
|
|
|
GroupId = reader.GetGuid(1),
|
2024-10-20 15:32:30 -04:00
|
|
|
Path = reader.GetString(2),
|
|
|
|
Usage = reader.GetInt32(3),
|
2024-10-21 16:44:20 -04:00
|
|
|
Span = reader.GetInt32(4),
|
2024-10-20 15:32:30 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
_logger.Information($"Loaded {_store.Count} policies from database.");
|
|
|
|
}
|
|
|
|
|
2024-10-21 16:44:20 -04:00
|
|
|
protected override void OnInitialAdd(string key, PolicyMessage value)
|
2024-10-20 15:32:30 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-10-21 16:44:20 -04:00
|
|
|
protected override void OnInitialModify(string key, PolicyMessage value)
|
2024-10-20 15:32:30 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnInitialRemove(string key)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public override async Task<bool> Save()
|
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
string sql = string.Empty;
|
|
|
|
|
|
|
|
if (_added.Any())
|
|
|
|
{
|
|
|
|
lock (_lock)
|
|
|
|
{
|
|
|
|
count = _added.Count;
|
|
|
|
sql = _generator.GenerateInsertSql("GroupPermissionPolicy", _added.Select(a => _store[a]), ["id", "userId", "groupId", "path", "count", "timespan"]);
|
|
|
|
_added.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
_logger.Debug($"GroupPermissionPolicy - Adding {count} rows to database: {sql}");
|
2024-10-21 16:44:20 -04:00
|
|
|
await _database.ExecuteScalarTransaction(sql);
|
2024-10-20 15:32:30 -04:00
|
|
|
}
|
|
|
|
if (_modified.Any())
|
|
|
|
{
|
|
|
|
lock (_lock)
|
|
|
|
{
|
|
|
|
count = _modified.Count;
|
|
|
|
sql = _generator.GenerateUpdateSql("GroupPermissionPolicy", _modified.Select(m => _store[m]), ["id"], ["userId", "groupId", "path", "count", "timespan"]);
|
|
|
|
_modified.Clear();
|
|
|
|
}
|
|
|
|
_logger.Debug($"GroupPermissionPolicy - Modifying {count} rows in database: {sql}");
|
2024-10-21 16:44:20 -04:00
|
|
|
await _database.ExecuteScalarTransaction(sql);
|
2024-10-20 15:32:30 -04:00
|
|
|
}
|
|
|
|
if (_deleted.Any())
|
|
|
|
{
|
|
|
|
lock (_lock)
|
|
|
|
{
|
|
|
|
count = _deleted.Count;
|
|
|
|
sql = _generator.GenerateDeleteSql("GroupPermissionPolicy", _deleted, ["id"]);
|
|
|
|
_deleted.Clear();
|
|
|
|
}
|
|
|
|
_logger.Debug($"GroupPermissionPolicy - Deleting {count} rows from database: {sql}");
|
2024-10-21 16:44:20 -04:00
|
|
|
await _database.ExecuteScalarTransaction(sql);
|
2024-10-20 15:32:30 -04:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|