Files
lmxopcua/src/Server/ZB.MOM.WW.OtOpcUa.AdminUI/Components/Pages/Clusters/ClusterAcls.razor
T

143 lines
6.4 KiB
Plaintext

@page "/clusters/{ClusterId}/acls"
@attribute [Authorize(Policy = AdminUiPolicies.AuthenticatedRead)]
@rendermode RenderMode.InteractiveServer
@using Microsoft.EntityFrameworkCore
@using ZB.MOM.WW.OtOpcUa.AdminUI.Acls
@using ZB.MOM.WW.OtOpcUa.Configuration
@using ZB.MOM.WW.OtOpcUa.Configuration.Entities
@inject IDbContextFactory<OtOpcUaConfigDbContext> DbFactory
@inject IAclScopeService Scopes
<PageErrorBoundary>
<PageTitle>Cluster ACLs · OtOpcUa</PageTitle>
<div class="page-head">
<h1>ACLs &middot; <span class="mono">@ClusterId</span></h1>
<span class="spacer"></span>
<a href="/clusters/@ClusterId/acls/new" class="btn btn-primary btn-sm">New ACL grant</a>
</div>
<ClusterNav ClusterId="@ClusterId" ActiveTab="acls" />
@if (_rows is null)
{
<p>Loading…</p>
}
else
{
<section class="panel notice rise" style="animation-delay:.02s">
The <strong>Scope target</strong> column resolves each row against the current configuration. A
row marked <span class="chip chip-alert">unresolved</span> points at nothing — it is shipped in
every deployment artifact but authorizes nobody, and the deploy gate rejects it. Open it and
re-pick the scope.
</section>
<section class="panel rise mt-3" style="animation-delay:.08s">
<h2 class="panel-head">@_rows.Count ACL row@(_rows.Count == 1 ? "" : "s")</h2>
@if (_rows.Count == 0)
{
<div style="padding:1rem" class="text-muted">
No ACL rows for this cluster.
</div>
}
else
{
<div class="table-wrap" role="region" aria-label="Cluster ACL grants" tabindex="0">
<table class="data-table">
<thead>
<tr>
<th scope="col">NodeAclId</th>
<th scope="col">Principal</th>
<th scope="col">Scope</th>
<th scope="col">Scope target</th>
<th scope="col">Permissions</th>
<th scope="col">Notes</th>
<th scope="col"><span class="visually-hidden">Actions</span></th>
</tr>
</thead>
<tbody>
@foreach (var a in _rows)
{
<tr>
<td><span class="mono small">@a.NodeAclId</span></td>
<td>
<span class="mono">@a.Principal</span>
@if (_principals.TryGetValue(a.Principal, out var use) && !use.ReachesSomething)
{
<span class="chip chip-warn ms-1" title="No active API key authenticates as this principal, so the grant currently authorizes nobody.">no key</span>
}
</td>
<td>@a.ScopeKind</td>
<td>
<span class="mono small">@(a.ScopeId ?? "—")</span>
@if (_scopes.TryGetValue(a.NodeAclId, out var check))
{
@if (check.Ok)
{
<div class="text-muted small">@check.Message</div>
}
else
{
<div><span class="chip chip-alert" title="@check.Message">unresolved</span></div>
<div class="text-danger small">@check.Message</div>
}
}
</td>
<td>
@foreach (var perm in PermissionChips(a.PermissionFlags))
{
<span class="chip chip-idle me-1">@perm</span>
}
</td>
<td class="text-muted small" title="@(a.Notes ?? "")">@(string.IsNullOrEmpty(a.Notes) ? "" : DisplayText.Abbreviate(a.Notes, 60))</td>
<td><a href="/clusters/@ClusterId/acls/@a.NodeAclId" class="btn btn-sm btn-outline-primary">Edit</a></td>
</tr>
}
</tbody>
</table>
</div>
}
</section>
}
</PageErrorBoundary>
@code {
[Parameter] public string ClusterId { get; set; } = "";
private List<NodeAcl>? _rows;
private Dictionary<string, AclScopeCheck> _scopes = new(StringComparer.Ordinal);
private Dictionary<string, PrincipalUsage> _principals = new(StringComparer.OrdinalIgnoreCase);
protected override async Task OnInitializedAsync()
{
await using var db = await DbFactory.CreateDbContextAsync();
_rows = await db.NodeAcls.AsNoTracking()
.Where(a => a.ClusterId == ClusterId)
.OrderBy(a => a.NodeAclId)
.ToListAsync();
// Resolve every row's scope and principal so an unresolved grant is visible from the list rather
// than only at deploy time. One indexed lookup each; the distinct-principal grouping keeps the
// key lookups down to the number of distinct principals, not the number of rows.
foreach (var row in _rows)
{
_scopes[row.NodeAclId] = await Scopes.CheckScopeAsync(ClusterId, row.ScopeKind, row.ScopeId);
}
foreach (var principal in _rows.Select(r => r.Principal).Distinct(StringComparer.OrdinalIgnoreCase))
{
_principals[principal] = await Scopes.CheckPrincipalAsync(principal);
}
}
private static IEnumerable<string> PermissionChips(ZB.MOM.WW.OtOpcUa.Configuration.Enums.NodePermissions flags)
{
foreach (var v in Enum.GetValues<ZB.MOM.WW.OtOpcUa.Configuration.Enums.NodePermissions>())
{
// Skip None (zero) and composite values that aren't single bits.
var n = (int)v;
if (n == 0) continue;
if ((n & (n - 1)) != 0) continue;
if (flags.HasFlag(v)) yield return v.ToString();
}
}
}