| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using Microsoft.EntityFrameworkCore.ChangeTracking; |
| | | 3 | | using Microsoft.Extensions.Options; |
| | | 4 | | using ProjectTemplate.Infrastructure.Data.Auditing; |
| | | 5 | | using ProjectTemplate.Infrastructure.Data.Entities; |
| | | 6 | | using ProjectTemplate.Infrastructure.Data.Options; |
| | | 7 | | |
| | | 8 | | namespace ProjectTemplate.Infrastructure.Data; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Applies application-owned mutation, normalization, concurrency, and audit preparation during EF Core saves. |
| | | 12 | | /// </summary> |
| | | 13 | | public sealed class ApplicationSaveChangesPipeline : |
| | | 14 | | IApplicationSaveChangesPipeline, |
| | | 15 | | IApplicationMutationAuditReceiptAccessor |
| | | 16 | | { |
| | | 17 | | private readonly ICurrentActorAccessor _currentActorAccessor; |
| | | 18 | | private readonly IApplicationAuditContextAccessor? _auditContextAccessor; |
| | | 19 | | private readonly IApplicationAuditValuePolicy _auditValuePolicy; |
| | | 20 | | private readonly IApplicationMutationManifestBuilder _manifestBuilder; |
| | | 21 | | private readonly IApplicationMutationManifestHasher _manifestHasher; |
| | | 22 | | private readonly bool _auditOptions; |
| | | 23 | | private readonly IApplicationAuditStore _auditStore; |
| | 154 | 24 | | private List<AuditEntry> _pendingAuditEntries = []; |
| | 154 | 25 | | private readonly List<AuditRecord> _activeAuditRecords = []; |
| | | 26 | | private ApplicationAuditContext? _activeAuditContext; |
| | | 27 | | private string? _activeMutationBatchId; |
| | | 28 | | private int _activeAuditRecordCount; |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Initializes a new instance of the <see cref="ApplicationSaveChangesPipeline" /> class. |
| | | 32 | | /// </summary> |
| | 154 | 33 | | public ApplicationSaveChangesPipeline( |
| | 154 | 34 | | ICurrentActorAccessor currentActorAccessor, |
| | 154 | 35 | | IOptions<DataAccessOptions> dataAccessOptions, |
| | 154 | 36 | | IApplicationAuditStore? auditStore = null, |
| | 154 | 37 | | IApplicationAuditContextAccessor? auditContextAccessor = null, |
| | 154 | 38 | | IApplicationAuditValuePolicy? auditValuePolicy = null, |
| | 154 | 39 | | IApplicationMutationManifestBuilder? manifestBuilder = null, |
| | 154 | 40 | | IApplicationMutationManifestHasher? manifestHasher = null) |
| | | 41 | | { |
| | 154 | 42 | | ArgumentNullException.ThrowIfNull(currentActorAccessor); |
| | 154 | 43 | | ArgumentNullException.ThrowIfNull(dataAccessOptions); |
| | | 44 | | |
| | 154 | 45 | | _currentActorAccessor = currentActorAccessor; |
| | 154 | 46 | | _auditContextAccessor = auditContextAccessor; |
| | 154 | 47 | | _auditValuePolicy = auditValuePolicy ?? new DefaultApplicationAuditValuePolicy(); |
| | 154 | 48 | | _manifestBuilder = manifestBuilder ?? new CanonicalApplicationMutationManifestBuilder(); |
| | 154 | 49 | | _manifestHasher = manifestHasher ?? new Sha256ApplicationMutationManifestHasher(); |
| | 154 | 50 | | _auditOptions = dataAccessOptions.Value.Auditing.Enabled; |
| | 154 | 51 | | _auditStore = ResolveAuditStore(dataAccessOptions.Value.Auditing, auditStore); |
| | 154 | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <inheritdoc /> |
| | 90 | 55 | | public ApplicationMutationAuditReceipt? LastCompletedReceipt { get; private set; } |
| | | 56 | | |
| | | 57 | | /// <inheritdoc /> |
| | | 58 | | public bool ApplyBeforeSaveChanges(ApplicationDbContext dbContext) |
| | | 59 | | { |
| | 14 | 60 | | ArgumentNullException.ThrowIfNull(dbContext); |
| | 14 | 61 | | ResetActiveAuditState(); |
| | | 62 | | |
| | 14 | 63 | | IReadOnlyList<EntityEntry> entries = GetSavePipelineEntries(dbContext); |
| | 14 | 64 | | if (entries.Count == 0) |
| | | 65 | | { |
| | 2 | 66 | | return false; |
| | | 67 | | } |
| | | 68 | | |
| | 12 | 69 | | ApplyPersistedStringCanonicalization(entries); |
| | 12 | 70 | | ApplyLookupStringNormalization(entries); |
| | 12 | 71 | | ApplyTimestampNormalization(entries); |
| | 12 | 72 | | ApplyConcurrencyStamps(entries); |
| | | 73 | | |
| | 12 | 74 | | if (_auditOptions) |
| | | 75 | | { |
| | 4 | 76 | | _pendingAuditEntries = OnBeforeSaveChanges(dbContext, entries); |
| | | 77 | | } |
| | | 78 | | |
| | 12 | 79 | | return true; |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <inheritdoc /> |
| | | 83 | | public async ValueTask<bool> ApplyBeforeSaveChangesAsync( |
| | | 84 | | ApplicationDbContext dbContext, |
| | | 85 | | CancellationToken cancellationToken = default) |
| | | 86 | | { |
| | 134 | 87 | | ArgumentNullException.ThrowIfNull(dbContext); |
| | 134 | 88 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 134 | 89 | | ResetActiveAuditState(); |
| | | 90 | | |
| | 134 | 91 | | IReadOnlyList<EntityEntry> entries = GetSavePipelineEntries(dbContext); |
| | 134 | 92 | | if (entries.Count == 0) |
| | | 93 | | { |
| | 2 | 94 | | return false; |
| | | 95 | | } |
| | | 96 | | |
| | 132 | 97 | | ApplyPersistedStringCanonicalization(entries); |
| | 132 | 98 | | ApplyLookupStringNormalization(entries); |
| | 132 | 99 | | ApplyTimestampNormalization(entries); |
| | 132 | 100 | | ApplyConcurrencyStamps(entries); |
| | | 101 | | |
| | 132 | 102 | | if (_auditOptions) |
| | | 103 | | { |
| | 50 | 104 | | _pendingAuditEntries = await OnBeforeSaveChangesAsync( |
| | 50 | 105 | | dbContext, |
| | 50 | 106 | | entries, |
| | 50 | 107 | | cancellationToken) |
| | 50 | 108 | | .ConfigureAwait(false); |
| | | 109 | | } |
| | | 110 | | |
| | 132 | 111 | | return true; |
| | 134 | 112 | | } |
| | | 113 | | |
| | | 114 | | /// <inheritdoc /> |
| | | 115 | | public bool ApplyAfterSaveChanges(ApplicationDbContext dbContext) |
| | | 116 | | { |
| | 12 | 117 | | ArgumentNullException.ThrowIfNull(dbContext); |
| | 12 | 118 | | bool appendedAdditionalAuditRecords = false; |
| | | 119 | | |
| | 24 | 120 | | foreach (AuditEntry auditEntry in _pendingAuditEntries) |
| | | 121 | | { |
| | 0 | 122 | | CompleteTemporaryAuditProperties(auditEntry); |
| | 0 | 123 | | AppendAuditRecord(dbContext, auditEntry.ToAuditRecord()); |
| | 0 | 124 | | appendedAdditionalAuditRecords = true; |
| | | 125 | | } |
| | | 126 | | |
| | 12 | 127 | | CompleteMutationReceipt(); |
| | 12 | 128 | | _pendingAuditEntries = []; |
| | 12 | 129 | | return appendedAdditionalAuditRecords; |
| | | 130 | | } |
| | | 131 | | |
| | | 132 | | /// <inheritdoc /> |
| | | 133 | | public async ValueTask<bool> ApplyAfterSaveChangesAsync( |
| | | 134 | | ApplicationDbContext dbContext, |
| | | 135 | | CancellationToken cancellationToken = default) |
| | | 136 | | { |
| | 130 | 137 | | ArgumentNullException.ThrowIfNull(dbContext); |
| | 130 | 138 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 130 | 139 | | bool appendedAdditionalAuditRecords = false; |
| | | 140 | | |
| | 266 | 141 | | foreach (AuditEntry auditEntry in _pendingAuditEntries) |
| | | 142 | | { |
| | 4 | 143 | | CompleteTemporaryAuditProperties(auditEntry); |
| | 4 | 144 | | await AppendAuditRecordAsync(dbContext, auditEntry.ToAuditRecord(), cancellationToken) |
| | 4 | 145 | | .ConfigureAwait(false); |
| | 2 | 146 | | appendedAdditionalAuditRecords = true; |
| | | 147 | | } |
| | | 148 | | |
| | 128 | 149 | | CompleteMutationReceipt(); |
| | 128 | 150 | | _pendingAuditEntries = []; |
| | 128 | 151 | | return appendedAdditionalAuditRecords; |
| | 128 | 152 | | } |
| | | 153 | | |
| | | 154 | | private void ResetActiveAuditState() |
| | | 155 | | { |
| | 148 | 156 | | _pendingAuditEntries = []; |
| | 148 | 157 | | _activeAuditRecords.Clear(); |
| | 148 | 158 | | _activeAuditContext = null; |
| | 148 | 159 | | _activeMutationBatchId = null; |
| | 148 | 160 | | _activeAuditRecordCount = 0; |
| | 148 | 161 | | } |
| | | 162 | | |
| | | 163 | | private static IReadOnlyList<EntityEntry> GetSavePipelineEntries(ApplicationDbContext dbContext) |
| | | 164 | | { |
| | 148 | 165 | | dbContext.ChangeTracker.DetectChanges(); |
| | 148 | 166 | | return [.. dbContext.ChangeTracker |
| | 148 | 167 | | .Entries() |
| | 322 | 168 | | .Where(entry => entry.State is EntityState.Added or EntityState.Modified or EntityState.Deleted)]; |
| | | 169 | | } |
| | | 170 | | |
| | | 171 | | private static void ApplyPersistedStringCanonicalization(IEnumerable<EntityEntry> entries) |
| | | 172 | | { |
| | 616 | 173 | | foreach (EntityEntry entry in entries) |
| | | 174 | | { |
| | 164 | 175 | | if (entry.Entity is AuditRecord || |
| | 164 | 176 | | entry.State is not (EntityState.Added or EntityState.Modified)) |
| | | 177 | | { |
| | | 178 | | continue; |
| | | 179 | | } |
| | | 180 | | |
| | 4532 | 181 | | foreach (PropertyEntry property in entry.Properties) |
| | | 182 | | { |
| | 2126 | 183 | | if (property.Metadata.ClrType != typeof(string) || |
| | 2126 | 184 | | property.Metadata.IsPrimaryKey() || |
| | 2126 | 185 | | property.Metadata.IsConcurrencyToken || |
| | 2126 | 186 | | (entry.State == EntityState.Modified && !property.IsModified) || |
| | 2126 | 187 | | property.CurrentValue is not string currentValue) |
| | | 188 | | { |
| | | 189 | | continue; |
| | | 190 | | } |
| | | 191 | | |
| | 760 | 192 | | string canonicalValue = PersistenceStringCanonicalizer.Canonicalize(currentValue); |
| | 760 | 193 | | if (!string.Equals(canonicalValue, currentValue, StringComparison.Ordinal)) |
| | | 194 | | { |
| | 24 | 195 | | property.CurrentValue = canonicalValue; |
| | | 196 | | } |
| | | 197 | | } |
| | | 198 | | } |
| | 144 | 199 | | } |
| | | 200 | | |
| | | 201 | | private static void ApplyLookupStringNormalization(IEnumerable<EntityEntry> entries) |
| | | 202 | | { |
| | 616 | 203 | | foreach (EntityEntry entry in entries) |
| | | 204 | | { |
| | 164 | 205 | | if (entry.Entity is not ExternalLoginAccount account || |
| | 164 | 206 | | entry.State is not (EntityState.Added or EntityState.Modified)) |
| | | 207 | | { |
| | | 208 | | continue; |
| | | 209 | | } |
| | | 210 | | |
| | 106 | 211 | | SetPropertyCurrentValue(entry, nameof(ExternalLoginAccount.ProviderName), |
| | 106 | 212 | | PersistenceStringComparisonNormalizer.NormalizeRequiredDisplayValue(account.ProviderName)); |
| | 106 | 213 | | SetPropertyCurrentValue(entry, nameof(ExternalLoginAccount.NormalizedProviderName), |
| | 106 | 214 | | PersistenceStringComparisonNormalizer.NormalizeRequiredLookupValue(account.ProviderName)); |
| | 106 | 215 | | SetPropertyCurrentValue(entry, nameof(ExternalLoginAccount.ProviderUserId), |
| | 106 | 216 | | PersistenceStringComparisonNormalizer.NormalizeRequiredDisplayValue(account.ProviderUserId)); |
| | 106 | 217 | | SetPropertyCurrentValue(entry, nameof(ExternalLoginAccount.DisplayName), |
| | 106 | 218 | | PersistenceStringComparisonNormalizer.NormalizeOptionalDisplayValue(account.DisplayName)); |
| | 106 | 219 | | SetPropertyCurrentValue(entry, nameof(ExternalLoginAccount.Email), |
| | 106 | 220 | | PersistenceStringComparisonNormalizer.NormalizeOptionalDisplayValue(account.Email)); |
| | 106 | 221 | | SetPropertyCurrentValue(entry, nameof(ExternalLoginAccount.NormalizedEmail), |
| | 106 | 222 | | PersistenceStringComparisonNormalizer.NormalizeOptionalLookupValue(account.Email)); |
| | | 223 | | } |
| | 144 | 224 | | } |
| | | 225 | | |
| | | 226 | | private static void ApplyTimestampNormalization(IEnumerable<EntityEntry> entries) |
| | | 227 | | { |
| | 616 | 228 | | foreach (EntityEntry entry in entries) |
| | | 229 | | { |
| | 164 | 230 | | if (entry.State is not (EntityState.Added or EntityState.Modified)) |
| | | 231 | | { |
| | | 232 | | continue; |
| | | 233 | | } |
| | | 234 | | |
| | 5544 | 235 | | foreach (PropertyEntry property in entry.Properties) |
| | | 236 | | { |
| | 2610 | 237 | | if (!IsUtcTimestampProperty(property.Metadata.Name)) |
| | | 238 | | { |
| | | 239 | | continue; |
| | | 240 | | } |
| | | 241 | | |
| | 510 | 242 | | Type propertyType = Nullable.GetUnderlyingType(property.Metadata.ClrType) |
| | 510 | 243 | | ?? property.Metadata.ClrType; |
| | | 244 | | |
| | 510 | 245 | | if (propertyType == typeof(DateTime) && property.CurrentValue is DateTime dateTimeValue) |
| | | 246 | | { |
| | 236 | 247 | | property.CurrentValue = PersistenceTimestamp.NormalizeUtc(dateTimeValue); |
| | | 248 | | } |
| | 274 | 249 | | else if (propertyType == typeof(DateTimeOffset) && property.CurrentValue is DateTimeOffset dateTimeOffse |
| | | 250 | | { |
| | 0 | 251 | | property.CurrentValue = PersistenceTimestamp.NormalizeUtc(dateTimeOffsetValue); |
| | | 252 | | } |
| | | 253 | | } |
| | | 254 | | } |
| | 144 | 255 | | } |
| | | 256 | | |
| | | 257 | | private static void ApplyConcurrencyStamps(IEnumerable<EntityEntry> entries) |
| | | 258 | | { |
| | 616 | 259 | | foreach (EntityEntry entry in entries) |
| | | 260 | | { |
| | 164 | 261 | | if (entry.Entity is not DataEntity entity) |
| | | 262 | | { |
| | | 263 | | continue; |
| | | 264 | | } |
| | | 265 | | |
| | 164 | 266 | | PropertyEntry concurrencyStampProperty = entry.Property(nameof(DataEntity.ConcurrencyStamp)); |
| | 164 | 267 | | if (entry.State == EntityState.Added) |
| | | 268 | | { |
| | 136 | 269 | | if (string.IsNullOrWhiteSpace(entity.ConcurrencyStamp)) |
| | | 270 | | { |
| | 6 | 271 | | concurrencyStampProperty.CurrentValue = DataEntity.NewConcurrencyStamp(); |
| | | 272 | | } |
| | | 273 | | } |
| | 28 | 274 | | else if (entry.State == EntityState.Modified) |
| | | 275 | | { |
| | 26 | 276 | | concurrencyStampProperty.CurrentValue = DataEntity.NewConcurrencyStamp(); |
| | | 277 | | } |
| | | 278 | | } |
| | 144 | 279 | | } |
| | | 280 | | |
| | | 281 | | private static bool IsUtcTimestampProperty(string propertyName) |
| | | 282 | | { |
| | 2610 | 283 | | return propertyName.EndsWith("Utc", StringComparison.Ordinal); |
| | | 284 | | } |
| | | 285 | | |
| | | 286 | | private List<AuditEntry> OnBeforeSaveChanges( |
| | | 287 | | ApplicationDbContext dbContext, |
| | | 288 | | IEnumerable<EntityEntry> entries) |
| | | 289 | | { |
| | 4 | 290 | | List<AuditEntry> auditEntries = CreateAuditEntries(entries); |
| | 20 | 291 | | foreach (AuditEntry auditEntry in auditEntries.Where(entry => !entry.HasTemporaryProperties)) |
| | | 292 | | { |
| | 4 | 293 | | AppendAuditRecord(dbContext, auditEntry.ToAuditRecord()); |
| | | 294 | | } |
| | | 295 | | |
| | 8 | 296 | | return [.. auditEntries.Where(entry => entry.HasTemporaryProperties)]; |
| | | 297 | | } |
| | | 298 | | |
| | | 299 | | private async ValueTask<List<AuditEntry>> OnBeforeSaveChangesAsync( |
| | | 300 | | ApplicationDbContext dbContext, |
| | | 301 | | IEnumerable<EntityEntry> entries, |
| | | 302 | | CancellationToken cancellationToken) |
| | | 303 | | { |
| | 50 | 304 | | List<AuditEntry> auditEntries = CreateAuditEntries(entries); |
| | | 305 | | foreach (AuditEntry auditEntry in auditEntries.Where(entry => !entry.HasTemporaryProperties)) |
| | | 306 | | { |
| | 48 | 307 | | await AppendAuditRecordAsync(dbContext, auditEntry.ToAuditRecord(), cancellationToken) |
| | 48 | 308 | | .ConfigureAwait(false); |
| | | 309 | | } |
| | | 310 | | |
| | | 311 | | return [.. auditEntries.Where(entry => entry.HasTemporaryProperties)]; |
| | 50 | 312 | | } |
| | | 313 | | |
| | | 314 | | private void AppendAuditRecord(ApplicationDbContext dbContext, AuditRecord auditRecord) |
| | | 315 | | { |
| | 4 | 316 | | _auditStore.Append(dbContext, auditRecord); |
| | 4 | 317 | | _activeAuditRecords.Add(auditRecord); |
| | 4 | 318 | | } |
| | | 319 | | |
| | | 320 | | private async ValueTask AppendAuditRecordAsync( |
| | | 321 | | ApplicationDbContext dbContext, |
| | | 322 | | AuditRecord auditRecord, |
| | | 323 | | CancellationToken cancellationToken) |
| | | 324 | | { |
| | 52 | 325 | | await _auditStore.AppendAsync(dbContext, auditRecord, cancellationToken).ConfigureAwait(false); |
| | 50 | 326 | | _activeAuditRecords.Add(auditRecord); |
| | 50 | 327 | | } |
| | | 328 | | |
| | | 329 | | private List<AuditEntry> CreateAuditEntries(IEnumerable<EntityEntry> entries) |
| | | 330 | | { |
| | 54 | 331 | | var auditEntries = new List<AuditEntry>(); |
| | 54 | 332 | | ApplicationAuditContext auditContext = ResolveAuditContext(); |
| | 54 | 333 | | string mutationBatchId = Guid.NewGuid().ToString("N"); |
| | | 334 | | |
| | 232 | 335 | | foreach (EntityEntry entry in entries) |
| | | 336 | | { |
| | 62 | 337 | | if (entry.Entity is AuditRecord || |
| | 62 | 338 | | entry.State == EntityState.Detached || |
| | 62 | 339 | | entry.State == EntityState.Unchanged) |
| | | 340 | | { |
| | | 341 | | continue; |
| | | 342 | | } |
| | | 343 | | |
| | 56 | 344 | | AuditEntry auditEntry = new(entry) |
| | 56 | 345 | | { |
| | 56 | 346 | | TableName = entry.Metadata.GetTableName() ?? string.Empty, |
| | 56 | 347 | | ModifiedBy = auditContext.ActorDisplayName, |
| | 56 | 348 | | ActorId = auditContext.ActorId, |
| | 56 | 349 | | ActorType = auditContext.ActorType, |
| | 56 | 350 | | MutationBatchId = mutationBatchId, |
| | 56 | 351 | | OperationExecutionId = auditContext.OperationExecutionId, |
| | 56 | 352 | | ExecutionAttemptId = auditContext.ExecutionAttemptId, |
| | 56 | 353 | | CorrelationId = auditContext.CorrelationId, |
| | 56 | 354 | | TraceId = auditContext.TraceId, |
| | 56 | 355 | | SpanId = auditContext.SpanId, |
| | 56 | 356 | | DecisionAuditRecordId = auditContext.DecisionAuditRecordId, |
| | 56 | 357 | | TenantHash = auditContext.TenantHash, |
| | 56 | 358 | | OrganizationHash = auditContext.OrganizationHash, |
| | 56 | 359 | | ModifiedOnUtc = PersistenceTimestamp.UtcNow(), |
| | 56 | 360 | | State = entry.State.ToString() |
| | 56 | 361 | | }; |
| | 56 | 362 | | auditEntries.Add(auditEntry); |
| | | 363 | | |
| | 1464 | 364 | | foreach (PropertyEntry property in entry.Properties) |
| | | 365 | | { |
| | 676 | 366 | | if (property.IsTemporary) |
| | | 367 | | { |
| | 4 | 368 | | auditEntry.TemporaryProperties.Add(property); |
| | 4 | 369 | | continue; |
| | | 370 | | } |
| | | 371 | | |
| | 672 | 372 | | string propertyName = property.Metadata.Name; |
| | 672 | 373 | | if (property.Metadata.IsPrimaryKey()) |
| | | 374 | | { |
| | 52 | 375 | | AddProtectedValue(auditEntry.KeyValues, entry, propertyName, property.CurrentValue); |
| | 52 | 376 | | continue; |
| | | 377 | | } |
| | | 378 | | |
| | 620 | 379 | | switch (entry.State) |
| | | 380 | | { |
| | | 381 | | case EntityState.Added: |
| | 554 | 382 | | AddProtectedValue(auditEntry.CurrentValues, entry, propertyName, property.CurrentValue); |
| | 554 | 383 | | break; |
| | | 384 | | case EntityState.Deleted: |
| | 22 | 385 | | AddProtectedValue(auditEntry.OriginalValues, entry, propertyName, property.OriginalValue); |
| | 22 | 386 | | break; |
| | 44 | 387 | | case EntityState.Modified when property.IsModified: |
| | 10 | 388 | | AddProtectedValue(auditEntry.OriginalValues, entry, propertyName, property.OriginalValue); |
| | 10 | 389 | | AddProtectedValue(auditEntry.CurrentValues, entry, propertyName, property.CurrentValue); |
| | 10 | 390 | | break; |
| | | 391 | | case EntityState.Modified: |
| | | 392 | | case EntityState.Detached: |
| | | 393 | | case EntityState.Unchanged: |
| | | 394 | | break; |
| | | 395 | | default: |
| | 0 | 396 | | throw new InvalidOperationException($"Unsupported entity state '{entry.State}'."); |
| | | 397 | | } |
| | | 398 | | } |
| | | 399 | | } |
| | | 400 | | |
| | 54 | 401 | | if (auditEntries.Count > 0) |
| | | 402 | | { |
| | 48 | 403 | | _activeAuditContext = auditContext; |
| | 48 | 404 | | _activeMutationBatchId = mutationBatchId; |
| | 48 | 405 | | _activeAuditRecordCount = auditEntries.Count; |
| | | 406 | | } |
| | | 407 | | |
| | 54 | 408 | | return auditEntries; |
| | | 409 | | } |
| | | 410 | | |
| | | 411 | | private ApplicationAuditContext ResolveAuditContext() |
| | | 412 | | { |
| | 54 | 413 | | if (_auditContextAccessor is not null) |
| | | 414 | | { |
| | 6 | 415 | | return _auditContextAccessor.Current |
| | 6 | 416 | | ?? throw new InvalidOperationException("The application audit context accessor returned no context."); |
| | | 417 | | } |
| | | 418 | | |
| | 48 | 419 | | string displayActor = _currentActorAccessor.CurrentActor; |
| | 48 | 420 | | string actorId = string.IsNullOrWhiteSpace(displayActor) ? "Unknown" : displayActor.Trim(); |
| | 48 | 421 | | return new ApplicationAuditContext( |
| | 48 | 422 | | actorId, |
| | 48 | 423 | | actorId.Equals(SystemCurrentActorAccessor.ActorName, StringComparison.Ordinal) |
| | 48 | 424 | | ? ApplicationAuditActorTypes.System |
| | 48 | 425 | | : ApplicationAuditActorTypes.Unknown, |
| | 48 | 426 | | actorId); |
| | | 427 | | } |
| | | 428 | | |
| | | 429 | | private void AddProtectedValue( |
| | | 430 | | Dictionary<string, object> target, |
| | | 431 | | EntityEntry entry, |
| | | 432 | | string propertyName, |
| | | 433 | | object? value) |
| | | 434 | | { |
| | 652 | 435 | | if (ApplicationAuditValueProtector.TryProtect( |
| | 652 | 436 | | _auditValuePolicy, |
| | 652 | 437 | | entry.Entity.GetType(), |
| | 652 | 438 | | propertyName, |
| | 652 | 439 | | value, |
| | 652 | 440 | | out object protectedValue)) |
| | | 441 | | { |
| | 648 | 442 | | target[propertyName] = protectedValue; |
| | | 443 | | } |
| | 652 | 444 | | } |
| | | 445 | | |
| | | 446 | | private static void SetPropertyCurrentValue(EntityEntry entry, string propertyName, object? value) |
| | | 447 | | { |
| | 636 | 448 | | entry.Property(propertyName).CurrentValue = value; |
| | 636 | 449 | | } |
| | | 450 | | |
| | | 451 | | private void CompleteTemporaryAuditProperties(AuditEntry auditEntry) |
| | | 452 | | { |
| | 16 | 453 | | foreach (PropertyEntry property in auditEntry.TemporaryProperties) |
| | | 454 | | { |
| | 4 | 455 | | if (property.Metadata.IsPrimaryKey()) |
| | | 456 | | { |
| | 4 | 457 | | AddProtectedValue(auditEntry.KeyValues, auditEntry.Entry, property.Metadata.Name, property.CurrentValue) |
| | | 458 | | } |
| | | 459 | | else |
| | | 460 | | { |
| | 0 | 461 | | AddProtectedValue(auditEntry.CurrentValues, auditEntry.Entry, property.Metadata.Name, property.CurrentVa |
| | | 462 | | } |
| | | 463 | | } |
| | 4 | 464 | | } |
| | | 465 | | |
| | | 466 | | private void CompleteMutationReceipt() |
| | | 467 | | { |
| | 140 | 468 | | if (_activeAuditContext is null || |
| | 140 | 469 | | string.IsNullOrWhiteSpace(_activeMutationBatchId) || |
| | 140 | 470 | | _activeAuditRecordCount == 0) |
| | | 471 | | { |
| | 96 | 472 | | return; |
| | | 473 | | } |
| | | 474 | | |
| | 44 | 475 | | if (_activeAuditRecords.Count != _activeAuditRecordCount) |
| | | 476 | | { |
| | 0 | 477 | | throw new InvalidOperationException("The mutation audit receipt cannot be completed because the retained aud |
| | | 478 | | } |
| | | 479 | | |
| | 44 | 480 | | ApplicationMutationManifest manifest = _manifestBuilder.Build(_activeAuditRecords); |
| | 44 | 481 | | string manifestHash = _manifestHasher.ComputeHash(manifest); |
| | | 482 | | |
| | 44 | 483 | | LastCompletedReceipt = new ApplicationMutationAuditReceipt( |
| | 44 | 484 | | _activeMutationBatchId, |
| | 44 | 485 | | _activeAuditRecordCount, |
| | 44 | 486 | | "Committed", |
| | 44 | 487 | | DateTimeOffset.UtcNow, |
| | 44 | 488 | | manifestHash, |
| | 44 | 489 | | _manifestHasher.Algorithm, |
| | 44 | 490 | | manifest.SchemaVersion, |
| | 44 | 491 | | _activeAuditContext.OperationExecutionId, |
| | 44 | 492 | | _activeAuditContext.ExecutionAttemptId, |
| | 44 | 493 | | _activeAuditContext.DecisionAuditRecordId, |
| | 44 | 494 | | _activeAuditContext.CorrelationId, |
| | 44 | 495 | | _activeAuditContext.TraceId); |
| | | 496 | | |
| | 44 | 497 | | _activeAuditContext = null; |
| | 44 | 498 | | _activeMutationBatchId = null; |
| | 44 | 499 | | _activeAuditRecordCount = 0; |
| | 44 | 500 | | _activeAuditRecords.Clear(); |
| | 44 | 501 | | } |
| | | 502 | | |
| | | 503 | | private static IApplicationAuditStore ResolveAuditStore( |
| | | 504 | | DataAuditingOptions auditingOptions, |
| | | 505 | | IApplicationAuditStore? auditStore) |
| | | 506 | | { |
| | 154 | 507 | | ArgumentNullException.ThrowIfNull(auditingOptions); |
| | 154 | 508 | | return auditStore is not null |
| | 154 | 509 | | ? auditStore |
| | 154 | 510 | | : !auditingOptions.Enabled || AuditStorageModes.IsLocal(auditingOptions.StorageMode) |
| | 154 | 511 | | ? new LocalApplicationAuditStore() |
| | 154 | 512 | | : throw new InvalidOperationException( |
| | 154 | 513 | | $"Application audit storage mode '{AuditStorageModes.Normalize(auditingOptions.StorageMode)}' requir |
| | | 514 | | } |
| | | 515 | | } |