hermes-server/Requests/DeletePolicy.cs
2024-12-27 22:29:54 +00:00

35 lines
1.1 KiB
C#

using HermesSocketServer.Services;
using ILogger = Serilog.ILogger;
namespace HermesSocketServer.Requests
{
public class DeletePolicy : IRequest
{
public string Name => "delete_policy";
public string[] RequiredKeys => ["id"];
private ChannelManager _channels;
private ILogger _logger;
public DeletePolicy(ChannelManager channels, ILogger logger)
{
_channels = channels;
_logger = logger;
}
public async Task<RequestResult> Grant(string sender, IDictionary<string, object>? data)
{
var channel = _channels.Get(sender);
string policyId = data!["id"].ToString()!;
var policy = channel.Policies.Get(policyId);
if (policy != null) {
channel.Policies.Remove(policyId);
_logger.Information($"Deleted a policy by id [policy id: {data["id"]}]");
return RequestResult.Successful(policy.GroupId + "/" + policy.Path);
}
_logger.Warning("Failed to find policy by id ");
return RequestResult.Failed("Cannot find the policy by id.");
}
}
}