-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
352 additions
and
1,496 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 was deleted.
Oops, something went wrong.
14 changes: 14 additions & 0 deletions
14
src/CleanArch.CrossCuttingConcerns/Identity/ILoggedInUserService.cs
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,14 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace CleanArch.CrossCuttingConcerns.Identity | ||
{ | ||
public interface ILoggedInUserService | ||
{ | ||
bool IsAuthenticated { get; } | ||
string UserId { get; } | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/CleanArch.CrossCuttingConcerns/OS/IDateTimeProvider.cs
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,19 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace CleanArch.CrossCuttingConcerns.OS | ||
{ | ||
public interface IDateTimeProvider | ||
{ | ||
DateTime Now { get; } | ||
|
||
DateTime UtcNow { get; } | ||
|
||
DateTimeOffset OffsetNow { get; } | ||
|
||
DateTimeOffset OffsetUtcNow { get; } | ||
} | ||
} |
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,22 @@ | ||
using CleanArch.Domain.Common; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace CleanArch.Domain.Entities | ||
{ | ||
public class AuditTrail : IHasKey<int> | ||
{ | ||
public int Id { get; set; } | ||
public string UserId { get; set; } | ||
public string Type { get; set; } | ||
public string TableName { get; set; } | ||
public DateTime DateTime { get; set; } | ||
public string OldValues { get; set; } | ||
public string NewValues { get; set; } | ||
public string AffectedColumns { get; set; } | ||
public string PrimaryKey { get; set; } | ||
} | ||
} |
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,16 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace CleanArch.Domain.Enum | ||
{ | ||
public enum AuditType | ||
{ | ||
None = 0, | ||
Create = 1, | ||
Update = 2, | ||
Delete = 3 | ||
} | ||
} |
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
53 changes: 53 additions & 0 deletions
53
src/CleanArch.Infrastructure/Identity/LoggedInUserService.cs
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,53 @@ | ||
using CleanArch.CrossCuttingConcerns.Identity; | ||
using Microsoft.AspNetCore.Http; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Security.Claims; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace CleanArch.Infrastructure.Identity | ||
{ | ||
|
||
// refer CurrentWebUser | ||
public class LoggedInUserService : ILoggedInUserService | ||
{ | ||
private readonly IHttpContextAccessor _context; | ||
public LoggedInUserService(IHttpContextAccessor httpContextAccessor) | ||
{ | ||
_context = httpContextAccessor; | ||
//UserId = httpContextAccessor.HttpContext?.User?.FindFirstValue(ClaimTypes.NameIdentifier); | ||
//Claims = httpContextAccessor.HttpContext?.User?.Claims.AsEnumerable().Select(item => new KeyValuePair<string, string>(item.Type, item.Value)).ToList(); | ||
} | ||
public bool IsAuthenticated | ||
{ | ||
get | ||
{ | ||
return _context.HttpContext.User.Identity.IsAuthenticated; | ||
} | ||
} | ||
|
||
public string UserId | ||
{ | ||
get | ||
{ | ||
var userId = _context.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value | ||
?? _context.HttpContext.User.FindFirst("sub")?.Value; | ||
|
||
return userId; | ||
} | ||
} | ||
|
||
public List<KeyValuePair<string, string>> Claims | ||
{ | ||
get | ||
{ | ||
return _context.HttpContext?.User?.Claims.AsEnumerable().Select(item => new KeyValuePair<string, string>( | ||
item.Type, | ||
item.Value)).ToList(); | ||
} | ||
} | ||
|
||
} | ||
} |
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,20 @@ | ||
using CleanArch.CrossCuttingConcerns.OS; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace CleanArch.Infrastructure.OS | ||
{ | ||
public class DateTimeProvider: IDateTimeProvider | ||
{ | ||
public DateTime Now => DateTime.Now; | ||
|
||
public DateTime UtcNow => DateTime.UtcNow; | ||
|
||
public DateTimeOffset OffsetNow => DateTimeOffset.Now; | ||
|
||
public DateTimeOffset OffsetUtcNow => DateTimeOffset.UtcNow; | ||
} | ||
} |
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,44 @@ | ||
using CleanArch.Domain.Entities; | ||
using CleanArch.Domain.Enum; | ||
using Microsoft.EntityFrameworkCore.ChangeTracking; | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace CleanArch.Persistence | ||
{ | ||
public class AuditEntry | ||
{ | ||
public AuditEntry(EntityEntry entry) | ||
{ | ||
Entry = entry; | ||
} | ||
public EntityEntry Entry { get; } | ||
public string UserId { get; set; } | ||
public string TableName { get; set; } | ||
public Dictionary<string, object> KeyValues { get; } = new Dictionary<string, object>(); | ||
public Dictionary<string, object> OldValues { get; } = new Dictionary<string, object>(); | ||
public Dictionary<string, object> NewValues { get; } = new Dictionary<string, object>(); | ||
public List<PropertyEntry> TemporaryProperties { get; } = new List<PropertyEntry>(); | ||
public AuditType AuditType { get; set; } | ||
public List<string> ChangedColumns { get; } = new List<string>(); | ||
public bool HasTemporaryProperties => TemporaryProperties.Any(); | ||
|
||
public AuditTrail ToAudit() | ||
{ | ||
var audit = new AuditTrail(); | ||
audit.UserId = UserId; | ||
audit.Type = AuditType.ToString(); | ||
audit.TableName = TableName; | ||
audit.DateTime = DateTime.UtcNow; | ||
audit.PrimaryKey = JsonConvert.SerializeObject(KeyValues); | ||
audit.OldValues = OldValues.Count == 0 ? null : JsonConvert.SerializeObject(OldValues); | ||
audit.NewValues = NewValues.Count == 0 ? null : JsonConvert.SerializeObject(NewValues); | ||
audit.AffectedColumns = ChangedColumns.Count == 0 ? null : JsonConvert.SerializeObject(ChangedColumns); | ||
return audit; | ||
} | ||
} | ||
} |
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
46 changes: 36 additions & 10 deletions
46
src/CleanArch.Persistence/Context/ApplicationDbContext.cs
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 |
---|---|---|
@@ -1,45 +1,71 @@ | ||
using CleanArch.Domain.Common; | ||
using CleanArch.CrossCuttingConcerns.Identity; | ||
using CleanArch.CrossCuttingConcerns.OS; | ||
using CleanArch.Domain.Common; | ||
using CleanArch.Domain.Entities; | ||
using CleanArch.Persistence.Context.Seeds.Application; | ||
using Microsoft.EntityFrameworkCore; | ||
using System; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace CleanArch.Persistence.Context | ||
{ | ||
public class ApplicationDbContext : DbContext | ||
public class ApplicationDbContext : AuditableContext | ||
{ | ||
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) | ||
private readonly ILoggedInUserService _loggedInUserService; | ||
private readonly IDateTimeProvider _dateTimeProvider; | ||
|
||
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options, | ||
ILoggedInUserService loggedInUserService, | ||
IDateTimeProvider dateTimeProvider) | ||
: base(options) | ||
{ | ||
ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking; | ||
_loggedInUserService = loggedInUserService; | ||
_dateTimeProvider = dateTimeProvider; | ||
} | ||
|
||
public DbSet<Event> Events { get; set; } | ||
public DbSet<Category> Categories { get; set; } | ||
|
||
protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
protected override void OnModelCreating(ModelBuilder builder) | ||
{ | ||
modelBuilder.ApplyConfigurationsFromAssembly(typeof(ApplicationDbContext).Assembly); | ||
modelBuilder.ApplicationSeed(); | ||
foreach (var property in builder.Model.GetEntityTypes() | ||
.SelectMany(t => t.GetProperties()) | ||
.Where(p => p.ClrType == typeof(decimal) || p.ClrType == typeof(decimal?))) | ||
{ | ||
property.SetColumnType("decimal(18,2)"); | ||
} | ||
|
||
builder.ApplyConfigurationsFromAssembly(typeof(ApplicationDbContext).Assembly); | ||
builder.ApplicationSeed(); | ||
} | ||
|
||
public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken()) | ||
public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken()) | ||
{ | ||
foreach (var entry in ChangeTracker.Entries<AuditableEntity>()) | ||
{ | ||
switch (entry.State) | ||
{ | ||
case EntityState.Added: | ||
entry.Entity.CreatedDate = DateTime.Now; | ||
entry.Entity.CreatedBy = _loggedInUserService.UserId; | ||
entry.Entity.CreatedDate = _dateTimeProvider.UtcNow; | ||
break; | ||
case EntityState.Modified: | ||
entry.Entity.LastModifiedDate = DateTime.Now; | ||
entry.Entity.LastModifiedBy = _loggedInUserService.UserId; | ||
entry.Entity.LastModifiedDate = _dateTimeProvider.UtcNow; | ||
break; | ||
} | ||
} | ||
return base.SaveChangesAsync(cancellationToken); | ||
if (_loggedInUserService.UserId == null) | ||
{ | ||
return await base.SaveChangesAsync(cancellationToken); | ||
} | ||
else | ||
{ | ||
return await base.SaveChangesAsync(_loggedInUserService.UserId); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.