-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #593 from osu-tournament-rating/feature/audit-blaming
API automatic audit blaming
- Loading branch information
Showing
7 changed files
with
52 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using API.Utilities.Extensions; | ||
using Database.Entities.Interfaces; | ||
using Database.Interceptors; | ||
using JetBrains.Annotations; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace API.Utilities; | ||
|
||
/// <summary> | ||
/// Extends the <see cref="AuditingInterceptor"/> to automatically blame actions on the currently authenticated user | ||
/// </summary> | ||
/// <param name="httpContextAccessor">Http context accessor</param> | ||
[UsedImplicitly] | ||
public class AuditBlamingInterceptor(IHttpContextAccessor httpContextAccessor) : AuditingInterceptor | ||
{ | ||
private readonly HttpContext? _httpContext = httpContextAccessor.HttpContext; | ||
|
||
protected override void OnSavingChanges(DbContext context) | ||
{ | ||
// Generate audits | ||
base.OnSavingChanges(context); | ||
|
||
// Get the currently authenticated user's id from the http context | ||
if (_httpContext == null || _httpContext.User.Identity is { IsAuthenticated: false }) | ||
{ | ||
return; | ||
} | ||
|
||
if (!_httpContext.User.TryGetSubjectId(out var subjectId)) | ||
{ | ||
return; | ||
} | ||
|
||
// Blame any created audits | ||
context.ChangeTracker | ||
.Entries<IAuditEntity>() | ||
.ToList() | ||
.ForEach(entry => entry.Entity.ActionUserId = subjectId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters