Files
raven/server/AyaNova/Controllers/GlobalOpsNotificationSettingsController.cs
John Cardinal 4141217c04 case 4724 - SMTP certificate checked, password never returned
Mailer checks the SMTP server's certificate (trust and name; no revocation
check). New config setting AYANOVA_SMTP_ACCEPT_ANY_CERTIFICATE accepts any
certificate (boot warning, shown in server info, manual page).
The notification settings GET returns ******** instead of the SMTP
password; a PUT with ******** or empty keeps the stored password.
F30 and F31 fixed. Changelog and manual updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-24 19:09:02 -07:00

141 lines
6.1 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Authorization;
using Microsoft.EntityFrameworkCore;
using AyaNova.Models;
using AyaNova.Api.ControllerHelpers;
using AyaNova.Biz;
using System.Threading.Tasks;
namespace AyaNova.Api.Controllers
{
[ApiController]
[Asp.Versioning.ApiVersion("8.0")]
[Route("api/v{version:apiVersion}/global-ops-notification-setting")]
[Produces("application/json")]
[Authorize]
public class GlobalOpsNotificationSettingsController : ControllerBase
{
private readonly AyContext ct;
private readonly ILogger<GlobalOpsNotificationSettingsController> log;
private readonly ApiServerState serverState;
/// <summary>
/// ctor
/// </summary>
/// <param name="dbcontext"></param>
/// <param name="logger"></param>
/// <param name="apiServerState"></param>
public GlobalOpsNotificationSettingsController(AyContext dbcontext, ILogger<GlobalOpsNotificationSettingsController> logger, ApiServerState apiServerState)
{
ct = dbcontext;
log = logger;
serverState = apiServerState;
}
/// <summary>
/// Get GlobalOpsNotificationSettings
/// </summary>
/// <returns>Global ops Notification settings object</returns>
[HttpGet]
public async Task<IActionResult> GetGlobalOpsNotificationSettings()
{
if (serverState.IsClosed)
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
//Instantiate the business object handler
GlobalOpsNotificationSettingsBiz biz = GlobalOpsNotificationSettingsBiz.GetBiz(ct, HttpContext);
if (!Authorized.HasReadFullRole(HttpContext.Items, biz.BizType))
return StatusCode(403, new ApiNotAuthorizedResponse());
if (!ModelState.IsValid)
return BadRequest(new ApiErrorResponse(ModelState));
var o = await biz.GetAsync();
if (o == null)
return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
//case 4724: the SMTP password never goes to the client (detached first so the mask can't be saved)
ct.Entry(o).State = EntityState.Detached;
o.SmtpPassword = GlobalOpsNotificationSettingsBiz.SMTP_PASSWORD_MASK;
return Ok(ApiOkResponse.Response(o));
}
/// <summary>
/// PUT Global ops Notification settings
/// </summary>
/// <param name="updatedObject"></param>
/// <returns>New concurrency token</returns>
[HttpPut]
public async Task<IActionResult> ReplaceGlobalOpsNotificationSettings([FromBody] GlobalOpsNotificationSettings updatedObject)
{
if (serverState.IsClosed)
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
if (!ModelState.IsValid)
return BadRequest(new ApiErrorResponse(ModelState));
GlobalOpsNotificationSettingsBiz biz = GlobalOpsNotificationSettingsBiz.GetBiz(ct, HttpContext);
if (!Authorized.HasModifyRole(HttpContext.Items, biz.BizType))
return StatusCode(403, new ApiNotAuthorizedResponse());
var o = await biz.PutAsync(updatedObject);
if (o == null)
return StatusCode(409, new ApiErrorResponse(biz.Errors));
return Ok(ApiOkResponse.Response(new { Concurrency = o.Concurrency }));
}
/// <summary>
/// Test SMTP mail delivery
///
/// </summary>
/// <returns>Status result</returns>
[HttpPost("test-smtp-settings/{toAddress}")]
[Authorize]
public async Task<IActionResult> PostTestSmtpDelivery([FromRoute] string toAddress)
{
if (serverState.IsClosed)
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
if (!Authorized.HasModifyRole(HttpContext.Items, AyaType.OpsNotificationSettings))//technically maybe this could be wider open, but for now keeping as locked down
return StatusCode(403, new ApiNotAuthorizedResponse());
var res = await CoreJobNotify.TestSMTPDelivery(toAddress);
if(res!="ok"){
return StatusCode(500, new ApiErrorResponse(ApiErrorCode.API_SERVER_ERROR, null, res));
}
return Ok(ApiOkResponse.Response(res));
}
// /// <summary>
// /// Get Client app relevant GlobalOpsNotificationSettings
// /// </summary>
// /// <returns>Global ops Notification settings object</returns>
// [HttpGet("client")]
// public async Task<ActionResult> GetClientGlobalOpsNotificationSettings()
// {
// //NOTE: currently this looks like a dupe of get above and it is
// //but it's kept here in case want to return a subset of the settings only to client users
// //where some internal server only stuff might not be desired to send to user
// if (serverState.IsClosed)
// return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
// GlobalOpsNotificationSettingsBiz biz = GlobalOpsNotificationSettingsBiz.GetBiz(ct, HttpContext);
// //this route is available to Ops role user only
// if (!Authorized.HasReadFullRole(HttpContext.Items, biz.BizType))
// return StatusCode(403, new ApiNotAuthorizedResponse());
// if (!ModelState.IsValid)
// return BadRequest(new ApiErrorResponse(ModelState));
// var o = await biz.GetAsync();
// if (o == null)
// return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
// return Ok(ApiOkResponse.Response(o));
// }
}//eoc
}//ens