Always pull all of the users for the blueprints

This commit is contained in:
Owen
2026-09-03 12:02:41 -04:00
parent be0dd65557
commit 1bc5fbbf0f
3 changed files with 73 additions and 46 deletions
+11 -8
View File
@@ -1,12 +1,12 @@
import { and, asc, eq, or } from "drizzle-orm"; import { and, asc, eq, or } from "drizzle-orm";
import { Transaction, User, userOrgs, users } from "@server/db"; import { Transaction, User, userOrgs, users } from "@server/db";
export async function findOrgUserByIdentifier( export async function findOrgUsersByIdentifier(
trx: Transaction, trx: Transaction,
orgId: string, orgId: string,
identifier: string identifier: string
): Promise<User | null> { ): Promise<User[]> {
const [match] = await trx const matches = await trx
.select() .select()
.from(users) .from(users)
.innerJoin(userOrgs, eq(users.userId, userOrgs.userId)) .innerJoin(userOrgs, eq(users.userId, userOrgs.userId))
@@ -16,10 +16,9 @@ export async function findOrgUserByIdentifier(
eq(userOrgs.orgId, orgId) eq(userOrgs.orgId, orgId)
) )
) )
.orderBy(asc(users.dateCreated), asc(users.userId)) .orderBy(asc(users.dateCreated), asc(users.userId));
.limit(1);
return match?.user ?? null; return matches.map((match) => match.user);
} }
export async function resolveOrgUserIds( export async function resolveOrgUserIds(
@@ -29,8 +28,12 @@ export async function resolveOrgUserIds(
): Promise<string[]> { ): Promise<string[]> {
const userIds = new Set<string>(); const userIds = new Set<string>();
for (const identifier of identifiers) { for (const identifier of identifiers) {
const user = await findOrgUserByIdentifier(trx, orgId, identifier); const matchedUsers = await findOrgUsersByIdentifier(
if (user) { trx,
orgId,
identifier
);
for (const user of matchedUsers) {
userIds.add(user.userId); userIds.add(user.userId);
} }
} }
+33 -21
View File
@@ -51,7 +51,7 @@ import { tierMatrix } from "../billing/tierMatrix";
import { isValidCIDR, isValidIP, isValidUrlGlobPattern } from "../validators"; import { isValidCIDR, isValidIP, isValidUrlGlobPattern } from "../validators";
import { Config, isTargetsOnlyResource, TargetData } from "./types"; import { Config, isTargetsOnlyResource, TargetData } from "./types";
import { getOrCreateLabelIds, syncResourceLabels } from "./labels"; import { getOrCreateLabelIds, syncResourceLabels } from "./labels";
import { findOrgUserByIdentifier } from "./findOrgUser"; import { findOrgUsersByIdentifier } from "./findOrgUser";
import { LimitId } from "../billing"; import { LimitId } from "../billing";
import { usageService } from "../billing/usageService"; import { usageService } from "../billing/usageService";
import { syncInferenceAiConfig } from "./aiProviders"; import { syncInferenceAiConfig } from "./aiProviders";
@@ -1564,21 +1564,27 @@ async function syncUserResources(
.where(eq(userResources.resourceId, resourceId)); .where(eq(userResources.resourceId, resourceId));
for (const username of ssoUsers) { for (const username of ssoUsers) {
const user = await findOrgUserByIdentifier(trx, orgId, username); const matchedUsers = await findOrgUsersByIdentifier(
trx,
orgId,
username
);
if (!user) { if (matchedUsers.length === 0) {
throw new Error(`User not found: ${username} in org ${orgId}`); throw new Error(`User not found: ${username} in org ${orgId}`);
} }
const existingUserResource = existingUserResources.find( for (const user of matchedUsers) {
(rr) => rr.userId === user.userId const existingUserResource = existingUserResources.find(
); (rr) => rr.userId === user.userId
);
if (!existingUserResource) { if (!existingUserResource) {
await trx.insert(userResources).values({ await trx.insert(userResources).values({
userId: user.userId, userId: user.userId,
resourceId: resourceId resourceId: resourceId
}); });
}
} }
} }
@@ -1946,21 +1952,27 @@ async function syncUserPolicies(
.where(eq(userPolicies.resourcePolicyId, policyId)); .where(eq(userPolicies.resourcePolicyId, policyId));
for (const username of ssoUsers) { for (const username of ssoUsers) {
const user = await findOrgUserByIdentifier(trx, orgId, username); const matchedUsers = await findOrgUsersByIdentifier(
trx,
orgId,
username
);
if (!user) { if (matchedUsers.length === 0) {
throw new Error(`User not found: ${username} in org ${orgId}`); throw new Error(`User not found: ${username} in org ${orgId}`);
} }
const existingUserPolicy = existingUserPoliciesList.find( for (const user of matchedUsers) {
(up) => up.userId === user.userId const existingUserPolicy = existingUserPoliciesList.find(
); (up) => up.userId === user.userId
);
if (!existingUserPolicy) { if (!existingUserPolicy) {
await trx.insert(userPolicies).values({ await trx.insert(userPolicies).values({
userId: user.userId, userId: user.userId,
resourcePolicyId: policyId resourcePolicyId: policyId
}); });
}
} }
} }
+29 -17
View File
@@ -22,7 +22,7 @@ import { idpExistsForOrg } from "@server/lib/idp/idpExistsForOrg";
import { isValidCIDR, isValidIP, isValidUrlGlobPattern } from "../validators"; import { isValidCIDR, isValidIP, isValidUrlGlobPattern } from "../validators";
import { isLicensedOrSubscribed } from "#dynamic/lib/isLicencedOrSubscribed"; import { isLicensedOrSubscribed } from "#dynamic/lib/isLicencedOrSubscribed";
import { tierMatrix } from "../billing/tierMatrix"; import { tierMatrix } from "../billing/tierMatrix";
import { findOrgUserByIdentifier } from "./findOrgUser"; import { findOrgUsersByIdentifier } from "./findOrgUser";
export type ResourcePoliciesResults = { export type ResourcePoliciesResults = {
resourcePolicyId: number; resourcePolicyId: number;
@@ -467,24 +467,30 @@ async function syncUserPolicies(
.where(eq(userPolicies.resourcePolicyId, policyId)); .where(eq(userPolicies.resourcePolicyId, policyId));
for (const username of ssoUsers) { for (const username of ssoUsers) {
const user = await findOrgUserByIdentifier(trx, orgId, username); const matchedUsers = await findOrgUsersByIdentifier(
trx,
orgId,
username
);
if (!user) { if (matchedUsers.length === 0) {
logger.warn( logger.warn(
`User '${username}' not found in org '${orgId}', skipping` `User '${username}' not found in org '${orgId}', skipping`
); );
continue; continue;
} }
const alreadyExists = existingUserPolicies.some( for (const user of matchedUsers) {
(up) => up.userId === user.userId const alreadyExists = existingUserPolicies.some(
); (up) => up.userId === user.userId
);
if (!alreadyExists) { if (!alreadyExists) {
await trx.insert(userPolicies).values({ await trx.insert(userPolicies).values({
userId: user.userId, userId: user.userId,
resourcePolicyId: policyId resourcePolicyId: policyId
}); });
}
} }
} }
@@ -527,19 +533,25 @@ async function addUserPolicies(
trx: Transaction trx: Transaction
) { ) {
for (const username of ssoUsers) { for (const username of ssoUsers) {
const user = await findOrgUserByIdentifier(trx, orgId, username); const matchedUsers = await findOrgUsersByIdentifier(
trx,
orgId,
username
);
if (!user) { if (matchedUsers.length === 0) {
logger.warn( logger.warn(
`User '${username}' not found in org '${orgId}', skipping` `User '${username}' not found in org '${orgId}', skipping`
); );
continue; continue;
} }
await trx.insert(userPolicies).values({ for (const user of matchedUsers) {
userId: user.userId, await trx.insert(userPolicies).values({
resourcePolicyId: policyId userId: user.userId,
}); resourcePolicyId: policyId
});
}
} }
} }