Add extension to ClaimsPrincipal

This commit is contained in:
2025-11-13 09:38:54 +09:00
parent 9bd8778e34
commit b5e5b0ecd2
7 changed files with 24 additions and 5 deletions

View File

@ -76,7 +76,7 @@
ChangeState(PageState.Loading);
var state = await AuthenticationStateProvider.GetAuthenticationStateAsync();
if (state.User.IsInRole(AppRole.ADMINISTRATOR_TYPE) || state.User.IsInRole(AppRole.OPERATOR_TYPE))
if (state.User.IsOperatorOrHigher())
{
campaigns = await HeadquartersService.GetCampaignsAsync();

View File

@ -72,7 +72,7 @@
StateHasChanged();
var state = await AuthenticationStateProvider.GetAuthenticationStateAsync();
if (state.User.IsInRole(AppRole.ADMINISTRATOR_TYPE))
if (state.User.IsAdministrator())
{
currentUserId = state.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
usersWithRoles = await UsersService.GetUsersWithRoleAsync();

View File

@ -57,7 +57,7 @@
ChangeState(PageState.Loading);
var state = await AuthenticationStateProvider.GetAuthenticationStateAsync();
if (state.User.IsInRole(AppRole.ADMINISTRATOR_TYPE) || state.User.IsInRole(AppRole.OPERATOR_TYPE))
if (state.User.IsOperatorOrHigher())
{
operations = await HeadquartersService.GetOperationsAsync();

View File

@ -80,7 +80,7 @@
var finalState = SyncedPageState.Idle;
var state = await AuthenticationStateProvider.GetAuthenticationStateAsync();
if (state.User.IsInRole(AppRole.ADMINISTRATOR_TYPE) || state.User.IsInRole(AppRole.OPERATOR_TYPE))
if (state.User.IsOperatorOrHigher())
{
var operationInProgress = await HeadquartersService.HasActiveCampaignAsync(Campaign.CampaignType.ExportCommonRegistryElements_15_7_0_1);
if (operationInProgress)

View File

@ -80,7 +80,7 @@
var finalState = SyncedPageState.Idle;
var state = await AuthenticationStateProvider.GetAuthenticationStateAsync();
if (state.User.IsInRole(AppRole.ADMINISTRATOR_TYPE) || state.User.IsInRole(AppRole.OPERATOR_TYPE))
if (state.User.IsOperatorOrHigher())
{
var operationInProgress = await HeadquartersService.HasActiveCampaignAsync(Campaign.CampaignType.ExportPrivateRegistryElements_15_7_0_1);
if (operationInProgress)

View File

@ -6,6 +6,7 @@
@using Hcs.WebApp.Components.Shared
@using Hcs.WebApp.Data.Hcs
@using Hcs.WebApp.Data.Identity
@using Hcs.WebApp.Utils
@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing

View File

@ -0,0 +1,18 @@
using Hcs.WebApp.Data.Identity;
using System.Security.Claims;
namespace Hcs.WebApp.Utils
{
public static class ClaimsPrincipalExtensions
{
public static bool IsAdministrator(this ClaimsPrincipal claimsPrincipal)
{
return claimsPrincipal.IsInRole(AppRole.ADMINISTRATOR_TYPE);
}
public static bool IsOperatorOrHigher(this ClaimsPrincipal claimsPrincipal)
{
return claimsPrincipal.IsInRole(AppRole.OPERATOR_TYPE) || claimsPrincipal.IsInRole(AppRole.ADMINISTRATOR_TYPE);
}
}
}