14 lines
712 B
C#
14 lines
712 B
C#
using System.Security.Claims;
|
|
using HealthApp.Contracts;
|
|
namespace HealthApp.Security;
|
|
|
|
public sealed class AccessPolicy(ProfileQueries profiles, ProfessionalAssignments assignments)
|
|
{
|
|
public async Task<AuthorizedPatientContext?> AuthorizeAsync(ClaimsPrincipal user, Guid actualPatient, bool write, CancellationToken ct)
|
|
{
|
|
var account = user.FindFirstValue(ClaimTypes.NameIdentifier)!;
|
|
bool allowed = user.IsInRole("HealthcareProfessional") ? !write && assignments.IsAssigned(account, actualPatient) : user.IsInRole("Patient") && await profiles.ResolveAccountAsync(account, ct) == actualPatient;
|
|
return allowed ? new AuthorizedPatientContext(actualPatient, write) : null;
|
|
}
|
|
}
|