- Runbooks: notes per server/integration, shown next to matching alerts - Backup coverage page across Proxmox and PBS, with a digest section - Hardware inventory with warranty, power and running cost - Authentik group to role mapping with preview and safeguards - /metrics (bearer token) and signed outgoing event webhooks - Docs: README, DATABASE, NOTIFICATIONS, ROLES and new API.md Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
84 lines
3.5 KiB
TypeScript
84 lines
3.5 KiB
TypeScript
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 };
|
|
}
|