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
Open
mzxchandra
wants to merge
5
commits into
staging
Choose a base branch
from
codex/shared-resource-lifecycle
base: staging
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 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 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,105 @@ | ||
| 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) | ||
| } | ||
|
|
||
| /** 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 [successor] = await 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) | ||
| .for('share') | ||
| if (!successor) { | ||
| 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 [account] = await tx | ||
| .select({ id: user.id }) | ||
| .from(user) | ||
| .where(eq(user.id, successor.userId)) | ||
| .for('key share') | ||
| if (!account) | ||
| throw new OrchestrationError( | ||
| 'conflict', | ||
| 'The organization owner changed. Nothing was changed; try again.' | ||
| ) | ||
| const userId = successor.userId | ||
| 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.