273 lines
12 KiB
Plaintext
273 lines
12 KiB
Plaintext
@using ZB.MOM.WW.ScadaBridge.CentralUI.Services
|
|
@using ZB.MOM.WW.ScadaBridge.Commons.Messages.Management
|
|
@inject IServiceProvider MutationServices
|
|
@page "/notifications/sms"
|
|
@using ZB.MOM.WW.ScadaBridge.Security
|
|
@using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories
|
|
@using SmsConfigurationEntity = ZB.MOM.WW.ScadaBridge.Commons.Entities.Notifications.SmsConfiguration
|
|
@attribute [Authorize(Policy = AuthorizationPolicies.RequireAdmin)]
|
|
@inject INotificationRepository NotificationRepository
|
|
@inject NavigationManager NavigationManager
|
|
|
|
<div class="container-fluid mt-3">
|
|
<div class="d-flex justify-content-between align-items-center mb-3">
|
|
<h4 class="mb-0">SMS Configuration</h4>
|
|
</div>
|
|
|
|
<ToastNotification @ref="_toast" />
|
|
|
|
@if (_loading)
|
|
{
|
|
<LoadingSpinner IsLoading="true" />
|
|
}
|
|
else if (_errorMessage != null)
|
|
{
|
|
<div class="alert alert-danger">@_errorMessage</div>
|
|
}
|
|
else
|
|
{
|
|
@if (_smsConfigs.Count == 0 && !_showForm)
|
|
{
|
|
<div class="text-center py-5 text-muted">
|
|
<p class="mb-3">No SMS configuration set.</p>
|
|
<button class="btn btn-primary btn-sm" @onclick="ShowAddForm">
|
|
Add SMS configuration
|
|
</button>
|
|
</div>
|
|
}
|
|
else
|
|
{
|
|
@foreach (var sms in _smsConfigs)
|
|
{
|
|
<div class="card mb-3" @key="sms.Id">
|
|
<div class="card-header d-flex justify-content-between align-items-center">
|
|
<strong>@sms.AccountSid</strong>
|
|
@if (_editingSms?.Id != sms.Id || !_showForm)
|
|
{
|
|
<button class="btn btn-outline-primary btn-sm" @onclick="() => StartEdit(sms)">Edit</button>
|
|
}
|
|
</div>
|
|
<div class="card-body small">
|
|
<div class="row g-2">
|
|
<div class="col-md-4 text-muted">Account SID</div>
|
|
<div class="col-md-8">@sms.AccountSid</div>
|
|
<div class="col-md-4 text-muted">From Number</div>
|
|
<div class="col-md-8">@sms.FromNumber</div>
|
|
<div class="col-md-4 text-muted">Messaging Service SID</div>
|
|
<div class="col-md-8">@(string.IsNullOrWhiteSpace(sms.MessagingServiceSid) ? "(not set)" : sms.MessagingServiceSid)</div>
|
|
<div class="col-md-4 text-muted">API Base URL</div>
|
|
@* An override base URL is an operator-supplied URL with no
|
|
break opportunities — clip it so it cannot widen the card. *@
|
|
<div class="col-md-8">
|
|
<span class="cell-clip cell-clip-lg" title="@sms.ApiBaseUrl">@(string.IsNullOrWhiteSpace(sms.ApiBaseUrl) ? "(provider default)" : sms.ApiBaseUrl)</span>
|
|
</div>
|
|
<div class="col-md-4 text-muted">Auth Token</div>
|
|
<div class="col-md-8">@(string.IsNullOrWhiteSpace(sms.AuthToken) ? "(not set)" : "(stored)")</div>
|
|
<div class="col-md-4 text-muted">Connection Timeout</div>
|
|
<div class="col-md-8">@sms.ConnectionTimeoutSeconds s</div>
|
|
<div class="col-md-4 text-muted">Max Retries</div>
|
|
<div class="col-md-8">@sms.MaxRetries</div>
|
|
<div class="col-md-4 text-muted">Retry Delay</div>
|
|
<div class="col-md-8">@((int)sms.RetryDelay.TotalSeconds) s</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
}
|
|
|
|
@if (_showForm)
|
|
{
|
|
<div class="card mb-3">
|
|
<div class="card-header">@(_editingSms != null ? "Edit SMS Configuration" : "Add SMS Configuration")</div>
|
|
<div class="card-body">
|
|
<div class="row g-3">
|
|
<div class="col-12">
|
|
<label class="form-label">Account SID</label>
|
|
<input type="text" class="form-control" @bind="_accountSid" placeholder="ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" />
|
|
</div>
|
|
<div class="col-12">
|
|
<label class="form-label">From Number</label>
|
|
<input type="text" class="form-control" @bind="_fromNumber" placeholder="+15551234567" />
|
|
<div class="form-text">Provide a From Number or a Messaging Service SID (at least one is required).</div>
|
|
</div>
|
|
<div class="col-12">
|
|
<label class="form-label">Messaging Service SID</label>
|
|
<input type="text" class="form-control" @bind="_messagingServiceSid"
|
|
placeholder="MGxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (optional)" />
|
|
<div class="form-text">Optional — used instead of the From number when set.</div>
|
|
</div>
|
|
<div class="col-12">
|
|
<label class="form-label">API Base URL</label>
|
|
<input type="text" class="form-control" @bind="_apiBaseUrl"
|
|
placeholder="Leave blank to use the provider default" />
|
|
</div>
|
|
<div class="col-12">
|
|
<label class="form-label">Auth Token</label>
|
|
<input type="password" class="form-control" @bind="_authToken"
|
|
placeholder="@(_editingSms != null ? "Leave blank to keep the stored token" : "Twilio Auth Token")" />
|
|
<div class="form-text">
|
|
Treat as sensitive — visible to admins only.
|
|
@if (_editingSms != null)
|
|
{
|
|
<span>Leave blank to keep the existing token.</span>
|
|
}
|
|
</div>
|
|
</div>
|
|
<div class="col-md-4">
|
|
<label class="form-label">Connection Timeout (s)</label>
|
|
<input type="number" class="form-control" @bind="_connectionTimeoutSeconds" min="1" />
|
|
</div>
|
|
<div class="col-md-4">
|
|
<label class="form-label">Max Retries</label>
|
|
<input type="number" class="form-control" @bind="_maxRetries" min="0" />
|
|
</div>
|
|
<div class="col-md-4">
|
|
<label class="form-label">Retry Delay (s)</label>
|
|
<input type="number" class="form-control" @bind="_retryDelaySeconds" min="0" />
|
|
</div>
|
|
@if (_formError != null)
|
|
{
|
|
<div class="col-12"><div class="text-danger small">@_formError</div></div>
|
|
}
|
|
<div class="col-12 text-end">
|
|
<div class="btn-group" role="group">
|
|
<button class="btn btn-outline-secondary" @onclick="CancelForm">Cancel</button>
|
|
<button class="btn btn-success" @onclick="Save">Save</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
}
|
|
else if (_smsConfigs.Count == 0)
|
|
{
|
|
<button class="btn btn-primary btn-sm" @onclick="ShowAddForm">Add SMS configuration</button>
|
|
}
|
|
}
|
|
}
|
|
</div>
|
|
|
|
@code {
|
|
private bool _loading = true;
|
|
private string? _errorMessage;
|
|
|
|
private List<SmsConfigurationEntity> _smsConfigs = new();
|
|
private bool _showForm;
|
|
private SmsConfigurationEntity? _editingSms;
|
|
|
|
private string _accountSid = string.Empty;
|
|
private string _fromNumber = string.Empty;
|
|
private string? _messagingServiceSid;
|
|
private string? _apiBaseUrl;
|
|
private string? _authToken;
|
|
private int _connectionTimeoutSeconds = 30;
|
|
private int _maxRetries = 10;
|
|
private int _retryDelaySeconds = 60;
|
|
private string? _formError;
|
|
|
|
private ToastNotification _toast = default!;
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
await LoadAsync();
|
|
}
|
|
|
|
private async Task LoadAsync()
|
|
{
|
|
_loading = true;
|
|
_errorMessage = null;
|
|
try
|
|
{
|
|
_smsConfigs = (await NotificationRepository.GetAllSmsConfigurationsAsync()).ToList();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_errorMessage = ex.Message;
|
|
}
|
|
_loading = false;
|
|
}
|
|
|
|
private void ShowAddForm()
|
|
{
|
|
_editingSms = null;
|
|
_accountSid = string.Empty;
|
|
_fromNumber = string.Empty;
|
|
_messagingServiceSid = null;
|
|
_apiBaseUrl = null;
|
|
_authToken = null;
|
|
_connectionTimeoutSeconds = 30;
|
|
_maxRetries = 10;
|
|
_retryDelaySeconds = 60;
|
|
_formError = null;
|
|
_showForm = true;
|
|
}
|
|
|
|
private void StartEdit(SmsConfigurationEntity sms)
|
|
{
|
|
_editingSms = sms;
|
|
_accountSid = sms.AccountSid;
|
|
_fromNumber = sms.FromNumber ?? string.Empty;
|
|
_messagingServiceSid = sms.MessagingServiceSid;
|
|
_apiBaseUrl = sms.ApiBaseUrl;
|
|
// Never pre-fill the stored secret; blank means "keep existing".
|
|
_authToken = null;
|
|
_connectionTimeoutSeconds = sms.ConnectionTimeoutSeconds;
|
|
_maxRetries = sms.MaxRetries;
|
|
_retryDelaySeconds = (int)sms.RetryDelay.TotalSeconds;
|
|
_formError = null;
|
|
_showForm = true;
|
|
}
|
|
|
|
private void CancelForm()
|
|
{
|
|
_showForm = false;
|
|
_formError = null;
|
|
}
|
|
|
|
private async Task Save()
|
|
{
|
|
_formError = null;
|
|
// A valid Twilio config needs an Account SID and at least one sender identity:
|
|
// a From number OR a Messaging Service SID (the latter is used instead of From).
|
|
if (string.IsNullOrWhiteSpace(_accountSid)
|
|
|| (string.IsNullOrWhiteSpace(_fromNumber) && string.IsNullOrWhiteSpace(_messagingServiceSid)))
|
|
{
|
|
_formError = "Account SID and either a From Number or a Messaging Service SID are required.";
|
|
return;
|
|
}
|
|
|
|
// The AccountSid is interpolated un-escaped into the Twilio request URI, so reject
|
|
// any malformed SID client-side too (the ManagementActor enforces the same guard
|
|
// server-side; keep the message identical).
|
|
if (!System.Text.RegularExpressions.Regex.IsMatch(_accountSid.Trim(), "^AC[0-9a-fA-F]{32}$"))
|
|
{
|
|
_formError = "Account SID must match Twilio's format: 'AC' followed by 32 hex characters.";
|
|
return;
|
|
}
|
|
|
|
var typedAuthToken = string.IsNullOrWhiteSpace(_authToken) ? null : _authToken.Trim();
|
|
|
|
try
|
|
{
|
|
if (_editingSms is null && typedAuthToken is null)
|
|
{
|
|
_formError = "Auth Token is required.";
|
|
return;
|
|
}
|
|
var from = string.IsNullOrWhiteSpace(_fromNumber) ? null : _fromNumber.Trim();
|
|
var messaging = string.IsNullOrWhiteSpace(_messagingServiceSid) ? null : _messagingServiceSid.Trim();
|
|
await MutationServices.ExecuteConfigurationAsync(_editingSms is not null
|
|
? new UpdateSmsConfigCommand(_editingSms.Id, _accountSid.Trim(), from, messaging, _apiBaseUrl?.Trim() ?? "", typedAuthToken,
|
|
_connectionTimeoutSeconds, _maxRetries, TimeSpan.FromSeconds(_retryDelaySeconds))
|
|
: new CreateSmsConfigCommand(_accountSid.Trim(), from, messaging, _apiBaseUrl?.Trim(), typedAuthToken,
|
|
_connectionTimeoutSeconds, _maxRetries, TimeSpan.FromSeconds(_retryDelaySeconds)));
|
|
_showForm = false;
|
|
_toast.ShowSuccess("SMS configuration saved.");
|
|
await LoadAsync();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_formError = ex.Message;
|
|
}
|
|
}
|
|
}
|