69 lines
3.0 KiB
C#
69 lines
3.0 KiB
C#
using HermesSocketLibrary.db;
|
|
using HermesSocketLibrary.Requests.Messages;
|
|
using HermesSocketServer.Store.Internal;
|
|
|
|
namespace HermesSocketServer.Store
|
|
{
|
|
public class GroupPermissionStore : AutoSavedStore<string, GroupPermission>
|
|
{
|
|
private readonly string _userId;
|
|
private readonly IStore<string, Group> _groups;
|
|
private readonly Database _database;
|
|
private readonly Serilog.ILogger _logger;
|
|
|
|
|
|
public GroupPermissionStore(string userId, DatabaseTable table, IStore<string, Group> 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<string, object>() { { "user", _userId } };
|
|
string sql = $"SELECT id, \"groupId\", path, allow FROM \"GroupPermission\" WHERE \"userId\" = @user";
|
|
await _database.Execute(sql, data, async (reader) =>
|
|
{
|
|
var id = reader.GetGuid(0).ToString();
|
|
_store.Add(id, new GroupPermission()
|
|
{
|
|
Id = id,
|
|
UserId = _userId,
|
|
GroupId = reader.GetGuid(1),
|
|
Path = reader.GetString(2),
|
|
Allow = await reader.IsDBNullAsync(3) ? null : reader.GetBoolean(3),
|
|
});
|
|
});
|
|
_logger.Information($"Loaded {_store.Count} group permissions from database.");
|
|
}
|
|
|
|
protected override void OnInitialAdd(string key, GroupPermission value)
|
|
{
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(key, nameof(key));
|
|
ArgumentNullException.ThrowIfNull(value, nameof(value));
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(value.UserId, nameof(value.UserId));
|
|
ArgumentNullException.ThrowIfNull(value.GroupId, nameof(value.GroupId));
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(value.Path, nameof(value.Path));
|
|
|
|
if (_groups.Get(value.GroupId.ToString()) == null)
|
|
throw new ArgumentException("The group id does not exist.");
|
|
}
|
|
|
|
protected override void OnInitialModify(string key, GroupPermission oldValue, GroupPermission newValue)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(newValue, nameof(newValue));
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(newValue.UserId, nameof(newValue.UserId));
|
|
ArgumentNullException.ThrowIfNull(newValue.GroupId, nameof(newValue.GroupId));
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(newValue.Path, nameof(newValue.Path));
|
|
ArgumentOutOfRangeException.ThrowIfNotEqual(oldValue.UserId, newValue.UserId, nameof(oldValue.UserId));
|
|
ArgumentOutOfRangeException.ThrowIfNotEqual(oldValue.GroupId, newValue.GroupId, nameof(oldValue.GroupId));
|
|
}
|
|
|
|
protected override void OnPostRemove(string key, GroupPermission value)
|
|
{
|
|
}
|
|
}
|
|
} |