Skip to content

Commit cb4d979

Browse files
committed
UAuthStateView Enhancement
1 parent 8da7c1b commit cb4d979

File tree

36 files changed

+242
-71
lines changed

36 files changed

+242
-71
lines changed

samples/blazor-server/CodeBeam.UltimateAuth.Sample.BlazorServer/Components/Dialogs/PermissionDialog.razor

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,20 @@
109109
Permissions = permissions
110110
};
111111

112-
var res = await UAuthClient.Authorization.SetPermissionsAsync(Role.Id, req);
112+
var result = await UAuthClient.Authorization.SetPermissionsAsync(Role.Id, req);
113113

114-
if (!res.IsSuccess)
114+
if (!result.IsSuccess)
115115
{
116-
Snackbar.Add(res.Problem?.Title ?? "Failed to update permissions", Severity.Error);
116+
Snackbar.Add(result.Problem?.Detail ?? result.Problem?.Title ?? "Failed to update permissions", Severity.Error);
117117
return;
118118
}
119119

120-
Role = (await UAuthClient.Authorization.QueryRolesAsync(new RoleQuery() { Search = Role.Name })).Value!.Items.First();
120+
var result2 = await UAuthClient.Authorization.QueryRolesAsync(new RoleQuery() { Search = Role.Name });
121+
if (result2.Value?.Items is not null)
122+
{
123+
Role = result2.Value.Items.First();
124+
}
125+
121126
Snackbar.Add("Permissions updated", Severity.Success);
122127
RefreshUI();
123128
}

samples/blazor-server/CodeBeam.UltimateAuth.Sample.BlazorServer/Components/Dialogs/RoleDialog.razor

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
<DialogContent>
1515
<MudDataGrid @ref="_grid" T="RoleInfo" ServerData="LoadServerData" Hover="true" Dense="true" Bordered="true" Striped="true"
16-
EditMode="DataGridEditMode.Form" CommittedItemChanges="CommittedItemChanges" Loading="_loading">
16+
EditMode="DataGridEditMode.Form" CommittedItemChanges="CommittedItemChanges" Loading="_loading" ReadOnly="false">
1717

1818
<ToolBarContent>
1919
<MudStack Class="mud-width-full" Row="true" AlignItems="AlignItems.Center" Justify="Justify.Center">
@@ -83,7 +83,6 @@
8383
@code {
8484
private MudDataGrid<RoleInfo>? _grid;
8585
private bool _loading;
86-
private bool _reloadQueued;
8786
private string? _newRoleName;
8887

8988
[CascadingParameter]
@@ -219,10 +218,13 @@
219218

220219
private async Task ReloadAsync()
221220
{
221+
_loading = true;
222+
await Task.Delay(300);
222223
if (_grid is null)
223224
return;
224225

225226
await _grid.ReloadServerData();
227+
_loading = false;
226228
}
227229

228230
private int GetPermissionCount(RoleInfo role)

samples/blazor-server/CodeBeam.UltimateAuth.Sample.BlazorServer/Components/Pages/Home.razor

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
@inject IDialogService DialogService
1010
@using System.Security.Claims
1111
@using CodeBeam.UltimateAuth.Core.Contracts
12+
@using CodeBeam.UltimateAuth.Core.Defaults
1213
@using CodeBeam.UltimateAuth.Sample.BlazorServer.Components.Custom
1314

1415
@if (AuthState?.Identity?.UserStatus == UserStatus.SelfSuspended)

samples/blazor-server/CodeBeam.UltimateAuth.Sample.BlazorServer/Components/Pages/Home.razor.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,18 @@ private async Task SetAccountActiveAsync()
217217
}
218218
}
219219

220+
private string? _roles = "Admin";
221+
private void RefreshHiddenState()
222+
{
223+
if (_roles == "Admin")
224+
{
225+
_roles = "User";
226+
return;
227+
}
228+
229+
_roles = "Admin";
230+
}
231+
220232
public override void Dispose()
221233
{
222234
base.Dispose();

samples/blazor-server/CodeBeam.UltimateAuth.Sample.BlazorServer/Components/Pages/LandingPage.razor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using CodeBeam.UltimateAuth.Core.Constants;
1+
using CodeBeam.UltimateAuth.Core.Defaults;
22

33
namespace CodeBeam.UltimateAuth.Sample.BlazorServer.Components.Pages;
44

src/CodeBeam.UltimateAuth.Client/AuthState/UAuthState.cs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using CodeBeam.UltimateAuth.Core.Contracts;
1+
using CodeBeam.UltimateAuth.Authorization.Contracts;
2+
using CodeBeam.UltimateAuth.Core.Contracts;
3+
using CodeBeam.UltimateAuth.Core.Defaults;
24
using CodeBeam.UltimateAuth.Core.Domain;
35
using CodeBeam.UltimateAuth.Core.Extensions;
46
using CodeBeam.UltimateAuth.Users.Contracts;
@@ -40,6 +42,8 @@ internal void ApplySnapshot(AuthStateSnapshot snapshot, DateTimeOffset validated
4042
Identity = snapshot.Identity;
4143
Claims = snapshot.Claims;
4244

45+
_compiledPermissions = new CompiledPermissionSet(Claims.Permissions.Select(Permission.From));
46+
4347
IsStale = false;
4448
LastValidatedAt = validatedAt;
4549

@@ -114,7 +118,28 @@ internal void Clear()
114118

115119
public bool IsInRole(string role) => IsAuthenticated && Claims.IsInRole(role);
116120

117-
public bool HasPermission(string permission) => IsAuthenticated && Claims.HasPermission(permission);
121+
private CompiledPermissionSet? _compiledPermissions;
122+
public bool HasPermission(string permission)
123+
{
124+
if (!IsAuthenticated)
125+
return false;
126+
127+
if (Claims.HasPermission(permission))
128+
return true;
129+
130+
return _compiledPermissions?.IsAllowed(permission) == true;
131+
}
132+
133+
public bool HasAnyPermission(params string[] permissions)
134+
{
135+
foreach (var perm in permissions)
136+
{
137+
if (HasPermission(perm))
138+
return true;
139+
}
140+
141+
return false;
142+
}
118143

119144
public bool HasClaim(string type, string value) => IsAuthenticated && Claims.HasValue(type, value);
120145

@@ -123,7 +148,7 @@ internal void Clear()
123148
/// <summary>
124149
/// Creates a ClaimsPrincipal view for ASP.NET / Blazor integration.
125150
/// </summary>
126-
public ClaimsPrincipal ToClaimsPrincipal(string authenticationType = "UltimateAuth")
151+
public ClaimsPrincipal ToClaimsPrincipal(string authenticationType = UAuthConstants.SchemeDefaults.GlobalScheme)
127152
{
128153
if (!IsAuthenticated || Identity is null)
129154
return new ClaimsPrincipal(new ClaimsIdentity());

src/CodeBeam.UltimateAuth.Client/Components/UALoginDispatch.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@page "/__uauth/login-redirect"
22

33
@namespace CodeBeam.UltimateAuth.Client
4-
@using CodeBeam.UltimateAuth.Core.Constants
4+
@using CodeBeam.UltimateAuth.Core.Defaults
55
@using Microsoft.AspNetCore.WebUtilities
66
@inject NavigationManager Nav
77

src/CodeBeam.UltimateAuth.Client/Components/UAuthLoginForm.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
@using CodeBeam.UltimateAuth.Client.Device
55
@using CodeBeam.UltimateAuth.Client.Options
66
@using CodeBeam.UltimateAuth.Core.Abstractions
7-
@using CodeBeam.UltimateAuth.Core.Constants
7+
@using CodeBeam.UltimateAuth.Core.Defaults
88
@using CodeBeam.UltimateAuth.Core.Contracts
99
@using CodeBeam.UltimateAuth.Core.Options
1010
@using Microsoft.Extensions.Options

src/CodeBeam.UltimateAuth.Client/Components/UAuthLoginForm.razor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using CodeBeam.UltimateAuth.Client.Infrastructure;
22
using CodeBeam.UltimateAuth.Core.Abstractions;
3-
using CodeBeam.UltimateAuth.Core.Constants;
43
using CodeBeam.UltimateAuth.Core.Contracts;
4+
using CodeBeam.UltimateAuth.Core.Defaults;
55
using CodeBeam.UltimateAuth.Core.Domain;
66
using Microsoft.AspNetCore.Components;
77
using Microsoft.AspNetCore.WebUtilities;

src/CodeBeam.UltimateAuth.Client/Components/UAuthStateView.razor

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
@inherits UAuthReactiveComponentBase
44
@using CodeBeam.UltimateAuth.Core.Domain
5+
@using Microsoft.AspNetCore.Authorization
56
@using Microsoft.AspNetCore.Components.Authorization
7+
@inject IAuthorizationService AuthorizationService
68

79
@if (_inactive)
810
{
@@ -15,21 +17,22 @@
1517
@NotAuthorized
1618
}
1719
}
20+
else if (_authorizing && Authorizing is not null)
21+
{
22+
@Authorizing
23+
}
24+
else if (!_authorized)
25+
{
26+
@NotAuthorized
27+
}
1828
else
1929
{
20-
<AuthorizeView Roles="@Roles" Policy="@Policy">
21-
<Authorized>
22-
@if (Authorized is not null)
23-
{
24-
@Authorized(AuthState)
25-
}
26-
else if (ChildContent is not null)
27-
{
28-
@ChildContent(AuthState)
29-
}
30-
</Authorized>
31-
<NotAuthorized>
32-
@NotAuthorized
33-
</NotAuthorized>
34-
</AuthorizeView>
30+
if (Authorized is not null)
31+
{
32+
@Authorized(AuthState)
33+
}
34+
else if (ChildContent is not null)
35+
{
36+
@ChildContent(AuthState)
37+
}
3538
}

0 commit comments

Comments
 (0)