/// <summary> /// Enlist an Entity for automatic Auditing. /// Use this overload to explicitly declare a primary key selector. /// </summary> /// <example>db.Audit<Product>(p => p.ProductID);</example> /// <typeparam name="TEntity">Type of Entity you wish to audit</typeparam> /// <param name="pkSelector">A lambda expression that accepts a TEntity and returns an object representing the primary key</param> public static EntityAuditor <TEntity> Audit <TEntity>(this IAuditableContext context, Expression <Func <TEntity, object> > pkSelector) { var def = new EntityAuditor <TEntity>(pkSelector.ToPropertyInfo()); context.EntityAuditors.Add(def); return(def); }
internal static LambdaExpression GetPropertySelector(this IAuditableContext context, Type entityType, string propertyName) { var param = Expression.Parameter(entityType, "e"); var selector = Expression.Lambda(Expression.Convert(Expression.Property(param, propertyName), typeof(object)), param); return(selector); }
/// <summary> /// Enlist an Entity for automatic Auditing. /// Use this overload to resolve primary keys automatically. /// </summary> /// <example>db.Audit<Product>();</example> /// <typeparam name="TEntity">Type of Entity you wish to audit</typeparam> public static EntityAuditor <TEntity> Audit <TEntity>(this IAuditableContext context) { var def = new EntityAuditor <TEntity>(GetEntityPkProperty <TEntity>(context).ToPropertyInfo()); context.EntityAuditors.Add(def); return(def); }
public PropertyInfo GetParentEntityPrimaryKey(IAuditableContext context) { var property = _fkSelector == null ? EntityType.GetProperty(context.GetForeignKeyPropertyName(EntityType, ParentEntityType)) : _fkSelector.ToPropertyInfo(); return(property); }
public PropertyInfo GetEntityPrimaryKey(IAuditableContext context) { var property = _pkSelector == null ? context.GetPrimaryKeyProperty(EntityType) : _pkSelector.ToPropertyInfo(); return(property); }
public ContextAuditor(IAuditableContext context) { if (context == null) { throw new ArgumentNullException("context"); } _context = context; }
public override void Hook(IIdentifiable entity, HookEntityMetadata metadata) { //// Check is auditable context, contains auditlog table IAuditableContext context = metadata.CurrentContext as IAuditableContext; if (context == null) { return; } //// Get current username var userName = "******"; if (this.HttpContext != null) { userName = this.HttpContext.User.Identity.Name; } //// Get entry, entity type and associate etadata var entry = ((IObjectContextAdapter)metadata.CurrentContext).ObjectContext.ObjectStateManager.GetObjectStateEntry(entity); var entityType = entity.GetType(); TypeDescriptor.AddProvider(new AssociatedMetadataTypeTypeDescriptionProvider(entityType), entityType); //// Get is entity modified property contains requireAudit Field, and add auditlog var properties = TypeDescriptor.GetProperties(entityType); foreach (string propertyName in entry.GetModifiedProperties()) { //// Check is property need io var propertyDescriptor = properties.Find(propertyName, true); var propRequireAudit = propertyDescriptor.Attributes.OfType <RequireAuditAttribute>().FirstOrDefault(); if (propRequireAudit == null) { continue; } //// Get original value DbDataRecord original = entry.OriginalValues; string oldValue = original.GetValue(original.GetOrdinal(propertyName)).ToString(); //// Get new value CurrentValueRecord current = entry.CurrentValues; string newValue = current.GetValue(current.GetOrdinal(propertyName)).ToString(); //// Write Audit Log AuditLog auditLog = new AuditLog(); auditLog.IdentifyKey = entity.IdentifyKey; auditLog.IdentifyName = entityType.Name; auditLog.OriginValue = oldValue; auditLog.NewValue = newValue; auditLog.CreatedAt = DateTime.Now; auditLog.CreatedBy = userName; context.AuditLogs.Add(auditLog); } }
public override AuditedEntity AuditEntity(IAuditableContext context, object entity, AuditAction action) { Action <AuditedEntity> updateKeys = audited => { var pkProperty = _relatationship.GetEntityPrimaryKey(context); var pk = pkProperty.GetValue(audited.Entity, null); audited.EntityKey = new EntityKey(pk); var field = audited.ModifiedProperties.SingleOrDefault(f => f.PropertyName == pkProperty.Name); if (field != null) { field.NewValue = pk.ToString(); } var fkProperty = _relatationship.GetParentEntityPrimaryKey(context); var fk = fkProperty.GetValue(audited.Entity, null); audited.ParentKey = new EntityKey(fk); var parentField = audited.ModifiedProperties.SingleOrDefault(f => f.PropertyName == fkProperty.Name); if (parentField != null) { parentField.NewValue = fk.ToString(); } //var fkProperty = _relatationship.FkSelector == null // ? context.GetEntityPrimaryKey(_relatationship.ParentEntityType) // : _relatationship.PkSelector.ToPropertyInfo(); //var fk = fkProperty.GetValue(audited.Entity, null); //audited.AssociationTableKey = new EntityKey(pk); //if (_relatationship.FkSelector == null) //{ // var relationshipKeyName = context.GetForeignKeyPropertyName(_relatationship.ParentEntityType, _relatationship.EntityType); // object fk = context.GetPropertySelector(_relatationship.EntityType, relationshipKeyName); // audited.EntityTableKey = new EntityKey(fk); //} //else //{ // object fk = _relatationship.FkSelector.Compile().DynamicInvoke(audited.Entity); // audited.EntityTableKey = new EntityKey(fk); //} }; var auditedEntity = new AuditedEntity(entity, action, updateKeys) { EntityType = context.GetEntityType(_relatationship.EntityType), ParentEntityType = context.GetEntityType(_relatationship.ParentEntityType), }; AddModifiedPropertiesToRecord(context, auditedEntity); return(auditedEntity); }
internal static bool ShouldAuditProperty(this IAuditableContext context, PropertyInfo property, object entity) { bool auditProperty = true; foreach (var rule in context.AuditConfiguration.PropertyAuditRules) { if (rule(property, entity) == false) { auditProperty = false; } } return(auditProperty); }
public override void Hook(IIdentifiable entity, HookEntityMetadata metadata) { //// Check is auditable context, contains auditlog table IAuditableContext context = metadata.CurrentContext as IAuditableContext; if (context == null) { return; } //// Get current username var userName = "******"; if (this.HttpContext != null) { userName = this.HttpContext.User.Identity.Name; } //// Get entity type and associate etadata var entityType = entity.GetType(); TypeDescriptor.AddProvider(new AssociatedMetadataTypeTypeDescriptionProvider(entityType), entityType); //// Get is entity contains requireAudit Field, and add auditlog var properties = TypeDescriptor.GetProperties(entityType); foreach (PropertyDescriptor propertyDescriptor in properties) { //// If contains requireAudit attribute, add audit log var propRequireAudit = propertyDescriptor.Attributes.OfType <RequireAuditAttribute>().FirstOrDefault(); if (propRequireAudit == null) { continue; } AuditLog auditLog = new AuditLog(); auditLog.IdentifyKey = entity.IdentifyKey; auditLog.IdentifyName = entityType.Name; auditLog.OriginValue = string.Empty; auditLog.NewValue = propertyDescriptor.GetValue(entity).ToString(); auditLog.CreatedAt = DateTime.Now; auditLog.CreatedBy = userName; context.AuditLogs.Add(auditLog); } }
public static IEntityAuditor GetEntityAuditor(this IAuditableContext context, Type entityType) { var entityAuditor = context.EntityAuditors.FirstOrDefault(d => d.CanAuditType(entityType)); if (entityAuditor == null) { var relationshipConfig = context.EntityAuditors.SelectMany(d => d.Configuration.Relationships).FirstOrDefault(d => d.EntityType.IsAssignableFrom(entityType)); if (relationshipConfig != null) { entityAuditor = new RelationshipEntityAuditor(context, relationshipConfig); } } return(entityAuditor); }
public RelationshipEntityAuditor(IAuditableContext context, IRelatationshipConfiguration relatationship) : base(relatationship.EntityType, relatationship.GetEntityPrimaryKey(context)) { _relatationship = relatationship; Configuration = relatationship.Configuration; }
internal static LambdaExpression GetPropertySelector <TEntity>(this IAuditableContext context, string propertyName) { return(context.GetPropertySelector(typeof(TEntity), propertyName)); }
internal static LambdaExpression GetEntityPkProperty <TEntity>(this IAuditableContext context) { var pk = context.GetPrimaryKeyProperty(typeof(TEntity)); return(GetPropertySelector <TEntity>(context, pk.Name)); }