fix(admin-ui): gate cluster and ACL actions by destination policies
This commit is contained in:
@@ -1,3 +1,14 @@
|
||||
using System.Security.Claims;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Components.Authorization;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Pages.Clusters;
|
||||
using ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Uns;
|
||||
using ZB.MOM.WW.OtOpcUa.AdminUI.Acls;
|
||||
using ZB.MOM.WW.OtOpcUa.AdminUI.Uns;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Enums;
|
||||
using Bunit;
|
||||
using Bunit.TestDoubles;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
@@ -74,4 +85,53 @@ public sealed class NavigationPolicyRenderTests : BunitContext
|
||||
cut.Find("a[href='/admin/secrets']").ShouldNotBeNull();
|
||||
cut.FindAll("a[href='/role-grants'],a[href='/apikeys']").ShouldBeEmpty();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("Viewer", false)]
|
||||
[InlineData("Operator", false)]
|
||||
[InlineData("Designer", true)]
|
||||
[InlineData("Administrator", true)]
|
||||
public void ClusterListAndAclActions_FollowDestinationPolicy(string role, bool allowed)
|
||||
{
|
||||
Services.RemoveAll<IAuthorizationService>();
|
||||
Services.RemoveAll<IAuthorizationPolicyProvider>();
|
||||
Services.AddAuthorizationCore( options =>
|
||||
options.AddPolicy(AdminUiPolicies.ConfigEditor, policy => policy.RequireRole("Administrator", "Designer")));
|
||||
var principal = new ClaimsPrincipal(new ClaimsIdentity([
|
||||
new(ClaimTypes.Name, "fixture"), new(ClaimTypes.Role, role)], "fixture"));
|
||||
var state = Task.FromResult(new AuthenticationState(principal));
|
||||
var name = Guid.NewGuid().ToString("N");
|
||||
using (var db = RawTreeTestDb.CreateNamed(name))
|
||||
{
|
||||
db.ServerClusters.Add(new ServerCluster {
|
||||
ClusterId="C1", Name="Readable cluster", Enterprise="zb", Site="local",
|
||||
RedundancyMode=RedundancyMode.None, CreatedBy="fixture" });
|
||||
db.NodeAcls.Add(new NodeAcl {
|
||||
NodeAclId="A1", ClusterId="C1", Principal="fixture-reader",
|
||||
ScopeKind=NodeAclScopeKind.Cluster,
|
||||
PermissionFlags=NodePermissions.Browse });
|
||||
db.SaveChanges();
|
||||
}
|
||||
var factory = RawTreeTestDb.Factory(name);
|
||||
Services.AddSingleton(factory);
|
||||
Services.AddSingleton<IAclScopeService>(
|
||||
new AclScopeService(factory,
|
||||
new RawTreeService(factory),
|
||||
new UnsTreeService(factory)));
|
||||
var list = Render<CascadingValue<Task<AuthenticationState>>>(p => p.Add(x => x.Value, state)
|
||||
.AddChildContent<ClustersList>());
|
||||
list.WaitForAssertion(() => list.Markup.ShouldContain("Readable cluster"));
|
||||
list.FindAll("a[href='/clusters/new']").Count.ShouldBe(allowed ? 1 : 0);
|
||||
var acls = Render<CascadingValue<Task<AuthenticationState>>>(p => p.Add(x => x.Value, state)
|
||||
.AddChildContent<ClusterAcls>(child => child.Add(x => x.ClusterId, "C1")));
|
||||
acls.WaitForAssertion(() => acls.Markup.ShouldContain("fixture-reader"));
|
||||
acls.Markup.Contains("re-pick the scope", StringComparison.Ordinal).ShouldBe(allowed);
|
||||
acls.FindAll("thead th").Count.ShouldBe(allowed ? 7 : 6);
|
||||
acls.FindAll("tbody tr").ShouldAllBe(row => row.QuerySelectorAll("td").Length == (allowed ? 7 : 6));
|
||||
foreach (var path in new[] { "/clusters/C1/acls/new", "/clusters/C1/acls/A1" })
|
||||
acls.FindAll($"a[href='{path}']").Count.ShouldBe(allowed ? 1 : 0, path);
|
||||
foreach (var type in new[] { typeof(NewCluster), typeof(AclEdit) })
|
||||
type.GetCustomAttributes(typeof(AuthorizeAttribute), true)
|
||||
.Cast<AuthorizeAttribute>().ShouldContain(x => x.Policy == AdminUiPolicies.ConfigEditor);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user