fix(audit): scope audit reads by site identifier, not numeric site id (MGT-2, UI-5, WP1 task 1.6)
PermittedSiteIds / SiteId claims are numeric Site.Id values while AuditLog.SourceSiteId holds the site identifier. /api/audit/query and /export compared the two (a scoped query was always empty, an explicit --site of one's own site was a 403), /api/audit/tree and Flow Debugger Replay dropped every site-side node, and the Central UI Audit Log grid applied no scope at all. New Commons AuditSiteScope: translate permitted ids to identifiers (Administrator / no claims = unscoped; unresolvable = fail closed) and apply them to a filter (never an empty SourceSiteIds, which the repository reads as unfiltered). Used by AuditEndpoints (query, export, tree), FlowDebugRunner.ReplayAsync, and AuditLogQueryService (grid QueryAsync - pushed into the query before paging - and the tree). ApplySiteScope gains the Administrator exemption. Tests: scoped query filters by identifier, own-site explicit filter allowed and foreign one 403, admin with claims unscoped, tree/replay get identifiers, UI grid scoped/out-of-scope/unscoped, tree per-call scope.
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
|
||||
|
||||
namespace ZB.MOM.WW.ScadaBridge.Commons.Types.Audit;
|
||||
|
||||
/// <summary>
|
||||
/// Site scoping for audit-log reads (code review 2026-12-02 MGT-2 / UI-5).
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// A caller's permitted sites arrive as <b>numeric</b> <c>Site.Id</c> values rendered as
|
||||
/// strings (the <c>SiteId</c> claim / <c>AuthenticatedUser.PermittedSiteIds</c>, sourced from
|
||||
/// <c>SiteScopeRule.SiteId</c>). <c>AuditLog.SourceSiteId</c> holds the site
|
||||
/// <b>identifier</b> (e.g. <c>site-a</c>). Comparing the two id spaces directly matched
|
||||
/// nothing — a scoped caller's query returned no site rows, an explicit site filter
|
||||
/// intersected to empty (403), and the execution tree dropped every site-side node. Every
|
||||
/// audit read surface therefore translates the permitted ids to identifiers through this one
|
||||
/// helper first, and applies the same Administrator exemption as every other scope check.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public static class AuditSiteScope
|
||||
{
|
||||
/// <summary>
|
||||
/// Translates the caller's permitted numeric site ids to site identifiers.
|
||||
/// </summary>
|
||||
/// <param name="sites">Site repository used to look the ids up.</param>
|
||||
/// <param name="permittedSiteIds">The caller's permitted numeric site ids, as strings.</param>
|
||||
/// <param name="isAdministrator">Whether the caller holds the Administrator role.</param>
|
||||
/// <param name="ct">Cancellation token.</param>
|
||||
/// <returns>
|
||||
/// <c>null</c> when the caller is unscoped (an Administrator, or no site claims at all);
|
||||
/// otherwise the permitted identifiers. The list may be EMPTY — a scoped caller whose
|
||||
/// sites no longer exist (or whose claims are not integers) sees no site rows. Null and
|
||||
/// empty are deliberately different: empty is the fail-closed outcome.
|
||||
/// </returns>
|
||||
public static async Task<IReadOnlyList<string>?> ResolvePermittedSiteIdentifiersAsync(
|
||||
ISiteRepository sites,
|
||||
IReadOnlyCollection<string> permittedSiteIds,
|
||||
bool isAdministrator,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(sites);
|
||||
ArgumentNullException.ThrowIfNull(permittedSiteIds);
|
||||
if (isAdministrator || permittedSiteIds.Count == 0)
|
||||
return null;
|
||||
|
||||
var identifiers = new List<string>();
|
||||
foreach (var raw in permittedSiteIds.Distinct(StringComparer.Ordinal))
|
||||
{
|
||||
if (!int.TryParse(raw, out var id))
|
||||
continue;
|
||||
var site = await sites.GetSiteByIdAsync(id, ct).ConfigureAwait(false);
|
||||
if (site is not null && !identifiers.Contains(site.SiteIdentifier, StringComparer.OrdinalIgnoreCase))
|
||||
identifiers.Add(site.SiteIdentifier);
|
||||
}
|
||||
|
||||
return identifiers;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Restricts an audit-log query filter to the caller's permitted site identifiers.
|
||||
/// </summary>
|
||||
/// <param name="filter">The caller-supplied filter.</param>
|
||||
/// <param name="permittedIdentifiers">
|
||||
/// From <see cref="ResolvePermittedSiteIdentifiersAsync"/>; <c>null</c> = unscoped.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The filter unchanged for an unscoped caller; for a scoped caller with no explicit site
|
||||
/// filter, the filter restricted to the permitted identifiers; for an explicit filter, its
|
||||
/// intersection with them (identifiers compare case-insensitively, like the database).
|
||||
/// <c>null</c> when nothing is left — an explicit filter entirely outside the permitted
|
||||
/// set, or a scoped caller with no resolvable site — so the caller can refuse (403) or
|
||||
/// return an empty page. Never returns an empty <see cref="AuditLogQueryFilter.SourceSiteIds"/>,
|
||||
/// which the repository would read as "no site filter".
|
||||
/// </returns>
|
||||
public static AuditLogQueryFilter? Apply(AuditLogQueryFilter filter, IReadOnlyList<string>? permittedIdentifiers)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(filter);
|
||||
if (permittedIdentifiers is null)
|
||||
return filter;
|
||||
if (permittedIdentifiers.Count == 0)
|
||||
return null;
|
||||
|
||||
var permitted = new HashSet<string>(permittedIdentifiers, StringComparer.OrdinalIgnoreCase);
|
||||
if (filter.SourceSiteIds is null || filter.SourceSiteIds.Count == 0)
|
||||
return filter with { SourceSiteIds = permitted.ToArray() };
|
||||
|
||||
var intersection = filter.SourceSiteIds.Where(permitted.Contains).ToArray();
|
||||
return intersection.Length == 0 ? null : filter with { SourceSiteIds = intersection };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user