From 74431fda132fa75ea1c3859942c34ea8cfd64d4d Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Tue, 15 Oct 2024 11:51:40 -0700 Subject: [PATCH] template: add IgnoreTenancy extension method --- docs/stacks/vue/getting-started.md | 1 + .../Auth/ClaimsPrincipalFactory.cs | 6 +++--- .../Coalesce.Starter.Vue.Data/GlobalUsings.cs | 1 + .../Utilities/QueryableExtensions.cs | 13 +++++++++++++ .../Pages/SelectTenant.cshtml.cs | 3 ++- 5 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 templates/Coalesce.Vue.Template/content/Coalesce.Starter.Vue.Data/Utilities/QueryableExtensions.cs diff --git a/docs/stacks/vue/getting-started.md b/docs/stacks/vue/getting-started.md index cd01c3580..365d1ef38 100644 --- a/docs/stacks/vue/getting-started.md +++ b/docs/stacks/vue/getting-started.md @@ -59,6 +59,7 @@ dotnet new install IntelliTect.Coalesce.Vue.Template dotnet new coalescevue -n {{effectiveNamespace}} -o {{effectiveFolder}} {{templateParams}} cd {{effectiveFolder}}/*.Web npm ci +npm run lint:fix dotnet restore dotnet coalesce ``` diff --git a/templates/Coalesce.Vue.Template/content/Coalesce.Starter.Vue.Data/Auth/ClaimsPrincipalFactory.cs b/templates/Coalesce.Vue.Template/content/Coalesce.Starter.Vue.Data/Auth/ClaimsPrincipalFactory.cs index 03058e183..9bcf9b7e6 100644 --- a/templates/Coalesce.Vue.Template/content/Coalesce.Starter.Vue.Data/Auth/ClaimsPrincipalFactory.cs +++ b/templates/Coalesce.Vue.Template/content/Coalesce.Starter.Vue.Data/Auth/ClaimsPrincipalFactory.cs @@ -19,12 +19,12 @@ public override async Task CreateAsync(User user) { // User doesn't have a selected tenant. Pick one for them. var membership = await db.TenantMemberships - .IgnoreQueryFilters() + .IgnoreTenancy() #if TrackingBase .OrderBy(m => m.CreatedOn) // Prefer oldest membership #endif .FirstOrDefaultAsync(tm => tm.UserId == user.Id); - + // Default to the "null" tenant if the user belongs to no tenants. // This allows the rest of the sign-in process to function, // but will never match a real tenant or produce real roles/permissions. @@ -72,7 +72,7 @@ public override async Task CreateAsync(User user) .Select(p => new Claim(AppClaimTypes.Permission, p.ToString())); var permissionIdentity = new ClaimsIdentity( - permissions, + permissions, "Permissions", Options.ClaimsIdentity.UserNameClaimType, AppClaimTypes.Permission); diff --git a/templates/Coalesce.Vue.Template/content/Coalesce.Starter.Vue.Data/GlobalUsings.cs b/templates/Coalesce.Vue.Template/content/Coalesce.Starter.Vue.Data/GlobalUsings.cs index 16998ad46..c8a1c4329 100644 --- a/templates/Coalesce.Vue.Template/content/Coalesce.Starter.Vue.Data/GlobalUsings.cs +++ b/templates/Coalesce.Vue.Template/content/Coalesce.Starter.Vue.Data/GlobalUsings.cs @@ -9,6 +9,7 @@ global using System.Security.Claims; global using Coalesce.Starter.Vue.Data.Auth; global using Coalesce.Starter.Vue.Data.Coalesce; +global using Coalesce.Starter.Vue.Data.Utilities; #if (Identity || ExampleModel) global using Coalesce.Starter.Vue.Data.Models; #endif diff --git a/templates/Coalesce.Vue.Template/content/Coalesce.Starter.Vue.Data/Utilities/QueryableExtensions.cs b/templates/Coalesce.Vue.Template/content/Coalesce.Starter.Vue.Data/Utilities/QueryableExtensions.cs new file mode 100644 index 000000000..acd8decdd --- /dev/null +++ b/templates/Coalesce.Vue.Template/content/Coalesce.Starter.Vue.Data/Utilities/QueryableExtensions.cs @@ -0,0 +1,13 @@ +namespace Coalesce.Starter.Vue.Data.Utilities; + +public static class QueryableExtensions +{ +#if Tenancy + /// + /// Perform an untracked query without any tenant filtering. + /// + public static IQueryable IgnoreTenancy(this IQueryable query) + where T : class + => query.IgnoreQueryFilters().AsNoTrackingWithIdentityResolution(); +#endif +} \ No newline at end of file diff --git a/templates/Coalesce.Vue.Template/content/Coalesce.Starter.Vue.Web/Pages/SelectTenant.cshtml.cs b/templates/Coalesce.Vue.Template/content/Coalesce.Starter.Vue.Web/Pages/SelectTenant.cshtml.cs index e61d41b7a..3d5dc43a3 100644 --- a/templates/Coalesce.Vue.Template/content/Coalesce.Starter.Vue.Web/Pages/SelectTenant.cshtml.cs +++ b/templates/Coalesce.Vue.Template/content/Coalesce.Starter.Vue.Web/Pages/SelectTenant.cshtml.cs @@ -1,6 +1,7 @@ using Coalesce.Starter.Vue.Data; using Coalesce.Starter.Vue.Data.Auth; using Coalesce.Starter.Vue.Data.Models; +using Coalesce.Starter.Vue.Data.Utilities; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; @@ -47,7 +48,7 @@ private async Task LoadTenants() { var userId = User.GetUserId(); Tenants = await db.TenantMemberships - .IgnoreQueryFilters() + .IgnoreTenancy() .Where(tm => tm.UserId == userId) .Select(tm => tm.Tenant!) .OrderBy(t => t.Name)