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.
This commit is contained in:
OlgunR
2026-05-13 09:05:04 +02:00
parent 91ee044b73
commit de5d1b666c
6 changed files with 35 additions and 6 deletions

View File

@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>

View File

@@ -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<IDashboardChangeNotifier, DashboardChangeNotifier>
builder.Services.AddScoped<DashboardConfigurator>(sp =>
DashboardConfiguratorFactory.Create(sp, builder.Configuration, builder.Environment));
builder.Services.AddHttpContextAccessor();
builder.Services.AddScoped<ICurrentUserService, CurrentUserService>();
var app = builder.Build();
// Configure the HTTP request pipeline.
@@ -65,6 +70,7 @@ app.UseMiddleware<ExceptionHandlingMiddleware>();
app.UseDevExpressControls();
app.UseHttpsRedirection();
app.UseCors();
app.UseAuthentication();
app.UseAuthorization();
app.MapDashboardRoute("api/dashboard", "DefaultDashboard");

View File

@@ -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";
}
}

View File

@@ -0,0 +1,7 @@
namespace DbFirst.Application.Abstractions
{
public interface ICurrentUserService
{
string UserName { get; }
}
}

View File

@@ -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<CreateCatalogCommand, Catalo
{
private readonly ICatalogRepository _repository;
private readonly IMapper _mapper;
private readonly ICurrentUserService _currentUserService;
public CreateCatalogHandler(ICatalogRepository repository, IMapper mapper)
public CreateCatalogHandler(ICatalogRepository repository, IMapper mapper, ICurrentUserService currentUserService)
{
_repository = repository;
_mapper = mapper;
_currentUserService = currentUserService;
}
public async Task<CatalogReadDto?> Handle(CreateCatalogCommand request, CancellationToken cancellationToken)
@@ -26,9 +29,9 @@ public class CreateCatalogHandler : IRequestHandler<CreateCatalogCommand, Catalo
}
var entity = _mapper.Map<VwmyCatalog>(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);

View File

@@ -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<UpdateCatalogCommand, Catalo
{
private readonly ICatalogRepository _repository;
private readonly IMapper _mapper;
private readonly ICurrentUserService _currentUserService;
public UpdateCatalogHandler(ICatalogRepository repository, IMapper mapper)
public UpdateCatalogHandler(ICatalogRepository repository, IMapper mapper, ICurrentUserService currentUserService)
{
_repository = repository;
_mapper = mapper;
_currentUserService = currentUserService;
}
public async Task<CatalogReadDto?> Handle(UpdateCatalogCommand request, CancellationToken cancellationToken)
@@ -36,7 +39,7 @@ public class UpdateCatalogHandler : IRequestHandler<UpdateCatalogCommand, Catalo
entity.Guid = request.Id;
entity.AddedWho = existing.AddedWho;
entity.AddedWhen = existing.AddedWhen;
entity.ChangedWho = "system";
entity.ChangedWho = _currentUserService.UserName;
entity.ChangedWhen = DateTime.UtcNow;
var updated = await _repository.UpdateAsync(request.Id, entity, request.Dto.UpdateProcedure, cancellationToken);