Guest User

Untitled

a guest
Oct 19th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. public class DBContextWithUserAuditing : IdentityDbContext<ApplicationUser, ApplicationRole, string>
  2. {
  3. public string UserId { get; set; }
  4. public int? TenantId { get; set; }
  5.  
  6. public DBContextWithUserAuditing(DbContextOptions<DBContextWithUserAuditing> options) : base(options) { }
  7.  
  8. // here we declare our db sets
  9.  
  10. protected override void OnModelCreating(ModelBuilder modelBuilder)
  11. {
  12. base.OnModelCreating(modelBuilder);
  13.  
  14. modelBuilder.NamesToSnakeCase(); // PostgreSQL
  15. modelBuilder.EnableSoftDelete();
  16. }
  17.  
  18. public override int SaveChanges()
  19. {
  20. ChangeTracker.DetectChanges();
  21. ChangeTracker.ProcessModification(UserId);
  22. ChangeTracker.ProcessDeletion(UserId);
  23. ChangeTracker.ProcessCreation(UserId, TenantId);
  24.  
  25. return base.SaveChanges();
  26. }
  27.  
  28. public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default(CancellationToken))
  29. {
  30. ChangeTracker.DetectChanges();
  31. ChangeTracker.ProcessModification(UserId);
  32. ChangeTracker.ProcessDeletion(UserId);
  33. ChangeTracker.ProcessCreation(UserId, TenantId);
  34.  
  35. return (await base.SaveChangesAsync(true, cancellationToken));
  36. }
  37. }
  38.  
  39. public class AppInitializerFilter : IAsyncActionFilter
  40. {
  41. private Session _session;
  42. private DBContextWithUserAuditing _dbContext;
  43. private ITenantService _tenantService;
  44.  
  45. public AppInitializerFilter(
  46. DBContextWithUserAuditing dbContext
  47. )
  48. {
  49. _dbContext = dbContext;
  50. }
  51.  
  52. public async Task OnActionExecutionAsync(
  53. ActionExecutingContext context,
  54. ActionExecutionDelegate next
  55. )
  56. {
  57. string userId = null;
  58. int? tenantId = null;
  59.  
  60. var claimsIdentity = (ClaimsIdentity)context.HttpContext.User.Identity;
  61.  
  62. var userIdClaim = claimsIdentity.Claims.SingleOrDefault(c => c.Type == ClaimTypes.NameIdentifier);
  63. if (userIdClaim != null)
  64. {
  65. userId = userIdClaim.Value;
  66. }
  67.  
  68. var tenantIdClaim = claimsIdentity.Claims.SingleOrDefault(c => c.Type == CustomClaims.TenantId);
  69. if (tenantIdClaim != null)
  70. {
  71. tenantId = !string.IsNullOrEmpty(tenantIdClaim.Value) ? int.Parse(tenantIdClaim.Value) : (int?)null;
  72. }
  73.  
  74. _dbContext.UserId = userId;
  75. _dbContext.TenantId = tenantId;
  76.  
  77. var resultContext = await next();
  78. }
  79. }
  80.  
  81. services
  82. .AddMvc(options =>
  83. {
  84. options.Filters.Add(typeof(OnRequestInit));
  85. })
  86.  
  87. public static class ChangeTrackerExtensions
  88. {
  89. public static void ProcessCreation(this ChangeTracker changeTracker, string userId, int? tenantId)
  90. {
  91. foreach (var item in changeTracker.Entries<IHasCreationTime>().Where(e => e.State == EntityState.Added))
  92. {
  93. item.Entity.CreationTime = DateTime.Now;
  94. }
  95.  
  96. foreach (var item in changeTracker.Entries<IHasCreatorUserId>().Where(e => e.State == EntityState.Added))
  97. {
  98. item.Entity.CreatorUserId = userId;
  99. }
  100.  
  101. foreach (var item in changeTracker.Entries<IMustHaveTenant>().Where(e => e.State == EntityState.Added))
  102. {
  103. if (tenantId.HasValue)
  104. {
  105. item.Entity.TenantId = tenantId.Value;
  106. }
  107. }
  108. }
Add Comment
Please, Sign In to add comment