import { useEffect, useState } from "react"; import { api, type GroupRolePreview, type ProfilePictureSettings, type SignInSettings as SignInSettingsData } from "../../api/client"; import { ErrorNotice } from "../../components/Notice"; import { PageSkeleton } from "../../components/Skeleton"; import { readableError } from "../../utils/errors"; import { toast } from "../../utils/toast"; import ExportTableButton from "../../components/ExportTableButton"; import { t } from "../../i18n"; const DEFAULTS: SignInSettingsData = { roleFromGroups: false, groupsClaim: "groups", adminGroups: [], operatorGroups: [], otherwise: "viewer" }; const NO_PICTURES: ProfilePictureSettings = { gravatar: false, identityProvider: false }; /** One name per line or separated by commas — whichever is easier to paste in. */ const parseNames = (text: string): string[] => [...new Set(text.split(/[\n,]/).map((s) => s.trim()).filter(Boolean))]; /** * Who is an administrator, an operator or a viewer, decided from the groups the identity provider (Authentik) says a person is in, each * time they sign in — instead of by hand on the Users page. Off by default. "Check" shows what each person would become, from the * groups they had at their last sign-in, before it is switched on. */ export default function SignInSettings() { const [loaded, setLoaded] = useState(null); const [error, setError] = useState(null); const [enabled, setEnabled] = useState(false); const [claim, setClaim] = useState("groups"); const [adminText, setAdminText] = useState(""); const [operatorText, setOperatorText] = useState(""); const [otherwise, setOtherwise] = useState<"viewer" | "keep">("viewer"); const [preview, setPreview] = useState(null); const [loadedPictures, setLoadedPictures] = useState(NO_PICTURES); const [pictures, setPictures] = useState(NO_PICTURES); const [busy, setBusy] = useState<"check" | "save" | null>(null); useEffect(() => { api.settings .get() .then((res) => { const s = res.settings.signIn ?? DEFAULTS; setLoaded(s); setEnabled(s.roleFromGroups); setClaim(s.groupsClaim); setAdminText(s.adminGroups.join("\n")); setOperatorText(s.operatorGroups.join("\n")); setOtherwise(s.otherwise); const p = res.settings.profilePictures ?? NO_PICTURES; setLoadedPictures(p); setPictures(p); }) .catch((err) => setError(readableError(err))); }, []); if (!loaded) return error ? : ; const draft = (): SignInSettingsData => ({ roleFromGroups: enabled, groupsClaim: claim.trim() || "groups", adminGroups: parseNames(adminText), operatorGroups: parseNames(operatorText), otherwise }); const dirty = JSON.stringify(draft()) !== JSON.stringify(loaded) || JSON.stringify(pictures) !== JSON.stringify(loadedPictures); async function check() { setBusy("check"); setError(null); try { const d = draft(); setPreview(await api.users.previewGroupRoles({ groupsClaim: d.groupsClaim, adminGroups: d.adminGroups, operatorGroups: d.operatorGroups, otherwise: d.otherwise })); } catch (err) { setError(readableError(err)); } finally { setBusy(null); } } async function save() { setBusy("save"); setError(null); try { const d = draft(); await api.settings.update({ signIn: d, profilePictures: pictures }); setLoaded(d); setLoadedPictures(pictures); toast.success(d.roleFromGroups ? "Saved. Roles now follow groups at each sign-in." : "Saved. Roles are set by hand on the Users page."); } catch (err) { setError(readableError(err)); } finally { setBusy(null); } } const noAdmin = enabled && preview !== null && preview.adminsAfter === 0; return ( <>

{t("Sign-in")}

{t("Roles from groups")}

By default a new person is a viewer until an administrator changes their role on the Users page. Here you can let the groups the identity provider reports decide it instead: at every sign-in a person is an administrator if they are in an admin group, an operator if they are in an operator group, and otherwise what you choose below. In Authentik the groups claim comes with the profile scope this app already asks for.

setClaim(e.target.value)} />