Skip to content

Commit

Permalink
Fixed audit logging when having shares owned types, that can not be f…
Browse files Browse the repository at this point in the history
…ound exclusively with context.GetEntry(entity)
  • Loading branch information
vivet committed Oct 5, 2024
1 parent fc5c704 commit a229550
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 28 deletions.
27 changes: 22 additions & 5 deletions Nano.Data/BaseDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.Extensions.Logging;
using Nano.Config;
using Nano.Data.Const;
using Nano.Data.Models;
Expand All @@ -26,7 +27,6 @@
using Nano.Security;
using Nano.Security.Const;
using Nano.Security.Data.Models;
using Serilog;
using Z.EntityFramework.Plus;

namespace Nano.Data;
Expand All @@ -45,6 +45,11 @@ public abstract class BaseDbContext<TIdentity> : IdentityDbContext<IdentityUser<
/// </summary>
public DataOptions Options { get; }

/// <summary>
/// Logger.
/// </summary>
public ILogger Logger { get; }

/// <summary>
/// Auto Save.
/// </summary>
Expand All @@ -67,6 +72,8 @@ protected BaseDbContext(DbContextOptions contextOptions, DataOptions dataOptions
{
this.Options = dataOptions ?? throw new ArgumentNullException(nameof(dataOptions));

this.Logger = this.GetService<ILogger>();

this.SavingChanges += (_, _) => this.SetPendingEntityEvents();
this.SavingChanges += (_, _) => this.SaveSoftDeletion();
this.SavedChanges += async (_, _) => await this.ExecuteEntityEvents();
Expand Down Expand Up @@ -281,7 +288,8 @@ internal virtual Task EnsureMigratedAsync(CancellationToken cancellationToken =
if (this.Options.ConnectionString == null)
return Task.CompletedTask;

Log.Information("Applying Migrations at start-up.");
this.Logger
.LogInformation("Applying Migrations at start-up.");

return this.Database
.MigrateAsync(cancellationToken);
Expand Down Expand Up @@ -386,7 +394,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
.ToTable(TableNames.IDENTITY_DATA_PROTECTION_KEYS);
}

private void SaveAudit(object entity, object tracked = null)
private void SaveAudit(object entity, object tracked = null, EntityEntry owner = null, string propertName = null)
{
if (entity == null)
throw new ArgumentNullException(nameof(entity));
Expand All @@ -400,7 +408,16 @@ private void SaveAudit(object entity, object tracked = null)

try
{
var entry = this.Entry(entity);
var entry = owner == null
? this.Entry(entity)
: propertName == null
? this.Entry(entity)
: owner.Reference(propertName).TargetEntry;

if (entry == null)
{
throw new NullReferenceException(nameof(entry));
}

var properties = entity
.GetType()
Expand Down Expand Up @@ -439,7 +456,7 @@ private void SaveAudit(object entity, object tracked = null)
continue;
}

this.SaveAudit(value, valueTracked);
this.SaveAudit(value, valueTracked, entry, propertyInfo.Name);
}
}
}
Expand Down
25 changes: 2 additions & 23 deletions NanoCore.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
<metadata>
<id>NanoCore</id>
<version>8.1.26</version>
<version>8.1.27</version>
<title>Nano Core</title>
<summary>Rapidly built and deploy rich .NET 8.0 applications. Configuration, Security, Logging, Data, Hosting, Docs, Localization, Versioning, Api, Error-Handling, and much more.</summary>
<description>The project is inspired by years of tedious repetitions, continuously re-writing similar code-snippets and libraries, to handle common functionality, not related to the business domain, such as logging, data persistence, message queuing, documentation, validation and similar.</description>
Expand All @@ -16,28 +16,7 @@
<projectUrl>https://github.com/Nano-Core</projectUrl>
<repository type="git" url="https://github.com/Nano-Core/Nano.Library.git" />
<releaseNotes>
- Added Storage provider and configuration.
- Fixed CSP error with style and script nonce token.
- Update error and exception handling. UX exceptions is no longer exposed as translated.
- Added Virus scan option
- Fixed typo.
- Improved Error response for exceptions, now including the type of exception in the messages.
- Fixed handling enum values in entity event handler.
- Added GuidNotEmpty validation attribute.
- Removed try/catch in entity event handlers, to allow retries.
- Fallback to update when adding in entity event handlers. Just in case the entity already exists.
- Fixed Minor Swagger issues.
- Fixed duplicate phone not being caught by exception handler.
- Removed type name for checking UX exceptions. Conflicted when using ToTable in mappings, so the table name and type name was not matching.
- Fixed Request TimeZone, using Microsoft serialization. Changed to Newtonsoft.
- Removed Simple types (Percentage, Address, Location, PhoneNumber, EmailAddress, etc).
- Removed support for Xml output formatter.
- Added additional IRepository methods, for complex order by expressions.
- Added Required annotation to Id property of BaseEntityIdentity.
- Property names of Publish annotation will now be concated when defined in a inheritance hierarchy.
- Changed serialization to newtonsoft for Swagger Gen.
- Removed Edit Query from api-client. No endpoint for that.
- Updated response serializer to only include IEntity references, when they are specifically included with include annotation.
- Fixed audit logging when having shares owned types, that can not be found exclusively with context.GetEntry(entity).
</releaseNotes>
<dependencies>
<group targetFramework="net8.0">
Expand Down

0 comments on commit a229550

Please sign in to comment.