d25d482dc2
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
117 lines
3.1 KiB
TypeScript
117 lines
3.1 KiB
TypeScript
/**
|
|
* Enterprise audit log authorization.
|
|
*
|
|
* Validates that the authenticated user is an admin/owner of an enterprise organization
|
|
* and returns the organization context needed for scoped queries.
|
|
*/
|
|
|
|
import { db } from '@sim/db'
|
|
import { member, subscription } from '@sim/db/schema'
|
|
import { createLogger } from '@sim/logger'
|
|
import { and, eq, inArray } from 'drizzle-orm'
|
|
import { NextResponse } from 'next/server'
|
|
import { getEffectiveBillingStatus } from '@/lib/billing/core/access'
|
|
import { USABLE_SUBSCRIPTION_STATUSES } from '@/lib/billing/subscriptions/utils'
|
|
|
|
const logger = createLogger('V1AuditLogsAuth')
|
|
|
|
interface EnterpriseAuditContext {
|
|
organizationId: string
|
|
orgMemberIds: string[]
|
|
}
|
|
|
|
type AuthResult =
|
|
| { success: true; context: EnterpriseAuditContext }
|
|
| { success: false; response: NextResponse }
|
|
|
|
/**
|
|
* Validates enterprise audit log access for the given user.
|
|
*
|
|
* Checks:
|
|
* 1. User belongs to an organization
|
|
* 2. User has admin or owner role
|
|
* 3. Organization has an active enterprise subscription
|
|
*
|
|
* Returns the organization ID and all member user IDs on success,
|
|
* or an error response on failure.
|
|
*/
|
|
export async function validateEnterpriseAuditAccess(userId: string): Promise<AuthResult> {
|
|
const [membership] = await db
|
|
.select({ organizationId: member.organizationId, role: member.role })
|
|
.from(member)
|
|
.where(eq(member.userId, userId))
|
|
.limit(1)
|
|
|
|
if (!membership) {
|
|
return {
|
|
success: false,
|
|
response: NextResponse.json({ error: 'Not a member of any organization' }, { status: 403 }),
|
|
}
|
|
}
|
|
|
|
if (membership.role !== 'admin' && membership.role !== 'owner') {
|
|
return {
|
|
success: false,
|
|
response: NextResponse.json(
|
|
{ error: 'Organization admin or owner role required' },
|
|
{ status: 403 }
|
|
),
|
|
}
|
|
}
|
|
|
|
const billingStatus = await getEffectiveBillingStatus(userId)
|
|
if (billingStatus.billingBlocked) {
|
|
return {
|
|
success: false,
|
|
response: NextResponse.json(
|
|
{ error: 'Active enterprise subscription required' },
|
|
{ status: 403 }
|
|
),
|
|
}
|
|
}
|
|
|
|
const [orgSub, orgMembers] = await Promise.all([
|
|
db
|
|
.select({ id: subscription.id })
|
|
.from(subscription)
|
|
.where(
|
|
and(
|
|
eq(subscription.referenceId, membership.organizationId),
|
|
eq(subscription.plan, 'enterprise'),
|
|
inArray(subscription.status, USABLE_SUBSCRIPTION_STATUSES)
|
|
)
|
|
)
|
|
.limit(1),
|
|
db
|
|
.select({ userId: member.userId })
|
|
.from(member)
|
|
.where(eq(member.organizationId, membership.organizationId)),
|
|
])
|
|
|
|
if (orgSub.length === 0) {
|
|
return {
|
|
success: false,
|
|
response: NextResponse.json(
|
|
{ error: 'Active enterprise subscription required' },
|
|
{ status: 403 }
|
|
),
|
|
}
|
|
}
|
|
|
|
const orgMemberIds = orgMembers.map((m) => m.userId)
|
|
|
|
logger.info('Enterprise audit access validated', {
|
|
userId,
|
|
organizationId: membership.organizationId,
|
|
memberCount: orgMemberIds.length,
|
|
})
|
|
|
|
return {
|
|
success: true,
|
|
context: {
|
|
organizationId: membership.organizationId,
|
|
orgMemberIds,
|
|
},
|
|
}
|
|
}
|