Repository navigation
fix(lifecycle): retain shared resources when creators leave #8762
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
817191c
fix(lifecycle): retain shared resources when creators leave
mzxchandra be01225
fix(lifecycle): preserve replacement workspace grants
mzxchandra 34a82c3
fix(lifecycle): preserve upload history during creator handoff
mzxchandra 08398e7
fix(lifecycle): preserve empty upload attribution through handoff
mzxchandra ef198ef
fix(lifecycle): order workspace locks before file mutations
mzxchandra bbafeb6
Merge staging into codex/shared-resource-lifecycle
mzxchandra 4d73711
fix(admin): use renamed member not-found response
mzxchandra bd24465
fix(lifecycle): acquire organization fences before workspace handoff
mzxchandra 9b5f468
fix(lifecycle): lock resource workspaces before account erasure barrier
mzxchandra b7c4cd1
fix(lifecycle): refuse erasure while creator references are in flight
mzxchandra f2c4d32
fix(lifecycle): preview successor blockers and sync Project enforcement
mzxchandra 8244ef2
fix(lifecycle): lock Project workspaces before successor grants
mzxchandra 01b4ff6
Merge remote-tracking branch 'origin/staging' into codex/shared-resou…
mzxchandra fb298fb
fix(lifecycle): make shared file contexts readonly
mzxchandra 5b4b904
Merge remote-tracking branch 'origin/staging' into codex/shared-resou…
mzxchandra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| import { | ||
| knowledgeBase, | ||
| member, | ||
| permissionGroup, | ||
| ssoProvider, | ||
| user, | ||
| workspaceFiles, | ||
| } from '@sim/db/schema' | ||
| import { and, asc, eq, isNull, ne, sql } from 'drizzle-orm' | ||
| import { OrchestrationError } from '@/lib/core/orchestration/types' | ||
| import type { DbOrTx, DbTransaction } from '@/lib/db/types' | ||
| import { handoffFileCreatorsInTx } from '@/lib/uploads/contexts/workspace/creator-handoff' | ||
|
|
||
| /** Organization references survive even when their creator no longer has a membership row. */ | ||
| export async function listSharedResourceOrganizationIdsForUser(executor: DbOrTx, userId: string) { | ||
| const rows = await executor.execute<{ organizationId: string }>(sql` | ||
| SELECT DISTINCT "organizationId" FROM ( | ||
| SELECT ${knowledgeBase.organizationId} AS "organizationId" FROM ${knowledgeBase} | ||
| WHERE ${knowledgeBase.userId} = ${userId} AND ${knowledgeBase.workspaceId} IS NULL | ||
| UNION ALL SELECT ${workspaceFiles.organizationId} FROM ${workspaceFiles} | ||
| WHERE ${workspaceFiles.userId} = ${userId} AND ${workspaceFiles.workspaceId} IS NULL | ||
| UNION ALL SELECT ${permissionGroup.organizationId} FROM ${permissionGroup} WHERE ${permissionGroup.createdBy} = ${userId} | ||
| UNION ALL SELECT ${ssoProvider.organizationId} FROM ${ssoProvider} WHERE ${ssoProvider.userId} = ${userId} | ||
| ) owned WHERE "organizationId" IS NOT NULL ORDER BY "organizationId" | ||
| `) | ||
| return rows.map((row) => row.organizationId) | ||
| } | ||
|
|
||
| /** Uses the same eligible owner in read-only previews and locked handoffs. */ | ||
| export async function findOrganizationSharedResourceSuccessor( | ||
| tx: DbOrTx, | ||
| organizationId: string, | ||
| departingUserId: string, | ||
| hold = false | ||
| ): Promise<string | null> { | ||
| const successorQuery = tx | ||
| .select({ userId: member.userId }) | ||
| .from(member) | ||
| .where( | ||
| and( | ||
| eq(member.organizationId, organizationId), | ||
| eq(member.role, 'owner'), | ||
| ne(member.userId, departingUserId) | ||
| ) | ||
| ) | ||
| .orderBy(asc(member.userId)) | ||
| .limit(1) | ||
| const [successor] = await (hold ? successorQuery.for('share') : successorQuery) | ||
| if (!successor) return null | ||
| const accountQuery = tx.select({ id: user.id }).from(user).where(eq(user.id, successor.userId)) | ||
| const [account] = await (hold ? accountQuery.for('key share') : accountQuery) | ||
| return account ? successor.userId : null | ||
| } | ||
|
|
||
| /** Retains organization-owned configuration without rewriting ACLs, credentials, or historical actors. */ | ||
| export async function reassignOrganizationSharedResourcesTx( | ||
| tx: DbTransaction, | ||
| organizationId: string, | ||
| departingUserId: string | ||
| ): Promise<string | null> { | ||
| const userId = await findOrganizationSharedResourceSuccessor( | ||
| tx, | ||
| organizationId, | ||
| departingUserId, | ||
| true | ||
| ) | ||
| if (!userId) { | ||
| const owned = await listSharedResourceOrganizationIdsForUser(tx, departingUserId) | ||
| if (owned.includes(organizationId)) { | ||
| throw new OrchestrationError( | ||
| 'conflict', | ||
| 'Shared organization resources need an active organization owner before this account can leave or be deleted.' | ||
| ) | ||
| } | ||
| return null | ||
| } | ||
| const updatedAt = new Date() | ||
| await tx | ||
| .update(knowledgeBase) | ||
| .set({ userId, updatedAt }) | ||
| .where( | ||
| and( | ||
| eq(knowledgeBase.organizationId, organizationId), | ||
| isNull(knowledgeBase.workspaceId), | ||
| eq(knowledgeBase.userId, departingUserId) | ||
| ) | ||
| ) | ||
| await handoffFileCreatorsInTx( | ||
| tx, | ||
| and( | ||
| eq(workspaceFiles.organizationId, organizationId), | ||
| isNull(workspaceFiles.workspaceId), | ||
| eq(workspaceFiles.userId, departingUserId) | ||
| ), | ||
| userId | ||
| ) | ||
| await tx | ||
| .update(permissionGroup) | ||
| .set({ createdBy: userId, updatedAt }) | ||
| .where( | ||
| and( | ||
| eq(permissionGroup.organizationId, organizationId), | ||
| eq(permissionGroup.createdBy, departingUserId) | ||
| ) | ||
| ) | ||
| await tx | ||
| .update(ssoProvider) | ||
| .set({ userId }) | ||
| .where( | ||
| and(eq(ssoProvider.organizationId, organizationId), eq(ssoProvider.userId, departingUserId)) | ||
| ) | ||
| return userId | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.