49 lines
1.7 KiB
C#
49 lines
1.7 KiB
C#
using HermesSocketServer.Models;
|
|
using HermesSocketServer.Services;
|
|
using ILogger = Serilog.ILogger;
|
|
|
|
namespace HermesSocketServer.Requests
|
|
{
|
|
public class CreatePolicy : IRequest
|
|
{
|
|
public string Name => "create_policy";
|
|
public string[] RequiredKeys => ["groupId", "path", "count", "span"];
|
|
private ChannelManager _channels;
|
|
private ILogger _logger;
|
|
|
|
public CreatePolicy(ChannelManager channels, ILogger logger)
|
|
{
|
|
_channels = channels;
|
|
_logger = logger;
|
|
}
|
|
|
|
public async Task<RequestResult> Grant(string sender, IDictionary<string, object>? data)
|
|
{
|
|
var id = Guid.NewGuid();
|
|
string groupId = data["groupId"].ToString()!;
|
|
string path = data["path"].ToString()!;
|
|
int count = int.Parse(data["count"].ToString()!);
|
|
int span = int.Parse(data["span"].ToString()!);
|
|
|
|
var policy = new PolicyMessage()
|
|
{
|
|
Id = id,
|
|
UserId = sender,
|
|
GroupId = Guid.Parse(groupId),
|
|
Path = path,
|
|
Usage = count,
|
|
Span = span,
|
|
};
|
|
|
|
var channel = _channels.Get(sender);
|
|
bool result = channel.Policies.Set(id.ToString(), policy);
|
|
|
|
if (result)
|
|
{
|
|
_logger.Information($"Added policy to channel [policy id: {id}][group id: {groupId}][path: {path}][count: {count}][span: {span}][channel: {sender}]");
|
|
return RequestResult.Successful(policy);
|
|
}
|
|
return RequestResult.Failed("Something went wrong when updating the cache.");
|
|
}
|
|
}
|
|
} |