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
All checks were successful
Build & Deploy Leave Service / build (push) Successful in 1m49s
This commit is contained in:
parent
b1df33dc20
commit
7e0f0485fd
5 changed files with 201 additions and 12 deletions
|
|
@ -79,13 +79,39 @@ namespace BMA.EHR.Domain.Middlewares
|
|||
GetProfileByKeycloakIdLocal? pf = null;
|
||||
var tokenUserInfo = await ExtractTokenUserInfoAsync(token);
|
||||
|
||||
// Store tokenUserInfo in HttpContext.Items for controllers to use
|
||||
context.Items["TokenUserInfo"] = tokenUserInfo;
|
||||
|
||||
// ดึง keycloakId จาก JWT token
|
||||
keycloakId = tokenUserInfo.KeycloakId;
|
||||
|
||||
// ดึง profile จาก cache หรือ API
|
||||
// ดึง profile จาก claims หรือ cache หรือ API
|
||||
if (Guid.TryParse(keycloakId, out var parsedId) && parsedId != Guid.Empty)
|
||||
{
|
||||
pf = await GetProfileWithCacheAsync(parsedId, token);
|
||||
// Build profile from token claims if available
|
||||
if (tokenUserInfo.OrgRootDnaId.HasValue && tokenUserInfo.ProfileId.HasValue)
|
||||
{
|
||||
pf = new GetProfileByKeycloakIdLocal
|
||||
{
|
||||
Id = tokenUserInfo.ProfileId.Value,
|
||||
CitizenId = tokenUserInfo.PreferredUsername,
|
||||
Prefix = tokenUserInfo.Prefix,
|
||||
FirstName = tokenUserInfo.GivenName,
|
||||
LastName = tokenUserInfo.FamilyName,
|
||||
RootDnaId = tokenUserInfo.OrgRootDnaId,
|
||||
Child1DnaId = tokenUserInfo.OrgChild1DnaId,
|
||||
Child2DnaId = tokenUserInfo.OrgChild2DnaId,
|
||||
Child3DnaId = tokenUserInfo.OrgChild3DnaId,
|
||||
Child4DnaId = tokenUserInfo.OrgChild4DnaId,
|
||||
};
|
||||
Console.WriteLine($"[INFO] Using claims for profile - OrgRootDnaId: {pf.RootDnaId}, ProfileId: {pf.Id}");
|
||||
}
|
||||
else
|
||||
{
|
||||
// Fallback to API only if critical claims are missing
|
||||
Console.WriteLine("[WARN] Critical claims missing, falling back to API call");
|
||||
pf = await GetProfileWithCacheAsync(parsedId, token);
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
|
|
@ -649,6 +675,87 @@ namespace BMA.EHR.Domain.Middlewares
|
|||
Console.WriteLine($"Extracted family_name: {result.FamilyName}");
|
||||
}
|
||||
|
||||
// ดึง empType
|
||||
if (jsonDoc.RootElement.TryGetProperty("empType", out var empTypeElement))
|
||||
{
|
||||
result.EmpType = empTypeElement.GetString();
|
||||
Console.WriteLine($"Extracted empType: {result.EmpType}");
|
||||
}
|
||||
|
||||
// ดึง orgChild1DnaId
|
||||
if (jsonDoc.RootElement.TryGetProperty("orgChild1DnaId", out var orgChild1Element))
|
||||
{
|
||||
if (Guid.TryParse(orgChild1Element.GetString(), out var orgChild1Guid))
|
||||
{
|
||||
result.OrgChild1DnaId = orgChild1Guid;
|
||||
Console.WriteLine($"Extracted orgChild1DnaId: {result.OrgChild1DnaId}");
|
||||
}
|
||||
}
|
||||
|
||||
// ดึง orgChild2DnaId
|
||||
if (jsonDoc.RootElement.TryGetProperty("orgChild2DnaId", out var orgChild2Element))
|
||||
{
|
||||
if (Guid.TryParse(orgChild2Element.GetString(), out var orgChild2Guid))
|
||||
{
|
||||
result.OrgChild2DnaId = orgChild2Guid;
|
||||
Console.WriteLine($"Extracted orgChild2DnaId: {result.OrgChild2DnaId}");
|
||||
}
|
||||
}
|
||||
|
||||
// ดึง orgChild3DnaId
|
||||
if (jsonDoc.RootElement.TryGetProperty("orgChild3DnaId", out var orgChild3Element))
|
||||
{
|
||||
if (Guid.TryParse(orgChild3Element.GetString(), out var orgChild3Guid))
|
||||
{
|
||||
result.OrgChild3DnaId = orgChild3Guid;
|
||||
Console.WriteLine($"Extracted orgChild3DnaId: {result.OrgChild3DnaId}");
|
||||
}
|
||||
}
|
||||
|
||||
// ดึง orgChild4DnaId
|
||||
if (jsonDoc.RootElement.TryGetProperty("orgChild4DnaId", out var orgChild4Element))
|
||||
{
|
||||
if (Guid.TryParse(orgChild4Element.GetString(), out var orgChild4Guid))
|
||||
{
|
||||
result.OrgChild4DnaId = orgChild4Guid;
|
||||
Console.WriteLine($"Extracted orgChild4DnaId: {result.OrgChild4DnaId}");
|
||||
}
|
||||
}
|
||||
|
||||
// ดึง orgRootDnaId
|
||||
if (jsonDoc.RootElement.TryGetProperty("orgRootDnaId", out var orgRootElement))
|
||||
{
|
||||
if (Guid.TryParse(orgRootElement.GetString(), out var orgRootGuid))
|
||||
{
|
||||
result.OrgRootDnaId = orgRootGuid;
|
||||
Console.WriteLine($"Extracted orgRootDnaId: {result.OrgRootDnaId}");
|
||||
}
|
||||
}
|
||||
|
||||
// ดึง profileId
|
||||
if (jsonDoc.RootElement.TryGetProperty("profileId", out var profileIdElement))
|
||||
{
|
||||
if (Guid.TryParse(profileIdElement.GetString(), out var profileIdGuid))
|
||||
{
|
||||
result.ProfileId = profileIdGuid;
|
||||
Console.WriteLine($"Extracted profileId: {result.ProfileId}");
|
||||
}
|
||||
}
|
||||
|
||||
// ดึง prefix
|
||||
if (jsonDoc.RootElement.TryGetProperty("prefix", out var prefixElement))
|
||||
{
|
||||
result.Prefix = prefixElement.GetString();
|
||||
Console.WriteLine($"Extracted prefix: {result.Prefix}");
|
||||
}
|
||||
|
||||
// ดึง name
|
||||
if (jsonDoc.RootElement.TryGetProperty("name", out var nameElement))
|
||||
{
|
||||
result.Name = nameElement.GetString();
|
||||
Console.WriteLine($"Extracted name: {result.Name}");
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
|
@ -767,14 +874,6 @@ namespace BMA.EHR.Domain.Middlewares
|
|||
}
|
||||
|
||||
// Model classes
|
||||
public class TokenUserInfo
|
||||
{
|
||||
public string KeycloakId { get; set; } = string.Empty;
|
||||
public string? PreferredUsername { get; set; }
|
||||
public string? GivenName { get; set; }
|
||||
public string? FamilyName { get; set; }
|
||||
}
|
||||
|
||||
public class GetProfileByKeycloakIdLocal
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue