7dcdcc46c7
All 22 occurrences of hardcoded "system" user string replaced with GetCurrentUserAsync() which reads the Username claim from AuthenticationState. Affected: Instances.razor (6), Sites.razor (2), Templates.razor (11), SharedScripts.razor (3).
297 lines
11 KiB
Plaintext
297 lines
11 KiB
Plaintext
@page "/design/shared-scripts"
|
|
@using ScadaLink.Security
|
|
@using ScadaLink.Commons.Entities.Scripts
|
|
@using ScadaLink.Commons.Interfaces.Repositories
|
|
@using ScadaLink.TemplateEngine
|
|
@attribute [Authorize(Policy = AuthorizationPolicies.RequireDesign)]
|
|
@inject ITemplateEngineRepository TemplateEngineRepository
|
|
@inject SharedScriptService SharedScriptService
|
|
@inject AuthenticationStateProvider AuthStateProvider
|
|
|
|
<div class="container-fluid mt-3">
|
|
<div class="d-flex justify-content-between align-items-center mb-3">
|
|
<h4 class="mb-0">Shared Scripts</h4>
|
|
<button class="btn btn-primary btn-sm" @onclick="ShowAddForm">New Script</button>
|
|
</div>
|
|
|
|
<ToastNotification @ref="_toast" />
|
|
<ConfirmDialog @ref="_confirmDialog" />
|
|
|
|
@if (_loading)
|
|
{
|
|
<LoadingSpinner IsLoading="true" />
|
|
}
|
|
else if (_errorMessage != null)
|
|
{
|
|
<div class="alert alert-danger">@_errorMessage</div>
|
|
}
|
|
else
|
|
{
|
|
@if (_showForm)
|
|
{
|
|
<div class="card mb-3">
|
|
<div class="card-body">
|
|
<h6 class="card-title">@(_editingScript == null ? "New Shared Script" : $"Edit: {_editingScript.Name}")</h6>
|
|
<div class="row g-2">
|
|
<div class="col-md-4">
|
|
<label class="form-label small">Name</label>
|
|
<input type="text" class="form-control form-control-sm" @bind="_formName"
|
|
disabled="@(_editingScript != null)" />
|
|
</div>
|
|
<div class="col-md-4">
|
|
<label class="form-label small">Parameters (JSON)</label>
|
|
<input type="text" class="form-control form-control-sm" @bind="_formParameters"
|
|
placeholder='e.g. [{"name":"x","type":"Int32"}]' />
|
|
</div>
|
|
<div class="col-md-4">
|
|
<label class="form-label small">Return Definition (JSON)</label>
|
|
<input type="text" class="form-control form-control-sm" @bind="_formReturn"
|
|
placeholder='e.g. {"type":"Boolean"}' />
|
|
</div>
|
|
</div>
|
|
<div class="mt-2">
|
|
<label class="form-label small">Code</label>
|
|
<textarea class="form-control form-control-sm font-monospace" rows="10" @bind="_formCode"
|
|
style="font-size: 0.8rem;"></textarea>
|
|
</div>
|
|
<div class="mt-2">
|
|
<button class="btn btn-success btn-sm me-1" @onclick="SaveScript">Save</button>
|
|
<button class="btn btn-outline-info btn-sm me-1" @onclick="CheckCompilation">Check Syntax</button>
|
|
<button class="btn btn-outline-secondary btn-sm" @onclick="CancelForm">Cancel</button>
|
|
</div>
|
|
@if (_formError != null)
|
|
{
|
|
<div class="text-danger small mt-1">@_formError</div>
|
|
}
|
|
@if (_syntaxCheckResult != null)
|
|
{
|
|
<div class="@(_syntaxCheckPassed ? "text-success" : "text-danger") small mt-1">@_syntaxCheckResult</div>
|
|
}
|
|
</div>
|
|
</div>
|
|
}
|
|
|
|
<table class="table table-sm table-striped table-hover">
|
|
<thead class="table-dark">
|
|
<tr>
|
|
<th>ID</th>
|
|
<th>Name</th>
|
|
<th>Code (preview)</th>
|
|
<th>Parameters</th>
|
|
<th>Returns</th>
|
|
<th style="width: 160px;">Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
@if (_scripts.Count == 0)
|
|
{
|
|
<tr>
|
|
<td colspan="6" class="text-muted text-center">No shared scripts configured.</td>
|
|
</tr>
|
|
}
|
|
@foreach (var script in _scripts)
|
|
{
|
|
<tr>
|
|
<td>@script.Id</td>
|
|
<td><strong>@script.Name</strong></td>
|
|
<td class="small text-muted font-monospace text-truncate" style="max-width: 300px;">
|
|
@script.Code[..Math.Min(60, script.Code.Length)]@(script.Code.Length > 60 ? "..." : "")
|
|
</td>
|
|
<td class="small text-muted">@(script.ParameterDefinitions ?? "—")</td>
|
|
<td class="small text-muted">@(script.ReturnDefinition ?? "—")</td>
|
|
<td>
|
|
<button class="btn btn-outline-primary btn-sm py-0 px-1 me-1"
|
|
@onclick="() => EditScript(script)">Edit</button>
|
|
<button class="btn btn-outline-danger btn-sm py-0 px-1"
|
|
@onclick="() => DeleteScript(script)">Delete</button>
|
|
</td>
|
|
</tr>
|
|
}
|
|
</tbody>
|
|
</table>
|
|
}
|
|
</div>
|
|
|
|
@code {
|
|
private async Task<string> GetCurrentUserAsync()
|
|
{
|
|
var authState = await AuthStateProvider.GetAuthenticationStateAsync();
|
|
return authState.User.FindFirst("Username")?.Value ?? "unknown";
|
|
}
|
|
|
|
private List<SharedScript> _scripts = new();
|
|
private bool _loading = true;
|
|
private string? _errorMessage;
|
|
|
|
private bool _showForm;
|
|
private SharedScript? _editingScript;
|
|
private string _formName = string.Empty;
|
|
private string _formCode = string.Empty;
|
|
private string? _formParameters;
|
|
private string? _formReturn;
|
|
private string? _formError;
|
|
private string? _syntaxCheckResult;
|
|
private bool _syntaxCheckPassed;
|
|
|
|
private ToastNotification _toast = default!;
|
|
private ConfirmDialog _confirmDialog = default!;
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
await LoadDataAsync();
|
|
}
|
|
|
|
private async Task LoadDataAsync()
|
|
{
|
|
_loading = true;
|
|
_errorMessage = null;
|
|
try
|
|
{
|
|
_scripts = (await SharedScriptService.GetAllSharedScriptsAsync()).ToList();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_errorMessage = $"Failed to load shared scripts: {ex.Message}";
|
|
}
|
|
_loading = false;
|
|
}
|
|
|
|
private void ShowAddForm()
|
|
{
|
|
_editingScript = null;
|
|
_formName = string.Empty;
|
|
_formCode = string.Empty;
|
|
_formParameters = null;
|
|
_formReturn = null;
|
|
_formError = null;
|
|
_syntaxCheckResult = null;
|
|
_showForm = true;
|
|
}
|
|
|
|
private void EditScript(SharedScript script)
|
|
{
|
|
_editingScript = script;
|
|
_formName = script.Name;
|
|
_formCode = script.Code;
|
|
_formParameters = script.ParameterDefinitions;
|
|
_formReturn = script.ReturnDefinition;
|
|
_formError = null;
|
|
_syntaxCheckResult = null;
|
|
_showForm = true;
|
|
}
|
|
|
|
private void CancelForm()
|
|
{
|
|
_showForm = false;
|
|
_editingScript = null;
|
|
}
|
|
|
|
private void CheckCompilation()
|
|
{
|
|
var syntaxError = ValidateSyntaxLocally(_formCode);
|
|
if (syntaxError == null)
|
|
{
|
|
_syntaxCheckResult = "Syntax check passed.";
|
|
_syntaxCheckPassed = true;
|
|
}
|
|
else
|
|
{
|
|
_syntaxCheckResult = syntaxError;
|
|
_syntaxCheckPassed = false;
|
|
}
|
|
}
|
|
|
|
private async Task SaveScript()
|
|
{
|
|
_formError = null;
|
|
_syntaxCheckResult = null;
|
|
|
|
try
|
|
{
|
|
if (_editingScript != null)
|
|
{
|
|
var user = await GetCurrentUserAsync();
|
|
var result = await SharedScriptService.UpdateSharedScriptAsync(
|
|
_editingScript.Id, _formCode, _formParameters?.Trim(), _formReturn?.Trim(), user);
|
|
if (result.IsSuccess)
|
|
{
|
|
_showForm = false;
|
|
_toast.ShowSuccess($"Script '{_editingScript.Name}' updated.");
|
|
await LoadDataAsync();
|
|
}
|
|
else
|
|
{
|
|
_formError = result.Error;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
var user = await GetCurrentUserAsync();
|
|
var result = await SharedScriptService.CreateSharedScriptAsync(
|
|
_formName.Trim(), _formCode, _formParameters?.Trim(), _formReturn?.Trim(), user);
|
|
if (result.IsSuccess)
|
|
{
|
|
_showForm = false;
|
|
_toast.ShowSuccess($"Script '{_formName}' created.");
|
|
await LoadDataAsync();
|
|
}
|
|
else
|
|
{
|
|
_formError = result.Error;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_formError = $"Save failed: {ex.Message}";
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Basic syntax check: balanced braces/brackets/parens.
|
|
/// Mirrors the internal SharedScriptService.ValidateSyntax logic.
|
|
/// </summary>
|
|
private static string? ValidateSyntaxLocally(string code)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(code)) return "Script code cannot be empty.";
|
|
int brace = 0, bracket = 0, paren = 0;
|
|
foreach (var ch in code)
|
|
{
|
|
switch (ch) { case '{': brace++; break; case '}': brace--; break; case '[': bracket++; break; case ']': bracket--; break; case '(': paren++; break; case ')': paren--; break; }
|
|
if (brace < 0) return "Syntax error: unmatched closing brace '}'.";
|
|
if (bracket < 0) return "Syntax error: unmatched closing bracket ']'.";
|
|
if (paren < 0) return "Syntax error: unmatched closing parenthesis ')'.";
|
|
}
|
|
if (brace != 0) return "Syntax error: unmatched opening brace '{'.";
|
|
if (bracket != 0) return "Syntax error: unmatched opening bracket '['.";
|
|
if (paren != 0) return "Syntax error: unmatched opening parenthesis '('.";
|
|
return null;
|
|
}
|
|
|
|
private async Task DeleteScript(SharedScript script)
|
|
{
|
|
var confirmed = await _confirmDialog.ShowAsync(
|
|
$"Delete shared script '{script.Name}'?", "Delete Shared Script");
|
|
if (!confirmed) return;
|
|
|
|
try
|
|
{
|
|
var user = await GetCurrentUserAsync();
|
|
var result = await SharedScriptService.DeleteSharedScriptAsync(script.Id, user);
|
|
if (result.IsSuccess)
|
|
{
|
|
_toast.ShowSuccess($"Script '{script.Name}' deleted.");
|
|
await LoadDataAsync();
|
|
}
|
|
else
|
|
{
|
|
_toast.ShowError(result.Error);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_toast.ShowError($"Delete failed: {ex.Message}");
|
|
}
|
|
}
|
|
}
|