Skip to content

Commit

Permalink
fix: only throw exception in EnforceMultiTenant for null tenant if th…
Browse files Browse the repository at this point in the history
…ere are entity changes.
  • Loading branch information
AndrewTriesToCode committed Apr 28, 2024
1 parent 5efd7c8 commit 88b1420
Showing 1 changed file with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
// Refer to the solution LICENSE file for more information.

using System.Linq;
using Finbuckle.MultiTenant.Abstractions;
using Finbuckle.MultiTenant.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

// ReSharper disable once CheckNamespace
namespace Finbuckle.MultiTenant;

public static class IMultiTenantDbContextExtensions
public static class MultiTenantDbContextExtensions
{
/// <summary>
/// Checks the TenantId on entities taking into account
Expand All @@ -17,7 +18,7 @@ public static class IMultiTenantDbContextExtensions
public static void EnforceMultiTenant<TContext>(this TContext context) where TContext : DbContext, IMultiTenantDbContext
{
var changeTracker = context.ChangeTracker;
var tenantInfo = context.TenantInfo;
ITenantInfo tenantInfo = context.TenantInfo!;
var tenantMismatchMode = context.TenantMismatchMode;
var tenantNotSetMode = context.TenantNotSetMode;

Expand All @@ -26,14 +27,17 @@ public static void EnforceMultiTenant<TContext>(this TContext context) where TCo
Where(e => e.Metadata.IsMultiTenant()).ToList();

// ensure tenant context is valid
if (tenantInfo is null)
throw new MultiTenantException("MultiTenant Entity cannot be changed if TenantInfo is null.");
if (changedMultiTenantEntities.Any())
{
if (tenantInfo == null)
throw new MultiTenantException("MultiTenant Entity cannot be changed if TenantInfo is null.");
}

// get list of all added entities with MultiTenant annotation
var addedMultiTenantEntities = changedMultiTenantEntities.
Where(e => e.State == EntityState.Added).ToList();

// handle Tenant Id mismatches for added entities
// handle Tenant ID mismatches for added entities
var mismatchedAdded = addedMultiTenantEntities.
Where(e => (string?)e.Property("TenantId").CurrentValue != null &&
(string?)e.Property("TenantId").CurrentValue != tenantInfo.Id).ToList();
Expand Down Expand Up @@ -71,7 +75,7 @@ public static void EnforceMultiTenant<TContext>(this TContext context) where TCo
var modifiedMultiTenantEntities = changedMultiTenantEntities.
Where(e => e.State == EntityState.Modified).ToList();

// handle Tenant Id mismatches for modified entities
// handle Tenant ID mismatches for modified entities
var mismatchedModified = modifiedMultiTenantEntities.
Where(e => (string?)e.Property("TenantId").CurrentValue != null &&
(string?)e.Property("TenantId").CurrentValue != tenantInfo.Id).ToList();
Expand All @@ -96,7 +100,7 @@ public static void EnforceMultiTenant<TContext>(this TContext context) where TCo
}
}

// handle Tenant Id not set for modified entities
// handle Tenant ID not set for modified entities
var notSetModified = modifiedMultiTenantEntities.
Where(e => (string?)e.Property("TenantId").CurrentValue == null).ToList();

Expand All @@ -120,7 +124,7 @@ public static void EnforceMultiTenant<TContext>(this TContext context) where TCo
var deletedMultiTenantEntities = changedMultiTenantEntities.
Where(e => e.State == EntityState.Deleted).ToList();

// handle Tenant Id mismatches for deleted entities
// handle Tenant ID mismatches for deleted entities
var mismatchedDeleted = deletedMultiTenantEntities.
Where(e => (string?)e.Property("TenantId").CurrentValue != null &&
(string?)e.Property("TenantId").CurrentValue != tenantInfo.Id).ToList();
Expand Down

0 comments on commit 88b1420

Please sign in to comment.