public bool IsAuditedEntity(EntityEntry entry)
#endif
        {
            if (ExcludeIncludeEntityPredicates.Count == 0)
            {
                return true;
            }

            var type = entry.Entity.GetType();
            var key = type.FullName;
            bool value;

            if (!IsAuditedDictionary.TryGetValue(key, out value))
            {
                value = true;

                foreach (var excludeIncludeEntityFunc in ExcludeIncludeEntityPredicates)
                {
                    var maybeIncluded = excludeIncludeEntityFunc(entry.Entity);
                    if (maybeIncluded.HasValue)
                    {
                        value = maybeIncluded.Value;
                    }
                }

                IsAuditedDictionary.TryAdd(key, value);
            }

            return value;
        }
        public bool IsAuditedProperty(EntityEntry entry, string propertyName)
#endif
        {
            if (ExcludeIncludePropertyPredicates.Count == 0)
            {
                return true;
            }

            var type = entry.Entity.GetType();
            var key = string.Concat(type.FullName, ";", propertyName);
            bool value;

            if (!IsAuditedDictionary.TryGetValue(key, out value))
            {
                value = true;

                foreach (var excludeIncludePropertyFuncs in ExcludeIncludePropertyPredicates)
                {
                    var maybeIncluded = excludeIncludePropertyFuncs(entry.Entity, propertyName);
                    if (maybeIncluded.HasValue)
                    {
                        value = maybeIncluded.Value;
                    }
                }

                IsAuditedDictionary.TryAdd(key, value);
            }

            return value;
        }
예제 #3
0
        public void Can_get_property_entry_by_name()
        {
            var stateEntryMock = CreateStateEntryMock();
            var entry = new EntityEntry<Random>(stateEntryMock.Object);

            Assert.Equal("Monkey", entry.Property("Monkey").Name);
        }
예제 #4
0
 private static KeyValuePair<string, string> BuildKeyRepresentation(EntityEntry entityEntry, string seperator)
 {
     var keyProperties = entityEntry.Metadata.GetProperties().Where(x => x.IsPrimaryKey()).ToList();
     if (keyProperties == null)
         throw new ArgumentException("No key found in");
     var keyPropertyEntries =
         keyProperties.Select(keyProperty => entityEntry.Property(keyProperty.Name)).ToList();
     var keyNameString = new StringBuilder();
     foreach (var keyProperty in keyProperties)
     {
         keyNameString.Append(keyProperty.Name);
         keyNameString.Append(seperator);
     }
     keyNameString.Remove(keyNameString.Length - 1, 1);
     var keyValueString = new StringBuilder();
     foreach (var keyPropertyEntry in keyPropertyEntries)
     {
         keyValueString.Append(keyPropertyEntry.CurrentValue);
         keyValueString.Append(seperator);
     }
     keyValueString.Remove(keyValueString.Length - 1, 1);
     var key = keyNameString.ToString();
     var value = keyValueString.ToString();
     return new KeyValuePair<string, string>(key, value);
 }
예제 #5
0
        public void Members_check_arguments()
        {
            Assert.Equal(
                "stateEntry",
                // ReSharper disable once AssignNullToNotNullAttribute
                Assert.Throws<ArgumentNullException>(() => new EntityEntry(null)).ParamName);

            Assert.Equal(
                "stateEntry",
                // ReSharper disable once AssignNullToNotNullAttribute
                Assert.Throws<ArgumentNullException>(() => new EntityEntry<Random>(null)).ParamName);

            var entry = new EntityEntry(new Mock<StateEntry>().Object);

            Assert.Equal(
                Strings.FormatArgumentIsEmpty("propertyName"),
                Assert.Throws<ArgumentException>(() => entry.Property("")).Message);

            var genericEntry = new EntityEntry<Random>(new Mock<StateEntry>().Object);

            Assert.Equal(
                "propertyExpression",
                // ReSharper disable once AssignNullToNotNullAttribute
                Assert.Throws<ArgumentNullException>(() => genericEntry.Property((Expression<Func<Random, int>>)null)).ParamName);
        }
예제 #6
0
        private static IEnumerable<AuditLog> GetAuditLogs(EntityEntry entityEntry, string userName, EntityState entityState)
        {
            var returnValue = new List<AuditLog>();
            var keyRepresentation = BuildKeyRepresentation(entityEntry, KeySeperator);

            var auditedPropertyNames =
                entityEntry.Entity.GetType()
                    .GetProperties().Where(p => !p.GetCustomAttributes(typeof (DoNotAudit), true).Any())
                    .Select(info => info.Name);
            foreach (var propertyEntry in entityEntry.Metadata.GetProperties()
                .Where(x => auditedPropertyNames.Contains(x.Name))
                .Select(property => entityEntry.Property(property.Name)))
            {
                if(entityState == EntityState.Modified)
                    if (Convert.ToString(propertyEntry.OriginalValue) == Convert.ToString(propertyEntry.CurrentValue)) //Values are the same, don't log
                        continue;
                returnValue.Add(new AuditLog
                {
                    KeyNames = keyRepresentation.Key,
                    KeyValues = keyRepresentation.Value,
                    OriginalValue = entityState != EntityState.Added
                        ? Convert.ToString(propertyEntry.OriginalValue)
                        : null,
                    NewValue = entityState == EntityState.Modified || entityState == EntityState.Added
                        ? Convert.ToString(propertyEntry.CurrentValue)
                        : null,
                    ColumnName = propertyEntry.Metadata.Name,
                    EventDateTime = DateTime.Now,
                    EventType = entityState.ToString(),
                    UserName = userName,
                    TableName = entityEntry.Entity.GetType().Name
                });
            }
            return returnValue;
        }
        public static void AuditEntityAdded(Audit audit, EntityEntry objectStateEntry)
#endif
        {
            var entry = new AuditEntry(audit, objectStateEntry)
            {
                State = AuditEntryState.EntityAdded
            };


            // CHECK if the key should be resolved in POST Action
#if EF5 || EF6
            if (objectStateEntry.EntityKey.IsTemporary)
            {
                entry.DelayedKey = objectStateEntry;
            }
            AuditEntityAdded(entry, objectStateEntry.CurrentValues);
#elif EF7
    // TODO: We must check if the key IsTemporary! We can maybe use flag...
    //if (!objectStateEntry.IsKeySet)
    //{
                entry.DelayedKey = objectStateEntry;
            //}
            AuditEntityAdded(entry, objectStateEntry);
#endif

            audit.Entries.Add(entry);
        }
    /// <summary>Audit entity deleted.</summary>
    /// <param name="objectStateEntry">The object state entry.</param>
        public static void AuditEntityDeleted(AuditEntry entry, EntityEntry objectStateEntry)
        {
            foreach (var propertyEntry in objectStateEntry.Metadata.GetProperties())
            {
                var property = objectStateEntry.Property(propertyEntry.Name);

                if (entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, propertyEntry.Name))
                {
                    entry.Properties.Add(new AuditEntryProperty(entry, propertyEntry.Name, property.OriginalValue, null));
                }
            }
        }
        public static void AuditEntityDeleted(Audit audit, EntityEntry objectStateEntry)
#endif
        {
            var entry = new AuditEntry(audit, objectStateEntry)
            {
                State = AuditEntryState.EntityDeleted
            };


#if EF5 || EF6
            AuditEntityDeleted(entry, objectStateEntry.OriginalValues);
#elif EFCORE
            AuditEntityDeleted(entry, objectStateEntry);
#endif
            audit.Entries.Add(entry);
        }
예제 #10
0
        private static IEnumerable<CustomAuditLog> GetAuditLogs(EntityEntry entityEntry, string userName, EntityState entityState)
        {
            var returnValue = new List<CustomAuditLog>();
            // var keyRepresentation = BuildKeyRepresentation(entityEntry, KeySeperator);
            DateTime RightNow = DateTime.Now;
            Guid AuditBatchID;
            UuidCreateSequential(out AuditBatchID);

            var auditedPropertyNames =
                entityEntry.Entity.GetType()
                    .GetProperties().Where(p => !p.GetCustomAttributes(typeof(DoNotAudit), true).Any())
                    .Select(info => info.Name);

            var differences = new List<PropertyDiff>();

            foreach (var propertyEntry in entityEntry.Metadata.GetProperties()
                           .Where(x => auditedPropertyNames.Contains(x.Name))
                           .Select(property => new { PropertyName = property.Name, Property = entityEntry.Property(property.Name) }))
            {
                var originalValue = Convert.ToString(propertyEntry.Property.OriginalValue);
                var currentValue = Convert.ToString(propertyEntry.Property.CurrentValue);

                if (entityState == EntityState.Modified && originalValue == currentValue) //Values are the same, don't log
                    continue;

                var diff = new PropertyDiff { PropertyName = propertyEntry.PropertyName, OriginalValue = originalValue, NewValue = currentValue } ;
                differences.Add(diff);

            }
            var ser = JsonConvert.SerializeObject(differences);

            returnValue.Add(new CustomAuditLog
            {
                Differences = ser,
                EventDateTime = RightNow,
                EventType = entityState.ToString(),
                UserName = userName,
                TableName = entityEntry.Entity.GetType().Name
            });
            return returnValue;
        }
        public static void AuditRelationDeleted(Audit audit, EntityEntry objectStateEntry)
#endif
        {
            var entry = new AuditEntry(audit, objectStateEntry)
            {
                State = AuditEntryState.RelationshipDeleted
            };

            var values = objectStateEntry.OriginalValues;
            for (var i = 0; i < values.FieldCount; i++)
            {
                var relationName = values.GetName(i);
                var value = (EntityKey) values.GetValue(i);
                foreach (var keyValue in value.EntityKeyValues)
                {
                    entry.Properties.Add(new AuditEntryProperty(entry, relationName, keyValue.Key, keyValue.Value, null));
                }
            }

            audit.Entries.Add(entry);
        }
예제 #12
0
        public string FormatValue(EntityEntry entry, string propertyName, object currentValue)
#endif
        {
            if (EntityValueFormatters.Count > 0)
            {
                if (entry.State != EntityState.Detached && entry.Entity != null)
                {
                    var type = entry.Entity.GetType();
                    var key = string.Concat(type.FullName, ";", propertyName);
                    Func<object, object> formatter;

                    if (!ValueFormatterDictionary.TryGetValue(key, out formatter))
                    {
                        if (EntityValueFormatters.Count > 0)
                        {
                            foreach (var formatProperty in EntityValueFormatters)
                            {
                                formatter = formatProperty(entry.Entity, propertyName);

                                if (formatter != null)
                                {
                                    break;
                                }
                            }
                        }

                        ValueFormatterDictionary.TryAdd(key, formatter);
                    }

                    if (formatter != null)
                    {
                        currentValue = formatter(currentValue);
                    }
                }
            }

            return currentValue != null ? currentValue.ToString() : null;
        }
        public AuditEntryState GetEntityModifiedState(EntityEntry entry)
#endif
        {
            // CHECK for soft added
            foreach (var softAddedFuncs in SoftAddedPredicates)
            {
                if (softAddedFuncs(entry.Entity))
                {
                    return AuditEntryState.EntitySoftAdded;
                }
            }

            // CHECK for soft deleted
            foreach (var softDeleteFunc in SoftDeletedPredicates)
            {
                if (softDeleteFunc(entry.Entity))
                {
                    return AuditEntryState.EntitySoftDeleted;
                }
            }

            return AuditEntryState.EntityModified;
        }
        public static void AuditRelationAdded(Audit audit, EntityEntry objectStateEntry)
#endif
        {
            var entry = new AuditEntry(audit, objectStateEntry)
            {
                State = AuditEntryState.RelationshipAdded
            };

            var values = objectStateEntry.CurrentValues;


            var leftKeys = (EntityKey) values.GetValue(0);
            var rightKeys = (EntityKey) values.GetValue(1);

            if (leftKeys.IsTemporary || rightKeys.IsTemporary)
            {
                entry.DelayedKey = objectStateEntry;
            }
            else
            {
                var leftRelationName = values.GetName(0);
                var rightRelationName = values.GetName(1);

                foreach (var keyValue in leftKeys.EntityKeyValues)
                {
                    entry.Properties.Add(new AuditEntryProperty(entry, leftRelationName, keyValue.Key, null, keyValue.Value));
                }

                foreach (var keyValue in rightKeys.EntityKeyValues)
                {
                    entry.Properties.Add(new AuditEntryProperty(entry, rightRelationName, keyValue.Key, null, keyValue.Value));
                }
            }

            audit.Entries.Add(entry);
        }
            public override EntityState DetermineState(EntityEntry entry)
            {
                if (!entry.IsKeySet)
                {
                    ((IAccessor<InternalEntityEntry>)entry).Service[entry.Metadata.GetPrimaryKey().Properties.Single()] = 777;
                    return EntityState.Added;
                }

                return base.DetermineState(entry);
            }
 private void TrackEntity(EntityEntry entry, bool updateExistingEntities)
 {
     new KeyValueEntityTracker(updateExistingEntities: updateExistingEntities).TrackEntity(entry);
 }
예제 #17
0
        public void Can_get_property_entry_by_lambda()
        {
            var stateEntryMock = CreateStateEntryMock();
            var entry = new EntityEntry<Chunky>(stateEntryMock.Object);

            Assert.Equal("Monkey", entry.Property(e => e.Monkey).Name);
        }
            public override EntityState DetermineState(EntityEntry entry)
            {
                if (!entry.IsKeySet)
                {
                    entry.GetService()[entry.Metadata.GetPrimaryKey().Properties.Single()] = 777;
                    return EntityState.Added;
                }

                return base.DetermineState(entry);
            }
 public virtual EntityState DetermineState(EntityEntry entry)
     => entry.IsKeySet
         ? (_updateExistingEntities ? EntityState.Modified : EntityState.Unchanged)
         : EntityState.Added;
 public virtual void TrackEntity(EntityEntry entry)
     => entry.GetService().SetEntityState(DetermineState(entry), acceptChanges: true);
예제 #21
0
 private static void target(EntityEntry obj)
 {
     obj.SetState(Microsoft.Data.Entity.EntityState.Added);
 }