1
0
mirror of https://github.com/chylex/Minecraft-Phantom-Panel.git synced 2024-12-22 16:42:50 +01:00
Minecraft-Phantom-Panel/Web/Phantom.Web.Services/Authentication/AuthenticationStateExtensions.cs

32 lines
1.2 KiB
C#

using System.Security.Claims;
using Microsoft.AspNetCore.Components.Authorization;
using Phantom.Common.Data.Web.Users;
namespace Phantom.Web.Services.Authentication;
public static class AuthenticationStateExtensions {
public static AuthenticatedUser? GetAuthenticatedUser(this AuthenticationState authenticationState) {
return authenticationState.User.GetAuthenticatedUser();
}
public static AuthenticatedUser? GetAuthenticatedUser(this ClaimsPrincipal claimsPrincipal) {
return claimsPrincipal is CustomClaimsPrincipal principal ? principal.User : null;
}
public static PermissionSet GetPermissions(this AuthenticationState authenticationState) {
return authenticationState.User.GetPermissions();
}
public static PermissionSet GetPermissions(this ClaimsPrincipal claimsPrincipal) {
return claimsPrincipal.GetAuthenticatedUser() is {} user ? user.Info.Permissions : PermissionSet.None;
}
public static bool CheckPermission(this AuthenticationState authenticationState, Permission permission) {
return authenticationState.User.CheckPermission(permission);
}
public static bool CheckPermission(this ClaimsPrincipal claimsPrincipal, Permission permission) {
return claimsPrincipal.GetPermissions().Check(permission);
}
}