| | | 1 | | using ArturRios.Data.Relational.Core.Interfaces; |
| | | 2 | | using ArturRios.Heimdall.Command.Input; |
| | | 3 | | using ArturRios.Heimdall.Command.Output; |
| | | 4 | | using ArturRios.Heimdall.Domain.Entities; |
| | | 5 | | using ArturRios.Heimdall.Domain.Persistence; |
| | | 6 | | using ArturRios.Heimdall.Shared.Messages; |
| | | 7 | | using ArturRios.Heimdall.Shared.Services; |
| | | 8 | | using ArturRios.Mediator.Command.Interfaces; |
| | | 9 | | using ArturRios.Output; |
| | | 10 | | using FluentValidation; |
| | | 11 | | using Microsoft.EntityFrameworkCore; |
| | | 12 | | |
| | | 13 | | namespace ArturRios.Heimdall.Command.Handlers; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Handles <see cref="SetScopeEmailLinksCommand" />: the same order of checks as |
| | | 17 | | /// <see cref="SetSelfRegistrationCommandHandler" /> — the links are usable, the scope is live, a |
| | | 18 | | /// Scope Admin actor owns it — then writes <c>EmailVerificationUrl</c> and <c>PasswordResetUrl</c> |
| | | 19 | | /// and returns the scope. Nothing else on the scope is touched. |
| | | 20 | | /// </summary> |
| | 17 | 21 | | public class SetScopeEmailLinksCommandHandler( |
| | 17 | 22 | | IValidator<SetScopeEmailLinksCommand> validator, |
| | 17 | 23 | | IAsyncReadOnlyRepository<Scope, long> scopeReader, |
| | 17 | 24 | | IAtomicWrites atomicWrites, |
| | 17 | 25 | | IScopeOwnershipChecker scopeOwnership) |
| | | 26 | | : ICommandHandlerAsync<SetScopeEmailLinksCommand, SetScopeEmailLinksCommandOutput> |
| | | 27 | | { |
| | | 28 | | public async Task<DataOutput<SetScopeEmailLinksCommandOutput?>> HandleAsync( |
| | | 29 | | SetScopeEmailLinksCommand command, CancellationToken cancellationToken = default) |
| | | 30 | | { |
| | 17 | 31 | | var output = DataOutput<SetScopeEmailLinksCommandOutput?>.New; |
| | | 32 | | |
| | 17 | 33 | | var validation = await validator.ValidateAsync(command, cancellationToken); |
| | | 34 | | |
| | 17 | 35 | | if (!validation.IsValid) |
| | | 36 | | { |
| | 4 | 37 | | return output.WithErrors(validation.Errors.Select(failure => failure.ErrorMessage)); |
| | | 38 | | } |
| | | 39 | | |
| | 15 | 40 | | var scope = await scopeReader.Query() |
| | 15 | 41 | | .FirstOrDefaultAsync(x => x.PublicId == command.Id && !x.IsDeleted, cancellationToken); |
| | | 42 | | |
| | 15 | 43 | | if (scope is null) |
| | | 44 | | { |
| | 3 | 45 | | return output.WithError(ScopeMessages.ScopeNotFound); |
| | | 46 | | } |
| | | 47 | | |
| | 12 | 48 | | if (!await scopeOwnership.ActorMayManageScopeAsync(command.ActingRole, command.ActingPersonId, scope.Id)) |
| | | 49 | | { |
| | 1 | 50 | | return output.WithError(ScopeMessages.NotScopeOwner); |
| | | 51 | | } |
| | | 52 | | |
| | 11 | 53 | | var ownerIds = await scopeReader.Query() |
| | 11 | 54 | | .Where(x => x.PublicId == command.Id) |
| | 11 | 55 | | .SelectMany(x => x.Owners.Select(owner => owner.Person.PublicId)) |
| | 11 | 56 | | .ToListAsync(cancellationToken); |
| | | 57 | | |
| | | 58 | | // The two links alone, and only while the scope is not deleted (#205), as UC-24 writes its flag: |
| | | 59 | | // one deleted since the read above is answered as one deleted before it. |
| | 11 | 60 | | if (!await atomicWrites.TrySetEmailLinksAsync(scope, Normalised(command.EmailVerificationUrl), |
| | 11 | 61 | | Normalised(command.PasswordResetUrl), DateTime.UtcNow)) |
| | | 62 | | { |
| | 1 | 63 | | return output.WithError(ScopeMessages.ScopeNotFound); |
| | | 64 | | } |
| | | 65 | | |
| | 10 | 66 | | return output |
| | 10 | 67 | | .WithData(new SetScopeEmailLinksCommandOutput |
| | 10 | 68 | | { |
| | 10 | 69 | | Id = scope.PublicId, |
| | 10 | 70 | | Name = scope.Name, |
| | 10 | 71 | | Description = scope.Description, |
| | 10 | 72 | | GoogleSignInEnabled = scope.GoogleSignInEnabled, |
| | 10 | 73 | | SelfRegistrationEnabled = scope.SelfRegistrationEnabled, |
| | 10 | 74 | | EmailVerificationUrl = scope.EmailVerificationUrl, |
| | 10 | 75 | | PasswordResetUrl = scope.PasswordResetUrl, |
| | 10 | 76 | | OwnerIds = ownerIds, |
| | 10 | 77 | | CreatedAt = scope.CreatedAt, |
| | 10 | 78 | | UpdatedAt = scope.UpdatedAt |
| | 10 | 79 | | }) |
| | 10 | 80 | | .WithMessage(ScopeMessages.EmailLinksUpdatedSuccessfully); |
| | 17 | 81 | | } |
| | | 82 | | |
| | | 83 | | // An empty or blank link is stored as "no link", so the senders have one way to read it. |
| | 22 | 84 | | private static string? Normalised(string? url) => string.IsNullOrWhiteSpace(url) ? null : url.Trim(); |
| | | 85 | | } |