hermes-server/Requests/CreatePolicy.cs
2024-12-28 17:24:02 +00:00

44 lines
1.5 KiB
C#

using HermesSocketServer.Models;
using ILogger = Serilog.ILogger;
namespace HermesSocketServer.Requests
{
public class CreatePolicy : IRequest
{
public string Name => "create_policy";
public string[] RequiredKeys => ["groupId", "path", "count", "span"];
private ILogger _logger;
public CreatePolicy(ILogger logger)
{
_logger = logger;
}
public Task<RequestResult> Grant(Channel channel, 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 = channel.Id,
GroupId = Guid.Parse(groupId),
Path = path,
Usage = count,
Span = span,
};
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: {channel.Id}]");
return Task.FromResult(RequestResult.Successful(policy));
}
return Task.FromResult(RequestResult.Failed("Something went wrong when updating the cache."));
}
}
}