169 lines
6.9 KiB
C#
169 lines
6.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using gehGassi.Core.Interfaces;
|
|
using gehGassi.Domain.Common;
|
|
|
|
namespace gehGassi.Core.Services
|
|
{
|
|
/// <summary>
|
|
/// Service der die Verwaltung von Audits realisiert
|
|
/// </summary>
|
|
public class AuditService : ServiceBase<Audit>, IAuditService
|
|
{
|
|
private readonly IRepository<AuditWithNames> _auditWithNamesRepository;
|
|
|
|
/// <summary>
|
|
/// Erstellt eine Instanz
|
|
/// </summary>
|
|
/// <param name="unitOfWork">Instanz eines IUnitOfWork</param>
|
|
public AuditService(IUnitOfWork unitOfWork) : base(unitOfWork)
|
|
{
|
|
_auditWithNamesRepository = unitOfWork.GetRepository<AuditWithNames>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine gefilterte Liste von audits zurück
|
|
/// </summary>
|
|
/// <param name="filter">Filterbegriff der in bestimmten Feldern gesucht wird</param>
|
|
/// <returns>List betroffener Kunden</returns>
|
|
public IQueryable<Audit> Filter(string filter)
|
|
{
|
|
return Repository.Query(c => c.Table.Contains(filter) || c.UserName.Contains(filter));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine Liste von Audits zurück
|
|
/// </summary>
|
|
/// <param name="key">Key des Objektes</param>
|
|
/// <param name="table">Typ des Objektes (Table)</param>
|
|
/// <returns>Liste von audits</returns>
|
|
public async Task<List<Audit>> GetListAsync(string key, string table)
|
|
{
|
|
return (await Repository.FindAsync(c => c.Key == key && c.Table == table)).ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine Liste von Audits zurück
|
|
/// </summary>
|
|
/// <param name="key">Key des Objektes</param>
|
|
/// <param name="table">Typ des Objektes (Table)</param>
|
|
/// <param name="action">
|
|
/// Durchgeführte Aktion</param>
|
|
/// <returns>Liste von audits</returns>
|
|
public async Task<List<Audit>> GetListAsync(string key, string table, AuditAction action)
|
|
{
|
|
return (await Repository.FindAsync(c => c.Key == key && c.Table == table && c.Action == action)).ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt einen Audit-Eintrag zurück
|
|
/// </summary>
|
|
/// <param name="key">Key des Objektes</param>
|
|
/// <param name="table">Typ des Objektes (Table)</param>
|
|
/// <param name="action">
|
|
/// Durchgeführte Aktion</param>
|
|
/// <returns></returns>
|
|
public async Task<Audit> GetAsync(string key, string table, AuditAction action)
|
|
{
|
|
return await Repository.FirstOrDefaultAsync(c => c.Key == key && c.Table == table && c.Action == action);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine gefilterte Liste von audits zurück
|
|
/// </summary>
|
|
/// <param name="filter">Filterbegriff der in bestimmten Feldern gesucht wird</param>
|
|
/// <returns>List betroffener Kunden</returns>
|
|
public IQueryable<AuditWithNames> FilterWithNames(string filter)
|
|
{
|
|
return _auditWithNamesRepository.Query(c => c.Table.Contains(filter) || c.UserName.Contains(filter));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine Liste von Audits zurück
|
|
/// </summary>
|
|
/// <param name="key">Key des Objektes</param>
|
|
/// <param name="table">Typ des Objektes (Table)</param>
|
|
/// <returns>Liste von audits</returns>
|
|
public async Task<List<AuditWithNames>> GetListWithNamesAsync(string key, string table)
|
|
{
|
|
return (await _auditWithNamesRepository.FindAsync(c => c.Key == key && c.Table == table)).ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine Liste von Audits zurück
|
|
/// </summary>
|
|
/// <param name="key">Key des Objektes</param>
|
|
/// <param name="table">Typ des Objektes (Table)</param>
|
|
/// <param name="action">
|
|
/// Durchgeführte Aktion</param>
|
|
/// <returns>Liste von audits</returns>
|
|
public async Task<List<AuditWithNames>> GetListWithNamesAsync(string key, string table, AuditAction action)
|
|
{
|
|
return (await _auditWithNamesRepository.FindAsync(c => c.Key == key && c.Table == table)).ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt einen Audit-Eintrag zurück
|
|
/// </summary>
|
|
/// <param name="key">Key des Objektes</param>
|
|
/// <param name="table">Typ des Objektes (Table)</param>
|
|
/// <param name="action">
|
|
/// Durchgeführte Aktion</param>
|
|
/// <returns></returns>
|
|
public async Task<AuditWithNames> GetWithNamesAsync(string key, string table, AuditAction action)
|
|
{
|
|
return await _auditWithNamesRepository.FirstOrDefaultAsync(c => c.Key == key && c.Table == table && c.Action == action);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Löschen von Audit-Einträgen
|
|
/// </summary>
|
|
/// <param name="key">Key des Objektes</param>
|
|
/// <param name="table">Typ des Objektes (Table)</param>
|
|
/// <returns>Anzahl gelöschte Daten</returns>
|
|
public async Task<int> DeleteAsync(string key, string table)
|
|
{
|
|
return await Repository.ExecuteRawSqlAsync("DELETE FROM Audits WHERE [Key] = @key AND [Table] = @table", new Dictionary<string, object> { { "@key", key }, { "@table", table } });
|
|
}
|
|
|
|
/// <summary>
|
|
/// Anonymisieren von Audit-Einträgen
|
|
/// </summary>
|
|
/// <param name="key">Key des Objektes</param>
|
|
/// <param name="table">Typ des Objektes (Table)</param>
|
|
/// <returns>Anzahl anonymisierte Daten</returns>
|
|
public async Task<int> AnonymizeAsync(string key, string table)
|
|
{
|
|
var anonymized = $"{{\"AnonymizedDate\":\"{DateTimeOffset.UtcNow:O}\"}}";
|
|
return await Repository.ExecuteRawSqlAsync("UPDATE Audits SET OldValues = @anonymized, NewValues = @anonymized WHERE [Key] = @key AND [Table] = @table", new Dictionary<string, object> { { "@anonymized", anonymized }, { "@key", key }, { "@table", table } });
|
|
}
|
|
|
|
/// <summary>
|
|
/// Loggen des Zugriffs eines Users auf einen Datensatz
|
|
/// </summary>
|
|
/// <param name="key">Key des Objektes</param>
|
|
/// <param name="table">Typ des Objektes (Table)</param>
|
|
/// <param name="userName">Benutzer der den Zugriff durchgeführt hat</param>
|
|
/// <returns>Task</returns>
|
|
public async Task LogAccessAsync(string key, string table, string userName)
|
|
{
|
|
var audit = new Audit()
|
|
{
|
|
Key = key,
|
|
Table = table,
|
|
UserName = userName,
|
|
Action = AuditAction.Access,
|
|
DateTime = DateTimeOffset.UtcNow,
|
|
KeyValues = string.Empty,
|
|
OldValues = string.Empty,
|
|
NewValues = string.Empty
|
|
};
|
|
|
|
Repository.Add(audit);
|
|
await CommitAsync(userName);
|
|
}
|
|
}
|
|
}
|