IW4M-Admin/SharedLibraryCore/Services/ChangeHistoryService.cs

110 lines
4.0 KiB
C#
Raw Permalink Normal View History

2022-01-26 11:32:16 -05:00
using System;
2019-02-17 14:16:48 -05:00
using System.Linq;
using System.Threading.Tasks;
using Data.Abstractions;
using Data.Models;
2022-01-26 11:32:16 -05:00
using Data.Models.Client;
using Microsoft.Extensions.Logging;
namespace SharedLibraryCore.Services
{
public class ChangeHistoryService
{
private readonly IDatabaseContextFactory _contextFactory;
2022-01-26 11:32:16 -05:00
private readonly ILogger _logger;
public ChangeHistoryService(ILogger<ChangeHistoryService> logger, IDatabaseContextFactory contextFactory)
{
_logger = logger;
_contextFactory = contextFactory;
}
public async Task Add(GameEvent e)
{
EFChangeHistory change = null;
switch (e.Type)
{
case GameEvent.EventType.Ban:
2022-01-26 11:32:16 -05:00
change = new EFChangeHistory
{
OriginEntityId = e.Origin.ClientId,
TargetEntityId = e.Target.ClientId,
ImpersonationEntityId = e.ImpersonationOrigin?.ClientId,
TypeOfChange = EFChangeHistory.ChangeType.Ban,
Comment = e.Data
};
break;
case GameEvent.EventType.Command:
2019-02-17 19:48:40 -05:00
// this prevents passwords/tokens being logged into the database in plain text
2019-02-17 14:16:48 -05:00
if (e.Extra is Command cmd)
{
if (cmd.Name == "login" || cmd.Name == "setpassword")
{
2022-01-26 11:32:16 -05:00
e.Message = string.Join(' ',
e.Message.Split(" ").Select((arg, index) => index > 0 ? "*****" : arg));
2019-02-17 14:16:48 -05:00
}
}
2022-01-26 11:32:16 -05:00
change = new EFChangeHistory
2018-09-23 20:45:54 -04:00
{
OriginEntityId = e.Origin.ClientId,
TargetEntityId = e.Target?.ClientId ?? 0,
ImpersonationEntityId = e.ImpersonationOrigin?.ClientId,
2018-09-23 20:45:54 -04:00
Comment = "Executed command",
CurrentValue = e.Message,
TypeOfChange = EFChangeHistory.ChangeType.Command
};
break;
case GameEvent.EventType.ChangePermission:
2022-01-26 11:32:16 -05:00
change = new EFChangeHistory
{
OriginEntityId = e.Origin.ClientId,
TargetEntityId = e.Target.ClientId,
ImpersonationEntityId = e.ImpersonationOrigin?.ClientId,
Comment = "Changed permission level",
TypeOfChange = EFChangeHistory.ChangeType.Permission,
CurrentValue = ((EFClient.Permission)e.Extra).ToString()
};
break;
case GameEvent.EventType.Login:
2022-01-26 11:32:16 -05:00
change = new EFChangeHistory
{
OriginEntityId = e.Origin.ClientId,
Comment = "Logged In To Webfront",
TypeOfChange = EFChangeHistory.ChangeType.Command,
CurrentValue = e.Data
};
break;
case GameEvent.EventType.Logout:
2022-01-26 11:32:16 -05:00
change = new EFChangeHistory
{
OriginEntityId = e.Origin.ClientId,
Comment = "Logged Out of Webfront",
TypeOfChange = EFChangeHistory.ChangeType.Command,
CurrentValue = e.Data
};
break;
}
if (change == null)
{
return;
}
await using var context = _contextFactory.CreateContext(false);
context.EFChangeHistory.Add(change);
try
{
await context.SaveChangesAsync();
}
catch (Exception ex)
{
_logger.LogError(ex, "Could not persist change @{change}", change);
}
}
}
2022-01-26 11:32:16 -05:00
}