Phases 3 & 4: Complete data access architecture migration
Phase 3 — eliminated ApplicationDbContext from all non-exempt controllers, routing all data access through IUnitOfWork. Added IPlainRepository<T> for the four platform entities (Announcement, BannedIp, DashboardTip, ReleaseNote) that intentionally don't extend BaseEntity and therefore can't use the constrained IRepository<T>. Added permanent-exception comments to the 18 controllers that legitimately retain direct DbContext access (Identity infra, cross-tenant platform ops, bulk streaming exports). Phase 4 — added EnforceDataAccessArchitecture() to Program.cs, a startup gate that reflects over every Controller subclass and throws at boot if any non-exempt controller injects ApplicationDbContext. The app cannot start with a violation. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -12,6 +12,7 @@ using PowderCoating.Application.Services;
|
||||
using PowderCoating.Core.Entities;
|
||||
using PowderCoating.Core.Enums;
|
||||
using PowderCoating.Core.Interfaces;
|
||||
using PowderCoating.Core.Interfaces.Services;
|
||||
using PowderCoating.Infrastructure.Data;
|
||||
using PowderCoating.Shared.Constants;
|
||||
using System.Security.Claims;
|
||||
@@ -29,7 +30,7 @@ public class CompanySettingsController : Controller
|
||||
private readonly ILookupCacheService _lookupCache;
|
||||
private readonly IStripeConnectService _stripeConnect;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly ApplicationDbContext _context;
|
||||
private readonly IAuditLogService _auditLog;
|
||||
private readonly UserManager<ApplicationUser> _userManager;
|
||||
private readonly SignInManager<ApplicationUser> _signInManager;
|
||||
|
||||
@@ -42,7 +43,7 @@ public class CompanySettingsController : Controller
|
||||
ILookupCacheService lookupCache,
|
||||
IStripeConnectService stripeConnect,
|
||||
IConfiguration configuration,
|
||||
ApplicationDbContext context,
|
||||
IAuditLogService auditLog,
|
||||
UserManager<ApplicationUser> userManager,
|
||||
SignInManager<ApplicationUser> signInManager)
|
||||
{
|
||||
@@ -54,7 +55,7 @@ public class CompanySettingsController : Controller
|
||||
_lookupCache = lookupCache;
|
||||
_stripeConnect = stripeConnect;
|
||||
_configuration = configuration;
|
||||
_context = context;
|
||||
_auditLog = auditLog;
|
||||
_userManager = userManager;
|
||||
_signInManager = signInManager;
|
||||
}
|
||||
@@ -126,9 +127,7 @@ public class CompanySettingsController : Controller
|
||||
var dto = _mapper.Map<CompanySettingsDto>(company);
|
||||
|
||||
// Populate AllowOnlinePayments from subscription plan config
|
||||
var planConfig = await _context.Set<SubscriptionPlanConfig>()
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(p => p.Plan == company.SubscriptionPlan);
|
||||
var planConfig = await _unitOfWork.SubscriptionPlanConfigs.FirstOrDefaultAsync(p => p.Plan == company.SubscriptionPlan);
|
||||
dto.AllowOnlinePayments = planConfig?.AllowOnlinePayments ?? false;
|
||||
|
||||
// Flag whether Stripe Connect is configured (non-placeholder client ID)
|
||||
@@ -2805,10 +2804,10 @@ public class CompanySettingsController : Controller
|
||||
if (company == null) return NotFound();
|
||||
|
||||
var userCount = await _userManager.Users.CountAsync(u => u.CompanyId == companyId.Value);
|
||||
var jobCount = await _context.Jobs.CountAsync(j => j.CompanyId == companyId.Value);
|
||||
var quoteCount = await _context.Quotes.CountAsync(q => q.CompanyId == companyId.Value);
|
||||
var custCount = await _context.Customers.CountAsync(c => c.CompanyId == companyId.Value);
|
||||
var invCount = await _context.Invoices.CountAsync(i => i.CompanyId == companyId.Value);
|
||||
var jobCount = await _unitOfWork.Jobs.CountAsync(j => j.CompanyId == companyId.Value);
|
||||
var quoteCount = await _unitOfWork.Quotes.CountAsync(q => q.CompanyId == companyId.Value);
|
||||
var custCount = await _unitOfWork.Customers.CountAsync(c => c.CompanyId == companyId.Value);
|
||||
var invCount = await _unitOfWork.Invoices.CountAsync(i => i.CompanyId == companyId.Value);
|
||||
|
||||
ViewBag.CompanyName = company.CompanyName;
|
||||
ViewBag.UserCount = userCount;
|
||||
@@ -2859,10 +2858,10 @@ public class CompanySettingsController : Controller
|
||||
|
||||
// ── Gather counts for the audit snapshot ─────────────────────────
|
||||
var userCount = company.Users.Count;
|
||||
var jobCount = await _context.Jobs.CountAsync(j => j.CompanyId == companyId.Value);
|
||||
var quoteCount = await _context.Quotes.CountAsync(q => q.CompanyId == companyId.Value);
|
||||
var custCount = await _context.Customers.CountAsync(c => c.CompanyId == companyId.Value);
|
||||
var invCount = await _context.Invoices.CountAsync(i => i.CompanyId == companyId.Value);
|
||||
var jobCount = await _unitOfWork.Jobs.CountAsync(j => j.CompanyId == companyId.Value);
|
||||
var quoteCount = await _unitOfWork.Quotes.CountAsync(q => q.CompanyId == companyId.Value);
|
||||
var custCount = await _unitOfWork.Customers.CountAsync(c => c.CompanyId == companyId.Value);
|
||||
var invCount = await _unitOfWork.Invoices.CountAsync(i => i.CompanyId == companyId.Value);
|
||||
|
||||
// ── Soft-delete the company ───────────────────────────────────────
|
||||
var now = DateTime.UtcNow;
|
||||
@@ -2881,7 +2880,7 @@ public class CompanySettingsController : Controller
|
||||
await _unitOfWork.CompleteAsync();
|
||||
|
||||
// ── Write audit log ───────────────────────────────────────────────
|
||||
_context.AuditLogs.Add(new AuditLog
|
||||
await _auditLog.LogAsync(new AuditLog
|
||||
{
|
||||
UserId = requestingUserId,
|
||||
UserName = requestingUserName,
|
||||
@@ -2899,7 +2898,6 @@ public class CompanySettingsController : Controller
|
||||
Timestamp = now,
|
||||
IpAddress = HttpContext.Connection.RemoteIpAddress?.ToString()
|
||||
});
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
_logger.LogWarning(
|
||||
"Self-service account deletion: company {CompanyName} (ID:{CompanyId}) deleted by {User}. " +
|
||||
|
||||
Reference in New Issue
Block a user