Skip to content

feature/AB#32325-BackgroundJobAuditing#2142

Merged
JamesPasta merged 2 commits into
devfrom
feature/AB#32325-AddInBacgroundJobCurrentUser
Mar 18, 2026
Merged

feature/AB#32325-BackgroundJobAuditing#2142
JamesPasta merged 2 commits into
devfrom
feature/AB#32325-AddInBacgroundJobCurrentUser

Conversation

@JamesPasta
Copy link
Copy Markdown
Collaborator

No description provided.

@JamesPasta JamesPasta requested a review from Copilot March 18, 2026 01:27
@JamesPasta JamesPasta marked this pull request as draft March 18, 2026 01:28
@github-actions
Copy link
Copy Markdown

🧪 Unit Test Results (Parallel Execution)

Tests

📊 Summary

Result Count
✅ Passed 561
❌ Failed 0
⚠️ Skipped 0

📄 HTML Reports

  • Merged Tests (HTML): Included in artifacts
    Generated automatically by CI.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces infrastructure to ensure ABP auditing/entity-change tracking works for background jobs and RabbitMQ message consumers, including establishing tenant + principal context and forcing audit collection when running outside HTTP requests.

Changes:

  • Enable ABP auditing for anonymous users/integration services and extend explicit entity-history selection to include Payment-related types.
  • Add SharedKernel utilities and ABP auditing overrides to force auditing during background job / message consumer execution.
  • Wrap tenanted queue message consumption in an audit scope with tenant + principal context; update payment queue messages/consumers to use the new tenanted message contract.

Reviewed changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
applications/Unity.GrantManager/src/Unity.GrantManager.Web/GrantManagerWebModule.cs Enables auditing for anonymous/integration services and expands entity history selection (incl. Payment).
applications/Unity.GrantManager/src/Unity.GrantManager.EntityFrameworkCore/Unity.GrantManager.EntityFrameworkCore.csproj Adds an additional project reference (Domain.Shared).
applications/Unity.GrantManager/src/Unity.GrantManager.Domain/GrantManagerDomainModule.cs Adds dependency on the new auditing override module.
applications/Unity.GrantManager/src/Unity.GrantManager.Domain/GrantManagerDataSeederContributor.cs Seeds a background-job identity user/person for auditing attribution.
applications/Unity.GrantManager/modules/Unity.SharedKernel/Utils/BackgroundJobExecutionContext.cs Async-flow marker to detect background job execution for auditing overrides.
applications/Unity.GrantManager/modules/Unity.SharedKernel/Utils/BackgroundJobContext.cs Helper to set tenant/principal + audit scope for background executions.
applications/Unity.GrantManager/modules/Unity.SharedKernel/MessageBrokers.RabbitMQ/QueueConsumerHandler.cs Wraps tenanted messages in background-job auditing context and persists audit logs.
applications/Unity.GrantManager/modules/Unity.SharedKernel/MessageBrokers.RabbitMQ/Interfaces/ITenantedQueueMessage.cs New interface to mark messages as carrying tenant context.
applications/Unity.GrantManager/modules/Unity.SharedKernel/Constants/BackgroundJobConstants.cs Adds well-known identifiers/claims constants for background-job user attribution.
applications/Unity.GrantManager/modules/Unity.SharedKernel/Auditing/UnityAuditingOverrideModule.cs Registers auditing overrides (helper + audit property setter).
applications/Unity.GrantManager/modules/Unity.SharedKernel/Auditing/UnityAuditingHelper.cs Forces audit saving/entity history while in background job context.
applications/Unity.GrantManager/modules/Unity.SharedKernel/Auditing/BackgroundJobAuditPropertySetter.cs Fallback audit property setter for background-job executions.
applications/Unity.GrantManager/modules/Unity.Payments/src/Unity.Payments.Application/PaymentsApplicationModule.cs Adds dependency on the auditing override module.
applications/Unity.GrantManager/modules/Unity.Payments/src/Unity.Payments.Application/PaymentRequests/CasPaymentRequestCoordinator.cs Refactors CAS payment status update flow to rely on caller-established tenant/audit scope.
applications/Unity.GrantManager/modules/Unity.Payments/src/Unity.Payments.Application/Integrations/RabbitMQ/ReconciliationConsumer.cs Removes manual tenant switching; relies on QueueConsumerHandler auditing/tenant wrapper.
applications/Unity.GrantManager/modules/Unity.Payments/src/Unity.Payments.Application/Integrations/RabbitMQ/InvoiceConsumer.cs Removes manual tenant switching; relies on QueueConsumerHandler auditing/tenant wrapper.
applications/Unity.GrantManager/modules/Unity.Payments/src/Unity.Payments.Application/Integrations/RabbitMQ/QueueMessages/ReconcilePaymentMessages.cs Marks reconcile messages as tenanted for automatic context setup.
applications/Unity.GrantManager/modules/Unity.Payments/src/Unity.Payments.Application/Integrations/RabbitMQ/QueueMessages/InvoiceMessages.cs Marks invoice messages as tenanted for automatic context setup.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

[DependsOn(
typeof(AbpAuditingModule)
)]
public class UnityAuditingOverideModule : AbpModule
Comment on lines +26 to +27
_isActive.Value = true;
return new DisposeAction(() => _isActive.Value = false);
new Claim(ClaimTypes.NameIdentifier, effectiveUserId.ToString()), // Standard claim for user ID
new Claim(AbpClaimTypes.UserName, BackgroundJobConstants.BackgroundJobUserName),
new Claim(AbpClaimTypes.Email, BackgroundJobConstants.BackgroundJobEmail),
new Claim(AbpClaimTypes.TenantId, tenantId?.ToString() ?? string.Empty),
Comment on lines +88 to +90
foreach (var disposable in _disposables)
{
disposable?.Dispose();
Comment on lines +93 to +134
@@ -105,6 +116,58 @@ private async Task HandleMessage(object sender, BasicDeliverEventArgs ea)
}
}

/// <summary>
/// Wraps consumer execution in a background-job auditing scope, mirroring the way
/// ASP.NET Core middleware wraps controller actions. Tenant context, identity, and
/// audit persistence are handled here so individual consumers stay free of
/// infrastructure concerns.
/// </summary>
private async Task ConsumeWithAuditingAsync(IServiceScope consumerScope, ITenantedQueueMessage tenantedMessage, TQueueMessage message)
{
var auditingManager = consumerScope.ServiceProvider.GetRequiredService<IAuditingManager>();
var principalAccessor = consumerScope.ServiceProvider.GetRequiredService<ICurrentPrincipalAccessor>();
var currentTenant = consumerScope.ServiceProvider.GetRequiredService<ICurrentTenant>();
var auditingStore = consumerScope.ServiceProvider.GetRequiredService<IAuditingStore>();

using (BackgroundJobExecutionContext.Use())
using (BackgroundJobContext.Set(auditingManager, principalAccessor, currentTenant, tenantedMessage.TenantId))
{
@github-actions
Copy link
Copy Markdown

🧪 Unit Test Results (Parallel Execution)

Tests

📊 Summary

Result Count
✅ Passed 561
❌ Failed 0
⚠️ Skipped 0

📄 HTML Reports

  • Merged Tests (HTML): Included in artifacts
    Generated automatically by CI.

@JamesPasta JamesPasta requested a review from Copilot March 18, 2026 21:50
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds infrastructure to capture ABP audit logs/entity history for background jobs and RabbitMQ message consumers by establishing a tenant + principal + audit scope during consumer execution and forcing auditing when running in a background-job context.

Changes:

  • Enable/extend auditing + entity history selection (including Payments) in the web host.
  • Introduce SharedKernel utilities/modules to force ABP auditing in background-job contexts and seed a well-known “background job” user/person per tenant.
  • Update RabbitMQ consumer pipeline and Payments consumers/messages to run under tenant-aware auditing scope.

Reviewed changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
applications/Unity.GrantManager/src/Unity.GrantManager.Web/GrantManagerWebModule.cs Enables auditing for anonymous/integration services and expands entity history selection to include payment-related entities.
applications/Unity.GrantManager/src/Unity.GrantManager.EntityFrameworkCore/Unity.GrantManager.EntityFrameworkCore.csproj Adds a project reference to Domain.Shared.
applications/Unity.GrantManager/src/Unity.GrantManager.Domain/GrantManagerDomainModule.cs Adds dependency on SharedKernel auditing override module.
applications/Unity.GrantManager/src/Unity.GrantManager.Domain/GrantManagerDataSeederContributor.cs Seeds a background-job IdentityUser + Person record per tenant for audit attribution.
applications/Unity.GrantManager/modules/Unity.SharedKernel/Utils/BackgroundJobExecutionContext.cs AsyncLocal marker to identify background-job execution across async boundaries.
applications/Unity.GrantManager/modules/Unity.SharedKernel/Utils/BackgroundJobContext.cs Helper to set tenant/principal and start an audit scope for jobs/consumers.
applications/Unity.GrantManager/modules/Unity.SharedKernel/MessageBrokers.RabbitMQ/QueueConsumerHandler.cs Wraps tenant-aware messages in background-job auditing context and persists audit logs when entity changes occur.
applications/Unity.GrantManager/modules/Unity.SharedKernel/MessageBrokers.RabbitMQ/Interfaces/ITenantedQueueMessage.cs New interface for queue messages that carry TenantId to trigger auditing/tenant setup.
applications/Unity.GrantManager/modules/Unity.SharedKernel/Constants/BackgroundJobConstants.cs Well-known IDs/claims for background job “user/person”.
applications/Unity.GrantManager/modules/Unity.SharedKernel/Auditing/UnityAuditingOverrideModule.cs Replaces ABP auditing services to force auditing during background-job execution.
applications/Unity.GrantManager/modules/Unity.SharedKernel/Auditing/UnityAuditingHelper.cs Forces ShouldSaveAudit and entity history when BackgroundJobExecutionContext is active.
applications/Unity.GrantManager/modules/Unity.SharedKernel/Auditing/BackgroundJobAuditPropertySetter.cs Ensures audited entity creator/modifier IDs are set in background job contexts.
applications/Unity.GrantManager/modules/Unity.Payments/src/Unity.Payments.Application/PaymentsApplicationModule.cs Adds dependency on the auditing override module for the Payments application module.
applications/Unity.GrantManager/modules/Unity.Payments/src/Unity.Payments.Application/PaymentRequests/CasPaymentRequestCoordinator.cs Refactors update flow to rely on caller-established tenant/audit context and ensures UOW completion for entity change capture.
applications/Unity.GrantManager/modules/Unity.Payments/src/Unity.Payments.Application/Integrations/RabbitMQ/ReconciliationConsumer.cs Removes manual tenant switching; relies on QueueConsumerHandler to set tenant/audit scope.
applications/Unity.GrantManager/modules/Unity.Payments/src/Unity.Payments.Application/Integrations/RabbitMQ/QueueMessages/ReconcilePaymentMessages.cs Marks message as tenant-aware (implements ITenantedQueueMessage).
applications/Unity.GrantManager/modules/Unity.Payments/src/Unity.Payments.Application/Integrations/RabbitMQ/QueueMessages/InvoiceMessages.cs Marks message as tenant-aware (implements ITenantedQueueMessage).
applications/Unity.GrantManager/modules/Unity.Payments/src/Unity.Payments.Application/Integrations/RabbitMQ/InvoiceConsumer.cs Removes manual tenant switching; relies on QueueConsumerHandler to set tenant/audit scope.
Comments suppressed due to low confidence (2)

applications/Unity.GrantManager/src/Unity.GrantManager.Web/GrantManagerWebModule.cs:180

  • Typo in selector name: "ExplictEntityAudit" looks like it should be "ExplicitEntityAudit" for readability/searchability (even though it’s just a label).
                 "ExplictEntityAudit",
                 type =>

applications/Unity.GrantManager/modules/Unity.Payments/src/Unity.Payments.Application/PaymentsApplicationModule.cs:28

  • The [DependsOn] list contains AbpVirtualFileSystemModule twice. This is redundant and can be removed to keep module dependencies tidy.
[DependsOn(
    typeof(UnityAuditingOverrideModule),
    typeof(AbpVirtualFileSystemModule),
    typeof(AbpDddApplicationModule),
    typeof(AbpAutoMapperModule),
    typeof(AbpVirtualFileSystemModule),
    typeof(PaymentsApplicationContractsModule),

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines +11 to +14
/// Processes payment reconciliation messages from RabbitMQ.
/// Tenant context and audit scope are established by <see cref="QueueConsumerHandler{TMessageConsumer,TQueueMessage}"/>
/// before this consumer is invoked — no manual wiring needed here.
/// </summary>
)
);

options.IsEnabledForAnonymousUsers = true;
Comment on lines 22 to 24
[DependsOn(
typeof(UnityAuditingOverrideModule),
typeof(GrantManagerDomainSharedModule),
/// <summary>
/// Updates payment request status from CAS integration results.
/// Tenant context and audit scope are already established by the caller
/// (via <see cref="QueueConsumerHandler{TMessageConsumer,TQueueMessage}"/>);
Comment on lines 6 to +20
using Unity.GrantManager.Identity;
using Unity.Modules.Shared.Constants;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Identity;
using Volo.Abp.MultiTenancy;

namespace Unity.GrantManager;

public class GrantManagerDataSeederContributor(
IApplicationStatusRepository applicationStatusRepository,
IPersonRepository personRepository) : IDataSeedContributor, ITransientDependency
IPersonRepository personRepository,
IIdentityUserRepository userRepository,
ICurrentTenant currentTenant) : IDataSeedContributor, ITransientDependency
Comment on lines +18 to +23
using Unity.Modules.Shared.Auditing;

namespace Unity.Payments;

[DependsOn(
typeof(UnityAuditingOverrideModule),
ICurrentTenant currentTenant) : IQueueConsumer<InvoiceMessages>
/// <summary>
/// Processes invoice creation messages from RabbitMQ.
/// Tenant context and audit scope are established by <see cref="QueueConsumerHandler{TMessageConsumer,TQueueMessage}"/>
@JamesPasta JamesPasta marked this pull request as ready for review March 18, 2026 22:33
@JamesPasta JamesPasta merged commit d2e7089 into dev Mar 18, 2026
26 checks passed
@JamesPasta JamesPasta deleted the feature/AB#32325-AddInBacgroundJobCurrentUser branch March 18, 2026 22:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants