| | | 1 | | using ArturRios.Data.Relational.Core.Interfaces; |
| | | 2 | | using ArturRios.Heimdall.Domain.Entities; |
| | | 3 | | using ArturRios.Heimdall.Query.Input; |
| | | 4 | | using ArturRios.Heimdall.Query.Output; |
| | | 5 | | using ArturRios.Heimdall.Shared.Messages; |
| | | 6 | | using ArturRios.Heimdall.Shared.Services; |
| | | 7 | | using ArturRios.Mediator.Query.Interfaces; |
| | | 8 | | using ArturRios.Output; |
| | | 9 | | using FluentValidation; |
| | | 10 | | using Microsoft.EntityFrameworkCore; |
| | | 11 | | |
| | | 12 | | namespace ArturRios.Heimdall.Query.Handlers; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Handles <see cref="ListPendingRegistrationsQuery" /> (UC-49, FR-SR-11): a scope's |
| | | 16 | | /// self-registrations awaiting confirmation, newest first, for a System Admin or an owner of the |
| | | 17 | | /// scope. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <remarks> |
| | | 20 | | /// <para> |
| | | 21 | | /// A missing or logically deleted scope is AF-49a (<c>ScopeNotFound</c>), as for the scope's |
| | | 22 | | /// owners listing; a caller who neither is a System Admin nor owns it is AF-49b |
| | | 23 | | /// (<c>NotScopeOwner</c>). |
| | | 24 | | /// </para> |
| | | 25 | | /// <para> |
| | | 26 | | /// Whether the scope is still open to self-registration does not matter. Closing sign-up is |
| | | 27 | | /// when an owner most wants to see, and clear, what was left behind. |
| | | 28 | | /// </para> |
| | | 29 | | /// <para> |
| | | 30 | | /// FR-SR-06's anti-enumeration guarantee is about the anonymous <c>POST /api/auth/register</c> |
| | | 31 | | /// and is untouched: this listing is authenticated and limited to the scope's owners, and a taken |
| | | 32 | | /// address never produces a pending row. |
| | | 33 | | /// </para> |
| | | 34 | | /// </remarks> |
| | 20 | 35 | | public class ListPendingRegistrationsQueryHandler( |
| | 20 | 36 | | IAsyncReadOnlyRepository<Scope, long> scopeReader, |
| | 20 | 37 | | IAsyncReadOnlyRepository<PendingRegistration, long> pendingReader, |
| | 20 | 38 | | IScopeOwnershipChecker scopeOwnership, |
| | 20 | 39 | | IValidator<ListPendingRegistrationsQuery> validator) |
| | | 40 | | : IPaginatedQueryHandlerAsync<ListPendingRegistrationsQuery, PendingRegistrationOutput> |
| | | 41 | | { |
| | | 42 | | public async Task<PaginatedOutput<PendingRegistrationOutput>> HandleAsync( |
| | | 43 | | ListPendingRegistrationsQuery query, CancellationToken cancellationToken = default) |
| | | 44 | | { |
| | 20 | 45 | | var output = PaginatedOutput<PendingRegistrationOutput>.New; |
| | | 46 | | |
| | | 47 | | // NFR-10: page bounds, before any query runs. |
| | 20 | 48 | | var validation = await validator.ValidateAsync(query, cancellationToken); |
| | | 49 | | |
| | 20 | 50 | | if (!validation.IsValid) |
| | | 51 | | { |
| | 2 | 52 | | return output.WithErrors(validation.Errors.Select(failure => failure.ErrorMessage)); |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | // AF-49a. |
| | 19 | 56 | | var scope = await scopeReader.Query() |
| | 19 | 57 | | .FirstOrDefaultAsync(x => x.PublicId == query.ScopeId && !x.IsDeleted, cancellationToken); |
| | | 58 | | |
| | 19 | 59 | | if (scope is null) |
| | | 60 | | { |
| | 4 | 61 | | return output.WithError(ScopeMessages.ScopeNotFound); |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | // AF-49b: a System Admin bypasses; anyone else must own the scope. |
| | 15 | 65 | | if (!await scopeOwnership.ActorMayManageScopeAsync(query.ActingRole, query.ActingPersonId, scope.Id)) |
| | | 66 | | { |
| | 2 | 67 | | return output.WithError(ScopeMessages.NotScopeOwner); |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | // One reading of the clock for every row, so "expired" means the same thing across a page. |
| | 13 | 71 | | var now = DateTime.UtcNow; |
| | | 72 | | const int budget = PendingRegistration.MaxConfirmationGuesses; |
| | | 73 | | |
| | | 74 | | // Newest first with the internal id as tiebreaker, ordered on the entity because the id never |
| | | 75 | | // reaches the output, then projected and paginated over that ordering. |
| | 13 | 76 | | var projected = pendingReader.Query() |
| | 13 | 77 | | .Where(x => x.ScopeId == scope.Id) |
| | 13 | 78 | | .OrderByDescending(x => x.CreatedAt) |
| | 13 | 79 | | .ThenByDescending(x => x.Id) |
| | 13 | 80 | | .Select(x => new PendingRegistrationOutput |
| | 13 | 81 | | { |
| | 13 | 82 | | Id = x.PublicId, |
| | 13 | 83 | | Name = x.Name, |
| | 13 | 84 | | Email = x.Email, |
| | 13 | 85 | | CreatedAt = x.CreatedAt, |
| | 13 | 86 | | ExpiresAt = x.ExpiresAt, |
| | 13 | 87 | | // Parallel wrong guesses can push the count past the budget before the row is |
| | 13 | 88 | | // discarded (see PendingRegistrationConfirmer), so the remainder is floored at zero. |
| | 13 | 89 | | AttemptsLeft = x.FailedAttempts >= budget ? 0 : budget - x.FailedAttempts, |
| | 13 | 90 | | Expired = x.ExpiresAt <= now |
| | 13 | 91 | | }); |
| | | 92 | | |
| | 13 | 93 | | var page = await projected.PaginateAsync(query.PageNumber, query.PageSize, orderBy: null); |
| | | 94 | | |
| | 13 | 95 | | return page.WithMessage(ScopeMessages.PendingRegistrationsRetrieved); |
| | 20 | 96 | | } |
| | | 97 | | } |