-
Notifications
You must be signed in to change notification settings - Fork 3.5k
feat(enterprise): cloud whitelabeling for enterprise orgs #4047
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
20 commits
Select commit
Hold shift + click to select a range
a2d5e1a
feat(enterprise): cloud whitelabeling for enterprise orgs
waleedlatif1 59870ac
fix(enterprise): scope enterprise plan check to target org in whitela…
waleedlatif1 4240b1c
fix(enterprise): use isOrganizationOnEnterprisePlan for org-scoped en…
waleedlatif1 96b7a2d
fix(enterprise): allow clearing whitelabel fields and guard against e…
waleedlatif1 ebe7e5c
fix(enterprise): remove webp from logo accept attribute to match uplo…
waleedlatif1 008cfd1
improvement(billing): use isBillingEnabled instead of isProd for plan…
waleedlatif1 71350ba
fix(enterprise): show whitelabeling nav item when billing is enabled …
waleedlatif1 0dadb6c
fix(enterprise): accept relative paths for logoUrl since upload API r…
waleedlatif1 e35132b
fix(whitelabeling): prevent logo flash on refresh by hiding logo whil…
waleedlatif1 e95b22a
fix(whitelabeling): wire hover color through CSS token on tertiary bu…
waleedlatif1 a9c9c29
fix(whitelabeling): show sim logo by default, only replace when org l…
waleedlatif1 93811da
fix(whitelabeling): cache org logo url in localstorage to eliminate f…
waleedlatif1 ad4dc96
feat(whitelabeling): add wordmark support with drag/drop upload
waleedlatif1 ed84438
updated turbo
waleedlatif1 55a1978
fix(whitelabeling): defer localstorage read to effect to prevent hydr…
waleedlatif1 0a09fe4
fix(whitelabeling): use layout effect for cache read to eliminate log…
waleedlatif1 4a10902
fix(whitelabeling): cache theme css to eliminate color flash before o…
waleedlatif1 6529e2a
fix(whitelabeling): deduplicate HEX_COLOR_REGEX into lib/branding and…
waleedlatif1 ac48b54
fix(whitelabeling): use cookie-based SSR cache to eliminate brand fla…
waleedlatif1 17dd6e8
fix(whitelabeling): use !orgSettings condition to fix SSR brand cache…
waleedlatif1 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
213 changes: 213 additions & 0 deletions
213
apps/sim/app/api/organizations/[id]/whitelabel/route.ts
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,213 @@ | ||
| import { db } from '@sim/db' | ||
| import { member, organization } from '@sim/db/schema' | ||
| import { createLogger } from '@sim/logger' | ||
| import { and, eq } from 'drizzle-orm' | ||
| import { type NextRequest, NextResponse } from 'next/server' | ||
| import { z } from 'zod' | ||
| import { AuditAction, AuditResourceType, recordAudit } from '@/lib/audit/log' | ||
| import { getSession } from '@/lib/auth' | ||
| import { isOrganizationOnEnterprisePlan } from '@/lib/billing/core/subscription' | ||
| import { HEX_COLOR_REGEX } from '@/lib/branding' | ||
| import type { OrganizationWhitelabelSettings } from '@/lib/branding/types' | ||
|
|
||
| const logger = createLogger('WhitelabelAPI') | ||
|
|
||
| const updateWhitelabelSchema = z.object({ | ||
| brandName: z | ||
| .string() | ||
| .trim() | ||
| .max(64, 'Brand name must be 64 characters or fewer') | ||
| .nullable() | ||
| .optional(), | ||
| logoUrl: z.string().min(1).nullable().optional(), | ||
| wordmarkUrl: z.string().min(1).nullable().optional(), | ||
| primaryColor: z | ||
| .string() | ||
| .regex(HEX_COLOR_REGEX, 'Primary color must be a valid hex color (e.g. #701ffc)') | ||
| .nullable() | ||
| .optional(), | ||
| primaryHoverColor: z | ||
| .string() | ||
| .regex(HEX_COLOR_REGEX, 'Primary hover color must be a valid hex color') | ||
| .nullable() | ||
| .optional(), | ||
| accentColor: z | ||
| .string() | ||
| .regex(HEX_COLOR_REGEX, 'Accent color must be a valid hex color') | ||
| .nullable() | ||
| .optional(), | ||
| accentHoverColor: z | ||
| .string() | ||
| .regex(HEX_COLOR_REGEX, 'Accent hover color must be a valid hex color') | ||
| .nullable() | ||
| .optional(), | ||
| supportEmail: z | ||
| .string() | ||
| .email('Support email must be a valid email address') | ||
| .nullable() | ||
| .optional(), | ||
| documentationUrl: z.string().url('Documentation URL must be a valid URL').nullable().optional(), | ||
| termsUrl: z.string().url('Terms URL must be a valid URL').nullable().optional(), | ||
| privacyUrl: z.string().url('Privacy URL must be a valid URL').nullable().optional(), | ||
| hidePoweredBySim: z.boolean().optional(), | ||
| }) | ||
|
|
||
| /** | ||
| * GET /api/organizations/[id]/whitelabel | ||
| * Returns the organization's whitelabel settings. | ||
| * Accessible by any member of the organization. | ||
| */ | ||
| export async function GET(_request: NextRequest, { params }: { params: Promise<{ id: string }> }) { | ||
| try { | ||
| const session = await getSession() | ||
|
|
||
| if (!session?.user?.id) { | ||
| return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) | ||
| } | ||
|
|
||
| const { id: organizationId } = await params | ||
|
|
||
| const [memberEntry] = await db | ||
| .select({ id: member.id }) | ||
| .from(member) | ||
| .where(and(eq(member.organizationId, organizationId), eq(member.userId, session.user.id))) | ||
| .limit(1) | ||
|
|
||
| if (!memberEntry) { | ||
| return NextResponse.json( | ||
| { error: 'Forbidden - Not a member of this organization' }, | ||
| { status: 403 } | ||
| ) | ||
| } | ||
|
|
||
| const [org] = await db | ||
| .select({ whitelabelSettings: organization.whitelabelSettings }) | ||
| .from(organization) | ||
| .where(eq(organization.id, organizationId)) | ||
| .limit(1) | ||
|
|
||
| if (!org) { | ||
| return NextResponse.json({ error: 'Organization not found' }, { status: 404 }) | ||
| } | ||
|
|
||
| return NextResponse.json({ | ||
| success: true, | ||
| data: (org.whitelabelSettings ?? {}) as OrganizationWhitelabelSettings, | ||
| }) | ||
| } catch (error) { | ||
| logger.error('Failed to get whitelabel settings', { error }) | ||
| return NextResponse.json({ error: 'Internal server error' }, { status: 500 }) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * PUT /api/organizations/[id]/whitelabel | ||
| * Updates the organization's whitelabel settings. | ||
| * Requires enterprise plan and owner/admin role. | ||
| */ | ||
| export async function PUT(request: NextRequest, { params }: { params: Promise<{ id: string }> }) { | ||
| try { | ||
| const session = await getSession() | ||
|
|
||
| if (!session?.user?.id) { | ||
| return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) | ||
| } | ||
|
|
||
| const { id: organizationId } = await params | ||
|
|
||
| const body = await request.json() | ||
| const parsed = updateWhitelabelSchema.safeParse(body) | ||
|
|
||
| if (!parsed.success) { | ||
| return NextResponse.json( | ||
| { error: parsed.error.errors[0]?.message ?? 'Invalid request body' }, | ||
| { status: 400 } | ||
| ) | ||
| } | ||
|
|
||
| const [memberEntry] = await db | ||
| .select({ role: member.role }) | ||
| .from(member) | ||
| .where(and(eq(member.organizationId, organizationId), eq(member.userId, session.user.id))) | ||
| .limit(1) | ||
|
|
||
| if (!memberEntry) { | ||
| return NextResponse.json( | ||
| { error: 'Forbidden - Not a member of this organization' }, | ||
| { status: 403 } | ||
| ) | ||
| } | ||
|
|
||
| if (memberEntry.role !== 'owner' && memberEntry.role !== 'admin') { | ||
| return NextResponse.json( | ||
| { error: 'Forbidden - Only organization owners and admins can update whitelabel settings' }, | ||
| { status: 403 } | ||
| ) | ||
| } | ||
|
|
||
| const hasEnterprisePlan = await isOrganizationOnEnterprisePlan(organizationId) | ||
|
|
||
| if (!hasEnterprisePlan) { | ||
| return NextResponse.json( | ||
| { error: 'Whitelabeling is available on Enterprise plans only' }, | ||
| { status: 403 } | ||
| ) | ||
| } | ||
|
|
||
| const [currentOrg] = await db | ||
| .select({ name: organization.name, whitelabelSettings: organization.whitelabelSettings }) | ||
| .from(organization) | ||
| .where(eq(organization.id, organizationId)) | ||
| .limit(1) | ||
|
|
||
| if (!currentOrg) { | ||
| return NextResponse.json({ error: 'Organization not found' }, { status: 404 }) | ||
| } | ||
|
|
||
| const current: OrganizationWhitelabelSettings = currentOrg.whitelabelSettings ?? {} | ||
| const incoming = parsed.data | ||
|
|
||
| const merged: OrganizationWhitelabelSettings = { ...current } | ||
|
|
||
| for (const key of Object.keys(incoming) as Array<keyof typeof incoming>) { | ||
| const value = incoming[key] | ||
| if (value === null) { | ||
| delete merged[key as keyof OrganizationWhitelabelSettings] | ||
| } else if (value !== undefined) { | ||
| ;(merged as Record<string, unknown>)[key] = value | ||
| } | ||
| } | ||
|
|
||
| const [updated] = await db | ||
| .update(organization) | ||
| .set({ whitelabelSettings: merged, updatedAt: new Date() }) | ||
| .where(eq(organization.id, organizationId)) | ||
| .returning({ whitelabelSettings: organization.whitelabelSettings }) | ||
|
|
||
| if (!updated) { | ||
| return NextResponse.json({ error: 'Organization not found' }, { status: 404 }) | ||
| } | ||
|
|
||
| recordAudit({ | ||
| workspaceId: null, | ||
| actorId: session.user.id, | ||
| action: AuditAction.ORGANIZATION_UPDATED, | ||
| resourceType: AuditResourceType.ORGANIZATION, | ||
| resourceId: organizationId, | ||
| actorName: session.user.name ?? undefined, | ||
| actorEmail: session.user.email ?? undefined, | ||
| resourceName: currentOrg.name, | ||
| description: 'Updated organization whitelabel settings', | ||
| metadata: { changes: Object.keys(incoming) }, | ||
| request, | ||
| }) | ||
|
|
||
| return NextResponse.json({ | ||
| success: true, | ||
| data: (updated.whitelabelSettings ?? {}) as OrganizationWhitelabelSettings, | ||
| }) | ||
| } catch (error) { | ||
| logger.error('Failed to update whitelabel settings', { error }) | ||
| return NextResponse.json({ error: 'Internal server error' }, { status: 500 }) | ||
| } | ||
| } | ||
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
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.