feat: add Logs

This commit is contained in:
2026-09-20 16:21:15 +07:00
parent 637ca51f47
commit d3c3ecd5e6
62 changed files with 1577 additions and 66 deletions
+8 -1
View File
@@ -4,6 +4,8 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using MediatR;
using mws.backend.dotnet.application.Audit;
using mws.backend.dotnet.application.Auth;
using mws.backend.dotnet.application.Common;
using mws.backend.dotnet.application.Documents;
@@ -25,7 +27,12 @@ public static class DependencyInjection
services.AddScoped<IUnitOfWork, UnitOfWork>();
services.AddAutoMapper(cfg => { }, typeof(MappingProfile).Assembly);
services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(typeof(IUnitOfWork).Assembly));
services.AddMediatR(cfg =>
{
cfg.RegisterServicesFromAssembly(typeof(IUnitOfWork).Assembly);
cfg.AddOpenBehavior(typeof(AuditLogBehavior<,>));
});
services.AddScoped<IAuditContext, AuditContext>();
services.AddScoped<IPasswordHasher, BcryptPasswordHasher>();
@@ -0,0 +1,594 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using mws.backend.dotnet.infrastructure.Persistence;
#nullable disable
namespace mws.backend.dotnet.infrastructure.Migrations
{
[DbContext(typeof(AppDbContext))]
[Migration("20260920085021_AddAuditLogs")]
partial class AddAuditLogs
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "10.0.11")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("mws.backend.dotnet.domain.Audit.ActionLog", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<string>("Action")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("character varying(100)");
b.Property<Guid>("ActorUserId")
.HasColumnType("uuid");
b.Property<string>("ActorUsername")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("character varying(100)");
b.Property<DateTimeOffset>("CreatedAt")
.HasColumnType("timestamp with time zone");
b.Property<Guid?>("EntityId")
.HasColumnType("uuid");
b.Property<string>("EntityName")
.HasMaxLength(255)
.HasColumnType("character varying(255)");
b.Property<string>("EntityType")
.IsRequired()
.HasMaxLength(50)
.HasColumnType("character varying(50)");
b.Property<string>("IpAddress")
.HasMaxLength(45)
.HasColumnType("character varying(45)");
b.Property<string>("UserAgent")
.HasMaxLength(512)
.HasColumnType("character varying(512)");
b.HasKey("Id");
b.HasIndex("Action");
b.HasIndex("ActorUserId");
b.HasIndex("CreatedAt");
b.HasIndex("EntityType");
b.ToTable("action_logs", (string)null);
});
modelBuilder.Entity("mws.backend.dotnet.domain.Audit.LoginLog", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<DateTimeOffset>("CreatedAt")
.HasColumnType("timestamp with time zone");
b.Property<string>("FailureReason")
.HasMaxLength(100)
.HasColumnType("character varying(100)");
b.Property<string>("IpAddress")
.HasMaxLength(45)
.HasColumnType("character varying(45)");
b.Property<bool>("Success")
.HasColumnType("boolean");
b.Property<string>("UserAgent")
.HasMaxLength(512)
.HasColumnType("character varying(512)");
b.Property<Guid?>("UserId")
.HasColumnType("uuid");
b.Property<string>("Username")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("character varying(100)");
b.HasKey("Id");
b.HasIndex("CreatedAt");
b.HasIndex("Success");
b.HasIndex("Username");
b.ToTable("login_logs", (string)null);
});
modelBuilder.Entity("mws.backend.dotnet.domain.Documents.Document", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<string>("ContentHash")
.HasMaxLength(64)
.HasColumnType("character varying(64)");
b.Property<long?>("ContentSize")
.HasColumnType("bigint");
b.Property<DateTime>("CreatedAt")
.HasColumnType("timestamp with time zone");
b.Property<Guid>("CreatedBy")
.HasColumnType("uuid");
b.Property<Guid?>("ParentId")
.HasColumnType("uuid");
b.Property<Guid>("ProjectId")
.HasColumnType("uuid");
b.Property<string>("StorageKey")
.HasMaxLength(512)
.HasColumnType("character varying(512)");
b.Property<string>("Title")
.IsRequired()
.HasMaxLength(300)
.HasColumnType("character varying(300)");
b.Property<string>("Type")
.IsRequired()
.HasMaxLength(20)
.HasColumnType("character varying(20)");
b.Property<DateTime>("UpdatedAt")
.HasColumnType("timestamp with time zone");
b.Property<Guid?>("UpdatedBy")
.HasColumnType("uuid");
b.Property<DateTime?>("UpdatedContentAt")
.HasColumnType("timestamp with time zone");
b.HasKey("Id");
b.HasIndex("ParentId");
b.HasIndex("ProjectId", "ParentId");
b.ToTable("documents", (string)null);
});
modelBuilder.Entity("mws.backend.dotnet.domain.MasterData.MasterDataEntry", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<DateTime>("CreatedAt")
.HasColumnType("timestamp with time zone");
b.Property<string>("Group")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("character varying(100)");
b.Property<bool>("IsActive")
.HasColumnType("boolean");
b.Property<string>("Label")
.IsRequired()
.HasMaxLength(200)
.HasColumnType("character varying(200)");
b.Property<int>("SortOrder")
.HasColumnType("integer");
b.Property<DateTime>("UpdatedAt")
.HasColumnType("timestamp with time zone");
b.Property<string>("Value")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("character varying(100)");
b.HasKey("Id");
b.HasIndex("Group", "Value")
.IsUnique();
b.ToTable("master_data", (string)null);
});
modelBuilder.Entity("mws.backend.dotnet.domain.Projects.Project", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<DateTime>("CreatedAt")
.HasColumnType("timestamp with time zone");
b.Property<Guid>("CreatedBy")
.HasColumnType("uuid");
b.Property<string>("Description")
.HasMaxLength(2000)
.HasColumnType("character varying(2000)");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(200)
.HasColumnType("character varying(200)");
b.Property<string>("Status")
.IsRequired()
.HasMaxLength(20)
.HasColumnType("character varying(20)");
b.Property<DateTime>("UpdatedAt")
.HasColumnType("timestamp with time zone");
b.Property<Guid?>("UpdatedBy")
.HasColumnType("uuid");
b.HasKey("Id");
b.HasIndex("CreatedBy");
b.HasIndex("UpdatedBy");
b.ToTable("projects", (string)null);
});
modelBuilder.Entity("mws.backend.dotnet.domain.Projects.ProjectMember", b =>
{
b.Property<Guid>("ProjectId")
.HasColumnType("uuid");
b.Property<Guid>("UserId")
.HasColumnType("uuid");
b.Property<string>("Role")
.IsRequired()
.HasMaxLength(20)
.HasColumnType("character varying(20)");
b.HasKey("ProjectId", "UserId");
b.HasIndex("UserId");
b.ToTable("project_members", (string)null);
});
modelBuilder.Entity("mws.backend.dotnet.domain.Projects.ProjectMemberPermission", b =>
{
b.Property<Guid>("ProjectId")
.HasColumnType("uuid");
b.Property<Guid>("UserId")
.HasColumnType("uuid");
b.Property<string>("Screen")
.HasMaxLength(50)
.HasColumnType("character varying(50)");
b.Property<bool>("CanCreate")
.HasColumnType("boolean");
b.Property<bool>("CanDelete")
.HasColumnType("boolean");
b.Property<bool>("CanEdit")
.HasColumnType("boolean");
b.Property<bool>("CanView")
.HasColumnType("boolean");
b.HasKey("ProjectId", "UserId", "Screen");
b.ToTable("project_member_permissions", (string)null);
});
modelBuilder.Entity("mws.backend.dotnet.domain.Roles.Role", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<DateTime>("CreatedAt")
.HasColumnType("timestamp with time zone");
b.Property<bool>("IsSystem")
.HasColumnType("boolean");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("character varying(100)");
b.Property<DateTime>("UpdatedAt")
.HasColumnType("timestamp with time zone");
b.HasKey("Id");
b.HasIndex("Name")
.IsUnique();
b.ToTable("roles", (string)null);
});
modelBuilder.Entity("mws.backend.dotnet.domain.Roles.RolePermission", b =>
{
b.Property<Guid>("RoleId")
.HasColumnType("uuid");
b.Property<string>("Screen")
.HasMaxLength(50)
.HasColumnType("character varying(50)");
b.Property<bool>("CanCreate")
.HasColumnType("boolean");
b.Property<bool>("CanDelete")
.HasColumnType("boolean");
b.Property<bool>("CanEdit")
.HasColumnType("boolean");
b.Property<bool>("CanView")
.HasColumnType("boolean");
b.HasKey("RoleId", "Screen");
b.ToTable("role_permissions", (string)null);
});
modelBuilder.Entity("mws.backend.dotnet.domain.Tasks.TaskItem", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<Guid?>("AssigneeId")
.HasColumnType("uuid");
b.Property<DateTime>("CreatedAt")
.HasColumnType("timestamp with time zone");
b.Property<Guid>("CreatedBy")
.HasColumnType("uuid");
b.Property<string>("Description")
.HasColumnType("text");
b.Property<DateTime?>("DueDate")
.HasColumnType("timestamp with time zone");
b.Property<string>("Priority")
.IsRequired()
.HasMaxLength(20)
.HasColumnType("character varying(20)");
b.Property<Guid>("ProjectId")
.HasColumnType("uuid");
b.Property<string>("Status")
.IsRequired()
.HasMaxLength(20)
.HasColumnType("character varying(20)");
b.Property<string>("Title")
.IsRequired()
.HasMaxLength(300)
.HasColumnType("character varying(300)");
b.Property<DateTime>("UpdatedAt")
.HasColumnType("timestamp with time zone");
b.HasKey("Id");
b.HasIndex("AssigneeId");
b.HasIndex("ProjectId");
b.HasIndex("Status");
b.ToTable("tasks", (string)null);
});
modelBuilder.Entity("mws.backend.dotnet.domain.Users.User", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<DateTime>("CreatedAt")
.HasColumnType("timestamp with time zone");
b.Property<string>("DisplayName")
.IsRequired()
.HasMaxLength(150)
.HasColumnType("character varying(150)");
b.Property<bool>("IsActive")
.HasColumnType("boolean");
b.Property<string>("PasswordHash")
.IsRequired()
.HasMaxLength(200)
.HasColumnType("character varying(200)");
b.Property<DateTime>("UpdatedAt")
.HasColumnType("timestamp with time zone");
b.Property<string>("Username")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("character varying(100)");
b.HasKey("Id");
b.HasIndex("Username")
.IsUnique();
b.ToTable("users", (string)null);
});
modelBuilder.Entity("mws.backend.dotnet.domain.Users.UserRole", b =>
{
b.Property<Guid>("UserId")
.HasColumnType("uuid");
b.Property<Guid>("RoleId")
.HasColumnType("uuid");
b.HasKey("UserId", "RoleId");
b.HasIndex("RoleId");
b.ToTable("user_roles", (string)null);
});
modelBuilder.Entity("mws.backend.dotnet.domain.Documents.Document", b =>
{
b.HasOne("mws.backend.dotnet.domain.Documents.Document", "Parent")
.WithMany("Children")
.HasForeignKey("ParentId")
.OnDelete(DeleteBehavior.Restrict);
b.Navigation("Parent");
});
modelBuilder.Entity("mws.backend.dotnet.domain.Projects.Project", b =>
{
b.HasOne("mws.backend.dotnet.domain.Users.User", "CreatedByUser")
.WithMany()
.HasForeignKey("CreatedBy")
.OnDelete(DeleteBehavior.Restrict)
.IsRequired();
b.HasOne("mws.backend.dotnet.domain.Users.User", "UpdatedByUser")
.WithMany()
.HasForeignKey("UpdatedBy")
.OnDelete(DeleteBehavior.Restrict);
b.Navigation("CreatedByUser");
b.Navigation("UpdatedByUser");
});
modelBuilder.Entity("mws.backend.dotnet.domain.Projects.ProjectMember", b =>
{
b.HasOne("mws.backend.dotnet.domain.Projects.Project", "Project")
.WithMany("Members")
.HasForeignKey("ProjectId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("mws.backend.dotnet.domain.Users.User", "User")
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Project");
b.Navigation("User");
});
modelBuilder.Entity("mws.backend.dotnet.domain.Projects.ProjectMemberPermission", b =>
{
b.HasOne("mws.backend.dotnet.domain.Projects.ProjectMember", null)
.WithMany()
.HasForeignKey("ProjectId", "UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("mws.backend.dotnet.domain.Roles.RolePermission", b =>
{
b.HasOne("mws.backend.dotnet.domain.Roles.Role", "Role")
.WithMany("Permissions")
.HasForeignKey("RoleId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Role");
});
modelBuilder.Entity("mws.backend.dotnet.domain.Tasks.TaskItem", b =>
{
b.HasOne("mws.backend.dotnet.domain.Users.User", "Assignee")
.WithMany()
.HasForeignKey("AssigneeId")
.OnDelete(DeleteBehavior.SetNull);
b.Navigation("Assignee");
});
modelBuilder.Entity("mws.backend.dotnet.domain.Users.UserRole", b =>
{
b.HasOne("mws.backend.dotnet.domain.Roles.Role", "Role")
.WithMany("UserRoles")
.HasForeignKey("RoleId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("mws.backend.dotnet.domain.Users.User", "User")
.WithMany("UserRoles")
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Role");
b.Navigation("User");
});
modelBuilder.Entity("mws.backend.dotnet.domain.Documents.Document", b =>
{
b.Navigation("Children");
});
modelBuilder.Entity("mws.backend.dotnet.domain.Projects.Project", b =>
{
b.Navigation("Members");
});
modelBuilder.Entity("mws.backend.dotnet.domain.Roles.Role", b =>
{
b.Navigation("Permissions");
b.Navigation("UserRoles");
});
modelBuilder.Entity("mws.backend.dotnet.domain.Users.User", b =>
{
b.Navigation("UserRoles");
});
#pragma warning restore 612, 618
}
}
}
@@ -0,0 +1,98 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace mws.backend.dotnet.infrastructure.Migrations
{
/// <inheritdoc />
public partial class AddAuditLogs : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "action_logs",
columns: table => new
{
Id = table.Column<Guid>(type: "uuid", nullable: false),
ActorUserId = table.Column<Guid>(type: "uuid", nullable: false),
ActorUsername = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
Action = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
EntityType = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false),
EntityId = table.Column<Guid>(type: "uuid", nullable: true),
EntityName = table.Column<string>(type: "character varying(255)", maxLength: 255, nullable: true),
IpAddress = table.Column<string>(type: "character varying(45)", maxLength: 45, nullable: true),
UserAgent = table.Column<string>(type: "character varying(512)", maxLength: 512, nullable: true),
CreatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_action_logs", x => x.Id);
});
migrationBuilder.CreateTable(
name: "login_logs",
columns: table => new
{
Id = table.Column<Guid>(type: "uuid", nullable: false),
Username = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
UserId = table.Column<Guid>(type: "uuid", nullable: true),
Success = table.Column<bool>(type: "boolean", nullable: false),
FailureReason = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true),
IpAddress = table.Column<string>(type: "character varying(45)", maxLength: 45, nullable: true),
UserAgent = table.Column<string>(type: "character varying(512)", maxLength: 512, nullable: true),
CreatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_login_logs", x => x.Id);
});
migrationBuilder.CreateIndex(
name: "IX_action_logs_Action",
table: "action_logs",
column: "Action");
migrationBuilder.CreateIndex(
name: "IX_action_logs_ActorUserId",
table: "action_logs",
column: "ActorUserId");
migrationBuilder.CreateIndex(
name: "IX_action_logs_CreatedAt",
table: "action_logs",
column: "CreatedAt");
migrationBuilder.CreateIndex(
name: "IX_action_logs_EntityType",
table: "action_logs",
column: "EntityType");
migrationBuilder.CreateIndex(
name: "IX_login_logs_CreatedAt",
table: "login_logs",
column: "CreatedAt");
migrationBuilder.CreateIndex(
name: "IX_login_logs_Success",
table: "login_logs",
column: "Success");
migrationBuilder.CreateIndex(
name: "IX_login_logs_Username",
table: "login_logs",
column: "Username");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "action_logs");
migrationBuilder.DropTable(
name: "login_logs");
}
}
}
@@ -22,6 +22,104 @@ namespace mws.backend.dotnet.infrastructure.Migrations
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("mws.backend.dotnet.domain.Audit.ActionLog", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<string>("Action")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("character varying(100)");
b.Property<Guid>("ActorUserId")
.HasColumnType("uuid");
b.Property<string>("ActorUsername")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("character varying(100)");
b.Property<DateTimeOffset>("CreatedAt")
.HasColumnType("timestamp with time zone");
b.Property<Guid?>("EntityId")
.HasColumnType("uuid");
b.Property<string>("EntityName")
.HasMaxLength(255)
.HasColumnType("character varying(255)");
b.Property<string>("EntityType")
.IsRequired()
.HasMaxLength(50)
.HasColumnType("character varying(50)");
b.Property<string>("IpAddress")
.HasMaxLength(45)
.HasColumnType("character varying(45)");
b.Property<string>("UserAgent")
.HasMaxLength(512)
.HasColumnType("character varying(512)");
b.HasKey("Id");
b.HasIndex("Action");
b.HasIndex("ActorUserId");
b.HasIndex("CreatedAt");
b.HasIndex("EntityType");
b.ToTable("action_logs", (string)null);
});
modelBuilder.Entity("mws.backend.dotnet.domain.Audit.LoginLog", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<DateTimeOffset>("CreatedAt")
.HasColumnType("timestamp with time zone");
b.Property<string>("FailureReason")
.HasMaxLength(100)
.HasColumnType("character varying(100)");
b.Property<string>("IpAddress")
.HasMaxLength(45)
.HasColumnType("character varying(45)");
b.Property<bool>("Success")
.HasColumnType("boolean");
b.Property<string>("UserAgent")
.HasMaxLength(512)
.HasColumnType("character varying(512)");
b.Property<Guid?>("UserId")
.HasColumnType("uuid");
b.Property<string>("Username")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("character varying(100)");
b.HasKey("Id");
b.HasIndex("CreatedAt");
b.HasIndex("Success");
b.HasIndex("Username");
b.ToTable("login_logs", (string)null);
});
modelBuilder.Entity("mws.backend.dotnet.domain.Documents.Document", b =>
{
b.Property<Guid>("Id")
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using mws.backend.dotnet.domain.Audit;
using mws.backend.dotnet.domain.Documents;
using mws.backend.dotnet.domain.MasterData;
using mws.backend.dotnet.domain.Projects;
@@ -20,6 +21,8 @@ public class AppDbContext(DbContextOptions<AppDbContext> options) : DbContext(op
public DbSet<Role> Roles => Set<Role>();
public DbSet<RolePermission> RolePermissions => Set<RolePermission>();
public DbSet<MasterDataEntry> MasterData => Set<MasterDataEntry>();
public DbSet<ActionLog> ActionLogs => Set<ActionLog>();
public DbSet<LoginLog> LoginLogs => Set<LoginLog>();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
@@ -0,0 +1,24 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using mws.backend.dotnet.domain.Audit;
namespace mws.backend.dotnet.infrastructure.Persistence.Configuration;
public class ActionLogConfiguration : IEntityTypeConfiguration<ActionLog>
{
public void Configure(EntityTypeBuilder<ActionLog> e)
{
e.ToTable("action_logs");
e.HasKey(x => x.Id);
e.Property(x => x.ActorUsername).HasMaxLength(100).IsRequired();
e.Property(x => x.Action).HasMaxLength(100).IsRequired();
e.Property(x => x.EntityType).HasMaxLength(50).IsRequired();
e.Property(x => x.EntityName).HasMaxLength(255);
e.Property(x => x.IpAddress).HasMaxLength(45);
e.Property(x => x.UserAgent).HasMaxLength(512);
e.HasIndex(x => x.CreatedAt);
e.HasIndex(x => x.ActorUserId);
e.HasIndex(x => x.Action);
e.HasIndex(x => x.EntityType);
}
}
@@ -0,0 +1,21 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using mws.backend.dotnet.domain.Audit;
namespace mws.backend.dotnet.infrastructure.Persistence.Configuration;
public class LoginLogConfiguration : IEntityTypeConfiguration<LoginLog>
{
public void Configure(EntityTypeBuilder<LoginLog> e)
{
e.ToTable("login_logs");
e.HasKey(x => x.Id);
e.Property(x => x.Username).HasMaxLength(100).IsRequired();
e.Property(x => x.FailureReason).HasMaxLength(100);
e.Property(x => x.IpAddress).HasMaxLength(45);
e.Property(x => x.UserAgent).HasMaxLength(512);
e.HasIndex(x => x.CreatedAt);
e.HasIndex(x => x.Username);
e.HasIndex(x => x.Success);
}
}
+1 -1
View File
@@ -141,7 +141,7 @@ public static class DbSeeder
"Member",
screen => screen.Key switch
{
"users" or "permissions" or "masterdata" => (View: false, Create: false, Edit: false, Delete: false),
"users" or "permissions" or "masterdata" or "actionlogs" or "loginlogs" => (View: false, Create: false, Edit: false, Delete: false),
"projects" => (View: true, Create: false, Edit: false, Delete: false),
_ => (View: true, Create: true, Edit: true, Delete: false),
},
@@ -0,0 +1,35 @@
using mws.backend.dotnet.application.Audit;
using mws.backend.dotnet.application.Common;
using mws.backend.dotnet.application.Common.Repositories;
using mws.backend.dotnet.domain.Audit;
namespace mws.backend.dotnet.infrastructure.Persistence.Repositories;
public class ActionLogRepository(AppDbContext db) : RepositoryBase<ActionLog>(db), IActionLogRepository
{
public Task<PagedResult<ActionLog>> SearchAsync(ActionLogFilter filter, int page, int pageSize, CancellationToken ct = default)
{
var query = Set.AsQueryable();
if (filter.From is { } from) query = query.Where(x => x.CreatedAt >= from);
if (filter.To is { } to) query = query.Where(x => x.CreatedAt <= to);
if (!string.IsNullOrWhiteSpace(filter.Actor))
{
var lower = filter.Actor.Trim().ToLower();
query = query.Where(x => x.ActorUsername.ToLower().Contains(lower));
}
if (!string.IsNullOrWhiteSpace(filter.Action))
{
var lower = filter.Action.Trim().ToLower();
query = query.Where(x => x.Action.ToLower().Contains(lower));
}
if (!string.IsNullOrWhiteSpace(filter.EntityType))
{
var lower = filter.EntityType.Trim().ToLower();
query = query.Where(x => x.EntityType.ToLower().Contains(lower));
}
if (filter.EntityId is { } entityId) query = query.Where(x => x.EntityId == entityId);
return query.OrderByDescending(x => x.CreatedAt).ToPagedResultAsync(page, pageSize, ct);
}
}
@@ -0,0 +1,25 @@
using mws.backend.dotnet.application.Audit;
using mws.backend.dotnet.application.Common;
using mws.backend.dotnet.application.Common.Repositories;
using mws.backend.dotnet.domain.Audit;
namespace mws.backend.dotnet.infrastructure.Persistence.Repositories;
public class LoginLogRepository(AppDbContext db) : RepositoryBase<LoginLog>(db), ILoginLogRepository
{
public Task<PagedResult<LoginLog>> SearchAsync(LoginLogFilter filter, int page, int pageSize, CancellationToken ct = default)
{
var query = Set.AsQueryable();
if (filter.From is { } from) query = query.Where(x => x.CreatedAt >= from);
if (filter.To is { } to) query = query.Where(x => x.CreatedAt <= to);
if (!string.IsNullOrWhiteSpace(filter.Username))
{
var lower = filter.Username.Trim().ToLower();
query = query.Where(x => x.Username.ToLower().Contains(lower));
}
if (filter.Success is { } success) query = query.Where(x => x.Success == success);
return query.OrderByDescending(x => x.CreatedAt).ToPagedResultAsync(page, pageSize, ct);
}
}
+4
View File
@@ -17,6 +17,8 @@ public class UnitOfWork : IUnitOfWork
Documents = new DocumentRepository(db);
Roles = new RoleRepository(db);
MasterData = new MasterDataRepository(db);
ActionLogs = new ActionLogRepository(db);
LoginLogs = new LoginLogRepository(db);
}
public IUserRepository Users { get; }
@@ -25,6 +27,8 @@ public class UnitOfWork : IUnitOfWork
public IDocumentRepository Documents { get; }
public IRoleRepository Roles { get; }
public IMasterDataRepository MasterData { get; }
public IActionLogRepository ActionLogs { get; }
public ILoginLogRepository LoginLogs { get; }
public Task<int> SaveChangesAsync(CancellationToken ct = default) => _db.SaveChangesAsync(ct);
}