GetRootFolderContents auto-personal-tenant never persisted (no-tracking user mutation) #44

Closed
opened 2026-11-04 03:23:27 +01:00 by amiryahaya · 0 comments
Owner

Found by the #27 coverage-gap tests (GetRootFolderContentsTests.TenantLessUser_AutoCreatesPersonalTenantAndRoot, red on current code).

GetRootFolderContents.Handle (Folders) mutates accessor.User.TenantId for tenant-less users and calls SaveChangesAsync — but accessor.User comes from SsdidAuthMiddleware, which loads users AsNoTracking(). The mutation happens on a detached instance: the new personal Tenant + UserTenant rows persist, but users.tenant_id stays NULL.

Effect: every call to GET /api/folders/root/contents by a tenant-less user (e.g. a fresh SuperAdmin) mints ANOTHER personal tenant, and the user never gains a persisted tenant — duplicate-tenant drift.

Fix: resolve a tracked user inside the handler (db.Users.FirstAsync(u => u.Id == accessor.UserId)) instead of mutating the detached accessor.User; add a regression assertion on persisted TenantId.

**Found by the #27 coverage-gap tests** (GetRootFolderContentsTests.TenantLessUser_AutoCreatesPersonalTenantAndRoot, red on current code). `GetRootFolderContents.Handle` (Folders) mutates `accessor.User.TenantId` for tenant-less users and calls `SaveChangesAsync` — but `accessor.User` comes from `SsdidAuthMiddleware`, which loads users **`AsNoTracking()`**. The mutation happens on a detached instance: the new personal Tenant + UserTenant rows persist, but `users.tenant_id` stays NULL. **Effect:** every call to `GET /api/folders/root/contents` by a tenant-less user (e.g. a fresh SuperAdmin) mints ANOTHER personal tenant, and the user never gains a persisted tenant — duplicate-tenant drift. **Fix:** resolve a tracked user inside the handler (`db.Users.FirstAsync(u => u.Id == accessor.UserId)`) instead of mutating the detached `accessor.User`; add a regression assertion on persisted TenantId.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
primatekuntech/failsulit#44
No description provided.