Renamed receiver roles FullyAuth → Receiver.Full and PreAuth → Receiver.TFA across the codebase for improved clarity and consistency. Updated all usages, [Authorize] attributes, role checks, authentication logic, and authorization policies to use the new role names. Marked old constants as obsolete and pointed them to the new values. This change enhances code readability and groups receiver roles under the Receiver static class.
100 lines
3.9 KiB
C#
100 lines
3.9 KiB
C#
using DigitalData.Core.Abstraction.Application.DTO;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Newtonsoft.Json;
|
|
using EnvelopeGenerator.Domain.Constants;
|
|
using EnvelopeGenerator.Web.Extensions;
|
|
using EnvelopeGenerator.Application.Common.Dto.EnvelopeReceiverReadOnly;
|
|
using EnvelopeGenerator.Application.Common.Interfaces.Services;
|
|
|
|
namespace EnvelopeGenerator.Web.Controllers
|
|
{
|
|
[Route("api/[controller]")]
|
|
[ApiController]
|
|
public class ReadOnlyController : ControllerBase
|
|
{
|
|
private readonly ILogger<ReadOnlyController> _logger;
|
|
|
|
[Obsolete("Use MediatR")]
|
|
private readonly IEnvelopeReceiverReadOnlyService _erroService;
|
|
|
|
[Obsolete("Use MediatR")]
|
|
private readonly IEnvelopeMailService _mailService;
|
|
|
|
[Obsolete("Use MediatR")]
|
|
private readonly IEnvelopeHistoryService _histService;
|
|
|
|
[Obsolete("Use MediatR")]
|
|
public ReadOnlyController(ILogger<ReadOnlyController> logger, IEnvelopeReceiverReadOnlyService erroService, IEnvelopeMailService mailService, IEnvelopeHistoryService histService)
|
|
{
|
|
_logger = logger;
|
|
_erroService = erroService;
|
|
_mailService = mailService;
|
|
_histService = histService;
|
|
}
|
|
|
|
[HttpPost]
|
|
[Authorize(Roles = Role.ReceiverFull)]
|
|
[Obsolete("Use MediatR")]
|
|
public async Task<IActionResult> CreateAsync([FromBody] EnvelopeReceiverReadOnlyCreateDto createDto)
|
|
{
|
|
//set AddedWho
|
|
var authReceiverMail = User.GetAuthReceiverMail();
|
|
if (authReceiverMail is null)
|
|
{
|
|
_logger.LogError("EmailAddress clam is not found in envelope-receiver-read-only creation process. Create DTO is:\n {dto}", JsonConvert.SerializeObject(createDto));
|
|
return Unauthorized();
|
|
}
|
|
|
|
var envelopeId = User.GetAuthEnvelopeId();
|
|
if (envelopeId is null)
|
|
{
|
|
_logger.LogError("Envelope Id clam is not found in envelope-receiver-read-only creation process. Create DTO is:\n {dto}", JsonConvert.SerializeObject(createDto));
|
|
return Unauthorized();
|
|
}
|
|
|
|
createDto.AddedWho = authReceiverMail;
|
|
createDto.EnvelopeId = envelopeId;
|
|
|
|
// create entity
|
|
var creation_res = await _erroService.CreateAsync(createDto: createDto);
|
|
|
|
if (creation_res.IsFailed)
|
|
{
|
|
_logger.LogNotice(creation_res);
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
|
|
//read new entity
|
|
var read_res = await _erroService.ReadByIdAsync(creation_res.Data.Id);
|
|
if (read_res.IsFailed)
|
|
{
|
|
_logger.LogNotice(creation_res);
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
|
|
var new_erro = read_res.Data;
|
|
|
|
//send email two receiver
|
|
return await _mailService.SendAsync(new_erro).ThenAsync<int, IActionResult>(SuccessAsync: async res =>
|
|
{
|
|
//TODO: implement multi-threading to history process (Task)
|
|
//TODO: remove casting after change the id type
|
|
var hist_res = await _histService.RecordAsync((int)createDto.EnvelopeId, createDto.AddedWho, EnvelopeStatus.EnvelopeShared);
|
|
if (hist_res.IsFailed)
|
|
{
|
|
_logger.LogError("Although the envelope was sent as read-only, the EnvelopeShared hisotry could not be saved. Create DTO:\n{createDto}", JsonConvert.SerializeObject(createDto));
|
|
_logger.LogNotice(hist_res.Notices);
|
|
}
|
|
|
|
return Ok();
|
|
},
|
|
|
|
Fail: (msg, ntc) =>
|
|
{
|
|
_logger.LogNotice(ntc);
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
});
|
|
}
|
|
}
|
|
} |