2024-10-21 20:44:20 +00:00
|
|
|
using HermesSocketServer.Models;
|
|
|
|
using ILogger = Serilog.ILogger;
|
|
|
|
|
|
|
|
namespace HermesSocketServer.Requests
|
|
|
|
{
|
|
|
|
public class UpdatePolicy : IRequest
|
|
|
|
{
|
|
|
|
public string Name => "update_policy";
|
|
|
|
public string[] RequiredKeys => ["id", "groupId", "path", "count", "span"];
|
|
|
|
private ILogger _logger;
|
|
|
|
|
2024-12-27 23:54:33 +00:00
|
|
|
public UpdatePolicy(ILogger logger)
|
2024-10-21 20:44:20 +00:00
|
|
|
{
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
2024-12-28 17:24:02 +00:00
|
|
|
public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
|
2024-10-21 20:44:20 +00:00
|
|
|
{
|
|
|
|
var id = Guid.Parse(data["id"].ToString()!);
|
|
|
|
string groupId = data["groupId"].ToString()!;
|
|
|
|
string path = data["path"].ToString()!;
|
|
|
|
int count = int.Parse(data["count"].ToString()!);
|
|
|
|
int span = int.Parse(data["span"].ToString()!);
|
|
|
|
|
|
|
|
bool result = channel.Policies.Set(id.ToString(), new PolicyMessage()
|
|
|
|
{
|
|
|
|
Id = id,
|
2024-12-27 23:31:36 +00:00
|
|
|
UserId = channel.Id,
|
2024-10-21 20:44:20 +00:00
|
|
|
GroupId = Guid.Parse(groupId),
|
|
|
|
Path = path,
|
|
|
|
Usage = count,
|
|
|
|
Span = span,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (result)
|
|
|
|
{
|
|
|
|
var policy = channel.Policies.Get(id.ToString());
|
2024-12-27 23:31:36 +00:00
|
|
|
_logger.Information($"Updated policy to channel [policy id: {id}][group id: {groupId}][path: {path}][count: {count}][span: {span}][channel: {channel.Id}]");
|
2024-12-28 17:24:02 +00:00
|
|
|
return Task.FromResult(RequestResult.Successful(policy));
|
2024-10-21 20:44:20 +00:00
|
|
|
}
|
2024-12-28 17:24:02 +00:00
|
|
|
return Task.FromResult(RequestResult.Failed("Something went wrong when updating the cache."));
|
2024-10-21 20:44:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|