Skip to content

Commit

Permalink
template: add IgnoreTenancy extension method
Browse files Browse the repository at this point in the history
  • Loading branch information
ascott18 committed Oct 15, 2024
1 parent 4b43777 commit 74431fd
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/stacks/vue/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ public override async Task<ClaimsPrincipal> 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.
Expand Down Expand Up @@ -72,7 +72,7 @@ public override async Task<ClaimsPrincipal> CreateAsync(User user)
.Select(p => new Claim(AppClaimTypes.Permission, p.ToString()));

var permissionIdentity = new ClaimsIdentity(
permissions,
permissions,
"Permissions",
Options.ClaimsIdentity.UserNameClaimType,
AppClaimTypes.Permission);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Coalesce.Starter.Vue.Data.Utilities;

public static class QueryableExtensions
{
#if Tenancy
/// <summary>
/// Perform an untracked query without any tenant filtering.
/// </summary>
public static IQueryable<T> IgnoreTenancy<T>(this IQueryable<T> query)
where T : class
=> query.IgnoreQueryFilters().AsNoTrackingWithIdentityResolution();
#endif
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 74431fd

Please sign in to comment.