Skip to content

Commit

Permalink
Update to v1.4.0
Browse files Browse the repository at this point in the history
Update to v1.4.0
  • Loading branch information
zzzprojects committed Sep 13, 2016
1 parent 4842bdf commit 6795c71
Show file tree
Hide file tree
Showing 303 changed files with 14,270 additions and 645 deletions.
17 changes: 17 additions & 0 deletions src/Z.EntityFramework.Plus.EF5.NET40/Audit/Audit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@ public Audit()
{
_configuration = new Lazy<AuditConfiguration>(() => AuditManager.DefaultConfiguration.Clone());
Entries = new List<AuditEntry>();

try
{
#if !NETSTANDARD1_3
CreatedBy = System.Threading.Thread.CurrentPrincipal.Identity.Name;
#endif

if (string.IsNullOrEmpty(CreatedBy))
{
CreatedBy = "System";
}
}
catch (Exception)
{
// Oops! it's k, this is the responsability of the user to set the default CreatedBy field
CreatedBy = "System";
}
}

/// <summary>Gets or sets the entries.</summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@

#if EF5
using System.Data.Objects;
using System.Linq;

#elif EF6
using System.Data.Entity.Core.Objects;
using System.Linq;

#elif EFCORE
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;

#endif
Expand All @@ -29,19 +32,20 @@ public static void AuditEntityAdded(Audit audit, ObjectStateEntry objectStateEnt
public static void AuditEntityAdded(Audit audit, EntityEntry objectStateEntry)
#endif
{
var entry = new AuditEntry(audit, objectStateEntry)
{
State = AuditEntryState.EntityAdded
};
var entry = audit.Configuration.AuditEntryFactory != null ?
audit.Configuration.AuditEntryFactory(new AuditEntryFactoryArgs(audit, objectStateEntry, AuditEntryState.EntityAdded)) :
new AuditEntry();

entry.Build(audit, objectStateEntry);
entry.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);
AuditEntityAdded(entry, objectStateEntry, objectStateEntry.CurrentValues);
#elif EFCORE
// TODO: We must check if the key IsTemporary! We can maybe use flag...
//if (!objectStateEntry.IsKeySet)
Expand All @@ -57,9 +61,10 @@ public static void AuditEntityAdded(Audit audit, EntityEntry objectStateEntry)
#if EF5 || EF6
/// <summary>Audit entity added.</summary>
/// <param name="auditEntry">The audit entry.</param>
/// <param name="objectStateEntry">The object state entry.</param>
/// <param name="record">The record.</param>
/// <param name="prefix">The prefix.</param>
public static void AuditEntityAdded(AuditEntry auditEntry, DbUpdatableDataRecord record, string prefix = "")
public static void AuditEntityAdded(AuditEntry auditEntry, ObjectStateEntry objectStateEntry, DbUpdatableDataRecord record, string prefix = "")
{
for (var i = 0; i < record.FieldCount; i++)
{
Expand All @@ -70,11 +75,16 @@ public static void AuditEntityAdded(AuditEntry auditEntry, DbUpdatableDataRecord
if (valueRecord != null)
{
// Complex Type
AuditEntityAdded(auditEntry, valueRecord, string.Concat(prefix, name, "."));
AuditEntityAdded(auditEntry, objectStateEntry, valueRecord, string.Concat(prefix, name, "."));
}
else if (auditEntry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(auditEntry.Entry, name))
else if (objectStateEntry.EntitySet.ElementType.KeyMembers.Any(x => x.Name == name) || auditEntry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(auditEntry.Entry, name))
{
auditEntry.Properties.Add(new AuditEntryProperty(auditEntry, string.Concat(prefix, name), null, value));
var auditEntryProperty = auditEntry.Parent.Configuration.AuditEntryPropertyFactory != null ?
auditEntry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(auditEntry, objectStateEntry, string.Concat(prefix, name), null, value)) :
new AuditEntryProperty();

auditEntryProperty.Build(auditEntry, string.Concat(prefix, name), null, value);
auditEntry.Properties.Add(auditEntryProperty);
}
}
}
Expand All @@ -87,9 +97,14 @@ public static void AuditEntityAdded(AuditEntry entry, EntityEntry objectStateEnt
{
var property = objectStateEntry.Property(propertyEntry.Name);

if (entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, propertyEntry.Name))
if (property.Metadata.IsKey() || entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, propertyEntry.Name))
{
entry.Properties.Add(new AuditEntryProperty(entry, propertyEntry.Name, null, property.CurrentValue));
var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ?
entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, propertyEntry.Name, null, property.CurrentValue)) :
new AuditEntryProperty();

auditEntryProperty.Build(entry, propertyEntry.Name, null, property.CurrentValue);
entry.Properties.Add(auditEntryProperty);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved.

using System.Data.Common;
using System.Linq;

#if EF5
using System.Data.Objects;

#elif EF6
using System.Data.Entity.Core.Objects;

#elif EFCORE
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;

#endif
Expand All @@ -30,14 +33,16 @@ public static void AuditEntityDeleted(Audit audit, ObjectStateEntry objectStateE
public static void AuditEntityDeleted(Audit audit, EntityEntry objectStateEntry)
#endif
{
var entry = new AuditEntry(audit, objectStateEntry)
{
State = AuditEntryState.EntityDeleted
};
var entry = audit.Configuration.AuditEntryFactory != null ?
audit.Configuration.AuditEntryFactory(new AuditEntryFactoryArgs(audit, objectStateEntry, AuditEntryState.EntityDeleted)) :
new AuditEntry();

entry.Build(audit, objectStateEntry);
entry.State = AuditEntryState.EntityDeleted;


#if EF5 || EF6
AuditEntityDeleted(entry, objectStateEntry.OriginalValues);
AuditEntityDeleted(entry, objectStateEntry, objectStateEntry.OriginalValues);
#elif EFCORE
AuditEntityDeleted(entry, objectStateEntry);
#endif
Expand All @@ -49,7 +54,7 @@ public static void AuditEntityDeleted(Audit audit, EntityEntry objectStateEntry)
/// <param name="entry">The entry.</param>
/// <param name="record">The record.</param>
/// <param name="prefix">The prefix.</param>
public static void AuditEntityDeleted(AuditEntry entry, DbDataRecord record, string prefix = "")
public static void AuditEntityDeleted(AuditEntry entry, ObjectStateEntry objectStateEntry, DbDataRecord record, string prefix = "")
{
for (var i = 0; i < record.FieldCount; i++)
{
Expand All @@ -60,11 +65,16 @@ public static void AuditEntityDeleted(AuditEntry entry, DbDataRecord record, str
if (valueRecord != null)
{
// Complex Type
AuditEntityDeleted(entry, valueRecord, string.Concat(prefix, name, "."));
AuditEntityDeleted(entry, objectStateEntry, valueRecord, string.Concat(prefix, name, "."));
}
else if (entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, name))
else if (objectStateEntry.EntityKey.EntityKeyValues.Any(x => x.Key == name) || entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, name))
{
entry.Properties.Add(new AuditEntryProperty(entry, string.Concat(prefix, name), value, null));
var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ?
entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, string.Concat(prefix, name), value, null)) :
new AuditEntryProperty();

auditEntryProperty.Build(entry, string.Concat(prefix, name), value, null);
entry.Properties.Add(auditEntryProperty);
}
}
}
Expand All @@ -77,9 +87,14 @@ public static void AuditEntityDeleted(AuditEntry entry, EntityEntry objectStateE
{
var property = objectStateEntry.Property(propertyEntry.Name);

if (entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, propertyEntry.Name))
if (property.Metadata.IsKey() || entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, propertyEntry.Name))
{
entry.Properties.Add(new AuditEntryProperty(entry, propertyEntry.Name, property.OriginalValue, null));
var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ?
entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, propertyEntry.Name, property.OriginalValue, null)) :
new AuditEntryProperty();

auditEntryProperty.Build(entry, propertyEntry.Name, property.OriginalValue, null);
entry.Properties.Add(auditEntryProperty);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ public static void AuditEntityModified(Audit audit, ObjectStateEntry objectState
public static void AuditEntityModified(Audit audit, EntityEntry objectStateEntry, AuditEntryState state)
#endif
{
var entry = new AuditEntry(audit, objectStateEntry)
{
State = state
};
var entry = audit.Configuration.AuditEntryFactory != null ?
audit.Configuration.AuditEntryFactory(new AuditEntryFactoryArgs(audit, objectStateEntry, state)) :
new AuditEntry();

entry.Build(audit, objectStateEntry);
entry.State = state;

#if EF5 || EF6
AuditEntityModified(audit, entry, objectStateEntry, objectStateEntry.OriginalValues, objectStateEntry.CurrentValues);
Expand Down Expand Up @@ -68,13 +70,19 @@ public static void AuditEntityModified(Audit audit, AuditEntry entry, ObjectStat
AuditEntityModified(audit, entry, objectStateEntry, valueRecord, currentValue as DbUpdatableDataRecord, string.Concat(prefix, name, "."));
}

else if (entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, name))
else if (objectStateEntry.EntityKey.EntityKeyValues.Any(x => x.Key == name)
|| entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, name))
{
if (!audit.Configuration.IgnorePropertyUnchanged
|| objectStateEntry.EntityKey.EntityKeyValues.Any(x => x.Key == name)
|| !Equals(currentValue, originalValue))
{
entry.Properties.Add(new AuditEntryProperty(entry, string.Concat(prefix, name), originalValue, currentValue));
var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ?
entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, string.Concat(prefix, name), originalValue, currentValue)) :
new AuditEntryProperty();

auditEntryProperty.Build(entry, string.Concat(prefix, name), originalValue, currentValue);
entry.Properties.Add(auditEntryProperty);
}
}
}
Expand All @@ -88,11 +96,16 @@ public static void AuditEntityModified(Audit audit, AuditEntry entry, EntityEntr
{
var property = objectStateEntry.Property(propertyEntry.Name);

if (entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, propertyEntry.Name))
if (property.Metadata.IsKey() || entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, propertyEntry.Name))
{
if (!audit.Configuration.IgnorePropertyUnchanged || property.Metadata.IsKey() || property.IsModified)
{
entry.Properties.Add(new AuditEntryProperty(entry, propertyEntry.Name, property.OriginalValue, property.CurrentValue));
var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ?
entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, propertyEntry.Name, property.OriginalValue, property.CurrentValue)) :
new AuditEntryProperty();

auditEntryProperty.Build(entry, propertyEntry.Name, property.OriginalValue, property.CurrentValue);
entry.Properties.Add(auditEntryProperty);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ public static void AuditRelationAdded(Audit audit, ObjectStateEntry objectStateE
public static void AuditRelationAdded(Audit audit, EntityEntry objectStateEntry)
#endif
{
var entry = new AuditEntry(audit, objectStateEntry)
{
State = AuditEntryState.RelationshipAdded
};
var entry = audit.Configuration.AuditEntryFactory != null ?
audit.Configuration.AuditEntryFactory(new AuditEntryFactoryArgs(audit, objectStateEntry, AuditEntryState.RelationshipAdded)) :
new AuditEntry();

entry.Build(audit, objectStateEntry);
entry.State = AuditEntryState.RelationshipAdded;

var values = objectStateEntry.CurrentValues;

Expand All @@ -55,12 +57,22 @@ public static void AuditRelationAdded(Audit audit, EntityEntry objectStateEntry)

foreach (var keyValue in leftKeys.EntityKeyValues)
{
entry.Properties.Add(new AuditEntryProperty(entry, leftRelationName, keyValue.Key, null, keyValue.Value));
var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ?
entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, leftRelationName, keyValue.Key, null, keyValue.Value)) :
new AuditEntryProperty();

auditEntryProperty.Build(entry, leftRelationName, keyValue.Key, null, keyValue.Value);
entry.Properties.Add(auditEntryProperty);
}

foreach (var keyValue in rightKeys.EntityKeyValues)
{
entry.Properties.Add(new AuditEntryProperty(entry, rightRelationName, keyValue.Key, null, keyValue.Value));
var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ?
entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, rightRelationName, keyValue.Key, null, keyValue.Value)) :
new AuditEntryProperty();

auditEntryProperty.Build(entry, rightRelationName, keyValue.Key, null, keyValue.Value);
entry.Properties.Add(auditEntryProperty);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ public static void AuditRelationDeleted(Audit audit, ObjectStateEntry objectStat
public static void AuditRelationDeleted(Audit audit, EntityEntry objectStateEntry)
#endif
{
var entry = new AuditEntry(audit, objectStateEntry)
{
State = AuditEntryState.RelationshipDeleted
};
var entry = audit.Configuration.AuditEntryFactory != null ?
audit.Configuration.AuditEntryFactory(new AuditEntryFactoryArgs(audit, objectStateEntry, AuditEntryState.RelationshipDeleted)) :
new AuditEntry();

entry.Build(audit, objectStateEntry);
entry.State = AuditEntryState.RelationshipDeleted;

var values = objectStateEntry.OriginalValues;
for (var i = 0; i < values.FieldCount; i++)
Expand All @@ -44,7 +46,12 @@ public static void AuditRelationDeleted(Audit audit, EntityEntry objectStateEntr
var value = (EntityKey) values.GetValue(i);
foreach (var keyValue in value.EntityKeyValues)
{
entry.Properties.Add(new AuditEntryProperty(entry, relationName, keyValue.Key, keyValue.Value, null));
var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ?
entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, relationName, keyValue.Key, keyValue.Value, null)) :
new AuditEntryProperty();

auditEntryProperty.Build(entry, relationName, keyValue.Key, keyValue.Value, null);
entry.Properties.Add(auditEntryProperty);
}
}

Expand Down
Loading

0 comments on commit 6795c71

Please sign in to comment.