fix(notification): enable web smtp test with saved password
Allow Web SMTP egress on TCP 587 and reuse the saved channel password only when connection settings match. Ref: IT-1601
This commit is contained in:
@@ -138,8 +138,21 @@ public class AdminChannelsController(IMediator mediator) : Controller
|
||||
[HttpPost("test-smtp")]
|
||||
public async Task<IActionResult> TestSmtp([FromBody] TestSmtpRequest request, CancellationToken ct)
|
||||
{
|
||||
var password = request.Password;
|
||||
if (string.IsNullOrEmpty(password) && request.ChannelId is Guid channelId)
|
||||
{
|
||||
var channelResult = await mediator.Send(new GetEmailChannelQuery(channelId), ct);
|
||||
if (!channelResult.IsSuccess || channelResult.Result?.Settings is not SmtpChannelSettings saved)
|
||||
return Ok(new { success = false, message = "SMTP channel not found." });
|
||||
|
||||
if (!MatchesSavedSmtpConnection(request, saved))
|
||||
return Ok(new { success = false, message = "Enter the password to test changed SMTP connection settings." });
|
||||
|
||||
password = saved.Password;
|
||||
}
|
||||
|
||||
var result = await mediator.Send(new TestSmtpCommand(
|
||||
request.Host, request.Port, request.Username, request.Password,
|
||||
request.Host, request.Port, request.Username, password ?? string.Empty,
|
||||
request.UseSsl, request.FromEmail, request.FromName, request.ToEmail), ct);
|
||||
|
||||
if (!result.IsSuccess)
|
||||
@@ -148,6 +161,12 @@ public class AdminChannelsController(IMediator mediator) : Controller
|
||||
return Ok(new { success = true, message = $"Test email sent to {request.ToEmail}." });
|
||||
}
|
||||
|
||||
private static bool MatchesSavedSmtpConnection(TestSmtpRequest request, SmtpChannelSettings saved) =>
|
||||
string.Equals(request.Host, saved.Host, StringComparison.OrdinalIgnoreCase)
|
||||
&& request.Port == saved.Port
|
||||
&& string.Equals(request.Username, saved.Username, StringComparison.Ordinal)
|
||||
&& request.UseSsl == saved.UseSsl;
|
||||
|
||||
// POST /admin/channels/{id}/test
|
||||
[HttpPost("{id:guid}/test")]
|
||||
public async Task<IActionResult> Test(Guid id, [FromBody] TestChannelRequest request, CancellationToken ct)
|
||||
@@ -180,6 +199,3 @@ public class AdminChannelsController(IMediator mediator) : Controller
|
||||
}
|
||||
|
||||
public record TestChannelRequest(string ToEmail);
|
||||
public record TestSmtpRequest(
|
||||
string Host, int Port, string Username, string Password,
|
||||
bool UseSsl, string FromEmail, string FromName, string ToEmail);
|
||||
|
||||
Reference in New Issue
Block a user