using HermesSocketLibrary.db; using HermesSocketLibrary.Requests.Messages; using HermesSocketServer.Messages; using HermesSocketServer.Store.Internal; namespace HermesSocketServer.Store { public class PolicyStore : AutoSavedStore { private readonly string _userId; private readonly IStore _groups; private readonly Database _database; private readonly Serilog.ILogger _logger; public PolicyStore(string userId, DatabaseTable table, IStore groups, Database database, Serilog.ILogger logger) : base(table, database, logger) { _userId = userId; _groups = groups; _database = database; _logger = logger; } public override async Task Load() { var data = new Dictionary() { { "user", _userId } }; string sql = $"SELECT id, \"groupId\", path, count, timespan FROM \"GroupPermissionPolicy\" WHERE \"userId\" = @user"; await _database.Execute(sql, data, (reader) => { var id = reader.GetGuid(0); _store.Add(id.ToString(), new Policy() { Id = id, UserId = _userId, GroupId = reader.GetGuid(1), Path = reader.GetString(2), Usage = reader.GetInt32(3), Span = reader.GetInt32(4), }); }); _logger.Information($"Loaded {_store.Count} policies from database."); } protected override void OnInitialAdd(string key, Policy value) { ArgumentException.ThrowIfNullOrWhiteSpace(key, nameof(key)); ArgumentNullException.ThrowIfNull(value, nameof(value)); ArgumentException.ThrowIfNullOrWhiteSpace(value.UserId, nameof(value.UserId)); ArgumentOutOfRangeException.ThrowIfEqual(value.GroupId, default, nameof(value.GroupId)); ArgumentOutOfRangeException.ThrowIfNegativeOrZero(value.Usage, nameof(value.Usage)); ArgumentOutOfRangeException.ThrowIfGreaterThan(value.Usage, 99, nameof(value.Usage)); ArgumentOutOfRangeException.ThrowIfNegativeOrZero(value.Span, nameof(value.Span)); ArgumentOutOfRangeException.ThrowIfLessThan(value.Span, 1000, nameof(value.Span)); ArgumentOutOfRangeException.ThrowIfGreaterThan(value.Span, 86400, nameof(value.Span)); if (_groups.Get(value.GroupId.ToString()) == null) throw new ArgumentException("The group id does not exist."); } protected override void OnInitialModify(string key, Policy oldValue, Policy newValue) { ArgumentNullException.ThrowIfNull(newValue, nameof(newValue)); ArgumentException.ThrowIfNullOrWhiteSpace(newValue.UserId, nameof(newValue.UserId)); ArgumentOutOfRangeException.ThrowIfEqual(newValue.GroupId, default, nameof(newValue.GroupId)); ArgumentOutOfRangeException.ThrowIfNegativeOrZero(newValue.Usage, nameof(newValue.Usage)); ArgumentOutOfRangeException.ThrowIfGreaterThan(newValue.Usage, 99, nameof(newValue.Usage)); ArgumentOutOfRangeException.ThrowIfNegativeOrZero(newValue.Span, nameof(newValue.Span)); ArgumentOutOfRangeException.ThrowIfLessThan(newValue.Span, 1000, nameof(newValue.Span)); ArgumentOutOfRangeException.ThrowIfGreaterThan(newValue.Span, 86400, nameof(newValue.Span)); ArgumentOutOfRangeException.ThrowIfNotEqual(oldValue.Id, newValue.Id, nameof(oldValue.Id)); ArgumentOutOfRangeException.ThrowIfNotEqual(oldValue.UserId, newValue.UserId, nameof(oldValue.UserId)); } protected override void OnPostRemove(string key, Policy value) { } } }