@page "/clusters/{ClusterId}/acls/new" @page "/clusters/{ClusterId}/acls/{NodeAclId}" @attribute [Microsoft.AspNetCore.Authorization.Authorize] @rendermode RenderMode.InteractiveServer @using Microsoft.AspNetCore.Components.Forms @using Microsoft.EntityFrameworkCore @using System.ComponentModel.DataAnnotations @using ZB.MOM.WW.OtOpcUa.Configuration @using ZB.MOM.WW.OtOpcUa.Configuration.Entities @using ZB.MOM.WW.OtOpcUa.Configuration.Enums @inject IDbContextFactory DbFactory @inject NavigationManager Nav

@(IsNew ? "New ACL grant" : "Edit ACL grant") · @ClusterId

Cancel
@if (!_loaded) {

Loading…

} else if (!IsNew && _existing is null) {
ACL @NodeAclId not found.
} else {
Grant
@foreach (var bit in PermissionBits) {
}
Bundles: · · ·
@if (!string.IsNullOrWhiteSpace(_error)) {
@_error
}
Cancel @if (!IsNew) { }
} @code { private static readonly NodePermissions[] PermissionBits = [ NodePermissions.Browse, NodePermissions.Read, NodePermissions.Subscribe, NodePermissions.HistoryRead, NodePermissions.WriteOperate, NodePermissions.WriteTune, NodePermissions.WriteConfigure, NodePermissions.AlarmRead, NodePermissions.AlarmAcknowledge, NodePermissions.AlarmConfirm, NodePermissions.AlarmShelve, NodePermissions.MethodCall, ]; [Parameter] public string ClusterId { get; set; } = ""; [Parameter] public string? NodeAclId { get; set; } private bool IsNew => string.IsNullOrEmpty(NodeAclId); private FormModel _form = new(); private NodeAcl? _existing; private bool _loaded; private bool _busy; private string? _error; protected override async Task OnInitializedAsync() { if (!IsNew) { await using var db = await DbFactory.CreateDbContextAsync(); _existing = await db.NodeAcls.AsNoTracking().FirstOrDefaultAsync( a => a.ClusterId == ClusterId && a.NodeAclId == NodeAclId); if (_existing is not null) { _form = new FormModel { NodeAclId = _existing.NodeAclId, LdapGroup = _existing.LdapGroup, ScopeKind = _existing.ScopeKind, ScopeId = _existing.ScopeId, PermissionFlags = _existing.PermissionFlags, Notes = _existing.Notes, RowVersion = _existing.RowVersion, }; } } _loaded = true; } private async Task SubmitAsync() { _busy = true; _error = null; try { await using var db = await DbFactory.CreateDbContextAsync(); if (IsNew) { if (await db.NodeAcls.AnyAsync(a => a.NodeAclId == _form.NodeAclId)) { _error = $"ACL '{_form.NodeAclId}' already exists."; return; } db.NodeAcls.Add(new NodeAcl { NodeAclId = _form.NodeAclId, ClusterId = ClusterId, LdapGroup = _form.LdapGroup, ScopeKind = _form.ScopeKind, ScopeId = string.IsNullOrWhiteSpace(_form.ScopeId) ? null : _form.ScopeId, PermissionFlags = _form.PermissionFlags, Notes = string.IsNullOrWhiteSpace(_form.Notes) ? null : _form.Notes, }); } else { var entity = await db.NodeAcls.FirstOrDefaultAsync(a => a.ClusterId == ClusterId && a.NodeAclId == NodeAclId); if (entity is null) { _error = "Row no longer exists."; return; } db.Entry(entity).Property(e => e.RowVersion).OriginalValue = _form.RowVersion; entity.LdapGroup = _form.LdapGroup; entity.ScopeKind = _form.ScopeKind; entity.ScopeId = string.IsNullOrWhiteSpace(_form.ScopeId) ? null : _form.ScopeId; entity.PermissionFlags = _form.PermissionFlags; entity.Notes = string.IsNullOrWhiteSpace(_form.Notes) ? null : _form.Notes; } await db.SaveChangesAsync(); Nav.NavigateTo($"/clusters/{ClusterId}/acls"); } catch (DbUpdateConcurrencyException) { _error = "Another user changed this ACL while you were editing."; } catch (Exception ex) { _error = ex.Message; } finally { _busy = false; } } private async Task DeleteAsync() { if (IsNew) return; _busy = true; _error = null; try { await using var db = await DbFactory.CreateDbContextAsync(); var entity = await db.NodeAcls.FirstOrDefaultAsync(a => a.ClusterId == ClusterId && a.NodeAclId == NodeAclId); if (entity is null) { Nav.NavigateTo($"/clusters/{ClusterId}/acls"); return; } db.Entry(entity).Property(e => e.RowVersion).OriginalValue = _form.RowVersion; db.NodeAcls.Remove(entity); await db.SaveChangesAsync(); Nav.NavigateTo($"/clusters/{ClusterId}/acls"); } catch (DbUpdateConcurrencyException) { _error = "Another user changed this ACL while you were viewing it."; } catch (Exception ex) { _error = ex.Message; } finally { _busy = false; } } private sealed class FormModel { [Required, RegularExpression("^[A-Za-z0-9_-]+$")] public string NodeAclId { get; set; } = ""; [Required] public string LdapGroup { get; set; } = ""; public NodeAclScopeKind ScopeKind { get; set; } = NodeAclScopeKind.Cluster; public string? ScopeId { get; set; } public NodePermissions PermissionFlags { get; set; } = NodePermissions.None; public string? Notes { get; set; } public byte[] RowVersion { get; set; } = []; public bool HasPerm(NodePermissions bit) => PermissionFlags.HasFlag(bit); public void SetPerm(NodePermissions bit, bool on) { if (on) PermissionFlags |= bit; else PermissionFlags &= ~bit; } } }