Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

11.8 KB
Raw Blame
import { z } from "zod";
import {
  createTRPCRouter,
  authenticatedProcedure,
} from "@/src/server/api/trpc";
import { TRPCError } from "@trpc/server";
import { ForbiddenError, StringNoHTML } from "@langfuse/shared";
import { Role, Prisma, type PrismaClient } from "@langfuse/shared/src/db";
import { canToggleV4 } from "@/src/features/events/lib/v4Rollout";
import { V4_PREVIEW_LABEL } from "@/src/features/events/lib/v4PreviewLabel";
import { env } from "@/src/env.mjs";
import { getSfdcService } from "@/src/ee/features/sfdc-sync/server/sfdcService";
import {
  isAdminOnlyFeaturePreviewFlag,
  personalFeaturePreviewFlags,
  INTERNAL_FEATURE_FLAG,
} from "@/src/features/feature-flags/available-flags";
import { setUserFeaturePreview } from "@/src/features/feature-flags/server/organizationFeatureFlags";
import { hasInternalAccess } from "@/src/features/feature-flags/utils";
import { advanceSessionsExpiredAtForUser } from "@/src/features/auth/lib/sessionExpiration";
import {
  deleteUserBoundApiKeys,
  evictRevokedApiKeys,
} from "@/src/features/ai-gateway/server/apiKey/userBoundApiKeys";

const updateDisplayNameSchema = z.object({
  name: StringNoHTML.min(1, "Name cannot be empty").max(
    100,
    "Name must be at most 100 characters",
  ),
});

/**
 * Helper function to check if a user can be deleted.
 * A user can only be deleted if they are not the last owner of any organization.
 */
async function checkUserCanBeDeleted(
  userId: string,
  prisma:
    | Omit<
        PrismaClient,
        | "$connect"
        | "$disconnect"
        | "$on"
        | "$transaction"
        | "$use"
        | "$extends"
      >
    | Prisma.TransactionClient,
) {
  // Find all organizations where user is an owner
  const organizationMemberships = await prisma.organizationMembership.findMany({
    where: {
      userId,
      role: Role.OWNER,
    },
    include: {
      organization: {
        include: {
          organizationMemberships: {
            where: {
              role: Role.OWNER,
            },
          },
        },
      },
    },
  });

  // Filter to find organizations where user is the ONLY owner
  const organizationsWhereLastOwner = organizationMemberships
    .filter((membership) => {
      const ownerCount = membership.organization.organizationMemberships.length;
      return ownerCount === 1; // User is the only owner
    })
    .map((membership) => ({
      id: membership.organization.id,
      name: membership.organization.name,
    }));

  return {
    canDelete: organizationsWhereLastOwner.length === 0,
    blockingOrganizations: organizationsWhereLastOwner,
  };
}

export const userAccountRouter = createTRPCRouter({
  setViewMode: authenticatedProcedure
    .input(z.object({ mode: z.enum(["INTERNAL", "EXTERNAL"]) }))
    .mutation(async ({ input, ctx }) => {
      const canEnableFeaturePreviews =
        Boolean(env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION) ||
        ctx.session.user.v4BetaEnabled === true;

      if (
        !hasInternalAccess({
          isAdmin: ctx.session.user.admin === true,
          isExperimentalFeaturesEnabled:
            env.LANGFUSE_ENABLE_EXPERIMENTAL_FEATURES === "true",
        }) ||
        !canEnableFeaturePreviews
      ) {
        throw new TRPCError({
          code: "PRECONDITION_FAILED",
          message: `Internal view mode requires ${V4_PREVIEW_LABEL} on self-hosted deployments.`,
        });
      }
      await setUserFeaturePreview({
        prisma: ctx.prisma,
        userId: ctx.session.user.id,
        flag: INTERNAL_FEATURE_FLAG,
        enabled: input.mode === "INTERNAL",
      });
      return { success: true };
    }),
  checkCanDelete: authenticatedProcedure.query(async ({ ctx }) => {
    const userId = ctx.session.user.id;
    return checkUserCanBeDeleted(userId, ctx.prisma);
  }),

  updateDisplayName: authenticatedProcedure
    .input(updateDisplayNameSchema)
    .mutation(async ({ input, ctx }) => {
      const userId = ctx.session.user.id;

      const updatedUser = await ctx.prisma.user.update({
        where: { id: userId },
        data: {
          name: input.name,
        },
      });

      return {
        success: true,
        name: updatedUser.name,
      };
    }),

  signOutAllSessions: authenticatedProcedure.mutation(async ({ ctx }) => {
    await advanceSessionsExpiredAtForUser(ctx.session.user.id, ctx.prisma);

    return { success: true };
  }),

  setFeaturePreviewEnabled: authenticatedProcedure
    .input(
      z.object({
        // Allowlist of user-toggleable Feature Preview flags (the Feature
        // Preview modal). Keep in sync with the modal's preview registry.
        flag: z.enum(personalFeaturePreviewFlags),
        enabled: z.boolean(),
      }),
    )
    .mutation(async ({ input, ctx }) => {
      const userId = ctx.session.user.id;

      if (
        isAdminOnlyFeaturePreviewFlag(input.flag) &&
        !ctx.session.user.admin
      ) {
        throw new ForbiddenError(
          "Only administrators can change this feature preview.",
        );
      }

      const canEnableFeaturePreviews =
        Boolean(env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION) ||
        ctx.session.user.v4BetaEnabled === true;

      if (
        input.enabled &&
        !isAdminOnlyFeaturePreviewFlag(input.flag) &&
        !canEnableFeaturePreviews
      ) {
        throw new TRPCError({
          code: "PRECONDITION_FAILED",
          message: `Feature previews require ${V4_PREVIEW_LABEL} on self-hosted deployments.`,
        });
      }

      // The helper serializes and retries the read-modify-write so parallel
      // toggles of different flags cannot silently drop one another.
      await setUserFeaturePreview({
        prisma: ctx.prisma,
        userId,
        flag: input.flag,
        enabled: input.enabled,
      });

      return {
        success: true,
        flag: input.flag,
        enabled: input.enabled,
      };
    }),

  delete: authenticatedProcedure.mutation(async ({ ctx }) => {
    const userId = ctx.session.user.id;

    // Wrap check and delete in a serializable transaction to prevent race conditions
    // when organization owners are removed concurrently
    const { sfdcRemovals, revokedKeys } = await ctx.prisma.$transaction(
      async (tx) => {
        // Verify user can be deleted
        const { canDelete } = await checkUserCanBeDeleted(userId, tx);

        if (!canDelete) {
          throw new TRPCError({
            code: "PRECONDITION_FAILED",
            message:
              "Cannot delete account. You are the last owner of one or more organizations. Please add another owner or delete the organizations first.",
          });
        }

        // Capture org memberships before the cascade delete wipes them; they
        // are synced to SFDC only after the transaction commits. NONE roles
        // hold no SFDC org-member bridge, so there is nothing to remove.
        const user = await tx.user.findUnique({
          where: { id: userId },
          select: { email: true },
        });
        const memberships = await tx.organizationMembership.findMany({
          where: { userId, role: { not: Role.NONE } },
          select: { orgId: true },
        });

        const revokedKeys = await deleteUserBoundApiKeys(tx, { userId });

        // Delete the user (cascade will handle related records)
        await tx.user.delete({
          where: { id: userId },
        });

        return {
          sfdcRemovals: memberships.map(({ orgId }) => ({
            orgId,
            email: user?.email,
          })),
          revokedKeys,
        };
      },
      {
        isolationLevel: Prisma.TransactionIsolationLevel.Serializable,
      },
    );

    await evictRevokedApiKeys(revokedKeys, { userId });

    // SFDC: remove every org-member bridge the cascade just deleted. After
    // commit so a rolled-back delete never desyncs SFDC; removeUser never
    // throws, so per-org failures cannot fail the mutation.
    await Promise.all(
      sfdcRemovals.map(({ orgId, email }) =>
        getSfdcService()?.removeUser({ orgId, userId, email }),
      ),
    );

    return {
      success: true,
    };
  }),

  setV4BetaEnabled: authenticatedProcedure
    .input(z.object({ enabled: z.boolean() }))
    .mutation(async ({ input, ctx }) => {
      // Mirror the V4 preview gating in the auth.ts session callback so the
      // write path agrees with what the session reports. Availability is
      // driven by the write mode.
      const v4WriteMode = env.LANGFUSE_MIGRATION_V4_WRITE_MODE;
      const isLangfuseCloud = Boolean(env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION);

      // In events_only mode the preview is mandatory and cannot be toggled off:
      // the legacy tables it would fall back to are no longer written. Ignore
      // the requested value so a stale client can't flip a user into broken
      // reads, and keep the returned session shape consistent with auth.ts.
      if (v4WriteMode === "events_only") {
        return {
          success: true,
          v4BetaEnabled: true,
          canToggleV4: false,
        };
      }

      // In legacy mode the events tables are not written, so the preview has
      // nothing correct to read — it stays off and cannot be toggled.
      if (v4WriteMode === "legacy") {
        return {
          success: true,
          v4BetaEnabled: false,
          canToggleV4: false,
        };
      }

      // dual mode. On Cloud the date-based rollout applies (handled below) —
      // users auto-enabled by the rollout are locked on and cannot toggle off.
      // Self-hosted deployments are opt-in, but only once they have also set
      // ALLOW_PREVIEW_OPT_IN=true; otherwise feature paths still gated on that
      // flag would fall back to legacy tables while the core UI reads events,
      // so the toggle is not offered (mirrors the auth.ts session callback).
      if (!isLangfuseCloud) {
        if (env.LANGFUSE_MIGRATION_V4_ALLOW_PREVIEW_OPT_IN !== "true") {
          return {
            success: true,
            v4BetaEnabled: false,
            canToggleV4: false,
          };
        }

        await ctx.prisma.user.update({
          where: { id: ctx.session.user.id },
          data: { v4BetaEnabled: input.enabled },
        });

        return {
          success: true,
          v4BetaEnabled: input.enabled,
          canToggleV4: true,
        };
      }

      const userRolloutState = await ctx.prisma.user.findUnique({
        where: { id: ctx.session.user.id },
        select: {
          createdAt: true,
          v4BetaEnabled: true,
          organizationMemberships: {
            select: {
              organization: {
                select: {
                  id: true,
                  createdAt: true,
                },
              },
            },
          },
        },
      });

      if (!userRolloutState) {
        throw new TRPCError({
          code: "NOT_FOUND",
          message: "User not found",
        });
      }

      const userCanToggleV4 = canToggleV4(
        {
          userCreatedAt: userRolloutState.createdAt,
          organizations: userRolloutState.organizationMemberships.map(
            (membership) => ({
              id: membership.organization.id,
              createdAt: membership.organization.createdAt,
            }),
          ),
          excludedOrganizationIds: env.NEXT_PUBLIC_DEMO_ORG_ID
            ? [env.NEXT_PUBLIC_DEMO_ORG_ID]
            : [],
        },
        { isLangfuseCloudAdmin: ctx.session.user.admin === true },
      );

      // Rollout-locked users may recover a missing opt-in, but cannot opt out.
      if (!userCanToggleV4 && !input.enabled) {
        return {
          success: true,
          v4BetaEnabled: userRolloutState.v4BetaEnabled,
          canToggleV4: false,
        };
      }

      await ctx.prisma.user.update({
        where: { id: ctx.session.user.id },
        data: { v4BetaEnabled: input.enabled },
      });

      return {
        success: true,
        v4BetaEnabled: input.enabled,
        canToggleV4: userCanToggleV4,
      };
    }),
});