fix(centralui): system-wide users can Retry/Discard rows from a deleted site (UI-11, WP5 task 5.4)

IsRowSiteAllowedAsync on the Notification Outbox and Site Calls pages returned
'resolved is not null && ...', so a parked row whose source site no longer
resolves (deleted / renamed) - which the grid deliberately keeps listing - was
refused for everyone, Administrator included, and could not be cleared.

Now a system-wide user short-circuits to allowed before the site lookup; a
site-scoped user is still denied an unresolvable row (no Site.Id to authorize
against). Deletes NotificationReport's unused IsRowSiteAllowedSync, whose
'unresolvable defaults to allowed' contract contradicted the async check.
Design doc updated (Notification Outbox, Site Calls).

Tests: NotificationReportPageTests / SiteCallsReportPageTests
Retry_OnRowFromADeletedSite_AllowedSystemWide_DeniedScoped (system-wide case fails
pre-fix; scoped denial pinned).
This commit is contained in:
Joseph Doherty
2026-12-03 10:03:12 -05:00
parent 7addd846a1
commit 2421a4e445
5 changed files with 119 additions and 21 deletions
+2 -2
View File
@@ -275,7 +275,7 @@ Per-leaf alarm rendering (leaf nodes are individual conditions for native alarms
- Monitor and manage centrally-delivered notifications. The Notification Outbox dispatches every notification store-and-forwarded from sites and logs each one to the central `Notifications` table. - Monitor and manage centrally-delivered notifications. The Notification Outbox dispatches every notification store-and-forwarded from sites and logs each one to the central `Notifications` table.
- **KPI tiles** at the top of the page: queue depth (`Pending` + `Retrying`), stuck count, parked count, delivered in the last interval, and oldest pending age. The KPIs are central-computed on demand from the `Notifications` table. - **KPI tiles** at the top of the page: queue depth (`Pending` + `Retrying`), stuck count, parked count, delivered in the last interval, and oldest pending age. The KPIs are central-computed on demand from the `Notifications` table.
- A **queryable notification list** filterable by status, type, source site, notification list, and time range, with a **stuck-only toggle** and keyword search on subject. Each row shows the notification's status, retry count, last error, and key timestamps. - A **queryable notification list** filterable by status, type, source site, notification list, and time range, with a **stuck-only toggle** and keyword search on subject. Each row shows the notification's status, retry count, last error, and key timestamps.
- **Retry** and **Discard** actions are available on parked notifications: Retry returns the notification to `Pending` and resets `RetryCount` / `NextAttemptAt`; Discard moves it to `Discarded`. The row is retained either way so the table stays a complete audit record. - **Retry** and **Discard** actions are available on parked notifications: Retry returns the notification to `Pending` and resets `RetryCount` / `NextAttemptAt`; Discard moves it to `Discarded`. The row is retained either way so the table stays a complete audit record. The action re-checks site scope at click time: a system-wide user may act on any row, including one whose source site no longer resolves (deleted or renamed — such rows stay listed); a site-scoped user may act only on rows of a permitted site, and not on an unresolvable one (code review 2026-12-02 UI-11).
- Each row exposes a **"View audit history"** action that opens the Audit Log page pre-filtered to `CorrelationId = NotificationId`, surfacing every operational audit event recorded for that notification. - Each row exposes a **"View audit history"** action that opens the Audit Log page pre-filtered to `CorrelationId = NotificationId`, surfacing every operational audit event recorded for that notification.
- **Stuck rows are visually badged** — a notification is stuck if it is `Pending` or `Retrying` and older than the configurable stuck-age threshold. Stuck detection is display-only; there is no automated escalation or alerting. - **Stuck rows are visually badged** — a notification is stuck if it is `Pending` or `Retrying` and older than the configurable stuck-age threshold. Stuck detection is display-only; there is no automated escalation or alerting.
- All queries are served from the central `Notifications` table — no remote per-site queries are needed, unlike the Parked Message Management page. - All queries are served from the central `Notifications` table — no remote per-site queries are needed, unlike the Parked Message Management page.
@@ -292,7 +292,7 @@ Per-leaf alarm rendering (leaf nodes are individual conditions for native alarms
### Site Calls (Deployment Role) ### Site Calls (Deployment Role)
- Monitor cached calls store-and-forwarded from sites — `ExternalSystem.CachedCall()` and `Database.CachedWrite()` operations. Scoped to the `ExternalCall` and `DatabaseWrite` kinds only; notifications keep their separate Notification Outbox page and are not merged here. - Monitor cached calls store-and-forwarded from sites — `ExternalSystem.CachedCall()` and `Database.CachedWrite()` operations. Scoped to the `ExternalCall` and `DatabaseWrite` kinds only; notifications keep their separate Notification Outbox page and are not merged here.
- A **queryable cached-call list** filterable by site, kind, status, and time range. Each row shows the call's timestamp, site, kind, target summary, status badge, retry count, and last error. - A **queryable cached-call list** filterable by site, kind, status, and time range. Each row shows the call's timestamp, site, kind, target summary, status badge, retry count, and last error.
- **Retry** and **Discard** actions are available on `Parked` rows only — `Failed` rows are not actionable, since a permanent failure would simply fail again and its error was already returned synchronously to the calling script. The actions issue central→site commands to the owning site; if the site is offline the UI surfaces a "site unreachable" message. - **Retry** and **Discard** actions are available on `Parked` rows only — `Failed` rows are not actionable, since a permanent failure would simply fail again and its error was already returned synchronously to the calling script. The actions issue central→site commands to the owning site; if the site is offline the UI surfaces a "site unreachable" message. Site scope is re-checked at click time exactly as on the Notification Outbox page: a system-wide user may act on a row whose source site no longer resolves, a site-scoped user may not (UI-11).
- Each row exposes a **"View audit history"** action that opens the Audit Log page pre-filtered to `CorrelationId = TrackedOperationId`, showing every operational audit event recorded for that cached call. - Each row exposes a **"View audit history"** action that opens the Audit Log page pre-filtered to `CorrelationId = TrackedOperationId`, showing every operational audit event recorded for that cached call.
- Data is served from the central Site Call Audit component's `SiteCalls` table. The page is **read-mostly** — an eventually-consistent mirror of site state; the site remains the source of truth. - Data is served from the central Site Call Audit component's `SiteCalls` table. The page is **read-mostly** — an eventually-consistent mirror of site state; the site remains the source of truth.
@@ -832,26 +832,18 @@
} }
/// <summary> /// <summary>
/// Server-side re-check for the Retry/Discard relay actions. Returns true for a /// Server-side re-check for the Retry/Discard relay actions (fresh authority).
/// system-wide user, or when the row's source site resolves to a Site.Id in the /// A system-wide user may act on any row — including one whose source site no
/// caller's permitted set. An unresolvable site identifier defaults to allowed /// longer resolves (deleted or renamed), which the grid deliberately keeps showing
/// (legacy behaviour); the relay's own site-scope re-check is then the /// (code review 2026-12-02 UI-11: such rows used to be refused for everyone, even an
/// final gate. /// Administrator, so they could not be cleared). A site-scoped user may act only
/// when the row's site resolves to a <c>Site.Id</c> in their permitted set; an
/// unresolvable site is denied for them.
/// </summary> /// </summary>
private bool IsRowSiteAllowedSync(string sourceSiteId)
{
if (_siteScopeSystemWide)
return true;
var resolved = _allSites.FirstOrDefault(s => s.SiteIdentifier == sourceSiteId);
if (resolved is null)
return true;
return _permittedSiteIds.Contains(resolved.Id);
}
private async Task<bool> IsRowSiteAllowedAsync(string sourceSiteId) private async Task<bool> IsRowSiteAllowedAsync(string sourceSiteId)
{ {
if (await SiteScope.IsSystemWideAsync())
return true;
var resolved = (await SiteRepository.GetAllSitesAsync()).FirstOrDefault(s => s.SiteIdentifier == sourceSiteId); var resolved = (await SiteRepository.GetAllSitesAsync()).FirstOrDefault(s => s.SiteIdentifier == sourceSiteId);
return resolved is not null && await SiteScope.IsSiteAllowedAsync(resolved.Id); return resolved is not null && await SiteScope.IsSiteAllowedAsync(resolved.Id);
} }
@@ -588,11 +588,17 @@ public partial class SiteCallsReport
} }
/// <summary> /// <summary>
/// Server-side re-check for the Retry/Discard relay. True for a system-wide /// Server-side re-check for the Retry/Discard relay (fresh authority). A system-wide
/// user, or when the row's source site maps to a Site.Id in the permitted set. /// user may act on any row, including one whose source site no longer resolves
/// (deleted or renamed) — code review 2026-12-02 UI-11: such rows used to be refused
/// for everyone, even an Administrator. A site-scoped user may act only when the
/// row's site maps to a Site.Id in their permitted set; an unresolvable site is
/// denied for them.
/// </summary> /// </summary>
private async Task<bool> IsRowSiteAllowedAsync(string sourceSite) private async Task<bool> IsRowSiteAllowedAsync(string sourceSite)
{ {
if (await SiteScope.IsSystemWideAsync())
return true;
var resolved = (await SiteRepository.GetAllSitesAsync()).FirstOrDefault(s => s.SiteIdentifier == sourceSite); var resolved = (await SiteRepository.GetAllSitesAsync()).FirstOrDefault(s => s.SiteIdentifier == sourceSite);
return resolved is not null && await SiteScope.IsSiteAllowedAsync(resolved.Id); return resolved is not null && await SiteScope.IsSiteAllowedAsync(resolved.Id);
} }
@@ -179,6 +179,56 @@ public class NotificationReportPageTests : BunitContext
}); });
} }
/// <summary>
/// Code review 2026-12-02 UI-11: a parked row whose source site no longer resolves (site
/// deleted or renamed) stays visible; a system-wide user can Retry it, while a site-scoped
/// user is still refused (there is no Site.Id to authorize against).
/// </summary>
/// <param name="siteScoped">Whether the caller carries a SiteId claim (site-scoped Deployer).</param>
[Theory]
[InlineData(false)]
[InlineData(true)]
public void Retry_OnRowFromADeletedSite_AllowedSystemWide_DeniedScoped(bool siteScoped)
{
_queryReply = _queryReply with
{
Notifications = new List<NotificationSummary>
{
new("notif-cccccccc-3333", "Email", "Ops On-Call", "Orphaned alert",
"Parked", RetryCount: 5, LastError: "SMTP timeout", SourceSiteId: "deleted-site",
SourceInstanceId: "Pump-009", CreatedAt: DateTimeOffset.UtcNow.AddMinutes(-30),
DeliveredAt: null, IsStuck: false),
},
TotalCount = 1,
};
if (siteScoped)
{
var claims = new[]
{
new Claim(JwtTokenService.UsernameClaimType, "tester"),
new Claim(JwtTokenService.RoleClaimType, "Deployer"),
new Claim(JwtTokenService.SiteIdClaimType, "1"),
};
Services.AddSingleton<AuthenticationStateProvider>(
new TestAuthStateProvider(new ClaimsPrincipal(new ClaimsIdentity(claims, "TestAuth"))));
}
var cut = Render<NotificationReportPage>();
cut.WaitForState(() => cut.Markup.Contains("Orphaned alert"));
cut.FindAll("tbody tr").First(r => r.TextContent.Contains("Orphaned alert"))
.QuerySelectorAll("button").First(b => b.TextContent.Contains("Retry")).Click();
if (siteScoped)
{
cut.WaitForAssertion(() => Assert.Contains("not permitted to act on notifications", cut.Markup));
Assert.Empty(_retryRequests);
}
else
{
cut.WaitForAssertion(() => Assert.Equal("notif-cccccccc-3333", Assert.Single(_retryRequests).NotificationId));
}
}
/// <summary>Verifies that a query failure shows an error message.</summary> /// <summary>Verifies that a query failure shows an error message.</summary>
[Fact] [Fact]
public void QueryFailure_ShowsErrorMessage() public void QueryFailure_ShowsErrorMessage()
@@ -221,6 +221,56 @@ public class SiteCallsReportPageTests : BunitContext
}); });
} }
/// <summary>
/// Code review 2026-12-02 UI-11: a parked cached call whose source site no longer resolves
/// (site deleted or renamed) stays visible; a system-wide user can Retry it, while a
/// site-scoped user is still refused.
/// </summary>
/// <param name="siteScoped">Whether the caller carries a SiteId claim (site-scoped Deployer).</param>
[Theory]
[InlineData(false)]
[InlineData(true)]
public void Retry_OnRowFromADeletedSite_AllowedSystemWide_DeniedScoped(bool siteScoped)
{
var orphanId = Guid.Parse("33333333-3333-3333-3333-333333333333");
_queryReply = _queryReply with
{
SiteCalls = new List<SiteCallSummary>
{
new(orphanId, "deleted-site", "ApiOutbound", "ERP.Orphan", "Parked",
RetryCount: 5, LastError: "HTTP 503", HttpStatus: 503,
CreatedAtUtc: DateTime.UtcNow.AddMinutes(-30), UpdatedAtUtc: DateTime.UtcNow.AddMinutes(-5),
TerminalAtUtc: null, IsStuck: false),
},
};
if (siteScoped)
{
var claims = new[]
{
new Claim(JwtTokenService.UsernameClaimType, "tester"),
new Claim(JwtTokenService.RoleClaimType, "Deployer"),
new Claim(JwtTokenService.SiteIdClaimType, "1"),
};
Services.AddSingleton<AuthenticationStateProvider>(
new TestAuthStateProvider(new ClaimsPrincipal(new ClaimsIdentity(claims, "TestAuth"))));
}
var cut = Render<SiteCallsReportPage>();
cut.WaitForState(() => cut.Markup.Contains("ERP.Orphan"));
cut.FindAll("tbody tr").First(r => r.TextContent.Contains("ERP.Orphan"))
.QuerySelectorAll("button").First(b => b.TextContent.Contains("Retry")).Click();
if (siteScoped)
{
cut.WaitForAssertion(() => Assert.Contains("not permitted to act on cached calls", cut.Markup));
Assert.Empty(_retryRequests);
}
else
{
cut.WaitForAssertion(() => Assert.Equal(orphanId, Assert.Single(_retryRequests).TrackedOperationId));
}
}
/// <summary>Verifies that clicking Discard on a parked row relays the discard to the owning site.</summary> /// <summary>Verifies that clicking Discard on a parked row relays the discard to the owning site.</summary>
[Fact] [Fact]
public void ClickDiscard_OnParkedRow_RelaysDiscardToOwningSite() public void ClickDiscard_OnParkedRow_RelaysDiscardToOwningSite()