public override int SaveChanges() { var modifiedEntries = ChangeTracker.Entries() .Where(x => x.Entity is AuditableEntity && (x.State == System.Data.Entity.EntityState.Added || x.State == System.Data.Entity.EntityState.Modified)); foreach (var entry in modifiedEntries) { AuditableEntity entity = entry.Entity as AuditableEntity; if (entity != null) { //string identityName = Thread.CurrentPrincipal.Identity.Name; string identityName = "admin"; DateTime now = DateTime.Now; if (entry.State == System.Data.Entity.EntityState.Added) { entity.CreatedBy = identityName; entity.CreatedDateTime = now; } else { base.Entry(entity).Property(x => x.CreatedBy).IsModified = false; base.Entry(entity).Property(x => x.CreatedDateTime).IsModified = false; } entity.UpdatedBy = identityName; entity.UpdatedDateTime = now; } } return(base.SaveChanges()); }
public static void EnsureNotModifiedAuditableEntity(AuditableEntity entity) { Assert.NotEmpty(entity.CreatedBy); Assert.IsType <DateTime>(entity.Created); Assert.Null(entity.LastModifiedBy); Assert.Null(entity.LastModified); }
public static void InitializeEntityToAudit() { var auditAccount = new AuditableEntity(); auditAccount.EntityName = "Account"; auditAccount.EnableAudit = true; auditAccount.AuditableAttributes.Add(new AuditableAttribute() { AttributeName = "CompanyId", EnableAudit = true }); auditAccount.AuditableAttributes.Add(new AuditableAttribute() { AttributeName = "AccountClassId", EnableAudit = true }); auditAccount.AuditableAttributes.Add(new AuditableAttribute() { AttributeName = "ParentAccountId", EnableAudit = true }); auditAccount.AuditableAttributes.Add(new AuditableAttribute() { AttributeName = "DrOrCrSide", EnableAudit = true }); auditAccount.AuditableAttributes.Add(new AuditableAttribute() { AttributeName = "AccountCode", EnableAudit = true }); auditAccount.AuditableAttributes.Add(new AuditableAttribute() { AttributeName = "AccountName", EnableAudit = true }); auditAccount.AuditableAttributes.Add(new AuditableAttribute() { AttributeName = "Description", EnableAudit = true }); auditAccount.AuditableAttributes.Add(new AuditableAttribute() { AttributeName = "IsCash", EnableAudit = true }); auditAccount.AuditableAttributes.Add(new AuditableAttribute() { AttributeName = "IsContraAccount", EnableAudit = true }); _context.AuditableEntities.Add(auditAccount); var auditJE = new AuditableEntity(); auditJE.EntityName = "JournalEntryHeader"; auditJE.EnableAudit = true; auditJE.AuditableAttributes.Add(new AuditableAttribute() { AttributeName = "Posted", EnableAudit = true }); _context.AuditableEntities.Add(auditJE); _context.SaveChanges(); }
public override int SaveChanges() { try { var modifiedEntries = ChangeTracker.Entries() .Where(x => x.Entity is AuditableEntity && (x.State == System.Data.Entity.EntityState.Added || x.State == System.Data.Entity.EntityState.Modified)); foreach (var entry in modifiedEntries) { AuditableEntity entity = entry.Entity as AuditableEntity; if (entity != null) { string identityName = Thread.CurrentPrincipal.Identity.Name; LocalDateTimeService obj = new LocalDateTimeService(); DateTime now = obj.GetDateTime(); if (entry.State == System.Data.Entity.EntityState.Added) { entity.CreatedBy = identityName; entity.CreatedOn = now; } else if (entry.State == System.Data.Entity.EntityState.Modified) { entity.UpdatedBy = identityName; entity.UpdatedOn = now; } else { base.Entry(entity).Property(x => x.CreatedBy).IsModified = false; base.Entry(entity).Property(x => x.CreatedOn).IsModified = false; entity.UpdatedBy = identityName; entity.UpdatedOn = now; } } } return(base.SaveChanges()); } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } catch (SqlException ex) { throw ex; } }
public IViewComponentResult Invoke(AuditableEntity entity) { return(View(new EditAuditViewModel { IsActive = entity.IsActive, IsDeleted = entity.IsDeleted, CreatedDate = entity.CreatedDate, LastModified = entity.LastModified })); }
private static void FillAuditableEntityAndAttributes(EntityEntry dbEntry) { Type entryType = dbEntry.Entity.GetType(); TableAttribute tableAttr = entryType.GetCustomAttributes(typeof(TableAttribute), false).SingleOrDefault() as TableAttribute; string tableName = tableAttr != null ? tableAttr.Name : dbEntry.Entity.GetType().Name; _auditableEntity = ((ApiDbContext)dbEntry.Context).AuditableEntities .Include(a => a.AuditableAttributes) .FirstOrDefault(e => e.EntityName == tableName); }
public override int SaveChanges() { try { var modifiedEntries = ChangeTracker.Entries() .Where(x => x.Entity is AuditableEntity && (x.State == EntityState.Added || x.State == EntityState.Modified)); foreach (var entry in modifiedEntries) { AuditableEntity entity = entry.Entity as AuditableEntity; if (entity != null) { string identityName = ""; try { identityName = Thread.CurrentPrincipal.Identity.Name; } catch { Thread.CurrentPrincipal = new WindowsPrincipal(WindowsIdentity.GetCurrent()); identityName = Thread.CurrentPrincipal.Identity.Name; } DateTime now = DateTime.UtcNow; if (string.IsNullOrEmpty(identityName)) { identityName = "System"; } if (entry.State == EntityState.Added) { entity.CreatedBy = identityName; entity.CreatedOn = now; } else { Entry(entity).Property(x => x.CreatedBy).IsModified = false; Entry(entity).Property(x => x.CreatedOn).IsModified = false; entity.UpdatedBy = identityName; entity.UpdatedOn = now; } } } return base.SaveChanges(); } catch (DbEntityValidationException e) { var newException = new FormattedDbEntityValidationException(e); throw newException; } }
public ListEntry(string typeName, AuditableEntity auditableEntity) { Type = typeName; // Entity Id = auditableEntity.Id; // AuditableEntity CreatedDate = auditableEntity.CreatedDate; ModifiedDate = auditableEntity.ModifiedDate; CreatedBy = auditableEntity.CreatedBy; ModifiedBy = auditableEntity.ModifiedBy; }
public virtual ListEntryBase FromModel(AuditableEntity entity) { // Entity Id = entity.Id; // AuditableEntity CreatedDate = entity.CreatedDate; ModifiedDate = entity.ModifiedDate; CreatedBy = entity.CreatedBy; ModifiedBy = entity.ModifiedBy; return(this); }
public void Audit_Properties_Are_Modified() { Mock<IRepository<AuditableEntity>> realRepo = new Mock<IRepository<AuditableEntity>>(); realRepo.Setup(rr => rr.Insert(It.IsAny<AuditableEntity>())); Mock<AuditableRepository<AuditableEntity>> deco = new Mock<AuditableRepository<AuditableEntity>>(realRepo.Object) {CallBase = true}; AuditableEntity ent = new AuditableEntity(); deco.Object.Insert(ent); Assert.That(ent, Is.AssignableTo<IAuditable>()); Assert.That(ent, Has.Property("Created").Not.Null); Assert.That(ent, Has.Property("CreatedBy").Not.Null); Assert.That(ent, Has.Property("CreatedBy").EqualTo(Thread.CurrentPrincipal.Identity.Name)); realRepo.VerifyAll(); }
/// <summary> /// Updates entity audit fields. /// </summary> /// <remarks>All exceptions will be propagated to caller method.</remarks> /// <param name="entity">The entity to update.</param> /// <param name="existing">The existing entity.</param> /// <param name="creating">Defines whether entity is being created.</param> protected void PopulateAuditFields(AuditableEntity entity, AuditableEntity existing = null, bool creating = false) { DateTime now = DateTime.Now; if (creating) { entity.CreatedBy = CurrentUser.Username; entity.CreatedTime = now; } else if (existing != null) { entity.CreatedBy = existing.CreatedBy; entity.CreatedTime = existing.CreatedTime; } entity.LastUpdatedBy = CurrentUser.Username; entity.LastUpdatedTime = now; }
public virtual ListEntryBase FromModel(AuditableEntity entity) { // Entity Id = entity.Id; // AuditableEntity CreatedDate = entity.CreatedDate; ModifiedDate = entity.ModifiedDate; CreatedBy = entity.CreatedBy; ModifiedBy = entity.ModifiedBy; if (entity is ISeoSupport seoSupport) { SeoObjectType = seoSupport.SeoObjectType; SeoInfos = seoSupport.SeoInfos; } return(this); }
public virtual async Task SaveAsync(AuditableEntity entity, bool saveChangesAsAtomic = true) { if (entity == null) { return; } if (entity.Id != Guid.Empty) { _genericRepo.Update(entity); } else { await _genericRepo.InsertAsync(entity); } if (saveChangesAsAtomic == true) { await _genericRepo.SaveChangesAsync(); } }
public override ListEntryBase FromModel(AuditableEntity entity) { base.FromModel(entity); if (entity is Category category) { Type = "category"; ImageUrl = category.ImgSrc; Code = category.Code; Name = category.Name; IsActive = category.IsActive; Links = category.Links; if (!category.Outlines.IsNullOrEmpty()) { //TODO: Use only physical catalog outline which this category belongs to Outline = category.Outlines.FirstOrDefault().Items.Select(x => x.Id).ToList(); Path = category.Outlines.FirstOrDefault().Items.Select(x => x.Name).ToList(); } } return(this); }
public override ListEntryBase FromModel(AuditableEntity entity) { base.FromModel(entity); if (entity is CatalogProduct product) { Type = "product"; ImageUrl = product.ImgSrc; Code = product.Code; Name = product.Name; IsActive = product.IsActive ?? true; ProductType = product.ProductType; Links = product.Links; CatalogId = product.CatalogId; if (!product.Outlines.IsNullOrEmpty()) { //TODO: Use only physical catalog outline which this category belongs to Outline = product.Outlines.FirstOrDefault().Items.Select(x => x.Id).ToList(); Path = product.Outlines.FirstOrDefault().Items.Select(x => x.Name).ToList(); } } return(this); }
public Task UpdateOffer(AuditableEntity entity, string methodName) { throw new NotImplementedException(); }
public ResourceOwnerNotProvidedException(Type type, AuditableEntity entity) : base($"{entity.GetType().ShortDisplayName()} resource has no owner") { Type = type; }
private static void FillAuditableEntityAndAttributes(string entityName) { _auditableEntity = _context.AuditableEntities.Where(e => e.EntityName == entityName).FirstOrDefault(); }
public void New_AuditableEntity_Is_Transient() { AuditableEntity entity = new AuditableEntity(); Assert.IsTrue(entity.IsTransient()); }