Enhancement to support audit fields on entities
This commit is contained in:
		| @ -9,10 +9,10 @@ namespace Oqtane.Repository | ||||
| { | ||||
|     public class AliasRepository : IAliasRepository | ||||
|     { | ||||
|         private MasterContext db; | ||||
|         private MasterDBContext db; | ||||
|         private readonly IMemoryCache _cache; | ||||
|  | ||||
|         public AliasRepository(MasterContext context, IMemoryCache cache) | ||||
|         public AliasRepository(MasterDBContext context, IMemoryCache cache) | ||||
|         { | ||||
|             db = context; | ||||
|             _cache = cache; | ||||
|  | ||||
| @ -1,33 +0,0 @@ | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using Oqtane.Models; | ||||
| using System; | ||||
|  | ||||
| namespace Oqtane.Repository | ||||
| { | ||||
|     public class ContextBase : DbContext | ||||
|     { | ||||
|         private Tenant tenant; | ||||
|  | ||||
|         public ContextBase(ITenantResolver TenantResolver) | ||||
|         { | ||||
|             tenant = TenantResolver.GetTenant(); | ||||
|         } | ||||
|  | ||||
|         protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | ||||
|         { | ||||
|             optionsBuilder.UseSqlServer(tenant.DBConnectionString | ||||
|                     .Replace("|DataDirectory|", AppDomain.CurrentDomain.GetData("DataDirectory").ToString()) | ||||
|             ); | ||||
|             base.OnConfiguring(optionsBuilder); | ||||
|         } | ||||
|  | ||||
|         protected override void OnModelCreating(ModelBuilder modelBuilder) | ||||
|         { | ||||
|             if (tenant.DBSchema != "") | ||||
|             { | ||||
|                 modelBuilder.HasDefaultSchema(tenant.DBSchema); | ||||
|             } | ||||
|             base.OnModelCreating(modelBuilder); | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										74
									
								
								Oqtane.Server/Repository/DBContextBase.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										74
									
								
								Oqtane.Server/Repository/DBContextBase.cs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,74 @@ | ||||
| using Microsoft.AspNetCore.Http; | ||||
| using Microsoft.AspNetCore.Identity; | ||||
| using Microsoft.AspNetCore.Identity.EntityFrameworkCore; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using Oqtane.Models; | ||||
| using System; | ||||
| using System.Linq; | ||||
|  | ||||
| namespace Oqtane.Repository | ||||
| { | ||||
|     public class DBContextBase : IdentityDbContext<IdentityUser> | ||||
|     { | ||||
|         private Tenant tenant; | ||||
|         private IHttpContextAccessor accessor; | ||||
|  | ||||
|         public DBContextBase(ITenantResolver TenantResolver, IHttpContextAccessor accessor) | ||||
|         { | ||||
|             tenant = TenantResolver.GetTenant(); | ||||
|             this.accessor = accessor; | ||||
|         } | ||||
|  | ||||
|         protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | ||||
|         { | ||||
|             optionsBuilder.UseSqlServer(tenant.DBConnectionString | ||||
|                     .Replace("|DataDirectory|", AppDomain.CurrentDomain.GetData("DataDirectory").ToString()) | ||||
|             ); | ||||
|             base.OnConfiguring(optionsBuilder); | ||||
|         } | ||||
|  | ||||
|         protected override void OnModelCreating(ModelBuilder modelBuilder) | ||||
|         { | ||||
|             base.OnModelCreating(modelBuilder); | ||||
|  | ||||
|             if (tenant.DBSchema != "") | ||||
|             { | ||||
|                 modelBuilder.HasDefaultSchema(tenant.DBSchema); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         public override int SaveChanges() | ||||
|         { | ||||
|             ChangeTracker.DetectChanges(); | ||||
|  | ||||
|             string username = accessor.HttpContext.User.Identity.Name; | ||||
|             DateTime date = DateTime.Now; | ||||
|  | ||||
|             var created = ChangeTracker.Entries() | ||||
|                 .Where(x => x.State == EntityState.Added); | ||||
|  | ||||
|             foreach(var item in created) | ||||
|             { | ||||
|                 if (item.Entity is IAuditable entity) | ||||
|                 { | ||||
|                     item.CurrentValues[nameof(IAuditable.CreatedBy)] = username; | ||||
|                     item.CurrentValues[nameof(IAuditable.CreatedOn)] = date; | ||||
|                 } | ||||
|             } | ||||
|  | ||||
|             var modified = ChangeTracker.Entries() | ||||
|                 .Where(x => x.State == EntityState.Modified || x.State == EntityState.Added); | ||||
|  | ||||
|             foreach (var item in modified) | ||||
|             { | ||||
|                 if (item.Entity is IAuditable entity) | ||||
|                 { | ||||
|                     item.CurrentValues[nameof(IAuditable.ModifiedBy)] = username; | ||||
|                     item.CurrentValues[nameof(IAuditable.ModifiedOn)] = date; | ||||
|                 } | ||||
|             } | ||||
|  | ||||
|             return base.SaveChanges(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @ -3,9 +3,9 @@ using Oqtane.Models; | ||||
| 
 | ||||
| namespace Oqtane.Repository | ||||
| { | ||||
|     public class MasterContext : DbContext | ||||
|     public class MasterDBContext : DbContext | ||||
|     { | ||||
|         public MasterContext(DbContextOptions<MasterContext> options) : base(options) { } | ||||
|         public MasterDBContext(DbContextOptions<MasterDBContext> options) : base(options) { } | ||||
| 
 | ||||
|         public virtual DbSet<Alias> Alias { get; set; } | ||||
|         public virtual DbSet<Tenant> Tenant { get; set; } | ||||
| @ -7,9 +7,9 @@ namespace Oqtane.Repository | ||||
| { | ||||
|     public class ModuleRepository : IModuleRepository | ||||
|     { | ||||
|         private TenantContext db; | ||||
|         private TenantDBContext db; | ||||
|  | ||||
|         public ModuleRepository(TenantContext context) | ||||
|         public ModuleRepository(TenantDBContext context) | ||||
|         { | ||||
|             db = context; | ||||
|         } | ||||
|  | ||||
| @ -7,9 +7,9 @@ namespace Oqtane.Repository | ||||
| { | ||||
|     public class PageModuleRepository : IPageModuleRepository | ||||
|     { | ||||
|         private TenantContext db; | ||||
|         private TenantDBContext db; | ||||
|  | ||||
|         public PageModuleRepository(TenantContext context) | ||||
|         public PageModuleRepository(TenantDBContext context) | ||||
|         { | ||||
|             db = context; | ||||
|         } | ||||
|  | ||||
| @ -7,9 +7,9 @@ namespace Oqtane.Repository | ||||
| { | ||||
|     public class PageRepository : IPageRepository | ||||
|     { | ||||
|         private TenantContext db; | ||||
|         private TenantDBContext db; | ||||
|  | ||||
|         public PageRepository(TenantContext context) | ||||
|         public PageRepository(TenantDBContext context) | ||||
|         { | ||||
|             db = context; | ||||
|         } | ||||
|  | ||||
| @ -7,9 +7,9 @@ namespace Oqtane.Repository | ||||
| { | ||||
|     public class SiteRepository : ISiteRepository | ||||
|     { | ||||
|         private TenantContext db; | ||||
|         private TenantDBContext db; | ||||
|  | ||||
|         public SiteRepository(TenantContext context) | ||||
|         public SiteRepository(TenantDBContext context) | ||||
|         { | ||||
|             db = context; | ||||
|         } | ||||
|  | ||||
| @ -1,41 +0,0 @@ | ||||
| using Microsoft.AspNetCore.Identity; | ||||
| using Microsoft.AspNetCore.Identity.EntityFrameworkCore; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using Oqtane.Models; | ||||
| using System; | ||||
|  | ||||
| namespace Oqtane.Repository | ||||
| { | ||||
|     public class TenantContext : IdentityDbContext<IdentityUser> | ||||
|     { | ||||
|         public virtual DbSet<Site> Site { get; set; } | ||||
|         public virtual DbSet<Page> Page { get; set; } | ||||
|         public virtual DbSet<PageModule> PageModule { get; set; } | ||||
|         public virtual DbSet<Module> Module { get; set; } | ||||
|         public virtual DbSet<User> User { get; set; } | ||||
|  | ||||
|         private readonly Tenant tenant; | ||||
|  | ||||
|         public TenantContext(ITenantResolver TenantResolver) | ||||
|         { | ||||
|             tenant = TenantResolver.GetTenant(); | ||||
|         } | ||||
|  | ||||
|         protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | ||||
|         { | ||||
|             optionsBuilder.UseSqlServer(tenant.DBConnectionString | ||||
|                     .Replace("|DataDirectory|", AppDomain.CurrentDomain.GetData("DataDirectory").ToString()) | ||||
|             ); | ||||
|             base.OnConfiguring(optionsBuilder); | ||||
|         } | ||||
|  | ||||
|         protected override void OnModelCreating(ModelBuilder modelBuilder) | ||||
|         { | ||||
|             if (tenant.DBSchema != "") | ||||
|             { | ||||
|                 modelBuilder.HasDefaultSchema(tenant.DBSchema); | ||||
|             } | ||||
|             base.OnModelCreating(modelBuilder); | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										21
									
								
								Oqtane.Server/Repository/TenantDBContext.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								Oqtane.Server/Repository/TenantDBContext.cs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,21 @@ | ||||
| using Microsoft.AspNetCore.Http; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using Oqtane.Models; | ||||
|  | ||||
| namespace Oqtane.Repository | ||||
| { | ||||
|     public class TenantDBContext : DBContextBase | ||||
|     { | ||||
|         public virtual DbSet<Site> Site { get; set; } | ||||
|         public virtual DbSet<Page> Page { get; set; } | ||||
|         public virtual DbSet<PageModule> PageModule { get; set; } | ||||
|         public virtual DbSet<Module> Module { get; set; } | ||||
|         public virtual DbSet<User> User { get; set; } | ||||
|  | ||||
|         public TenantDBContext(ITenantResolver TenantResolver, IHttpContextAccessor accessor) : base(TenantResolver, accessor) | ||||
|         { | ||||
|             // ContextBase handles multi-tenant database connections | ||||
|         } | ||||
|  | ||||
|     } | ||||
| } | ||||
| @ -10,10 +10,10 @@ namespace Oqtane.Repository | ||||
| { | ||||
|     public class TenantRepository : ITenantRepository | ||||
|     { | ||||
|         private MasterContext db; | ||||
|         private MasterDBContext db; | ||||
|         private readonly IMemoryCache _cache; | ||||
|  | ||||
|         public TenantRepository(MasterContext context, IMemoryCache cache) | ||||
|         public TenantRepository(MasterDBContext context, IMemoryCache cache) | ||||
|         { | ||||
|             db = context; | ||||
|             _cache = cache; | ||||
|  | ||||
| @ -7,12 +7,12 @@ namespace Oqtane.Repository | ||||
| { | ||||
|     public class TenantResolver : ITenantResolver | ||||
|     { | ||||
|         private MasterContext db; | ||||
|         private MasterDBContext db; | ||||
|         private readonly string aliasname; | ||||
|         private readonly IAliasRepository _aliasrepository; | ||||
|         private readonly ITenantRepository _tenantrepository; | ||||
|  | ||||
|         public TenantResolver(MasterContext context, IHttpContextAccessor accessor, IAliasRepository aliasrepository, ITenantRepository tenantrepository) | ||||
|         public TenantResolver(MasterDBContext context, IHttpContextAccessor accessor, IAliasRepository aliasrepository, ITenantRepository tenantrepository) | ||||
|         { | ||||
|             db = context; | ||||
|             _aliasrepository = aliasrepository; | ||||
|  | ||||
| @ -7,9 +7,9 @@ namespace Oqtane.Repository | ||||
| { | ||||
|     public class UserRepository : IUserRepository | ||||
|     { | ||||
|         private TenantContext db; | ||||
|         private TenantDBContext db; | ||||
|  | ||||
|         public UserRepository(TenantContext context) | ||||
|         public UserRepository(TenantDBContext context) | ||||
|         { | ||||
|             db = context; | ||||
|         } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 Shaun Walker
					Shaun Walker