| | | 1 | | using System.Security.Claims; |
| | | 2 | | using Elsa.UserTasks.Contracts; |
| | | 3 | | using Elsa.UserTasks.Models; |
| | | 4 | | using Elsa.UserTasks.Options; |
| | | 5 | | using Elsa.UserTasks.Permissions; |
| | | 6 | | using Microsoft.Extensions.Options; |
| | | 7 | | |
| | | 8 | | namespace Elsa.UserTasks.Services; |
| | | 9 | | |
| | 3 | 10 | | public sealed class DefaultClaimsIdentityResolver(IOptions<UserTasksOptions> options) : IUserTaskIdentityResolver |
| | | 11 | | { |
| | | 12 | | /// <summary>An ASP.NET role, not a permission, despite reading like one. Kept for hosts that grant it.</summary> |
| | | 13 | | private const string ManagerRole = "user-tasks:manager"; |
| | | 14 | | |
| | 3 | 15 | | private readonly UserTasksOptions _options = options.Value; |
| | | 16 | | |
| | | 17 | | public ValueTask<UserTaskActor?> ResolveAsync(ClaimsPrincipal principal, CancellationToken cancellationToken = defau |
| | | 18 | | { |
| | 3 | 19 | | var subjectId = principal.FindFirstValue(_options.SubjectClaimType) |
| | 3 | 20 | | ?? principal.FindFirstValue(ClaimTypes.NameIdentifier); |
| | | 21 | | |
| | 3 | 22 | | if (string.IsNullOrWhiteSpace(subjectId)) |
| | 0 | 23 | | return ValueTask.FromResult<UserTaskActor?>(null); |
| | | 24 | | |
| | 3 | 25 | | var tenantId = principal.FindFirstValue(_options.TenantClaimType) ?? _options.DefaultTenantId; |
| | 3 | 26 | | var provider = principal.FindFirstValue(_options.ProviderClaimType) ?? _options.DefaultProvider; |
| | 3 | 27 | | var subject = new ParticipantReference(tenantId, provider, UserTaskParticipantType.User, subjectId, |
| | 3 | 28 | | principal.FindFirstValue(_options.DisplayNameClaimType) ?? principal.Identity?.Name); |
| | 3 | 29 | | var groups = _options.GroupClaimTypes |
| | 6 | 30 | | .SelectMany(type => principal.FindAll(type)) |
| | 2 | 31 | | .Select(x => x.Value) |
| | 2 | 32 | | .Where(value => !string.IsNullOrWhiteSpace(value)) |
| | 3 | 33 | | .Distinct(StringComparer.Ordinal) |
| | 2 | 34 | | .Select(id => new ParticipantReference(tenantId, provider, UserTaskParticipantType.Group, id)) |
| | 3 | 35 | | .ToArray(); |
| | | 36 | | // Ordinal, matching the permission model everywhere else: folding case here would collapse two |
| | | 37 | | // spellings into whichever arrived first, and the survivor might be the one that no longer matches. |
| | 3 | 38 | | var permissions = _options.PermissionClaimTypes |
| | 6 | 39 | | .SelectMany(type => principal.FindAll(type)) |
| | 4 | 40 | | .Select(x => x.Value) |
| | 4 | 41 | | .Where(value => !string.IsNullOrWhiteSpace(value)) |
| | 3 | 42 | | .ToHashSet(StringComparer.Ordinal); |
| | 3 | 43 | | var actor = new UserTaskActor(subject, groups, subject.DisplayName) { Permissions = permissions }; |
| | | 44 | | |
| | 3 | 45 | | return ValueTask.FromResult<UserTaskActor?>(actor with |
| | 3 | 46 | | { |
| | 3 | 47 | | // Asked of the actor rather than of the raw strings, so a subtree or verb wildcard confers |
| | 3 | 48 | | // manager standing here exactly as it does at every other check. |
| | 3 | 49 | | IsManager = principal.IsInRole(ManagerRole) |
| | 3 | 50 | | || actor.HasPermission(UserTasksResourcePermissions.UserTasks, UserTaskVerbs.Supervise) |
| | 3 | 51 | | }); |
| | | 52 | | } |
| | | 53 | | } |