Merge branch 'dev' into aig

This commit is contained in:
Owen
2026-08-12 16:34:52 -04:00
9 changed files with 128 additions and 21 deletions
+2 -1
View File
@@ -1,8 +1,9 @@
import { db, idp, idpOrg, Transaction } from "@server/db";
import { and, eq } from "drizzle-orm";
import { build } from "@server/build";
export function isOrgIdentityProviderMode(): boolean {
return process.env.IDENTITY_PROVIDER_MODE === "org";
return build === "saas" || process.env.IDENTITY_PROVIDER_MODE === "org";
}
/**
+24 -10
View File
@@ -22,6 +22,7 @@ import { calculateUserClientsForOrgs } from "@server/lib/calculateUserClientsFor
import { build } from "@server/build";
import { assignUserToOrg } from "@server/lib/userOrg";
import { isOrgRebuildRateLimited } from "@server/lib/rebuildClientAssociations";
import { UserType } from "@server/types/UserTypes";
const acceptInviteBodySchema = z.strictObject({
token: z.string(),
@@ -66,12 +67,17 @@ export async function acceptInvite(
);
}
const existingUser = await db
const [existingInternalUser] = await db
.select()
.from(users)
.where(eq(users.email, existingInvite.email))
.where(
and(
eq(users.email, existingInvite.email),
eq(users.type, UserType.Internal)
)
)
.limit(1);
if (!existingUser.length) {
if (!existingInternalUser) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
@@ -80,9 +86,8 @@ export async function acceptInvite(
);
}
const { user, session } = await verifySession(req);
const { user } = await verifySession(req);
// at this point we know the user exists
if (!user) {
return next(
createHttpError(
@@ -92,7 +97,7 @@ export async function acceptInvite(
);
}
if (user && user.email !== existingInvite.email) {
if (user.email !== existingInvite.email) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
@@ -101,6 +106,15 @@ export async function acceptInvite(
);
}
if (user.type !== UserType.Internal) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
"Invites can only be accepted by internal users."
)
);
}
if (build == "saas") {
const usage = await usageService.getUsage(
existingInvite.orgId,
@@ -195,7 +209,7 @@ export async function acceptInvite(
await assignUserToOrg(
org,
{
userId: existingUser[0].userId,
userId: user.userId,
orgId: existingInvite.orgId
},
inviteRoleIds,
@@ -208,13 +222,13 @@ export async function acceptInvite(
.where(eq(userInvites.inviteId, inviteId));
logger.debug(
`User ${existingUser[0].userId} accepted invite to org ${existingInvite.orgId}`
`User ${user.userId} accepted invite to org ${existingInvite.orgId}`
);
});
calculateUserClientsForOrgs(existingUser[0].userId).catch((e) => {
calculateUserClientsForOrgs(user.userId).catch((e) => {
logger.error(
`Failed to calculate user clients after accepting invite for user ${existingUser[0].userId}: ${e}`
`Failed to calculate user clients after accepting invite for user ${user.userId}: ${e}`
);
});
+11
View File
@@ -20,6 +20,7 @@ import { TierFeature, tierMatrix } from "@server/lib/billing/tierMatrix";
import { assignUserToOrg } from "@server/lib/userOrg";
import { isLicensedOrSubscribed } from "#dynamic/lib/isLicencedOrSubscribed";
import { isOrgRebuildRateLimited } from "@server/lib/rebuildClientAssociations";
import { idpExistsForOrg } from "@server/lib/idp/idpExistsForOrg";
const paramsSchema = z.strictObject({
orgId: z.string().nonempty()
@@ -239,6 +240,16 @@ export async function createOrgUser(
);
}
const providerExists = await idpExistsForOrg(idpId, orgId);
if (!providerExists) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
"Identity provider not found in this organization"
)
);
}
const [idpRes] = await db
.select()
.from(idp)