2025-02-27 01:59:17 -05:00
|
|
|
#nullable enable
|
2025-02-27 16:50:32 +09:00
|
|
|
using System;
|
2025-02-27 01:59:17 -05:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
using BTCPayServer.Abstractions.Constants;
|
|
|
|
using BTCPayServer.Abstractions.Extensions;
|
|
|
|
using BTCPayServer.Client;
|
|
|
|
using BTCPayServer.Client.Models;
|
|
|
|
using BTCPayServer.Models.ServerViewModels;
|
|
|
|
using BTCPayServer.Services;
|
|
|
|
using BTCPayServer.Services.Mails;
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
using Microsoft.AspNetCore.Cors;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2025-02-27 16:50:32 +09:00
|
|
|
using Newtonsoft.Json.Linq;
|
2025-02-27 01:59:17 -05:00
|
|
|
|
|
|
|
namespace BTCPayServer.Controllers.GreenField
|
|
|
|
{
|
|
|
|
[ApiController]
|
|
|
|
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
|
|
|
[EnableCors(CorsPolicies.All)]
|
|
|
|
public class GreenfieldServerEmailController : Controller
|
|
|
|
{
|
|
|
|
private readonly EmailSenderFactory _emailSenderFactory;
|
|
|
|
private readonly PoliciesSettings _policiesSettings;
|
|
|
|
readonly SettingsRepository _settingsRepository;
|
|
|
|
|
|
|
|
public GreenfieldServerEmailController(EmailSenderFactory emailSenderFactory, PoliciesSettings policiesSettings, SettingsRepository settingsRepository)
|
|
|
|
{
|
|
|
|
_emailSenderFactory = emailSenderFactory;
|
|
|
|
_policiesSettings = policiesSettings;
|
|
|
|
_settingsRepository = settingsRepository;
|
|
|
|
}
|
2025-03-05 21:22:19 -06:00
|
|
|
|
|
|
|
private ServerEmailSettingsData ToApiModel(EmailSettings email)
|
|
|
|
{
|
|
|
|
var data = email.ToData<ServerEmailSettingsData>();
|
|
|
|
data.EnableStoresToUseServerEmailSettings = !_policiesSettings.DisableStoresToUseServerEmailSettings;
|
|
|
|
return data;
|
|
|
|
}
|
2025-02-27 01:59:17 -05:00
|
|
|
|
|
|
|
[Authorize(Policy = Policies.CanModifyServerSettings, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
|
|
|
[HttpGet("~/api/v1/server/email")]
|
|
|
|
public async Task<IActionResult> ServerEmailSettings()
|
|
|
|
{
|
|
|
|
var email = await _emailSenderFactory.GetSettings() ?? new EmailSettings();
|
2025-03-05 21:22:19 -06:00
|
|
|
return Ok(ToApiModel(email));
|
2025-02-27 01:59:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
[Authorize(Policy = Policies.CanModifyServerSettings, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
|
|
|
[HttpPut("~/api/v1/server/email")]
|
|
|
|
public async Task<IActionResult> ServerEmailSettings(ServerEmailSettingsData request)
|
|
|
|
{
|
2025-03-05 21:22:19 -06:00
|
|
|
if (!MailboxAddressValidator.IsMailboxAddress(request.From))
|
|
|
|
{
|
|
|
|
ModelState.AddModelError(nameof(request.From), "Invalid email address");
|
2025-02-27 16:50:32 +09:00
|
|
|
return this.CreateValidationError(ModelState);
|
2025-03-05 21:22:19 -06:00
|
|
|
}
|
2025-02-27 16:50:32 +09:00
|
|
|
|
2025-02-27 01:59:17 -05:00
|
|
|
if (_policiesSettings.DisableStoresToUseServerEmailSettings == request.EnableStoresToUseServerEmailSettings)
|
|
|
|
{
|
|
|
|
_policiesSettings.DisableStoresToUseServerEmailSettings = !request.EnableStoresToUseServerEmailSettings;
|
|
|
|
await _settingsRepository.UpdateSetting(_policiesSettings);
|
|
|
|
}
|
2025-02-27 16:50:32 +09:00
|
|
|
|
|
|
|
var settings = await _emailSenderFactory.GetSettings();
|
|
|
|
settings = EmailSettings.FromData(request, settings?.Password);
|
|
|
|
|
2025-02-27 01:59:17 -05:00
|
|
|
// important to save as EmailSettings otherwise it won't be able to be fetched
|
2025-02-27 16:50:32 +09:00
|
|
|
await _settingsRepository.UpdateSetting(settings);
|
2025-03-05 21:22:19 -06:00
|
|
|
return Ok(ToApiModel(settings));
|
2025-02-27 01:59:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|