feat: add Logs
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
using System.Security.Claims;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.IdentityModel.JsonWebTokens;
|
||||
using mws.backend.dotnet.application.Common;
|
||||
|
||||
namespace mws.backend.dotnet.api.Authentication;
|
||||
|
||||
public class HttpCurrentUser(IHttpContextAccessor accessor) : ICurrentUser
|
||||
{
|
||||
public Guid? UserId
|
||||
{
|
||||
get
|
||||
{
|
||||
var value = accessor.HttpContext?.User.FindFirstValue(JwtRegisteredClaimNames.Sub)
|
||||
?? accessor.HttpContext?.User.FindFirstValue(ClaimTypes.NameIdentifier)
|
||||
?? accessor.HttpContext?.User.FindFirstValue("sub");
|
||||
return value is not null && Guid.TryParse(value, out var id) ? id : null;
|
||||
}
|
||||
}
|
||||
|
||||
public string? Username =>
|
||||
accessor.HttpContext?.User.FindFirstValue(ClaimTypes.Name)
|
||||
?? accessor.HttpContext?.User.FindFirstValue("name");
|
||||
|
||||
public string? IpAddress
|
||||
{
|
||||
get
|
||||
{
|
||||
var context = accessor.HttpContext;
|
||||
if (context is null) return null;
|
||||
var forwarded = context.Request.Headers["X-Forwarded-For"].FirstOrDefault();
|
||||
if (!string.IsNullOrWhiteSpace(forwarded))
|
||||
{
|
||||
var first = forwarded.Split(',')[0].Trim();
|
||||
if (first.Length > 0) return Truncate(first, 45);
|
||||
}
|
||||
return Truncate(context.Connection.RemoteIpAddress?.ToString(), 45);
|
||||
}
|
||||
}
|
||||
|
||||
public string? UserAgent =>
|
||||
Truncate(accessor.HttpContext?.Request.Headers.UserAgent.ToString(), 512);
|
||||
|
||||
private static string? Truncate(string? value, int max) =>
|
||||
string.IsNullOrEmpty(value) ? value : (value.Length <= max ? value : value[..max]);
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using MediatR;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using mws.backend.dotnet.application.Audit;
|
||||
using mws.backend.dotnet.application.Common;
|
||||
|
||||
namespace mws.backend.dotnet.api.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/audit")]
|
||||
[Authorize]
|
||||
public class AuditController(ISender sender) : ControllerBase
|
||||
{
|
||||
[HttpGet("actions")]
|
||||
public async Task<ActionResult<PagedResult<ActionLogDto>>> GetActions(
|
||||
[FromQuery] DateTimeOffset? from,
|
||||
[FromQuery] DateTimeOffset? to,
|
||||
[FromQuery] string? actor,
|
||||
[FromQuery] string? action,
|
||||
[FromQuery] string? entityType,
|
||||
[FromQuery] Guid? entityId,
|
||||
[FromQuery] int page = 1,
|
||||
[FromQuery] int pageSize = 20,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
var filter = new ActionLogFilter(from, to, actor, action, entityType, entityId);
|
||||
return Ok(await sender.Send(new GetActionLogsQuery(User.GetUserId(), filter, page, pageSize), ct));
|
||||
}
|
||||
|
||||
[HttpGet("logins")]
|
||||
public async Task<ActionResult<PagedResult<LoginLogDto>>> GetLogins(
|
||||
[FromQuery] DateTimeOffset? from,
|
||||
[FromQuery] DateTimeOffset? to,
|
||||
[FromQuery] string? username,
|
||||
[FromQuery] bool? success,
|
||||
[FromQuery] int page = 1,
|
||||
[FromQuery] int pageSize = 20,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
var filter = new LoginLogFilter(from, to, username, success);
|
||||
return Ok(await sender.Send(new GetLoginLogsQuery(User.GetUserId(), filter, page, pageSize), ct));
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,6 @@ using Microsoft.EntityFrameworkCore;
|
||||
using mws.backend.dotnet.application.Common;
|
||||
using mws.backend.dotnet.application.Permissions;
|
||||
using mws.backend.dotnet.application.Users;
|
||||
using mws.backend.dotnet.domain.Users;
|
||||
using mws.backend.dotnet.infrastructure.Persistence;
|
||||
|
||||
namespace mws.backend.dotnet.api.Controllers;
|
||||
@@ -95,32 +94,14 @@ public class UsersController(ISender sender, AppDbContext db, IPermissionService
|
||||
[HttpPost("{id:guid}/roles")]
|
||||
public async Task<IActionResult> AssignRole(Guid id, [FromBody] AssignRoleRequest req, CancellationToken ct)
|
||||
{
|
||||
await permissions.EnsureAsync(User.GetUserId(), "permissions", PermissionAction.Edit, ct);
|
||||
|
||||
var user = await db.Users.Include(u => u.UserRoles).FirstOrDefaultAsync(u => u.Id == id, ct);
|
||||
if (user is null) return NotFound("User not found");
|
||||
|
||||
if (!user.UserRoles.Any(ur => ur.RoleId == req.RoleId))
|
||||
{
|
||||
user.UserRoles.Add(new UserRole { UserId = id, RoleId = req.RoleId });
|
||||
await db.SaveChangesAsync(ct);
|
||||
}
|
||||
|
||||
await sender.Send(new AssignUserRoleCommand(User.GetUserId(), id, req.RoleId), ct);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpDelete("{id:guid}/roles/{roleId:guid}")]
|
||||
public async Task<IActionResult> UnassignRole(Guid id, Guid roleId, CancellationToken ct)
|
||||
{
|
||||
await permissions.EnsureAsync(User.GetUserId(), "permissions", PermissionAction.Edit, ct);
|
||||
|
||||
var ur = await db.UserRoles.FirstOrDefaultAsync(x => x.UserId == id && x.RoleId == roleId, ct);
|
||||
if (ur != null)
|
||||
{
|
||||
db.UserRoles.Remove(ur);
|
||||
await db.SaveChangesAsync(ct);
|
||||
}
|
||||
|
||||
await sender.Send(new UnassignUserRoleCommand(User.GetUserId(), id, roleId), ct);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
|
||||
@@ -3,8 +3,10 @@ using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using Microsoft.OpenApi;
|
||||
using mws.backend.dotnet.api.Authentication;
|
||||
using mws.backend.dotnet.api.Middleware;
|
||||
using mws.backend.dotnet.application.Auth;
|
||||
using mws.backend.dotnet.application.Common;
|
||||
using mws.backend.dotnet.infrastructure;
|
||||
using mws.backend.dotnet.infrastructure.Persistence;
|
||||
|
||||
@@ -46,6 +48,8 @@ builder.Services.AddSwaggerGen(options =>
|
||||
});
|
||||
|
||||
builder.Services.AddInfrastructure(builder.Configuration);
|
||||
builder.Services.AddHttpContextAccessor();
|
||||
builder.Services.AddScoped<ICurrentUser, HttpCurrentUser>();
|
||||
|
||||
var jwtSection = builder.Configuration.GetSection("Jwt");
|
||||
var jwtSecret = jwtSection["Secret"];
|
||||
|
||||
Reference in New Issue
Block a user