Add TokenUserInfo class and extend ClaimsPrincipal with methods for user claims retrieval
All checks were successful
Build & Deploy Leave Service / build (push) Successful in 1m49s

This commit is contained in:
Suphonchai Phoonsawat 2026-03-17 15:47:03 +07:00
parent b1df33dc20
commit 7e0f0485fd
5 changed files with 201 additions and 12 deletions

View file

@ -0,0 +1,30 @@
using BMA.EHR.Domain.Common;
using System.Security.Claims;
namespace BMA.EHR.Domain.Extensions
{
public static class ClaimsPrincipalExtensions
{
public static string? GetClaimValue(this ClaimsPrincipal user, string claimType)
{
return user?.FindFirst(claimType)?.Value;
}
public static Guid? GetGuidClaim(this ClaimsPrincipal user, string claimType)
{
var value = user?.GetClaimValue(claimType);
return Guid.TryParse(value, out var guid) ? guid : null;
}
// Convenience methods for common claims
public static string? GetEmpType(this ClaimsPrincipal user) => user.GetClaimValue(BmaClaimTypes.EmpType);
public static Guid? GetOrgChild1DnaId(this ClaimsPrincipal user) => user.GetGuidClaim(BmaClaimTypes.OrgChild1DnaId);
public static Guid? GetOrgChild2DnaId(this ClaimsPrincipal user) => user.GetGuidClaim(BmaClaimTypes.OrgChild2DnaId);
public static Guid? GetOrgChild3DnaId(this ClaimsPrincipal user) => user.GetGuidClaim(BmaClaimTypes.OrgChild3DnaId);
public static Guid? GetOrgChild4DnaId(this ClaimsPrincipal user) => user.GetGuidClaim(BmaClaimTypes.OrgChild4DnaId);
public static Guid? GetOrgRootDnaId(this ClaimsPrincipal user) => user.GetGuidClaim(BmaClaimTypes.OrgRootDnaId);
public static Guid? GetProfileId(this ClaimsPrincipal user) => user.GetGuidClaim(BmaClaimTypes.ProfileId);
public static string? GetPrefix(this ClaimsPrincipal user) => user.GetClaimValue(BmaClaimTypes.Prefix);
public static string? GetName(this ClaimsPrincipal user) => user.GetClaimValue(BmaClaimTypes.Name);
}
}