Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions src/LinqTests/Bugs/Bug_1884_multi_tenancy_and_Any_query.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Marten;
using Marten.Testing.Harness;
using Shouldly;
namespace LinqTests.Bugs;

public class Bug_1884_multi_tenancy_and_Any_query: BugIntegrationContext
{
public class ComplexRole(string id)
{
public string Id { get; set; } = id;
}

public class User
{
public string Id { get; set; }
public string UserName { get; set; }
public string[] Roles { get; set; }
public IReadOnlyCollection<ComplexRole> ComplexRoles { get; set; } = [];
}

public Bug_1884_multi_tenancy_and_Any_query()
Expand All @@ -21,6 +29,24 @@ public Bug_1884_multi_tenancy_and_Any_query()
});
}

[Fact]
public async Task any_tenant_with_child_collection_any_in_separate_where_clauses()
{
var store = theStore;

var validRoles = new List<string> { "admin", "user" };

// GH-4146: AnyTenant() in a separate Where clause from a child collection Any()
// should not filter by tenant
await using var query = store.QuerySession("tenant1");
var sql = query.Query<User>()
.Where(x => x.AnyTenant())
.Where(x => x.ComplexRoles.Any(cr => validRoles.Contains(cr.Id)))
.ToCommand().CommandText;

sql.ShouldNotContain("tenant_id");
}

[Fact]
public async Task any_filter_honors_tenancy()
{
Expand Down Expand Up @@ -48,11 +74,14 @@ public async Task any_filter_honors_tenancy()

await using (var query = store.QuerySession("tenant1"))
{
query.Query<User>()
var linq = query.Query<User>()
.Where(x => x.Roles.Any(_ => validRoles.Contains(_)) && x.AnyTenant())
.OrderBy(x => x.UserName)
.Select(x => x.UserName)
.ShouldHaveTheSameElementsAs("Bill", "Jill");
.Select(x => x.UserName);

linq.ToCommand().CommandText.ShouldNotContain("tenant_id");

linq.ShouldHaveTheSameElementsAs("Bill", "Jill");
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/Marten/Linq/SqlGeneration/Filters/SubQueryFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public SubQueryFilter(ICollectionMember member, ISqlFragment inner)
/// </summary>
public bool Not { get; set; }

/// <summary>
/// When true, skips the automatic tenant_id filter in Apply().
/// Set when AnyTenant() is used in the query.
/// </summary>
internal bool BypassTenantFilter { get; set; }

public ISqlFragment Reverse()
{
Not = !Not;
Expand All @@ -45,7 +51,7 @@ void ISqlFragment.Apply(ICommandBuilder builder)
builder.Append("NOT(");
}

if (builder.TenantId != StorageConstants.DefaultTenantId && Member is ChildCollectionMember child)
if (!BypassTenantFilter && builder.TenantId != StorageConstants.DefaultTenantId && Member is ChildCollectionMember child)
{
if (child.Ancestors[0] is DocumentQueryableMemberCollection c && c.TenancyStyle == TenancyStyle.Conjoined)
{
Expand Down
15 changes: 14 additions & 1 deletion src/Marten/Linq/SqlGeneration/SelectorStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Marten.Internal;
using Marten.Linq.QueryHandlers;
using Marten.Linq.Selectors;
using Marten.Linq.Parsing.Methods;
using Marten.Linq.SqlGeneration.Filters;
using Weasel.Postgresql;
using Weasel.Postgresql.SqlGeneration;
Expand Down Expand Up @@ -151,7 +152,19 @@ protected override void compileAnySubQueries(IMartenSession session)
break;
}

foreach (var subQuery in subQueries) subQuery.PlaceUnnestAbove(session, this, topLevel);
// If AnyTenant() is among the top-level filters, tell SubQueryFilters
// to skip their hardcoded tenant_id filter (GH-4146)
var bypassTenant = topLevel != null && topLevel.ContainsAny<AnyTenant>();

foreach (var subQuery in subQueries)
{
if (bypassTenant && subQuery is SubQueryFilter sqf)
{
sqf.BypassTenantFilter = true;
}

subQuery.PlaceUnnestAbove(session, this, topLevel);
}

// We've moved all the non-sub query filters up to the various explode statements
Wheres.Clear();
Expand Down
Loading