2018-09-16 16:34:16 -04:00
|
|
|
|
using SharedLibraryCore.Database;
|
|
|
|
|
using SharedLibraryCore.Database.Models;
|
|
|
|
|
using SharedLibraryCore.Interfaces;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2019-02-17 14:16:48 -05:00
|
|
|
|
using System.Linq;
|
2018-09-16 16:34:16 -04:00
|
|
|
|
using System.Threading.Tasks;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
2018-09-16 16:34:16 -04:00
|
|
|
|
|
|
|
|
|
namespace SharedLibraryCore.Services
|
|
|
|
|
{
|
|
|
|
|
public class ChangeHistoryService : IEntityService<EFChangeHistory>
|
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
|
|
|
|
public ChangeHistoryService(ILogger<ChangeHistoryService> logger)
|
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
public Task<EFChangeHistory> Create(EFChangeHistory entity)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-02 21:20:37 -04:00
|
|
|
|
public async Task<EFChangeHistory> Add(GameEvent e, DatabaseContext ctx = null)
|
2018-09-16 16:34:16 -04:00
|
|
|
|
{
|
|
|
|
|
EFChangeHistory change = null;
|
|
|
|
|
|
|
|
|
|
switch (e.Type)
|
|
|
|
|
{
|
|
|
|
|
case GameEvent.EventType.Ban:
|
|
|
|
|
change = new EFChangeHistory()
|
|
|
|
|
{
|
|
|
|
|
OriginEntityId = e.Origin.ClientId,
|
|
|
|
|
TargetEntityId = e.Target.ClientId,
|
2020-04-26 22:12:49 -04:00
|
|
|
|
ImpersonationEntityId = e.ImpersonationOrigin?.ClientId,
|
2018-09-16 16:34:16 -04:00
|
|
|
|
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")
|
|
|
|
|
{
|
|
|
|
|
e.Message = string.Join(' ', e.Message.Split(" ").Select((arg, index) => index > 0 ? "*****" : arg));
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-23 20:45:54 -04:00
|
|
|
|
change = new EFChangeHistory()
|
|
|
|
|
{
|
|
|
|
|
OriginEntityId = e.Origin.ClientId,
|
|
|
|
|
TargetEntityId = e.Target?.ClientId ?? 0,
|
2020-04-26 22:12:49 -04:00
|
|
|
|
ImpersonationEntityId = e.ImpersonationOrigin?.ClientId,
|
2018-09-23 20:45:54 -04:00
|
|
|
|
Comment = "Executed command",
|
|
|
|
|
CurrentValue = e.Message,
|
|
|
|
|
TypeOfChange = EFChangeHistory.ChangeType.Command
|
|
|
|
|
};
|
2018-09-16 16:34:16 -04:00
|
|
|
|
break;
|
|
|
|
|
case GameEvent.EventType.ChangePermission:
|
|
|
|
|
change = new EFChangeHistory()
|
|
|
|
|
{
|
|
|
|
|
OriginEntityId = e.Origin.ClientId,
|
|
|
|
|
TargetEntityId = e.Target.ClientId,
|
2020-04-26 22:12:49 -04:00
|
|
|
|
ImpersonationEntityId = e.ImpersonationOrigin?.ClientId,
|
2019-04-02 21:20:37 -04:00
|
|
|
|
Comment = "Changed permission level",
|
2018-09-16 16:34:16 -04:00
|
|
|
|
TypeOfChange = EFChangeHistory.ChangeType.Permission,
|
2019-03-24 22:34:20 -04:00
|
|
|
|
CurrentValue = ((EFClient.Permission)e.Extra).ToString()
|
2018-09-16 16:34:16 -04:00
|
|
|
|
};
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (change != null)
|
|
|
|
|
{
|
2019-04-02 21:20:37 -04:00
|
|
|
|
bool existingCtx = ctx != null;
|
|
|
|
|
ctx = ctx ?? new DatabaseContext(true);
|
|
|
|
|
|
|
|
|
|
ctx.EFChangeHistory.Add(change);
|
|
|
|
|
|
|
|
|
|
try
|
2018-09-16 16:34:16 -04:00
|
|
|
|
{
|
2019-04-02 21:20:37 -04:00
|
|
|
|
await ctx.SaveChangesAsync();
|
|
|
|
|
}
|
2018-09-16 16:34:16 -04:00
|
|
|
|
|
2019-04-02 21:20:37 -04:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
_logger.LogError(ex, "Could not persist change @{change}", change);
|
2019-04-02 21:20:37 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
if (!existingCtx)
|
2018-09-16 16:34:16 -04:00
|
|
|
|
{
|
2019-04-02 21:20:37 -04:00
|
|
|
|
ctx.Dispose();
|
2018-09-16 16:34:16 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return change;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task<EFChangeHistory> Delete(EFChangeHistory entity)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task<IList<EFChangeHistory>> Find(Func<EFChangeHistory, bool> expression)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task<EFChangeHistory> Get(int entityID)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task<EFChangeHistory> GetUnique(long entityProperty)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task<EFChangeHistory> Update(EFChangeHistory entity)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|