btcpayserver/BTCPayServer/Controllers/GreenField/GreenfieldStoreEmailController.cs

95 lines
3.8 KiB
C#
Raw Normal View History

#nullable enable
using System.Threading.Tasks;
using BTCPayServer.Abstractions.Constants;
using BTCPayServer.Abstractions.Extensions;
using BTCPayServer.Client;
using BTCPayServer.Client.Models;
2022-03-11 10:17:50 +01:00
using BTCPayServer.Data;
using BTCPayServer.Services.Mails;
2022-03-11 10:17:50 +01:00
using BTCPayServer.Services.Stores;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
namespace BTCPayServer.Controllers.GreenField
{
[ApiController]
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
[EnableCors(CorsPolicies.All)]
public class GreenfieldStoreEmailController : Controller
{
private readonly EmailSenderFactory _emailSenderFactory;
2022-03-11 10:17:50 +01:00
private readonly StoreRepository _storeRepository;
public GreenfieldStoreEmailController(EmailSenderFactory emailSenderFactory, StoreRepository storeRepository)
{
_emailSenderFactory = emailSenderFactory;
2022-03-11 10:17:50 +01:00
_storeRepository = storeRepository;
}
[Authorize(Policy = Policies.CanModifyStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
[HttpPost("~/api/v1/stores/{storeId}/email/send")]
public async Task<IActionResult> SendEmailFromStore(string storeId,
[FromBody] SendEmailRequest request)
{
var store = HttpContext.GetStoreData();
if (store == null)
{
return this.CreateAPIError(404, "store-not-found", "The store was not found");
}
if (!MailboxAddressValidator.TryParse(request.Email, out var to))
{
ModelState.AddModelError(nameof(request.Email), "Invalid email");
return this.CreateValidationError(ModelState);
}
var emailSender = await _emailSenderFactory.GetEmailSender(storeId);
if (emailSender is null)
{
return this.CreateAPIError(404, "smtp-not-configured", "Store does not have an SMTP server configured.");
}
emailSender.SendEmail(to, request.Subject, request.Body);
return Ok();
}
2025-03-05 21:22:19 -06:00
private EmailSettingsData ToApiModel(Data.StoreData data)
{
var storeEmailSettings = data.GetStoreBlob().EmailSettings ?? new();
return storeEmailSettings.ToData<EmailSettingsData>();
}
2022-03-11 10:17:50 +01:00
[Authorize(Policy = Policies.CanModifyStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
[HttpGet("~/api/v1/stores/{storeId}/email")]
public IActionResult GetStoreEmailSettings()
{
var store = HttpContext.GetStoreData();
2025-03-05 21:22:19 -06:00
return store == null ? StoreNotFound() : Ok(ToApiModel(store));
2022-03-11 10:17:50 +01:00
}
[Authorize(Policy = Policies.CanModifyStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
[HttpPut("~/api/v1/stores/{storeId}/email")]
2025-02-27 16:50:32 +09:00
public async Task<IActionResult> UpdateStoreEmailSettings(string storeId, EmailSettingsData request)
2022-03-11 10:17:50 +01:00
{
if (!string.IsNullOrWhiteSpace(request.From) && !MailboxAddressValidator.IsMailboxAddress(request.From))
2025-03-05 21:22:19 -06:00
ModelState.AddModelError(nameof(request.From), "Invalid email address");
if (!ModelState.IsValid)
2025-03-05 21:22:19 -06:00
return this.CreateValidationError(ModelState);
2025-03-05 21:22:19 -06:00
var store = HttpContext.GetStoreData();
2022-03-11 10:17:50 +01:00
var blob = store.GetStoreBlob();
2025-02-27 16:50:32 +09:00
var settings = EmailSettings.FromData(request, blob.EmailSettings?.Password);
blob.EmailSettings = settings;
2022-03-11 10:17:50 +01:00
if (store.SetStoreBlob(blob))
await _storeRepository.UpdateStore(store);
2025-03-05 21:22:19 -06:00
return Ok(ToApiModel(store));
2022-03-11 10:17:50 +01:00
}
2025-02-27 16:50:32 +09:00
2022-03-11 10:17:50 +01:00
private IActionResult StoreNotFound()
{
return this.CreateAPIError(404, "store-not-found", "The store was not found");
}
}
}