import { and, eq, ne } from "drizzle-orm"; import { db } from "../db/client.js"; import { users, type UserRole } from "../db/schema.js"; import { getSettings } from "../services/settingsStore.js"; import { roleFromGroups } from "./groups.js"; export interface RoleChange { from: UserRole; to: UserRole; groups: string[]; /** The group rules would have taken the last administrator's rights away, so the role was left alone. */ blocked?: boolean; } /** * Called on every successful OIDC login. The very first user ever to sign in * becomes admin; everyone after defaults to viewer until an admin promotes * them from the Users page — or, when roles follow groups (Settings → Sign-in), * until the groups the identity provider reports decide it. * * `groups` is what the provider said, or null/undefined when it said nothing about groups at all. That is not "in no groups": with no * information the role is left exactly as it is, so a wrong claim name can't quietly demote everyone. And the last administrator is * never demoted by a group rule, whatever the groups say. */ export async function upsertUserFromLogin(params: { sub: string; email?: string; name?: string; groups?: string[] | null; }): Promise<{ user: typeof users.$inferSelect; created: boolean; roleChange?: RoleChange }> { const [existing] = await db.select().from(users).where(eq(users.oidcSub, params.sub)).limit(1); const now = new Date().toISOString(); const { signIn } = await getSettings(); const mapping = signIn.roleFromGroups && params.groups !== null && params.groups !== undefined; const groupsJson = params.groups ? JSON.stringify(params.groups) : undefined; if (existing) { let role = existing.role; let roleChange: RoleChange | undefined; if (mapping) { const wanted = roleFromGroups(signIn, params.groups!, existing.role); if (wanted !== existing.role) { const demotesAdmin = existing.role === "admin" && wanted !== "admin"; const otherAdmin = demotesAdmin ? await db.select({ id: users.id }).from(users).where(and(eq(users.role, "admin"), ne(users.id, existing.id))).limit(1) : []; if (demotesAdmin && otherAdmin.length === 0) { roleChange = { from: existing.role, to: wanted, groups: params.groups!, blocked: true }; } else { role = wanted; roleChange = { from: existing.role, to: wanted, groups: params.groups! }; } } } const [updated] = await db .update(users) .set({ email: params.email ?? existing.email, name: params.name ?? existing.name, lastLoginAt: now, role, ...(groupsJson !== undefined ? { groups: groupsJson } : {}), }) .where(eq(users.id, existing.id)) .returning(); return { user: updated, created: false, roleChange }; } const anyUser = await db.select({ id: users.id }).from(users).limit(1); // The first person ever is the administrator, so there is always one; after that the groups decide, when they are used, else viewer. const role: UserRole = anyUser.length === 0 ? "admin" : mapping ? roleFromGroups({ ...signIn, otherwise: "viewer" }, params.groups!, "viewer") : "viewer"; const [created] = await db .insert(users) .values({ oidcSub: params.sub, email: params.email, name: params.name, role, lastLoginAt: now, ...(groupsJson !== undefined ? { groups: groupsJson } : {}), }) .returning(); return { user: created, created: true }; }