Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 3.8k
fix(admin): delete workspaces on ban#4029
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -80,6 +80,7 @@ import { quickValidateEmail } from '@/lib/messaging/email/validation' | ||
| import { scheduleLifecycleEmail } from '@/lib/messaging/lifecycle' | ||
| import { captureServerEvent } from '@/lib/posthog/server' | ||
| import { syncAllWebhooksForCredentialSet } from '@/lib/webhooks/utils.server' | ||
| import { disableUserResources } from '@/lib/workflows/lifecycle' | ||
| import { SSO_TRUSTED_PROVIDERS } from '@/ee/sso/constants' | ||
| import { createAnonymousSession, ensureAnonymousUserExists } from './anonymous' | ||
| @@ -241,6 +242,13 @@ export const auth = betterAuth({ | ||
| } | ||
| }, | ||
| }, | ||
| update: { | ||
| after: async (user) => { | ||
| if (user.banned) { | ||
| await disableUserResources(user.id) | ||
| } | ||
| }, | ||
| }, | ||
TheodoreSpeaks marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. TheodoreSpeaks marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| }, | ||
| account: { | ||
| create: { | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,22 @@ | ||
| import { db } from '@sim/db' | ||
| import { | ||
| a2aAgent, | ||
| apiKey, | ||
| chat, | ||
| form, | ||
| webhook, | ||
| workflow, | ||
| workflowDeploymentVersion, | ||
| workflowMcpTool, | ||
| workflowSchedule, | ||
| workspace, | ||
| } from '@sim/db/schema' | ||
| import { createLogger } from '@sim/logger' | ||
| import { and, eq, inArray, isNull } from 'drizzle-orm' | ||
| import { env } from '@/lib/core/config/env' | ||
| import { getRedisClient } from '@/lib/core/config/redis' | ||
| import { PlatformEvents } from '@/lib/core/telemetry' | ||
| import { generateRequestId } from '@/lib/core/utils/request' | ||
| import { mcpPubSub } from '@/lib/mcp/pubsub' | ||
| import { getWorkflowById } from '@/lib/workflows/utils' | ||
| @@ -361,3 +364,29 @@ export async function archiveWorkflowsByIdsInWorkspace( | ||
| options | ||
| ) | ||
| } | ||
| /** | ||
| * Disables all resources owned by a banned user by archiving every workspace | ||
| * they own (cascading to workflows, chats, forms, KBs, tables, files, etc.) | ||
| * and deleting their personal API keys. | ||
| */ | ||
| export async function disableUserResources(userId: string): Promise<void> { | ||
| const requestId = generateRequestId() | ||
| logger.info(`[${requestId}] Disabling resources for banned user ${userId}`) | ||
| const { archiveWorkspace } = await import('@/lib/workspaces/lifecycle') | ||
| const ownedWorkspaces = await db | ||
| .select({ id: workspace.id }) | ||
| .from(workspace) | ||
| .where(and(eq(workspace.ownerId, userId), isNull(workspace.archivedAt))) | ||
| await Promise.all([ | ||
| ...ownedWorkspaces.map((w) => archiveWorkspace(w.id, { requestId })), | ||
| db.delete(apiKey).where(eq(apiKey.userId, userId)), | ||
| ]) | ||
TheodoreSpeaks marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| logger.info( | ||
| `[${requestId}] Disabled resources for user ${userId}: archived ${ownedWorkspaces.length} workspaces, deleted API keys` | ||
| ) | ||
| } | ||
TheodoreSpeaks marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.