| | | 1 | | using Elsa.ExternalAuthentication.Contracts; |
| | | 2 | | using Elsa.ExternalAuthentication.Models; |
| | | 3 | | using Elsa.Identity.Contracts; |
| | | 4 | | using Elsa.Identity.Models; |
| | | 5 | | |
| | | 6 | | namespace Elsa.ExternalAuthentication.Permissions; |
| | | 7 | | |
| | 5 | 8 | | public sealed class ElsaRolePermissionGrantSource(IUserProvider userProvider, IRoleProvider roleProvider) : IPermissionG |
| | | 9 | | { |
| | | 10 | | public const string SourceType = "elsa-roles"; |
| | 7 | 11 | | public string Type => SourceType; |
| | | 12 | | |
| | 0 | 13 | | public PermissionGrantSourceDescriptor Describe() => new(Type, "Elsa roles", "Grants the permissions assigned to the |
| | | 14 | | |
| | | 15 | | public async ValueTask<PermissionGrantResult> GetGrantsAsync(PermissionGrantContext context, CancellationToken cance |
| | | 16 | | { |
| | 1 | 17 | | var user = await userProvider.FindAsync(new UserFilter { Id = context.UserId }, cancellationToken); |
| | 1 | 18 | | if (user is null || !string.Equals(user.TenantId, context.TargetTenantId, StringComparison.Ordinal)) |
| | 0 | 19 | | return new PermissionGrantResult([], [new PermissionGrantWarning("user_not_available", "The Elsa user is not |
| | | 20 | | |
| | 1 | 21 | | var roles = await roleProvider.FindManyAsync(new RoleFilter { Ids = user.Roles.ToArray() }, cancellationToken); |
| | 1 | 22 | | var grants = roles |
| | 1 | 23 | | .Where(x => string.Equals(x.TenantId, context.TargetTenantId, StringComparison.Ordinal)) |
| | 1 | 24 | | .OrderBy(x => x.Id, StringComparer.Ordinal) |
| | 7 | 25 | | .SelectMany(role => role.Permissions.Where(permission => !string.IsNullOrWhiteSpace(permission)).OrderBy(per |
| | 1 | 26 | | .ToArray(); |
| | 1 | 27 | | return new PermissionGrantResult(grants, []); |
| | 1 | 28 | | } |
| | | 29 | | } |