-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
ChangeHistoryService.cs
110 lines (100 loc) · 4.04 KB
/
ChangeHistoryService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System;
using System.Linq;
using System.Threading.Tasks;
using Data.Abstractions;
using Data.Models;
using Data.Models.Client;
using Microsoft.Extensions.Logging;
namespace SharedLibraryCore.Services
{
public class ChangeHistoryService
{
private readonly IDatabaseContextFactory _contextFactory;
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:
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:
// this prevents passwords/tokens being logged into the database in plain text
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));
}
}
change = new EFChangeHistory
{
OriginEntityId = e.Origin.ClientId,
TargetEntityId = e.Target?.ClientId ?? 0,
ImpersonationEntityId = e.ImpersonationOrigin?.ClientId,
Comment = "Executed command",
CurrentValue = e.Message,
TypeOfChange = EFChangeHistory.ChangeType.Command
};
break;
case GameEvent.EventType.ChangePermission:
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:
change = new EFChangeHistory
{
OriginEntityId = e.Origin.ClientId,
Comment = "Logged In To Webfront",
TypeOfChange = EFChangeHistory.ChangeType.Command,
CurrentValue = e.Data
};
break;
case GameEvent.EventType.Logout:
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);
}
}
}
}