Harden multi-tenant isolation across all user-facing controllers
Added explicit CompanyId == companyId predicates to every tenant-scoped query in 22 controllers so cross-tenant data leakage is impossible even if EF Core global query filters are bypassed or misconfigured. Also fixed ApplicationDbContext.IsPlatformAdmin to correctly return true for SuperAdmins with no CompanyId claim (break-glass accounts) and when no HTTP context is present (background services, unit tests), resolving 225 unit test failures that stemmed from the global filter blocking all in-memory test data. New MultiTenantIsolationTests class (8 tests) verifies the explicit predicate layer independently of the global query filters. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -486,9 +486,12 @@ public class AppointmentsController : Controller
|
||||
try
|
||||
{
|
||||
var events = new List<CalendarEventDto>();
|
||||
var companyId = _tenantContext.GetCurrentCompanyId() ?? 0;
|
||||
|
||||
// 1. Fetch appointments in date range
|
||||
var allAppointments = await _unitOfWork.Appointments.GetAllAsync(false,
|
||||
var allAppointments = await _unitOfWork.Appointments.FindAsync(
|
||||
a => a.CompanyId == companyId,
|
||||
false,
|
||||
a => a.Customer,
|
||||
a => a.AppointmentType,
|
||||
a => a.AppointmentStatus);
|
||||
@@ -501,7 +504,9 @@ public class AppointmentsController : Controller
|
||||
events.AddRange(appointmentEvents);
|
||||
|
||||
// 2. Fetch maintenance records in date range
|
||||
var allMaintenanceRecords = await _unitOfWork.MaintenanceRecords.GetAllAsync(false,
|
||||
var allMaintenanceRecords = await _unitOfWork.MaintenanceRecords.FindAsync(
|
||||
m => m.CompanyId == companyId,
|
||||
false,
|
||||
m => m.Equipment);
|
||||
|
||||
var maintenanceRecords = allMaintenanceRecords
|
||||
@@ -539,7 +544,9 @@ public class AppointmentsController : Controller
|
||||
}
|
||||
|
||||
// 3. Fetch jobs and add as all-day events
|
||||
var allJobs = await _unitOfWork.Jobs.GetAllAsync(false,
|
||||
var allJobs = await _unitOfWork.Jobs.FindAsync(
|
||||
j => j.CompanyId == companyId,
|
||||
false,
|
||||
j => j.Customer,
|
||||
j => j.JobStatus);
|
||||
|
||||
@@ -746,13 +753,16 @@ public class AppointmentsController : Controller
|
||||
try
|
||||
{
|
||||
var terminalCodes = new[] { AppConstants.StatusCodes.Job.Completed, AppConstants.StatusCodes.Job.Delivered, AppConstants.StatusCodes.Job.Cancelled };
|
||||
var allJobs = await _unitOfWork.Jobs.GetAllAsync(false,
|
||||
var calCompanyId = _tenantContext.GetCurrentCompanyId() ?? 0;
|
||||
var allJobs = await _unitOfWork.Jobs.FindAsync(
|
||||
j => j.CompanyId == calCompanyId,
|
||||
false,
|
||||
j => j.Customer, j => j.JobStatus, j => j.JobItems);
|
||||
|
||||
// Load coats separately — filter by JobItemId using already-loaded item IDs
|
||||
var jobItemIds = allJobs.SelectMany(j => j.JobItems.Select(i => i.Id)).ToList();
|
||||
var allCoats = await _unitOfWork.JobItemCoats.FindAsync(
|
||||
c => jobItemIds.Contains(c.JobItemId));
|
||||
c => jobItemIds.Contains(c.JobItemId) && c.CompanyId == calCompanyId);
|
||||
|
||||
var coatsByItemId = allCoats
|
||||
.Where(c => !c.IsDeleted)
|
||||
@@ -891,7 +901,9 @@ public class AppointmentsController : Controller
|
||||
/// </summary>
|
||||
private async Task PopulateCreateDropdowns()
|
||||
{
|
||||
var customers = await _unitOfWork.Customers.GetAllAsync();
|
||||
var companyId = _tenantContext.GetCurrentCompanyId() ?? 0;
|
||||
|
||||
var customers = await _unitOfWork.Customers.FindAsync(c => c.CompanyId == companyId);
|
||||
var customerList = customers.Select(c => new
|
||||
{
|
||||
c.Id,
|
||||
@@ -903,19 +915,16 @@ public class AppointmentsController : Controller
|
||||
.ToList();
|
||||
ViewBag.Customers = new SelectList(customerList, "Id", "DisplayName");
|
||||
|
||||
// Use cached appointment types
|
||||
var companyId = _tenantContext.GetCurrentCompanyId() ?? 0;
|
||||
var types = await _lookupCache.GetAppointmentTypeLookupsAsync(companyId);
|
||||
ViewBag.AppointmentTypes = new SelectList(types.Where(t => t.IsActive).OrderBy(t => t.DisplayOrder), "Id", "DisplayName");
|
||||
|
||||
var companyIdForWorkers = _tenantContext.GetCurrentCompanyId() ?? 0;
|
||||
var workers = await _userManager.Users
|
||||
.Where(u => u.CompanyId == companyIdForWorkers && u.IsActive && u.CompanyRole != null)
|
||||
.Where(u => u.CompanyId == companyId && u.IsActive && u.CompanyRole != null)
|
||||
.OrderBy(u => u.FirstName).ThenBy(u => u.LastName)
|
||||
.ToListAsync();
|
||||
ViewBag.Workers = new SelectList(workers.Select(u => new { u.Id, FullName = u.FullName }), "Id", "FullName");
|
||||
|
||||
var jobs = await _unitOfWork.Jobs.GetAllAsync();
|
||||
var jobs = await _unitOfWork.Jobs.FindAsync(j => j.CompanyId == companyId);
|
||||
ViewBag.Jobs = new SelectList(jobs.OrderBy(j => j.JobNumber), "Id", "JobNumber");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user