From de5d1b666c4ac99a79ea793492a5964f42c6b55c Mon Sep 17 00:00:00 2001 From: OlgunR Date: Wed, 13 May 2026 09:05:04 +0200 Subject: [PATCH] Add current user service and use for catalog audit fields Introduce ICurrentUserService and its implementation to access the current user's username. Inject this service into CreateCatalogHandler and UpdateCatalogHandler to set AddedWho and ChangedWho fields dynamically. Register the service and IHttpContextAccessor in DI, and enable authentication middleware. Update project and using statements accordingly. --- DbFirst.API/DbFirst.API.csproj | 2 +- DbFirst.API/Program.cs | 6 ++++++ DbFirst.API/Services/CurrentUserService.cs | 10 ++++++++++ .../Abstractions/ICurrentUserService.cs | 7 +++++++ .../Catalogs/Commands/CreateCatalogHandler.cs | 9 ++++++--- .../Catalogs/Commands/UpdateCatalogHandler.cs | 7 +++++-- 6 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 DbFirst.API/Services/CurrentUserService.cs create mode 100644 DbFirst.Application/Abstractions/ICurrentUserService.cs diff --git a/DbFirst.API/DbFirst.API.csproj b/DbFirst.API/DbFirst.API.csproj index 20cf89e..9f1aa16 100644 --- a/DbFirst.API/DbFirst.API.csproj +++ b/DbFirst.API/DbFirst.API.csproj @@ -1,4 +1,4 @@ - + net8.0 diff --git a/DbFirst.API/Program.cs b/DbFirst.API/Program.cs index 350b3bd..702c5a7 100644 --- a/DbFirst.API/Program.cs +++ b/DbFirst.API/Program.cs @@ -1,7 +1,9 @@ using DbFirst.API.Dashboards; using DbFirst.API.Hubs; using DbFirst.API.Middleware; +using DbFirst.API.Services; using DbFirst.Application; +using DbFirst.Application.Abstractions; using DbFirst.Infrastructure; using DevExpress.AspNetCore; using DevExpress.DashboardAspNetCore; @@ -51,6 +53,9 @@ builder.Services.AddSingleton builder.Services.AddScoped(sp => DashboardConfiguratorFactory.Create(sp, builder.Configuration, builder.Environment)); +builder.Services.AddHttpContextAccessor(); +builder.Services.AddScoped(); + var app = builder.Build(); // Configure the HTTP request pipeline. @@ -65,6 +70,7 @@ app.UseMiddleware(); app.UseDevExpressControls(); app.UseHttpsRedirection(); app.UseCors(); +app.UseAuthentication(); app.UseAuthorization(); app.MapDashboardRoute("api/dashboard", "DefaultDashboard"); diff --git a/DbFirst.API/Services/CurrentUserService.cs b/DbFirst.API/Services/CurrentUserService.cs new file mode 100644 index 0000000..6846003 --- /dev/null +++ b/DbFirst.API/Services/CurrentUserService.cs @@ -0,0 +1,10 @@ +using DbFirst.Application.Abstractions; + +namespace DbFirst.API.Services +{ + public class CurrentUserService(IHttpContextAccessor httpContextAccessor) : ICurrentUserService + { + public string UserName => + httpContextAccessor.HttpContext?.User.Identity?.Name ?? "unknown"; + } +} diff --git a/DbFirst.Application/Abstractions/ICurrentUserService.cs b/DbFirst.Application/Abstractions/ICurrentUserService.cs new file mode 100644 index 0000000..837a51f --- /dev/null +++ b/DbFirst.Application/Abstractions/ICurrentUserService.cs @@ -0,0 +1,7 @@ +namespace DbFirst.Application.Abstractions +{ + public interface ICurrentUserService + { + string UserName { get; } + } +} \ No newline at end of file diff --git a/DbFirst.Application/Catalogs/Commands/CreateCatalogHandler.cs b/DbFirst.Application/Catalogs/Commands/CreateCatalogHandler.cs index 26bd3d4..6e5f68b 100644 --- a/DbFirst.Application/Catalogs/Commands/CreateCatalogHandler.cs +++ b/DbFirst.Application/Catalogs/Commands/CreateCatalogHandler.cs @@ -1,4 +1,5 @@ using AutoMapper; +using DbFirst.Application.Abstractions; using DbFirst.Application.Repositories; using DbFirst.Contracts.Catalogs; using DbFirst.Domain.Entities; @@ -10,11 +11,13 @@ public class CreateCatalogHandler : IRequestHandler Handle(CreateCatalogCommand request, CancellationToken cancellationToken) @@ -26,9 +29,9 @@ public class CreateCatalogHandler : IRequestHandler(request.Dto); - entity.AddedWho = "system"; + entity.AddedWho = _currentUserService.UserName; entity.AddedWhen = DateTime.UtcNow; - entity.ChangedWho = "system"; + entity.ChangedWho = _currentUserService.UserName; entity.ChangedWhen = DateTime.UtcNow; var created = await _repository.InsertAsync(entity, cancellationToken); diff --git a/DbFirst.Application/Catalogs/Commands/UpdateCatalogHandler.cs b/DbFirst.Application/Catalogs/Commands/UpdateCatalogHandler.cs index bedb479..c3f56d2 100644 --- a/DbFirst.Application/Catalogs/Commands/UpdateCatalogHandler.cs +++ b/DbFirst.Application/Catalogs/Commands/UpdateCatalogHandler.cs @@ -1,4 +1,5 @@ using AutoMapper; +using DbFirst.Application.Abstractions; using DbFirst.Application.Repositories; using DbFirst.Contracts.Catalogs; using DbFirst.Domain; @@ -11,11 +12,13 @@ public class UpdateCatalogHandler : IRequestHandler Handle(UpdateCatalogCommand request, CancellationToken cancellationToken) @@ -36,7 +39,7 @@ public class UpdateCatalogHandler : IRequestHandler