Files
EnvelopeGenerator/EnvelopeGenerator.API/Controllers/DocumentController.cs
TekH 5465996563 Refactor document retrieval endpoints and authorization
- Updated DocumentController to use class-level [Authorize] and method-level role-based authorization for sender and receiver endpoints.
- Replaced ReadEnvelopeReceiverQuery with ReadDocumentQuery for sender document retrieval; simplified response logic.
- Added a new endpoint for fully authenticated receivers to fetch documents by envelope ID from user claims.
- Refactored ReadDocumentQuery and handler to always return DocumentDto, throw NotFoundException when needed, and use _repo.Query.
- Cleaned up using directives and removed legacy error handling and logging.
2026-02-03 09:48:33 +01:00

51 lines
1.8 KiB
C#

using EnvelopeGenerator.API.Extensions;
using EnvelopeGenerator.Application.Documents.Queries;
using EnvelopeGenerator.Domain.Constants;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace EnvelopeGenerator.API.Controllers;
/// <summary>
/// Provides access to envelope documents for authenticated receivers.
/// </summary>
/// <remarks>
/// Initializes a new instance of the <see cref="DocumentController"/> class.
/// </remarks>
[Authorize]
[ApiController]
[Route("api/[controller]")]
public class DocumentController(IMediator mediator, ILogger<DocumentController> logger) : ControllerBase
{
/// <summary>
/// Returns the document bytes for the specified envelope receiver key.
/// </summary>
/// <param name="query">Encoded envelope key.</param>
/// <param name="cancel">Cancellation token.</param>
[HttpGet]
[Authorize(Roles = Role.Sender)]
public async Task<IActionResult> GetDocument(ReadDocumentQuery query, CancellationToken cancel)
{
var doc = await mediator.Send(query, cancel);
return doc.ByteData is byte[] docByte
? File(docByte, "application/octet-stream")
: NotFound("Document is empty.");
}
/// <summary>
/// Returns the document bytes for the receiver.
/// </summary>
/// <param name="cancel">Cancellation token.</param>
[HttpGet]
[Authorize(Roles = Role.Receiver.FullyAuth)]
public async Task<IActionResult> GetDocument(CancellationToken cancel)
{
var envelopeId = User.GetEnvelopeIdOfReceiver();
var doc = await mediator.Send(new ReadDocumentQuery() { EnvelopeId = envelopeId }, cancel);
return doc.ByteData is byte[] docByte
? File(docByte, "application/octet-stream")
: NotFound("Document is empty.");
}
}