|
| 1 | +import { db, eq, tables } from "@llmgateway/db"; |
| 2 | + |
| 3 | +export type OrgRole = "owner" | "admin" | "developer"; |
| 4 | + |
| 5 | +export interface RoleChange { |
| 6 | + old: OrgRole; |
| 7 | + new: OrgRole; |
| 8 | +} |
| 9 | + |
| 10 | +const ROLE_RANK: Record<OrgRole, number> = { |
| 11 | + developer: 1, |
| 12 | + admin: 2, |
| 13 | + owner: 3, |
| 14 | +}; |
| 15 | + |
| 16 | +// Recompute an org member's role from their SCIM group memberships and the |
| 17 | +// org's group->role mappings. The highest-precedence mapped role wins; the |
| 18 | +// default is `developer`. Owners are never auto-demoted — owner is only ever |
| 19 | +// assigned manually (or by an explicit owner mapping), so an admin who set up |
| 20 | +// SSO can't be locked out by a group that maps to a lower role. |
| 21 | +// |
| 22 | +// Returns the {old,new} role change when it updated the membership, or null |
| 23 | +// when nothing changed, so callers (e.g. SCIM) can audit the transition. |
| 24 | +export async function recomputeUserRole( |
| 25 | + userId: string, |
| 26 | + organizationId: string, |
| 27 | +): Promise<RoleChange | null> { |
| 28 | + const membership = await db.query.userOrganization.findFirst({ |
| 29 | + where: { |
| 30 | + userId: { eq: userId }, |
| 31 | + organizationId: { eq: organizationId }, |
| 32 | + }, |
| 33 | + columns: { id: true, role: true }, |
| 34 | + }); |
| 35 | + if (!membership) { |
| 36 | + return null; |
| 37 | + } |
| 38 | + |
| 39 | + const groupMemberships = await db.query.scimGroupMember.findMany({ |
| 40 | + where: { userId: { eq: userId } }, |
| 41 | + columns: { scimGroupId: true }, |
| 42 | + }); |
| 43 | + const groupIds = groupMemberships.map((m) => m.scimGroupId); |
| 44 | + |
| 45 | + let mappedRole: OrgRole = "developer"; |
| 46 | + if (groupIds.length) { |
| 47 | + const groups = await db.query.scimGroup.findMany({ |
| 48 | + where: { |
| 49 | + id: { in: groupIds }, |
| 50 | + organizationId: { eq: organizationId }, |
| 51 | + }, |
| 52 | + columns: { displayName: true }, |
| 53 | + }); |
| 54 | + const names = groups.map((g) => g.displayName); |
| 55 | + if (names.length) { |
| 56 | + const mappings = await db.query.ssoRoleMapping.findMany({ |
| 57 | + where: { |
| 58 | + organizationId: { eq: organizationId }, |
| 59 | + groupName: { in: names }, |
| 60 | + }, |
| 61 | + columns: { role: true }, |
| 62 | + }); |
| 63 | + for (const mapping of mappings) { |
| 64 | + if (ROLE_RANK[mapping.role] > ROLE_RANK[mappedRole]) { |
| 65 | + mappedRole = mapping.role; |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + if (membership.role === "owner" && ROLE_RANK[mappedRole] < ROLE_RANK.owner) { |
| 72 | + return null; |
| 73 | + } |
| 74 | + if (membership.role !== mappedRole) { |
| 75 | + await db |
| 76 | + .update(tables.userOrganization) |
| 77 | + .set({ role: mappedRole }) |
| 78 | + .where(eq(tables.userOrganization.id, membership.id)); |
| 79 | + return { old: membership.role, new: mappedRole }; |
| 80 | + } |
| 81 | + return null; |
| 82 | +} |
| 83 | + |
| 84 | +// Recompute the role of every member of the SCIM group(s) with `groupName` in |
| 85 | +// this org. Used when a role mapping is added or removed after the IdP has |
| 86 | +// already pushed the group and its members, so existing members pick up (or |
| 87 | +// lose) the mapped role without waiting for a later SCIM membership event. |
| 88 | +export async function recomputeRoleForGroupName( |
| 89 | + organizationId: string, |
| 90 | + groupName: string, |
| 91 | +) { |
| 92 | + const groups = await db.query.scimGroup.findMany({ |
| 93 | + where: { |
| 94 | + organizationId: { eq: organizationId }, |
| 95 | + displayName: { eq: groupName }, |
| 96 | + }, |
| 97 | + columns: { id: true }, |
| 98 | + }); |
| 99 | + if (!groups.length) { |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + const members = await db.query.scimGroupMember.findMany({ |
| 104 | + where: { scimGroupId: { in: groups.map((g) => g.id) } }, |
| 105 | + columns: { userId: true }, |
| 106 | + }); |
| 107 | + const userIds = [...new Set(members.map((m) => m.userId))]; |
| 108 | + for (const userId of userIds) { |
| 109 | + await recomputeUserRole(userId, organizationId); |
| 110 | + } |
| 111 | +} |
0 commit comments